Skip to content

Commit

Permalink
added last_seen, retain and qos
Browse files Browse the repository at this point in the history
  • Loading branch information
wassfila committed Jan 4, 2020
1 parent 5cdba46 commit ab93be9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"python.pythonPath": "D:\\Python36\\python.exe"
"python.pythonPath": "D:\\Python36\\python.exe",
"liveServer.settings.port": 5501
}
13 changes: 6 additions & 7 deletions py/nrf_mesh/config_WassDell.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"serial":{
"port":"COM10",
"port":"COM9",
"baud":460800
},
"mesh":{
Expand All @@ -12,16 +12,15 @@
"enable":true,
"subscribe" :false,
"publish" :true,
"base_topic" : "nrf",
"base_topic" : "pc_nrf",
"last_seen":true,
"retain":true,
"qos":1,
"host":"10.0.0.42",
"port":1883,
"keepalive":60,
"client_id":"nrf_mesh",
"subscriptions":[
"Nodes/#",
"cmd/request/#",
"remote_cmd/request/#"
],
"subscriptions":[],
"actions" :["dimmer","heat","ping"]
},
"log":{
Expand Down
3 changes: 3 additions & 0 deletions py/nrf_mesh/config_lifo.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"subscribe" :false,
"publish" :true,
"base_topic" : "nrf",
"last_seen":true,
"retain":true,
"qos":1,
"host":"10.0.0.42",
"port":1883,
"keepalive":60,
Expand Down
6 changes: 3 additions & 3 deletions py/nrf_mesh/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def publish(msg):
path_entry["nodeid"] = txpow_rssi_nid[2]
json_payload["path"].append(path_entry)
json_payload["rssi"] = int(last_rssi)
pub[topic] = json.dumps(json_payload)
pub[topic] = json_payload
elif(inv_pid[int(msg["pid"])] == "bme280"):
if("temp" in msg):
json_payload["temperature"] = float(msg["temp"])
Expand All @@ -184,15 +184,15 @@ def publish(msg):
json_payload["x"] = float(msg["accx"])
json_payload["y"] = float(msg["accy"])
json_payload["z"] = float(msg["accz"])
pub[topic] = json.dumps(json_payload)
pub[topic] = json_payload
elif(inv_pid[int(msg["pid"])] == "button"):
if(int(msg["button"]) == 1):
json_payload["button"] = "down"
else:
json_payload["button"] = "up"
elif(inv_pid[int(msg["pid"])] == "reset"):
json_payload["reset"] = float(msg["reset"])
pub[topic] = json.dumps(json_payload)
pub[topic] = json_payload
return pub

def line2dict(line):
Expand Down
29 changes: 28 additions & 1 deletion py/nrf_mesh/nrf_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import mesh as mesh
import cfg

import datetime
from tzlocal import get_localzone

#%%
def mesh_do_action(cmd,remote,params):
control = 0x71
Expand Down Expand Up @@ -98,6 +101,10 @@ def mqtt_on_message(client, userdata, msg):
mesh_do_action(cmd,topics[1],params)
return

def get_last_seen_now():
time_now = datetime.datetime.now()
return str(time_now.astimezone(local_zone))

'''It's important to provide the msg dictionnary here as it might be used in a multitude of ways
by other modules
'''
Expand All @@ -107,10 +114,17 @@ def mesh_on_broadcast(msg):
log.info(f"mesh> {msg['src']} as {node_name} => {sensor_name}")
if(config["mqtt"]["enable"] and config["mqtt"]["publish"]):
publishing = mesh.publish(msg)
use_last_seen = False
if("last_seen" in config["mqtt"]):
if(config["mqtt"]["last_seen"] == True):
last_seen = get_last_seen_now()
use_last_seen = True
for topic,payload in publishing.items():
if(config["mqtt"]["base_topic"]):
topic = config["mqtt"]["base_topic"]+ "/" + topic
clientMQTT.publish(topic,payload)
if(use_last_seen):
payload["last_seen"] = last_seen
clientMQTT.publish(topic,json.dumps(payload),mqtt_qos,mqtt_retain)
return

def mesh_on_message(msg):
Expand Down Expand Up @@ -198,6 +212,17 @@ def test1():
parser.add_argument("-f","--function",default="x")
args = parser.parse_args()

local_zone = get_localzone()
mqtt_qos = 0
if("qos" in config["mqtt"]):
mqtt_qos = config["mqtt"]["qos"]
if(mqtt_qos == 2):
print("qos 2 not supported")
sys.exit(1)
mqtt_retain = False
if("retain" in config["mqtt"]):
mqtt_retain = config["mqtt"]["qos"]

#will not start a separate thread for looping
clientMQTT = mqtt_start(config,mqtt_on_message,start_looping=False)

Expand All @@ -208,6 +233,8 @@ def test1():
this_node_id = 0
get_node_id()



try:
loop_forever()
except KeyboardInterrupt:
Expand Down

0 comments on commit ab93be9

Please sign in to comment.