micrOSDevToolKit 2.10.2__py3-none-any.whl → 2.10.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of micrOSDevToolKit might be problematic. Click here for more details.

Files changed (44) hide show
  1. micrOS/release_info/micrOS_ReleaseInfo/system_analysis_sum.json +28 -20
  2. micrOS/source/Common.py +2 -2
  3. micrOS/source/Espnow.py +245 -123
  4. micrOS/source/Files.py +101 -0
  5. micrOS/source/LM_espnow.py +10 -7
  6. micrOS/source/LM_mqtt_pro.py +211 -0
  7. micrOS/source/LM_oled_ui.py +18 -5
  8. micrOS/source/LM_pacman.py +37 -57
  9. micrOS/source/LM_rest.py +3 -3
  10. micrOS/source/LM_system.py +1 -1
  11. micrOS/source/Logger.py +5 -11
  12. micrOS/source/Shell.py +33 -28
  13. micrOS/source/Tasks.py +2 -2
  14. micrOS/source/__pycache__/Common.cpython-312.pyc +0 -0
  15. micrOS/source/__pycache__/Logger.cpython-312.pyc +0 -0
  16. micrOS/source/microIO.py +3 -2
  17. micrOS/source/urequests.py +10 -1
  18. {micrOSDevToolKit-2.10.2.dist-info → microsdevtoolkit-2.10.6.dist-info}/METADATA +3 -2
  19. {micrOSDevToolKit-2.10.2.dist-info → microsdevtoolkit-2.10.6.dist-info}/RECORD +44 -40
  20. {micrOSDevToolKit-2.10.2.dist-info → microsdevtoolkit-2.10.6.dist-info}/WHEEL +1 -1
  21. toolkit/Gateway.py +3 -3
  22. toolkit/simulator_lib/__pycache__/machine.cpython-312.pyc +0 -0
  23. toolkit/simulator_lib/__pycache__/sim_console.cpython-312.pyc +0 -0
  24. toolkit/simulator_lib/__pycache__/uos.cpython-312.pyc +0 -0
  25. toolkit/simulator_lib/uos.py +5 -5
  26. toolkit/user_data/webhooks/generic.py +1 -1
  27. toolkit/user_data/webhooks/macro.py +1 -1
  28. toolkit/user_data/webhooks/template.py +1 -1
  29. toolkit/workspace/precompiled/Espnow.mpy +0 -0
  30. toolkit/workspace/precompiled/Files.mpy +0 -0
  31. toolkit/workspace/precompiled/LM_espnow.py +10 -7
  32. toolkit/workspace/precompiled/LM_mqtt_pro.py +211 -0
  33. toolkit/workspace/precompiled/LM_oled_ui.mpy +0 -0
  34. toolkit/workspace/precompiled/LM_pacman.mpy +0 -0
  35. toolkit/workspace/precompiled/LM_rest.mpy +0 -0
  36. toolkit/workspace/precompiled/LM_system.mpy +0 -0
  37. toolkit/workspace/precompiled/Logger.mpy +0 -0
  38. toolkit/workspace/precompiled/Shell.mpy +0 -0
  39. toolkit/workspace/precompiled/Tasks.mpy +0 -0
  40. toolkit/workspace/precompiled/microIO.mpy +0 -0
  41. toolkit/workspace/precompiled/urequests.mpy +0 -0
  42. {micrOSDevToolKit-2.10.2.data → microsdevtoolkit-2.10.6.data}/scripts/devToolKit.py +0 -0
  43. {micrOSDevToolKit-2.10.2.dist-info → microsdevtoolkit-2.10.6.dist-info/licenses}/LICENSE +0 -0
  44. {micrOSDevToolKit-2.10.2.dist-info → microsdevtoolkit-2.10.6.dist-info}/top_level.txt +0 -0
@@ -6,13 +6,14 @@ def load():
6
6
  """
7
7
  return Espnow.initialize()
8
8
 
9
- def send(peer, msg='modules'):
9
+ def send(peer:bytes|str, msg:str='modules'):
10
10
  """
11
11
  Send message to peer (by mac address)
12
12
  :param peer: mac address of espnow device
13
13
  :param msg: message string/load module call
14
14
  """
15
- return Espnow.espnow_send(peer, msg)
15
+ now = Espnow.initialize()
16
+ return now.send(peer, msg)
16
17
 
17
18
  def start_server():
18
19
  """
@@ -20,21 +21,23 @@ def start_server():
20
21
  - this can receive espnow messages
21
22
  - it includes Load Module execution logic (beta)
22
23
  """
23
- return Espnow.espnow_server()
24
+ now = Espnow.initialize()
25
+ return now.start_server()
24
26
 
25
27
  def stats():
26
28
  """
27
29
  Get ESPNOW stats
28
30
  """
29
- return Espnow.stats()
31
+ now = Espnow.initialize()
32
+ return now.stats()
30
33
 
31
- def add_peer(peer):
34
+ def add_peer(peer:bytes, dev_name:str=None):
32
35
  """
33
36
  Add ESPNOW peer to known hosts
34
37
  - It is needed before first send(...)
