From 82a214c209b31627687d58406b0af8203c4c18e1 Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Thu, 5 Mar 2020 21:43:22 +1030 Subject: [PATCH] Refactor python module structure --- Messaging/clientEx copy.py | 58 ------------------- Messaging/clientEx.py | 42 -------------- {MotorControl => control}/MotorServer.py | 10 ++-- .../PythonRemoteController.py | 0 {MotorControl => control}/__init__.py | 0 .../gpiozero/__init__.py | 0 .../gpiozero/motor_session.py | 0 {MotorControl => control}/motorService_pb2.py | 0 .../motorService_pb2_grpc.py | 2 +- .../control}/motorService.proto | 0 .../proto => proto/slam}/SlamController.proto | 0 .../tracking}/lidar_tracker.proto | 0 .../SlamController_pb2.py | 0 .../SlamController_pb2_grpc.py | 0 {SlamController => slam}/__init__.py | 0 {SlamController => slam}/slam_servicer.py | 12 ++-- {SlamController => slam}/slam_streamer.py | 4 +- .../zmq_pair_testing/pair.py | 0 {persontracking => tracking}/__init__.py | 0 {persontracking => tracking}/algorithms.py | 0 {persontracking => tracking}/lidar_cache.py | 4 +- .../lidar_servicer.py | 6 +- .../lidar_tracker_pb2.py | 0 .../lidar_tracker_pb2_grpc.py | 2 +- 24 files changed, 20 insertions(+), 120 deletions(-) delete mode 100644 Messaging/clientEx copy.py delete mode 100644 Messaging/clientEx.py rename {MotorControl => control}/MotorServer.py (89%) rename {MotorControl => control}/PythonRemoteController.py (100%) rename {MotorControl => control}/__init__.py (100%) rename {MotorControl => control}/gpiozero/__init__.py (100%) rename {MotorControl => control}/gpiozero/motor_session.py (100%) rename {MotorControl => control}/motorService_pb2.py (100%) rename {MotorControl => control}/motorService_pb2_grpc.py (97%) rename {MotorControl/protos => proto/control}/motorService.proto (100%) rename {SlamController/proto => proto/slam}/SlamController.proto (100%) rename {persontracking/protos/persontracking => proto/tracking}/lidar_tracker.proto (100%) rename {SlamController => slam}/SlamController_pb2.py (100%) rename {SlamController => slam}/SlamController_pb2_grpc.py (100%) rename {SlamController => slam}/__init__.py (100%) rename {SlamController => slam}/slam_servicer.py (78%) rename {SlamController => slam}/slam_streamer.py (98%) rename {SlamController => slam}/zmq_pair_testing/pair.py (100%) rename {persontracking => tracking}/__init__.py (100%) rename {persontracking => tracking}/algorithms.py (100%) rename {persontracking => tracking}/lidar_cache.py (95%) rename {persontracking => tracking}/lidar_servicer.py (70%) rename {persontracking => tracking}/lidar_tracker_pb2.py (100%) rename {persontracking => tracking}/lidar_tracker_pb2_grpc.py (97%) diff --git a/Messaging/clientEx copy.py b/Messaging/clientEx copy.py deleted file mode 100644 index b9d21ee..0000000 --- a/Messaging/clientEx copy.py +++ /dev/null @@ -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() \ No newline at end of file diff --git a/Messaging/clientEx.py b/Messaging/clientEx.py deleted file mode 100644 index 5c799bb..0000000 --- a/Messaging/clientEx.py +++ /dev/null @@ -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) - diff --git a/MotorControl/MotorServer.py b/control/MotorServer.py similarity index 89% rename from MotorControl/MotorServer.py rename to control/MotorServer.py index cbc408c..cd29eb6 100755 --- a/MotorControl/MotorServer.py +++ b/control/MotorServer.py @@ -7,11 +7,11 @@ import time import grpc -import MotorControl.motorService_pb2 as motorService_pb2 -import MotorControl.motorService_pb2_grpc as motorService_pb2_grpc -from MotorControl.gpiozero.motor_session import Motor -from SlamController.slam_servicer import SlamServicer -import SlamController.SlamController_pb2_grpc as SlamController_pb2_grpc +import control.motorService_pb2 as motorService_pb2 +import control.motorService_pb2_grpc as motorService_pb2_grpc +from control.gpiozero.motor_session import Motor +from slam.slam_servicer import SlamServicer +import slam.SlamController_pb2_grpc as SlamController_pb2_grpc class MotorServicer(motorService_pb2_grpc.CarControlServicer): diff --git a/MotorControl/PythonRemoteController.py b/control/PythonRemoteController.py similarity index 100% rename from MotorControl/PythonRemoteController.py rename to control/PythonRemoteController.py diff --git a/MotorControl/__init__.py b/control/__init__.py similarity index 100% rename from MotorControl/__init__.py rename to control/__init__.py diff --git a/MotorControl/gpiozero/__init__.py b/control/gpiozero/__init__.py similarity index 100% rename from MotorControl/gpiozero/__init__.py rename to control/gpiozero/__init__.py diff --git a/MotorControl/gpiozero/motor_session.py b/control/gpiozero/motor_session.py similarity index 100% rename from MotorControl/gpiozero/motor_session.py rename to control/gpiozero/motor_session.py diff --git a/MotorControl/motorService_pb2.py b/control/motorService_pb2.py similarity index 100% rename from MotorControl/motorService_pb2.py rename to control/motorService_pb2.py diff --git a/MotorControl/motorService_pb2_grpc.py b/control/motorService_pb2_grpc.py similarity index 97% rename from MotorControl/motorService_pb2_grpc.py rename to control/motorService_pb2_grpc.py index 36234b2..3b77fff 100644 --- a/MotorControl/motorService_pb2_grpc.py +++ b/control/motorService_pb2_grpc.py @@ -1,7 +1,7 @@ # Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! import grpc -import motorService_pb2 as motorService__pb2 +import control.motorService_pb2 as motorService__pb2 class CarControlStub(object): diff --git a/MotorControl/protos/motorService.proto b/proto/control/motorService.proto similarity index 100% rename from MotorControl/protos/motorService.proto rename to proto/control/motorService.proto diff --git a/SlamController/proto/SlamController.proto b/proto/slam/SlamController.proto similarity index 100% rename from SlamController/proto/SlamController.proto rename to proto/slam/SlamController.proto diff --git a/persontracking/protos/persontracking/lidar_tracker.proto b/proto/tracking/lidar_tracker.proto similarity index 100% rename from persontracking/protos/persontracking/lidar_tracker.proto rename to proto/tracking/lidar_tracker.proto diff --git a/SlamController/SlamController_pb2.py b/slam/SlamController_pb2.py similarity index 100% rename from SlamController/SlamController_pb2.py rename to slam/SlamController_pb2.py diff --git a/SlamController/SlamController_pb2_grpc.py b/slam/SlamController_pb2_grpc.py similarity index 100% rename from SlamController/SlamController_pb2_grpc.py rename to slam/SlamController_pb2_grpc.py diff --git a/SlamController/__init__.py b/slam/__init__.py similarity index 100% rename from SlamController/__init__.py rename to slam/__init__.py diff --git a/SlamController/slam_servicer.py b/slam/slam_servicer.py similarity index 78% rename from SlamController/slam_servicer.py rename to slam/slam_servicer.py index f76a114..3ee7bf8 100644 --- a/SlamController/slam_servicer.py +++ b/slam/slam_servicer.py @@ -1,10 +1,10 @@ -from . import SlamController_pb2_grpc -from . import SlamController_pb2 -from . import slam_streamer as slam +import slam.SlamController_pb2_grpc as grpc +import slam.SlamController_pb2 as proto +import slam.slam_streamer as slam from multiprocessing import Process -class SlamServicer(SlamController_pb2_grpc.SlamControlServicer): +class SlamServicer(grpc.SlamControlServicer): slam_thread = None 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_thread = Process(target=self.slam.start) self.slam_thread.start() - return SlamController_pb2.Empty() + return proto.Empty() def stop_streaming(self, request, context): if self.slam_thread is not None: self.slam.stop_scanning() self.slam_thread.join() self.slam = None - return SlamController_pb2.Empty() + return proto.Empty() diff --git a/SlamController/slam_streamer.py b/slam/slam_streamer.py similarity index 98% rename from SlamController/slam_streamer.py rename to slam/slam_streamer.py index be77d0e..5c30126 100644 --- a/SlamController/slam_streamer.py +++ b/slam/slam_streamer.py @@ -3,8 +3,8 @@ from breezyslam.algorithms import RMHC_SLAM from breezyslam.sensors import RPLidarA1 as LaserModel from rplidar import RPLidar as Lidar from .SlamController_pb2 import SlamScan, SlamLocation -import Messaging.message_factory as mf -import Messaging.messages as messages +import messaging.message_factory as mf +import messaging.messages as messages # Left here as was used in the example, configure as necessary. diff --git a/SlamController/zmq_pair_testing/pair.py b/slam/zmq_pair_testing/pair.py similarity index 100% rename from SlamController/zmq_pair_testing/pair.py rename to slam/zmq_pair_testing/pair.py diff --git a/persontracking/__init__.py b/tracking/__init__.py similarity index 100% rename from persontracking/__init__.py rename to tracking/__init__.py diff --git a/persontracking/algorithms.py b/tracking/algorithms.py similarity index 100% rename from persontracking/algorithms.py rename to tracking/algorithms.py diff --git a/persontracking/lidar_cache.py b/tracking/lidar_cache.py similarity index 95% rename from persontracking/lidar_cache.py rename to tracking/lidar_cache.py index ce5c294..22d60ef 100644 --- a/persontracking/lidar_cache.py +++ b/tracking/lidar_cache.py @@ -1,8 +1,8 @@ import rplidar from rplidar import RPLidar from threading import Thread -from persontracking import algorithms -import persontracking.lidar_tracker_pb2 as tracker_pb +from tracking import algorithms +import tracking.lidar_tracker_pb2 as tracker_pb import zmq import Messaging.message_factory as mf import Messaging.messages as messages diff --git a/persontracking/lidar_servicer.py b/tracking/lidar_servicer.py similarity index 70% rename from persontracking/lidar_servicer.py rename to tracking/lidar_servicer.py index e4a5315..f2cd66c 100644 --- a/persontracking/lidar_servicer.py +++ b/tracking/lidar_servicer.py @@ -1,6 +1,6 @@ -import persontracking.lidar_tracker_pb2 as lidar_tracker_pb2 -from persontracking.lidar_tracker_pb2_grpc import PersonTrackingServicer -from persontracking.lidar_cache import LidarCache +import tracking.lidar_tracker_pb2 as lidar_tracker_pb2 +from tracking.lidar_tracker_pb2_grpc import PersonTrackingServicer +from tracking.lidar_cache import LidarCache from multiprocessing import Process diff --git a/persontracking/lidar_tracker_pb2.py b/tracking/lidar_tracker_pb2.py similarity index 100% rename from persontracking/lidar_tracker_pb2.py rename to tracking/lidar_tracker_pb2.py diff --git a/persontracking/lidar_tracker_pb2_grpc.py b/tracking/lidar_tracker_pb2_grpc.py similarity index 97% rename from persontracking/lidar_tracker_pb2_grpc.py rename to tracking/lidar_tracker_pb2_grpc.py index 187f25f..e25a1f9 100644 --- a/persontracking/lidar_tracker_pb2_grpc.py +++ b/tracking/lidar_tracker_pb2_grpc.py @@ -1,7 +1,7 @@ # Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! 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):