44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
""" Module to Store Sensor Data into InfluxDB """
|
|
|
|
from influxdb import InfluxDBClient
|
|
from copy import copy
|
|
|
|
class InfluxDBWriter(object):
|
|
""" Class to Represent the RRD Data of a Single Multi-Value Sensor """
|
|
|
|
def __init__(self, host, port=8086, dbname="sensors", user=None, passwd=None):
|
|
""" Initialize Class
|
|
|
|
Arguments:
|
|
host : InfluxDB Hostname (mandatory)
|
|
port: InfluxDB HTTP Port
|
|
dbname: Name of the InfluxDB database
|
|
user: Username
|
|
pass: Passwort (mandatory if user is set)
|
|
"""
|
|
|
|
self.__host = host
|
|
self.__port = port
|
|
self.__dbname = dbname
|
|
if user != None and passwd != None:
|
|
self.__client = InfluxDBClient(host, port, user, passwd, dbname)
|
|
else:
|
|
self.__client = InfluxDBClient(host, port, database=dbname)
|
|
|
|
|
|
def notify(self, msg):
|
|
""" Notify Method Called from the Receiver Class, Inserts Values into InfluxDB
|
|
|
|
Arguments:
|
|
msg: Object Representation of the Sensor Data
|
|
"""
|
|
tmp = copy(msg)
|
|
json_body = [{"measurement": str(tmp['sid']), "fields": {}}]
|
|
tmp.pop('sid', None)
|
|
json_body[0]["fields"] = tmp
|
|
|
|
try:
|
|
self.__client.write_points(json_body)
|
|
except:
|
|
print("Writing to InfluxDB Failed")
|