-
Notifications
You must be signed in to change notification settings - Fork 0
/
influx_client.py
executable file
·89 lines (75 loc) · 2.99 KB
/
influx_client.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import paho.mqtt.client as mqtt
from influxdb import InfluxDBClient
import influxdb
import requests
import datetime
import logging as log
import cfg
from time import sleep
import socket
# -------------------- mqtt events --------------------
def on_connect(lclient, userdata, flags, rc):
log.info("mqtt connected with result code "+str(rc))
lclient.subscribe("Nodes/#")
def on_message(client, userdata, msg):
topic_parts = msg.topic.split('/')
try:
if( (len(topic_parts) == 3) and (topic_parts[0] == "Nodes") ):
nodeid = topic_parts[1]
sensor = topic_parts[2]
measurement = "node"+nodeid
value = float(str(msg.payload))
post = [
{
"measurement": measurement,
"time": datetime.datetime.utcnow(),
"fields": {
sensor: value
}
}
]
try:
clientDB.write_points(post)
log.debug(msg.topic+" "+str(msg.payload)+" posted")
except requests.exceptions.ConnectionError:
log.error("ConnectionError sample skipped "+msg.topic)
except influxdb.exceptions.InfluxDBServerError:
log.error("InfluxDBServerError sample skipped "+msg.topic)
except ValueError:
log.error(" ValueError with : "+msg.topic+" "+str(msg.payload))
def mqtt_connect_retries():
connected = False
while(not connected):
try:
clientMQTT.connect(config["mqtt"]["host"], config["mqtt"]["port"], config["mqtt"]["keepalive"])
connected = True
except socket.error:
log.error("socket.error will try a reconnection in 10 s")
sleep(10)
return
config = cfg.get_local_json("config.json")
# -------------------- logging --------------------
log.basicConfig( filename=config["influxdb"]["logfile"],
level=log.INFO,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%d %H:%M:%S'
)
log.getLogger('').addHandler(log.StreamHandler())
log.info("influx client started")
# -------------------- influxDB client --------------------
clientDB = InfluxDBClient( config["influxdb"]["host"],
config["influxdb"]["port"],
'root', 'root',
config["influxdb"]["db"])
#clientDB.create_database(config["influxdb"]["db"])
#print("database created")
#clientDB.write_points(post)
#result = clientDB.query('select temperature from node15;')
#print("Query Result: {0}".format(result))
# -------------------- Mqtt Client --------------------
cid = config["influxdb"]["mqtt_client_id"] +"_"+socket.gethostname()
clientMQTT = mqtt.Client(client_id=cid)
clientMQTT.on_connect = on_connect
clientMQTT.on_message = on_message
mqtt_connect_retries()
clientMQTT.loop_forever()