-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeedmqtt_decoder.py
41 lines (32 loc) · 1.29 KB
/
feedmqtt_decoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import paho.mqtt.client as mqtt
from datetime import datetime, timezone
import json
with open('config_local.json') as json_file:
config = json.load(json_file)
def get_msg_to_publish(msg):
sid = msg.topic.split('/')[1]
ts = str(((datetime.now())
.replace(tzinfo = timezone.utc))
.timestamp())
publish_msg = '{\"acp_id\":\"'+sid+'\", \"acp_ts\": \"'+ts+'\", \"payload\": ' + str(msg.payload.decode("utf-8"))+'}'
return(sid,publish_msg)
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe(config['mqttTopic'])
def on_message(client, userdata, msg):
#print(msg.topic+" "+str(msg.payload.decode("utf-8")))
#print(msg.topic)
sensor_id, pub_msg = get_msg_to_publish(msg)
topic = 'tfc_prod/'+sensor_id
#print(topic,pub_msg)
pubClient = mqtt.Client()
pubClient.connect('localhost', 1883)
ret = pubClient.publish(topic, pub_msg)
client = mqtt.Client()
client.username_pw_set(username=config['username'],password=config['password'])
client.on_connect = on_connect
client.on_message = on_message
client.connect(config['hostname'])
client.loop_forever()