Skip to content

Commit

Permalink
Merge pull request #1 from Tom-Hirschberger/development
Browse files Browse the repository at this point in the history
added possibility to set qos and retain flag either in mqtt configura…
  • Loading branch information
Tom-Hirschberger authored Nov 23, 2022
2 parents 8adbea8 + a98126a commit 6cc3302
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,4 @@ The following example contains all possible configuration options:
]
}
```
>>>>>>> main
6 changes: 5 additions & 1 deletion flowercare.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
"host": "10.1.1.1",
"port": 1883,
"username": "test",
"password": "test123"
"password": "test123",
"retain": False,
"qos": 0
},
"sensors": [
{
"topic": "flowercare/flowerone",
"qos": 1,
"retain": true,
"interval": 1,
"name": "flowerone",
"mac": "AA:BB:CC:DD:EE:FF",
Expand Down
12 changes: 8 additions & 4 deletions flowercare.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def callback_on_message(client, userdata, message):
print ("Received message %s for topic %s" %(message_str, message.topic))


def publish_values(topic,values):
def publish_values(topic,values,qos=0,retain=False):
if client != None and client.connected_flag:
client.publish(topic,json.dumps(values))
client.publish(topic,json.dumps(values),qos=qos,retain=retain)
else:
print("Client is not initialized or not connected!")

Expand Down Expand Up @@ -134,8 +134,12 @@ def restoreSkipConfig(intervalConfig=0, key=None):

print("Sensor: %s" %cur_sensor_config["name"])
pprint.pprint(cur_result, indent=2)

publish_values(cur_sensor_config["topic"], cur_result)

curQos = cur_sensor_config.get("qos", config.get("mqtt",{}).get("qos", 0))
curRetain = cur_sensor_config.get("retain", config.get("mqtt",{}).get("retain", False))

print("Publishing to topic %s with qos %d and retain flag set to %s!" %(cur_sensor_config["topic"],curQos,curRetain))
publish_values(cur_sensor_config["topic"], cur_result, curQos, curRetain)
except Exception:
print("Problems while reading sensor %s. Skipping!" % cur_sensor_config["name"])
# traceback.print_exc()
Expand Down

0 comments on commit 6cc3302

Please sign in to comment.