sensorgauges
[svn42.git] / sensorvalues-to-json-zmq.py
index 6608086..d6bc31e 100755 (executable)
@@ -40,7 +40,7 @@ class UWSConfig:
     self.config_parser.add_section('json')
     self.config_parser.set('json','write_path',"/dev/shm/wget/r3sensors.json")
     self.config_parser.set('json','moveto_path',"/dev/shm/www/r3sensors.json")
-    self.config_parser.set('json','backup_path',"/home/guests/realraum.wirdorange.org/public_html/r3sensors.json.bak")
+    self.config_parser.set('json','backup_path',"/home/guests/realraum.wirdorange.org/r3sensors.json.bak")
     self.config_parser.set('json','backup_every',"50")
     self.config_parser.set('json','limit_list_len',"10000")
     self.config_parser.set('json','updateinterval',"30")
@@ -48,7 +48,7 @@ class UWSConfig:
     self.config_parser.set('zmq','remote_uri',"tcp://wuzzler.realraum.at:4244")
     self.config_parser.set('zmq','sshtunnel',"realraum@wuzzler.realraum.at:22000")
     self.config_parser.set('zmq','sshkeyfile',"/home/guests/realraum.wirdorange.org/id_rsa")
-    self.config_parser.set('zmq','subscribe',"TempSensorUpdate IlluminationSensorUpdate DustSensorUpdate RelativeHumiditySensorUpdate MovementSensorUpdate PresenceUpdate")
+    self.config_parser.set('zmq','subscribe',"TempSensorUpdate IlluminationSensorUpdate DustSensorUpdate RelativeHumiditySensorUpdate MovementSensorUpdate")
     self.config_parser.add_section('debug')
     self.config_parser.set('debug','enabled',"False")
     self.config_mtime=0
@@ -162,40 +162,43 @@ def exitHandler(signum, frame):
   sys.exit(0)
 
 time_column_name_="Time"
-lastest_values_ = {}
+latest_values_ = {}
 sensor_store_ = {}
 sensor_cols_num_ = {} #stores number of columns for a sensor not counting Time (x-axis) column. AKA the number of data-rows. Equals highest SensorIndex +1
 reset_these_structnames_ = {}
 
 def addEventToTempLastValueStore(structname, msgdata):
-    global lastest_values_
-    sensorindex = msgdata["Sensorindex"] if "Sensorindex" in msgdata else 0
-    if not structname in lastest_values_:
-        lastest_values_[structname]=[]
+    global latest_values_
+    sensorindex = int(msgdata["Sensorindex"]) if "Sensorindex" in msgdata else 0
+    if not structname in latest_values_:
+        latest_values_[structname]=[]
     if not structname in sensor_cols_num_ or sensor_cols_num_[structname] < sensorindex +1:
         sensor_cols_num_[structname] = sensorindex +1
-    if len(lastest_values_[structname]) < sensor_cols_num_[structname]:
-        lastest_values_[structname] += [0] * (sensor_cols_num_[structname] - len(lastest_values_[structname]))
+    if len(latest_values_[structname]) < sensor_cols_num_[structname]:
+        latest_values_[structname] += [0] * (sensor_cols_num_[structname] - len(latest_values_[structname]))
         expandSensorStoreLists(structname, sensor_cols_num_[structname])
     # store Value in temp last value store:
     try:
-        del dictdata["Sensorindex"]
-        del dictdata["Ts"]
+        del msgdata["Sensorindex"]
+    except:
+        pass
+    try:
+        del msgdata["Ts"]
     except:
         pass
     if len(msgdata) > 0:
         #store first value that is not Sensorindex or Ts into store
-        lastest_values_[structname][sensorindex] = msgdata.values()[0]
+        latest_values_[structname][sensorindex] = msgdata.values()[0]
     else:
         #if that value does not exist, (i.e. movementevent), count event occurances
-        lastest_values_[structname][sensorindex] += 1
+        latest_values_[structname][sensorindex] += 1
         reset_these_structnames_[structname] = True
 
 
 def cleanTempLastValueOfMovementValues():
