Переход на mosquitto
[weathermon.git] / weathermon-mqtt
index 6c5fd378d949bcefddb8194820febff88f6e44fe..c6b6a82db1f6959e4bd297eb0fb868f5d09551de 100755 (executable)
@@ -1,28 +1,40 @@
-#!/usr/bin/python
+#!/usr/bin/python -u
 
 import paho.mqtt.client as paho
 import sys
 from ConfigParser import ConfigParser
 import MySQLdb
 from pprint import pprint
+import json
+from dateutil.parser import parser
+tparser=parser()
 
 def on_message(mosq, obj, msg):
   topic=msg.topic
-  payload=msg.payload
-  try:
-    c = database.cursor()
-    c.execute('CALL meteo.submit_mqtt(%s,%s,NULL)', (topic,payload))
-    database.commit()
-    print topic,payload
-  except:
-    print "Failed to submit data"  
+  payload=json.loads(msg.payload)
+  timestamp=tparser.parse(payload['Time'])
+  for sensor_type in payload:
+    if sensor_type != 'Time' and sensor_type != 'TempUnit':
+      sensor_data=payload[sensor_type]
+      for param in sensor_data:
+        try:
+          value=sensor_data[param]
+          try:
+            c = database.cursor()
+            c.execute('CALL meteo.submit_mqtt(%s,%s,%s,%s,NULL)', (topic,sensor_type,param,value))
+            database.commit()
+            print topic,sensor_type,param,value
+          except:
+            print "Failed to submit data"
+        except:
+          None    
 
 def Topics():
 
   c = database.cursor()
   c.execute(
     '''
-    select topic from mqtt_topics
+    select topic from mqtt_topics where topic<>""
     '''
   )