Refactor python module structure
This commit is contained in:
@@ -1,58 +0,0 @@
|
|||||||
import umsgpack
|
|
||||||
import paho.mqtt.client as mqtt
|
|
||||||
import time
|
|
||||||
import ssl
|
|
||||||
import json
|
|
||||||
|
|
||||||
cfg = None
|
|
||||||
with open('config.json') as json_config:
|
|
||||||
cfg = json.load(json_config)
|
|
||||||
connected = False
|
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
|
||||||
print("Connected with result code " + str(rc))
|
|
||||||
if rc == 0:
|
|
||||||
global connected
|
|
||||||
connected = True
|
|
||||||
|
|
||||||
client.subscribe('hello/test', qos=1)
|
|
||||||
|
|
||||||
def on_message(client, userdata, message):
|
|
||||||
p = umsgpack.unpackb(message.payload)
|
|
||||||
print("Received message '" + p["message"] + "' on topic '"
|
|
||||||
+ message.topic + "' with QoS " + str(message.qos))
|
|
||||||
|
|
||||||
def on_disconnect(client, userdata, rc):
|
|
||||||
if rc != 0:
|
|
||||||
print("Unexpected disconnection.")
|
|
||||||
|
|
||||||
mqttc = mqtt.Client()
|
|
||||||
|
|
||||||
# mqttc.tls_set_context(context = ssl.create_default_context())
|
|
||||||
|
|
||||||
mqttc.on_connect = on_connect
|
|
||||||
mqttc.on_disconnect = on_disconnect
|
|
||||||
mqttc.on_message = on_message
|
|
||||||
|
|
||||||
mqttc.connect(cfg["mqtt"]["host"], cfg["mqtt"]["port"], cfg["mqtt"]["timeout"])
|
|
||||||
|
|
||||||
mqttc.loop_start()
|
|
||||||
|
|
||||||
# mqttc.will_set("hello/test", "Disconnected", )
|
|
||||||
|
|
||||||
pub = input("Enter something to publish: ")
|
|
||||||
|
|
||||||
while pub != 'q':
|
|
||||||
if(connected):
|
|
||||||
pub = umsgpack.packb({"message":pub})
|
|
||||||
mqttc.publish('hello/test', pub)
|
|
||||||
pub = input("Enter something to publish: ")
|
|
||||||
print('Message is: ' + pub)
|
|
||||||
else:
|
|
||||||
pub = input('Would you like to reconnect? ')
|
|
||||||
if pub == 'y':
|
|
||||||
mqttc.reconnect()
|
|
||||||
time.sleep(2)
|
|
||||||
|
|
||||||
mqttc.loop_stop()
|
|
||||||
mqttc.disconnect()
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
|
||||||
Created on Tue Nov 27 14:17:45 2018
|
|
||||||
|
|
||||||
@author: pivatom
|
|
||||||
"""
|
|
||||||
|
|
||||||
import paho.mqtt.client as mqtt
|
|
||||||
|
|
||||||
# These are just the defaults.
|
|
||||||
mqttc = mqtt.Client(client_id="", clean_session=True, userdata=None, protocol=MQTTv311, transport="tcp")
|
|
||||||
|
|
||||||
mqttc.tls_set(ca_certs=None, certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED,
|
|
||||||
tls_version=ssl.PROTOCOL_TLS, ciphers=None)
|
|
||||||
|
|
||||||
# Use port 8883 for SSL
|
|
||||||
# Host is just an ip address or hostname of the broker.
|
|
||||||
mqttc.connect(host, port=1883, keepalive=60, bind_address="")
|
|
||||||
|
|
||||||
# Topic is a string of the topic to publish to.
|
|
||||||
mqttc.publish(topic, payload=None, qos=0, retain=False)
|
|
||||||
|
|
||||||
# Just subscribe to a topic.
|
|
||||||
mqttc.subscribe(topic, qos=0)
|
|
||||||
|
|
||||||
def on_message(client, userdata, message):
|
|
||||||
'''
|
|
||||||
Do something with the message. Choose based on what the topic is.
|
|
||||||
|
|
||||||
Can also just assign different callback functions for different topics
|
|
||||||
using message_callback_add(sub, callback)
|
|
||||||
'''
|
|
||||||
print("Received message '" + str(message.payload) + "' on topic '"
|
|
||||||
+ message.topic + "' with QoS " + str(message.qos))
|
|
||||||
|
|
||||||
mqttc.on_message = on_message
|
|
||||||
|
|
||||||
# Or for specific callback:
|
|
||||||
# Can use foo/# for all subtopics of foo/, or
|
|
||||||
# +/bar for all topics that include bar.
|
|
||||||
mqttc.message_callback_add(sub, on_message)
|
|
||||||
|
|
||||||
@@ -7,11 +7,11 @@ import time
|
|||||||
|
|
||||||
import grpc
|
import grpc
|
||||||
|
|
||||||
import MotorControl.motorService_pb2 as motorService_pb2
|
import control.motorService_pb2 as motorService_pb2
|
||||||
import MotorControl.motorService_pb2_grpc as motorService_pb2_grpc
|
import control.motorService_pb2_grpc as motorService_pb2_grpc
|
||||||
from MotorControl.gpiozero.motor_session import Motor
|
from control.gpiozero.motor_session import Motor
|
||||||
from SlamController.slam_servicer import SlamServicer
|
from slam.slam_servicer import SlamServicer
|
||||||
import SlamController.SlamController_pb2_grpc as SlamController_pb2_grpc
|
import slam.SlamController_pb2_grpc as SlamController_pb2_grpc
|
||||||
|
|
||||||
|
|
||||||
class MotorServicer(motorService_pb2_grpc.CarControlServicer):
|
class MotorServicer(motorService_pb2_grpc.CarControlServicer):
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
|
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
|
||||||
import grpc
|
import grpc
|
||||||
|
|
||||||
import motorService_pb2 as motorService__pb2
|
import control.motorService_pb2 as motorService__pb2
|
||||||
|
|
||||||
|
|
||||||
class CarControlStub(object):
|
class CarControlStub(object):
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
from . import SlamController_pb2_grpc
|
import slam.SlamController_pb2_grpc as grpc
|
||||||
from . import SlamController_pb2
|
import slam.SlamController_pb2 as proto
|
||||||
from . import slam_streamer as slam
|
import slam.slam_streamer as slam
|
||||||
from multiprocessing import Process
|
from multiprocessing import Process
|
||||||
|
|
||||||
|
|
||||||
class SlamServicer(SlamController_pb2_grpc.SlamControlServicer):
|
class SlamServicer(grpc.SlamControlServicer):
|
||||||
slam_thread = None
|
slam_thread = None
|
||||||
|
|
||||||
def __init__(self, lidar_connection):
|
def __init__(self, lidar_connection):
|
||||||
@@ -21,11 +21,11 @@ class SlamServicer(SlamController_pb2_grpc.SlamControlServicer):
|
|||||||
self.slam.map_meters = request.map_size_meters
|
self.slam.map_meters = request.map_size_meters
|
||||||
self.slam_thread = Process(target=self.slam.start)
|
self.slam_thread = Process(target=self.slam.start)
|
||||||
self.slam_thread.start()
|
self.slam_thread.start()
|
||||||
return SlamController_pb2.Empty()
|
return proto.Empty()
|
||||||
|
|
||||||
def stop_streaming(self, request, context):
|
def stop_streaming(self, request, context):
|
||||||
if self.slam_thread is not None:
|
if self.slam_thread is not None:
|
||||||
self.slam.stop_scanning()
|
self.slam.stop_scanning()
|
||||||
self.slam_thread.join()
|
self.slam_thread.join()
|
||||||
self.slam = None
|
self.slam = None
|
||||||
return SlamController_pb2.Empty()
|
return proto.Empty()
|
||||||
@@ -3,8 +3,8 @@ from breezyslam.algorithms import RMHC_SLAM
|
|||||||
from breezyslam.sensors import RPLidarA1 as LaserModel
|
from breezyslam.sensors import RPLidarA1 as LaserModel
|
||||||
from rplidar import RPLidar as Lidar
|
from rplidar import RPLidar as Lidar
|
||||||
from .SlamController_pb2 import SlamScan, SlamLocation
|
from .SlamController_pb2 import SlamScan, SlamLocation
|
||||||
import Messaging.message_factory as mf
|
import messaging.message_factory as mf
|
||||||
import Messaging.messages as messages
|
import messaging.messages as messages
|
||||||
|
|
||||||
|
|
||||||
# Left here as was used in the example, configure as necessary.
|
# Left here as was used in the example, configure as necessary.
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
import rplidar
|
import rplidar
|
||||||
from rplidar import RPLidar
|
from rplidar import RPLidar
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from persontracking import algorithms
|
from tracking import algorithms
|
||||||
import persontracking.lidar_tracker_pb2 as tracker_pb
|
import tracking.lidar_tracker_pb2 as tracker_pb
|
||||||
import zmq
|
import zmq
|
||||||
import Messaging.message_factory as mf
|
import Messaging.message_factory as mf
|
||||||
import Messaging.messages as messages
|
import Messaging.messages as messages
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import persontracking.lidar_tracker_pb2 as lidar_tracker_pb2
|
import tracking.lidar_tracker_pb2 as lidar_tracker_pb2
|
||||||
from persontracking.lidar_tracker_pb2_grpc import PersonTrackingServicer
|
from tracking.lidar_tracker_pb2_grpc import PersonTrackingServicer
|
||||||
from persontracking.lidar_cache import LidarCache
|
from tracking.lidar_cache import LidarCache
|
||||||
from multiprocessing import Process
|
from multiprocessing import Process
|
||||||
|
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
|
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
|
||||||
import grpc
|
import grpc
|
||||||
|
|
||||||
from persontracking import lidar_tracker_pb2 as persontracking_dot_lidar__tracker__pb2
|
from tracking import lidar_tracker_pb2 as persontracking_dot_lidar__tracker__pb2
|
||||||
|
|
||||||
|
|
||||||
class PersonTrackingStub(object):
|
class PersonTrackingStub(object):
|
||||||
Reference in New Issue
Block a user