+ with open(uwscfg.json_moveto_path,"rb") as fh:
+ 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())
+ except Exception, e:
+ logging.debug(e)
+
+ while True:
+ try:
+ #Start zmq connection to publish / forward sensor data
+ zmqctx = zmq.Context()
+ zmqctx.linger = 0
+ zmqsub = zmqctx.socket(zmq.SUB)
+ for topic in uwscfg.zmq_subscribe.split(" "):
+ zmqsub.setsockopt(zmq.SUBSCRIBE, topic)
+ if uwscfg.zmq_sshtunnel:
+ zmq.ssh.tunnel.tunnel_connection(zmqsub, uwscfg.zmq_remote_uri, uwscfg.zmq_sshtunnel, keyfile=uwscfg.zmq_sshkeyfile)
+ else:
+ zmqsub.connect(uwscfg.zmq_remote_uri)
+ backup_counter = 0
+ last_update = int(time.time())
+
+ while True:
+ #receive sensor updates
+ data = zmqsub.recv_multipart()
+ (structname, dictdata) = decodeR3Message(data)
+ logging.debug("Got data: " + structname + ":"+ str(dictdata))
+
+ uwscfg.checkConfigUpdates()
+
+ addEventToTempLastValueStore(structname, dictdata)
+
+ logging.debug("lastdata:"+str(lastest_values_))
+ if int(time.time()) - last_update < int(uwscfg.json_updateinterval):
+ continue
+
+ logging.debug("update interval elapsed")
+ last_update = int(time.time())
+
+ addEventsToStore()
+ cleanTempLastValueOfMovementValues()
+
+ backup_counter += 1
+ # save sensor_store to json for apache
+ with open(uwscfg.json_write_path,"wb") as fh:
+ fh.truncate()
+ fh.write(json.dumps(sensor_store_))
+ if backup_counter > uwscfg.json_backup_every:
+ backup_counter = 0
+ shutil.copy(uwscfg.json_write_path, uwscfg.json_backup_path)
+ shutil.move(uwscfg.json_write_path, uwscfg.json_moveto_path)
+
+ except Exception, ex:
+ logging.error("main: "+str(ex))
+ traceback.print_exc(file=sys.stdout)
+ try:
+ zmqsub.close()
+ zmqctx.destroy()
+ except:
+ pass
+ time.sleep(5)