scripts: add new module boilerplate

This commit is contained in:
Romain Bazile 2021-02-27 10:17:36 +01:00
parent d42fbe2cff
commit b69e088f41
2 changed files with 106 additions and 0 deletions

View file

@ -129,6 +129,13 @@ if __name__ == "__main__":
segmenter_thread = planktoscope.segmenter.SegmenterProcess(shutdown_event) segmenter_thread = planktoscope.segmenter.SegmenterProcess(shutdown_event)
segmenter_thread.start() 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") logger.info("Starting the display module")
display = planktoscope.display.Display() display = planktoscope.display.Display()
@ -155,8 +162,12 @@ if __name__ == "__main__":
stepper_thread.join() stepper_thread.join()
imager_thread.join() imager_thread.join()
segmenter_thread.join() segmenter_thread.join()
# Uncomment this for clean shutdown
# module_thread.join()
stepper_thread.close() stepper_thread.close()
imager_thread.close() imager_thread.close()
segmenter_thread.close() segmenter_thread.close()
# Uncomment this for clean shutdown
# module_thread.close()
display.stop() display.stop()
logger.info("Bye") logger.info("Bye")

View file

@ -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