https://github.com/postor/sync-mqtt
a python lib for easy sync data with mqtt
https://github.com/postor/sync-mqtt
Last synced: 4 months ago
JSON representation
a python lib for easy sync data with mqtt
- Host: GitHub
- URL: https://github.com/postor/sync-mqtt
- Owner: postor
- License: mit
- Created: 2024-07-05T11:42:52.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-07-05T11:55:24.000Z (11 months ago)
- Last Synced: 2025-01-27T16:51:21.084Z (5 months ago)
- Language: Python
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# syncmqtt
sync your data with mqtt
## example usage:
```
from syncmqtt import qtt, attr, sub, conn@qtt()
class SyncedMqtt:
# push_on_set will pub to topic when assign new value
# sync wil update value when new message from topic
# json means value are serialized before send and parsed after receive
test_a = attr(val='test_a', topic='/a', push_on_set=True, sync=True, retained=True)
test_b = attr(val=20, topic='/b', push_on_set=False, sync=True, json=True)
test_c = attr(val='test_c', topic='/c', push_on_set=True, sync=False, retained=False)
@sub(topic='/c', json=True) # make sure test_cb is called when topic='/c' got new message
def test_cb(self, val):
print(val)
self.test_a = valasync def main():
conn.connect(broker='mqtt://localhost:8000', live_topic='/test_status')
# init with last_will off => /test_status
# pub on => /test_status retained, let other know I'm online
# during conn.connect
sq = SyncedMqtt() # if use qttclass before conn.connect, raise errorsq.test_c = 'hello'
# pub hello => /c
# test_cb
# print hello
# set test_a
# pub hello => /aconn.destory() # stop connection
# mqtt connect broken and off => /test_status base on last wil setting```