44# Author: Ian Patterson
55# Contact Email: ian@botts-inc.com
66# ==============================================================================
7+ from __future__ import annotations
8+
79import websockets
810from consys4py .comm .mqtt import MQTTCommClient
911from consys4py .datamodels .commands import CommandJSON
@@ -18,16 +20,17 @@ class ControlSchema:
1820
1921class ControlStream :
2022 name : str = None
21- _parent_systems : System = None
23+ _parent_system : System = None
2224 _strategy : str = "mqtt"
2325 _resource_endpoint = None
2426 # _auth: str = None
2527 _websocket : websockets .WebSocketServerProtocol = None
2628 _schema : ControlStreamJSONSchema = None
2729 _mqtt_client : MQTTCommClient = None
30+ _id : str = None
2831
2932 def __init__ (self , parent_system : System , resource_endpoint : str , name = None , strategy = "mqtt" ):
30- self ._parent_systems = parent_system
33+ self ._parent_system = parent_system
3134 self .name = name
3235 self ._strategy = strategy
3336 self ._resource_endpoint = resource_endpoint
@@ -46,7 +49,15 @@ def subscribe(self):
4649 elif self ._strategy == "websocket" :
4750 pass
4851
49- def publish (self , payload : CommandJSON ):
52+ def publish_status (self , payload : CommandJSON ):
53+ if self ._strategy == "mqtt" and self ._mqtt_client is not None :
54+ self ._mqtt_client .publish (f'{ self ._resource_endpoint } /status' , payload = payload , qos = 1 )
55+ elif self ._strategy == "mqtt" and self ._mqtt_client is None :
56+ raise ValueError ("No MQTT Client found." )
57+ elif self ._strategy == "websocket" :
58+ pass
59+
60+ def publish_command (self , payload : CommandJSON ):
5061 if self ._strategy == "mqtt" and self ._mqtt_client is not None :
5162 self ._mqtt_client .publish (f'{ self ._resource_endpoint } /status' , payload = payload , qos = 1 )
5263 elif self ._strategy == "mqtt" and self ._mqtt_client is None :
@@ -60,6 +71,45 @@ def disconnect(self):
6071 def unsubscribe (self ):
6172 self ._mqtt_client .unsubscribe (f'{ self ._resource_endpoint } /commands' )
6273
74+ def get_schema (self ):
75+ return self ._schema
76+
77+ def set_id (self , id : str ):
78+ self ._id = id
79+
80+ def get_id (self ):
81+ return self ._id
82+
83+ def add_status_listener (self , listener : callable ):
84+ """
85+ Adds a callback function which will be called when a status message is received via MQTT.
86+ :param listener: callback function that executes when the status message is received.
87+ :return: None
88+ """
89+ self ._mqtt_client .subscribe (f'{ self ._resource_endpoint } /status' , msg_callback = listener )
90+
91+ def add_command_listener_and_callback (self , callback : callable ):
92+ """
93+ Creates a listener for the command stream and adds a callback function which will be called when a command message is received.
94+ """
95+ self ._mqtt_client .subscribe (f'{ self ._resource_endpoint } /commands' , msg_callback = callback )
96+
97+ def send_command (self , command : CommandJSON | Command ):
98+ if isinstance (command , CommandJSON ):
99+ self .publish_command (command )
100+ elif isinstance (command , Command ):
101+ self .publish_status (command .record )
102+
63103
64104class Command :
65- pass
105+ _id : str = None
106+ record : CommandJSON = None
107+ status : str = None
108+ _parent_stream : ControlStream = None
109+
110+ def send (self ):
111+ self ._parent_stream .send_command (self )
112+
113+ def update_status (self , status_val : str ):
114+ self ._parent_stream .publish_status (status_val )
115+
0 commit comments