From b69e088f41bb1a57a528436fb8d3664d3e56cd8e Mon Sep 17 00:00:00 2001 From: Romain Bazile Date: Sat, 27 Feb 2021 10:17:36 +0100 Subject: [PATCH] scripts: add new module boilerplate --- scripts/main.py | 11 ++++ scripts/planktoscope/module.py | 95 ++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 scripts/planktoscope/module.py diff --git a/scripts/main.py b/scripts/main.py index dbae999..1cf598c 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -129,6 +129,13 @@ if __name__ == "__main__": segmenter_thread = planktoscope.segmenter.SegmenterProcess(shutdown_event) segmenter_thread.start() + + # Starts the module process + # Uncomment here as needed + # logger.info("Starting the module process") + # module_thread = planktoscope.module.ModuleProcess(shutdown_event) + # module_thread.start() + logger.info("Starting the display module") display = planktoscope.display.Display() @@ -155,8 +162,12 @@ if __name__ == "__main__": stepper_thread.join() imager_thread.join() segmenter_thread.join() + # Uncomment this for clean shutdown + # module_thread.join() stepper_thread.close() imager_thread.close() segmenter_thread.close() + # Uncomment this for clean shutdown + # module_thread.close() display.stop() logger.info("Bye") \ No newline at end of file diff --git a/scripts/planktoscope/module.py b/scripts/planktoscope/module.py new file mode 100644 index 0000000..0912777 --- /dev/null +++ b/scripts/planktoscope/module.py @@ -0,0 +1,95 @@ +################################################################################ +# Practical Libraries +################################################################################ +# Logger library compatible with multiprocessing +from loguru import logger + +import os, time + +# Library for starting processes +import multiprocessing + +# Basic planktoscope communication libraries +import planktoscope.mqtt + +logger.info("planktoscope.module is loaded") + +################################################################################ +# Main Segmenter class +################################################################################ +class ModuleProcess(multiprocessing.Process): + """This class contains the main definitions for the module of the PlanktoScope""" + + @logger.catch + def __init__(self, event): + """Initialize the Module class + + Args: + event (multiprocessing.Event): shutdown event + """ + super(ModuleProcess, self).__init__(name="light") + + logger.info("planktoscope.module is initialising") + + # Do all your initialisation here + + logger.success("planktoscope.mdule is initialised and ready to go!") + + @logger.catch + def treat_message(self): + action = "" + if self.module_client.new_message_received(): + logger.info("We received a new message") + last_message = self.module_client.msg["payload"] + logger.debug(last_message) + self.module_client.read_message() + if "action" not in last_message: + logger.error( + f"The received message has the wrong argument {last_message}" + ) + self.module_client.client.publish("status/module", '{"status":"Error"}') + return + action = last_message["action"] + if action == "on": + # Treat the received messages here + + elif action != "": + logger.warning( + f"We did not understand the received request {action} - {last_message}" + ) + + ################################################################################ + # While loop for capturing commands from Node-RED + ################################################################################ + @logger.catch + def run(self): + """This is the function that needs to be started to create a thread""" + logger.info( + f"The module control thread has been started in process {os.getpid()}" + ) + + # MQTT Service connection + self.module_client = planktoscope.mqtt.MQTT_Client( + topic="module/#", name="module_client" + ) + + # Publish the status "Ready" to via MQTT to Node-RED + self.module_client.client.publish("status/module", '{"status":"Ready"}') + + logger.success("Module is READY!") + + # This is the loop + while not self.stop_event.is_set(): + self.treat_message() + time.sleep(0.1) + + logger.info("Shutting down the module process") + # Do your deinit and ressources cleanup here + self.module_client.client.publish("status/module", '{"status":"Dead"}') + self.module_client.shutdown() + logger.success("Module process shut down! See you!") + + +# This is called if this script is launched directly +if __name__ == "__main__": + # This should be tests of your module \ No newline at end of file