2017-08-20 22:56:21 +02:00
|
|
|
# Pérenisation de données arrivant sur un MQTT
|
|
|
|
# vers une base de données MariaDb/MySQL
|
|
|
|
# Auteur : Arofarn
|
|
|
|
# v0.2
|
|
|
|
|
|
|
|
#################
|
|
|
|
# Configuration #
|
|
|
|
#################
|
|
|
|
|
|
|
|
###########
|
|
|
|
# IMPORTS #
|
|
|
|
###########
|
|
|
|
|
|
|
|
from cameteo import *
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
import json
|
2017-09-16 22:40:14 +02:00
|
|
|
from math import isnan
|
|
|
|
|
|
|
|
mqtt_client_id = "sql_persistance"
|
2017-08-20 22:56:21 +02:00
|
|
|
|
|
|
|
#############
|
|
|
|
# CALLBACKS #
|
|
|
|
#############
|
|
|
|
|
|
|
|
#Callback pour la connection au MQTT : souscriptions aux topics
|
|
|
|
def on_connect(client, userdata, flags, rc):
|
2017-09-16 01:36:47 +02:00
|
|
|
print(mqtt.connack_string(rc))
|
|
|
|
if rc == 0:
|
|
|
|
print("Subscribing to %s ..." % mqtt_topic)
|
2017-09-19 16:35:17 +02:00
|
|
|
client.subscribe([(mqtt_topic, 0),
|
|
|
|
(camera_mqtt_topic + "/last_photo", 0)
|
|
|
|
])
|
2017-09-16 01:36:47 +02:00
|
|
|
print("OK")
|
|
|
|
|
2017-08-20 22:56:21 +02:00
|
|
|
|
|
|
|
#Callback de gestion des messages arrivant au MQTT :
|
|
|
|
# affichage et enregistrement en base de données
|
|
|
|
def on_message(client, userdata, msg):
|
|
|
|
top=msg.topic[len(mqtt_topic)-1:].strip()
|
|
|
|
|
|
|
|
subtopics = top.split("/")
|
2017-09-16 01:36:47 +02:00
|
|
|
payload = msg.payload.decode()
|
2017-09-19 16:35:17 +02:00
|
|
|
#print(payload)
|
2017-08-20 22:56:21 +02:00
|
|
|
val = json.loads(payload)
|
|
|
|
|
|
|
|
#Test présence et cohérence de la valeur
|
|
|
|
try:
|
|
|
|
val['value'] = float(val['value'] )
|
|
|
|
except:
|
|
|
|
print("Value error: {}".format(val['value']))
|
|
|
|
val['value'] = float('nan')
|
|
|
|
|
|
|
|
#Remplacement de la valeur en cas de nullité
|
|
|
|
if isnan(val['value']) or val['value'] == -99.9:
|
|
|
|
val['value'] = 'NULL'
|
|
|
|
|
|
|
|
#Gestion du symbole des degrés parfois difficile pour certaines sources
|
|
|
|
val['unit'] = val['unit'].replace('deg', '°')
|
|
|
|
|
|
|
|
#print("%s : %s %s (%s)" % (val['type'], val['value'], val['unit'], subtopics[0]))
|
|
|
|
|
|
|
|
#Enregistrement des données en base
|
|
|
|
data= Data(valdate = datetime.strptime(val['date'], "%H:%M:%S %d/%m/%Y"),
|
2017-09-19 16:35:17 +02:00
|
|
|
dbdate = datetime.now(),
|
2017-08-20 22:56:21 +02:00
|
|
|
value = val['value'],
|
|
|
|
unit = val['unit'],
|
|
|
|
type_id = val['type'],
|
|
|
|
sensor_id = subtopics[0] )
|
|
|
|
|
|
|
|
print(data)
|
|
|
|
|
2017-09-16 01:36:47 +02:00
|
|
|
try:
|
|
|
|
db.session.add(data)
|
|
|
|
db.session.commit()
|
|
|
|
except:
|
|
|
|
print("Erreur lors de l'enregistrement en base de données")
|
|
|
|
finally:
|
|
|
|
db.session.close()
|
|
|
|
|
2017-08-20 22:56:21 +02:00
|
|
|
#Callback particulier pour gérer les date/heure arrivent sur le broker MQTT
|
|
|
|
def on_message_date(client, userdata, msg):
|
|
|
|
payload = msg.payload.decode()
|
|
|
|
try:
|
|
|
|
d = datetime.strptime(payload, "%H:%M:%S %d/%m/%Y")
|
|
|
|
print("Date : %s" % d)
|
|
|
|
except:
|
|
|
|
print("Date mal formatée : %s" % payload)
|
2017-09-19 16:35:17 +02:00
|
|
|
|
|
|
|
#Update display with info from camera (camera shooting new photo or name of the
|
|
|
|
# last photo)
|
|
|
|
def on_message_camera(client, userdata, msg):
|
|
|
|
pl = msg.payload.decode()
|
|
|
|
print("New photo : " + pl)
|
2017-08-20 22:56:21 +02:00
|
|
|
|
2017-09-19 16:35:17 +02:00
|
|
|
# New photo to add to database
|
|
|
|
photo = Photo(file_name = pl,
|
|
|
|
file_date = datetime.strptime(os.path.splitext(pl)[0], photo_name),
|
|
|
|
image_type = photo_format)
|
|
|
|
try:
|
|
|
|
db.session.add(photo)
|
|
|
|
db.session.commit()
|
|
|
|
except:
|
|
|
|
print("Erreur lors de l'enregistrement en base de données")
|
|
|
|
finally:
|
|
|
|
db.session.close()
|
|
|
|
|
2017-08-20 22:56:21 +02:00
|
|
|
#Callback de déconnexion au broker MQTT
|
|
|
|
def on_disconnect(client, userdata, msg):
|
|
|
|
if msg != 0:
|
|
|
|
print("Déconnexion imprévu : %s" % msg)
|
|
|
|
exit()
|
|
|
|
|
|
|
|
########
|
|
|
|
# Main #
|
|
|
|
########
|
|
|
|
|
|
|
|
#Connect to MQTT broker and loop...
|
|
|
|
mqtt_client = mqtt.Client(mqtt_client_id, clean_session=False)
|
2017-09-16 01:36:47 +02:00
|
|
|
mqtt_client.username_pw_set(mqtt_user, mqtt_pass)
|
2017-08-20 22:56:21 +02:00
|
|
|
mqtt_client.on_connect = on_connect
|
|
|
|
mqtt_client.on_message = on_message
|
|
|
|
mqtt_client.on_disconnect = on_disconnect
|
|
|
|
|
2017-09-19 16:35:17 +02:00
|
|
|
mqtt_client.message_callback_add(camera_mqtt_topic + "/last_photo", on_message_camera)
|
2017-08-20 22:56:21 +02:00
|
|
|
mqtt_client.message_callback_add("huzzah0/NTP/date", on_message_date)
|
|
|
|
|
|
|
|
mqtt_client.connect(mqtt_host, int(mqtt_port), 60)
|
|
|
|
|
|
|
|
mqtt_client.loop_forever()
|