35
38
  """
36
39
  now = Espnow.initialize()
37
- return Espnow.add_peer(now, peer)
40
+ return now.add_peer(peer, dev_name)
38
41
 
39
42
  def mac_address():
40
43
  """
@@ -46,4 +49,4 @@ def help():
46
49
  """
47
50
  [beta] ESPNOW sender/receiver with LM execution
48
51
  """
49
- return 'load', 'send <peer> "ping"', 'start_server', 'add_peer <peer>', 'stats', 'mac_address'
52
+ return 'load', 'send <peer> "ping"', 'start_server', 'add_peer <peer> dev_name=None', 'stats', 'mac_address'
@@ -0,0 +1,211 @@
1
+ # async_message.py Test of asynchronous mqtt client with async Broker class
2
+ # (C) Copyright Peter Hinch 2024.
3
+ # Released under the MIT licence.
4
+ # Public brokers https://github.com/mqtt/mqtt.github.io/wiki/public_brokers
5
+ # mip command: ???
6
+
7
+ from Config import cfgget
8
+ from mqtt_as import MQTTClient, config
9
+ from Common import micro_task, console, syslog, exec_cmd
10
+
11
+
12
+ # Set up MQTT
13
+ class MQTT:
14
+ CLIENT:MQTTClient = None # MQTT Client (broker) instance
15
+ TOPIC = "micros" # Default topic
16
+ TOPIC_COMMAND_LUT = {} # Lookup table for command/topic pairs
17
+ # Example:
18
+ # {"topic1": ["mod func", "mod2 func"], "topic2": []}
19
+
20
+
21
+ async def _receiver(task):
22
+ """
23
+ MQTT AS receiver loop
24
+ """
25
+ async for topic, msg, retained in MQTT.CLIENT.queue:
26
+ topic, msg = topic.decode(), msg.decode()
27
+ console(f'Topic: "{topic}" Message: "{msg}" Retained: {retained}')
28
+
29
+ # Command execution... use MQTT.TOPIC_COMMAND_LUT
30
+ topic_commands:list = MQTT.TOPIC_COMMAND_LUT.get(topic, None)
31
+ output_struct:list = []
32
+ if topic_commands is None:
33
+ syslog(f"[WARN] mqtt Unknown topic: {topic}")
34
+ elif len(topic_commands) == 0:
35
+ syslog(f"[WARN] mqtt No commands for {topic}")
36
+ else:
37
+ task.out = f"Handle topic: {topic}"
38
+ for cmd in topic_commands:
39
+ single_command = cmd.split()
40
+ if len(single_command) > 0:
41
+ state, output = exec_cmd(single_command, jsonify=True, skip_check=True)
42
+ output_struct.append({"state": state, "result": output, "cmd": cmd})
43
+ if len(output_struct) > 0:
44
+ console(f'\tMQTT Publish: {output_struct}')
45
+ task.out = f"Publish {topic}"
46
+ MQTT.CLIENT.publish(topic, str(output_struct))
47
+ else:
48
+ task.out = f"Nothing to publish {topic}"
49
+ task.feed()
50
+
51
+
52
+ async def _subscribe():
53
+ """
54
+ MQTT AS Topic subscribe towards server
55
+ """
56
+ with micro_task(tag="mqtt.subscribe") as my_task:
57
+ my_task.out = "Started"
58
+ try:
59
+ for t in MQTT.TOPIC_COMMAND_LUT:
60
+ console(f"Subscribe topic: {t}")
61
+ await MQTT.CLIENT.subscribe(t, 1)
62
+ my_task.out = "Done"
63
+ except Exception as e:
64
+ my_task.out = f"Error: {e}"
65
+
66
+
67
+ async def _publish(message, topic):
68
+ """
69
+ Send message to topic with mqtt
70
+ """
71
+ tag = f"mqtt.publish.{topic}"
72
+ with micro_task(tag=tag) as my_task:
73
+ console(f"mqtt send: [{topic}] {message}")
74
+ await MQTT.CLIENT.publish(topic, message, qos=1)
75
+ my_task.out = "Sent"
76
+
77
+
78
+ async def _up():
79
+ """
80
+ UP Listener - resubscribe
81
+ """
82
+ with micro_task(tag="mqtt.up") as my_task:
83
+ while True:
84
+ # Wait for UP Event - (re)subscribe
85
+ my_task.out = "Wait"
86
+ await MQTT.CLIENT.up.wait()
87
+ MQTT.CLIENT.up.clear()
88
+ micro_task(tag="mqtt.subscribe", task=_subscribe())
89
+ my_task.out = "Re-Subscription"
90
+ my_task.feed()
91
+
92
+
93
+ async def _init_client(topic:str=None, commands:str=None, raw_dict:dict|None=None):
94
+ """
95
+ Initialize main mqtt receiver and topics
96
+ :param topic: topic string, ex.: 'lights'
97
+ :param commands: semicolon separated commands. ex.: 'rgb toggle; cct toggle'
98
+ OR
99
+ :param raw_dict: python dict string for multi topic subscription, ex.: {"lights": ["rgb toggle", "cct toggle"], ...}
100
+ """
101
+ with micro_task(tag="mqtt.client") as my_task:
102
+ try:
103
+ await MQTT.CLIENT.connect()
104
+ my_task.out = "Connection successful."
105
+ except OSError:
106
+ my_task.out = "Connection failed."
107
+ return
108
+ # Wait for mqtt client connected successfully
109
+ await MQTT.CLIENT.up.wait()
110
+ MQTT.CLIENT.up.clear()
111
+ # Initialize mqtt topics, ha
112
+ subscribe(topic, commands, raw_dict)
113
+ micro_task(tag="mqtt.up", task=_up())
114
+ # Async listener loop
115
+ await _receiver(my_task)
116
+ my_task.out = "Receiver closed"
117
+ # Close when listener exits
118
+ MQTT.CLIENT.close()
119
+
120
+ #########################################
121
+ # PUBLIC FUNCTIONS #
122
+ #########################################
123
+
124
+ def publish(message:str, topic:str=MQTT.TOPIC):
125
+ """
126
+ Publish message
127
+ :param message: string to be sent
128
+ :param topic: topic for message
129
+ """
130
+ state = micro_task(tag=f"mqtt.publish.{topic}", task=_publish(message, topic))
131
+ state = "starting" if state else "already running"
132
+ return f"Message send, {state}"
133
+
134
+
135
+ def subscribe(topic:str=None, commands:str=None, raw_dict:dict|None=None):
136
+ """
137
+ Subscribe for single topics and set callback function(s) aka command(s)
138
+ :param topic: topic string, ex.: 'lights'
139
+ :param commands: semicolon separated commands. ex.: 'rgb toggle; cct toggle'
140
+ OR
141
+ :param raw_dict: python dict string for multi topic subscription, ex.: {"lights": ["rgb toggle", "cct toggle"], ...}
142
+
143
+ return: all or selected topics command
144
+ """
145
+ updated = False
146
+ topic = topic.strip()
147
+ # Register single topic
148
+ if topic and commands:
149
+ # raw commands structure: 'rgb toggle; cct toggle'
150
+ commands = [ c.strip() for c in commands.split(";") ]
151
+ # commands: Topic LUT structure: {'topic': ['mod func'], ..., 'lights': ['rgb toggle', 'cct toggle']}
152
+ updated = True if MQTT.TOPIC_COMMAND_LUT.get(topic, None) is None else False
153
+ MQTT.TOPIC_COMMAND_LUT[topic] = commands
154
+ # Register multiple topics at once
155
+ elif isinstance(raw_dict, dict):
156
+ updated = True
157
+ MQTT.TOPIC_COMMAND_LUT.update(raw_dict)
158
+ # Start subscribe task
159
+ if updated:
160
+ state = micro_task(tag="mqtt.subscribe", task=_subscribe())
161
+ state = "starting" if state else "already running"
162
+ return f"Subscribe, {state}"
163
+
164
+ # Return handling
165
+ if topic is not None:
166
+ # Return selected topic commands
167
+ return MQTT.TOPIC_COMMAND_LUT.get(topic, None)
168
+ # Return registered topics
169
+ return MQTT.TOPIC_COMMAND_LUT
170
+
171
+
172
+ def _configure(server_ip:str, username:str, password:str):
173
+ # Define configuration
174
+ config["keepalive"] = 120
175
+ config["queue_len"] = 1 # Use event interface with default queue
176
+ # Define configuration
177
+ config['client_id'] = cfgget("devfid")
178
+ config['ssid'] = cfgget("staessid")
179
+ config['wifi_pw'] = cfgget("stapwd")
180
+ config['port'] = 1883 # expose????
181
+ config['server'] = server_ip # '172.20.10.2'
182
+ config['user'] = username # test
183
+ config['password'] = password # '12345'
184
+ return config
185
+
186
+
187
+ def load(server_ip:str, username:str, password:str, topic:str=None, commands:str=None, raw_dict:dict|None=None):
188
+ """
189
+ Load MQTT_AS receiver...
190
+ :param server_ip: server IP address
191
+ :param username: server user
192
+ :param password: server user password
193
+
194
+ :param topic: topic string, ex.: 'lights'
195
+ :param commands: semicolon separated commands. ex.: 'rgb toggle; cct toggle'
196
+ OR
197
+ :param raw_dict: python dict string for multi topic subscription, ex.: {"lights": ["rgb toggle", "cct toggle"], ...}
198
+ """
199
+ MQTTClient.DEBUG = True
200
+ MQTT.CLIENT = MQTTClient(_configure(server_ip, username, password))
201
+
202
+ state = micro_task(tag="mqtt.client", task=_init_client(topic, commands, raw_dict))
203
+ return "Starting" if state else "Already running"
204
+
205
+
206
+ def help():
207
+ return ("load <server_ip> <username> <password> topic='micros', commands='rgb toggle; cct toggle'",
208
+ "subscribe topic='micros', commands='rgb toggle; cct toggle'",
209
+ "subscribe #without params dumps the topic-command data structure",
210
+ "publish message='hello' topic='micros'",
211
+ "HINT: task show mqtt.*")
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file