-    global lastest_values_
+    global latest_values_
     for k in reset_these_structnames_.keys():
-        lastest_values_[k] = [0] * sensor_cols_num_[structname]
+        latest_values_[k] = [0] * sensor_cols_num_[k]
 
 
 def expandSensorStoreLists(structname, newlength):
@@ -205,26 +208,33 @@ def expandSensorStoreLists(structname, newlength):
     #remove old headings so we can add them again below
     try:
         if sensor_store_[structname][0][0] == time_column_name_:
-            sensor_store_[structname][0].pop(0)
+            sensor_store_[structname].pop(0)
     except:
         pass
     #expand all previous value lists
-    sensor_store_[structname] = map(lambda l: l + ([0] * (newlength +1 - len(l)))  , sensor_store_[structname])
+    newlength_including_time = newlength +1
+    sensor_store_[structname] = map(lambda l: l[:newlength_including_time] + ([0] * (newlength_including_time - len(l)))  , sensor_store_[structname])
 
 
 def addEventsToStore():
     global sensor_store_
     ts = int(time.time())
-    for structname in lastest_values_.keys():
+    for structname in latest_values_.keys():
         if not structname in sensor_store_:
             sensor_store_[structname]=[]
 
         #if missing, add Header List [Time, 0, 1, 2]
         if len(sensor_store_[structname]) == 0 or len(sensor_store_[structname][0]) < 2 or sensor_store_[structname][0][0] != time_column_name_:
-            sensor_store_[structname].insert(0,[time_column_name_] + list(map(str,range(0,sensor_cols_num_[structname]))))
+            sensor_store_[structname].insert(0,[time_column_name_] + list(map(lambda n: "Sensor %d"%n,range(0,sensor_cols_num_[structname]))))
 
         # add values
-        sensor_store_[structname].append([ts] + lastest_values_[structname])
+        try:
+            # if latest values are identical, just update timestamp
+            if sensor_store_[structname][-1][1:] == latest_values_[structname] and sensor_store_[structname][-1][1:] == sensor_store_[structname][-2][1:]:
+                sensor_store_[structname].pop()
+        except:
+            pass
+        sensor_store_[structname].append([ts] + latest_values_[structname])
 
         #cap list lenght
         if uwscfg.json_limit_list_len:
@@ -246,15 +256,26 @@ if __name__ == "__main__":
 
     try:
         with open(uwscfg.json_moveto_path,"rb") as fh:
-            sensor_store = json.loads(fh.read())
+            sensor_store_ = json.loads(fh.read())
     except Exception, e:
         logging.debug(e)
         try:
             with open(uwscfg.json_backup_path,"rb") as fh:
-                sensor_store = json.loads(fh.read())
+                sensor_store_ = json.loads(fh.read())
         except Exception, e:
             logging.debug(e)
 
+
+    for k in set(sensor_store_.keys()).difference(set(uwscfg.zmq_subscribe.split(" "))):
+      del sensor_store_[k]  # del old sensordata of sensor we do not subscribe to
+
+    for k in sensor_store_.keys():
+      try:
+       if len(sensor_store_[k][0]) > 1:
+          sensor_cols_num_[k] = len(sensor_store_[k][0]) -1
+      except:
+        pass
+
     while True:
       try:
         #Start zmq connection to publish / forward sensor data
@@ -280,7 +301,7 @@ if __name__ == "__main__":
 
             addEventToTempLastValueStore(structname, dictdata)
 
-            logging.debug("lastdata:"+str(lastest_values_))
+            logging.debug("lastdata:"+str(latest_values_))
             if int(time.time()) - last_update < int(uwscfg.json_updateinterval):
                 continue
 
@@ -289,9 +310,10 @@ if __name__ == "__main__":
 
             addEventsToStore()
             cleanTempLastValueOfMovementValues()
+            logging.debug("post-cleanMovement lastdata:"+str(latest_values_))
 
             backup_counter += 1
-            # save sensor_store to json for apache
+            # save sensor_store_ to json for apache
             with open(uwscfg.json_write_path,"wb") as fh:
                 fh.truncate()
                 fh.write(json.dumps(sensor_store_))