def get_state(self): msg = { "type": "geigerjson", "node_uuid": cfg.get('node','uuid'), "timestamp": int(datetime.datetime.now().strftime("%s")), "geostamp": { "lat": cfg.getfloat('node','lat'), "lon": cfg.getfloat('node','lon'), "alt": cfg.getfloat('node','alt') }, "parameters": { "tube_id": cfg.get('geigercounter','tube_id'), "dead_time": cfg.getfloat('geigercounter','tube_dead_time'), "tube_factor": cfg.getfloat('geigercounter','tube_rate_factor'), "opmode": cfg.get('node', 'opmode'), "window": cfg.get('geigercounter', 'window') }, "data": { "source": cfg.get('geigercounter', 'source') if gpio_available else "sim", "cps": self.cps, "cps_dtc": self.cps_dtc, "cpm": self.cpm, "cpm_dtc": self.cpm_dtc, "totalcount": self.totalcount, "totalcount_dtc": self.totalcount_dtc, "edr": self.edr }, "annotation": "" } return msg
def __init__(self,geiger): self.db = leveldb.LevelDB(cfg.get('db','path')) self.db_annotation = leveldb.LevelDB(cfg.get('db','path')+".idx-annotation") self.geiger = geiger threading.Thread.__init__(self) self.daemon = True self.start() self.last_log = None
def get_last_totalcount(): log.info("Getting last totalcount") db = leveldb.LevelDB(cfg.get('db','path')) now = dt2unix(datetime.now()) d = 1 last_entries_keys = [] i = 0 # Check for empty leveldb instance try: db.RangeIter(include_value=False).next() except StopIteration: log.info("Empty LevelDB") return (0,0) while not last_entries_keys: log.debug("Searching further (%d)..."%d) last_entries_keys = list(db.RangeIter(key_from=str(now-d),include_value=False)) d = d*2 i = i+1 last_key = last_entries_keys[-1] entry_json = db.Get(last_key) entry = json.loads(entry_json) return (entry['data']['totalcount'],entry['data']['totalcount_dtc'])
def get_state(self): msg = { "type": "geigerjson", "node_uuid": cfg.get('node', 'uuid'), "timestamp": int(datetime.datetime.now().strftime("%s")), "geostamp": { "lat": cfg.getfloat('node', 'lat'), "lon": cfg.getfloat('node', 'lon'), "alt": cfg.getfloat('node', 'alt') }, "parameters": { "tube_id": cfg.get('geigercounter', 'tube_id'), "dead_time": cfg.getfloat('geigercounter', 'tube_dead_time'), "tube_factor": cfg.getfloat('geigercounter', 'tube_rate_factor'), "opmode": cfg.get('node', 'opmode'), "window": cfg.get('geigercounter', 'window') }, "data": { "source": cfg.get('geigercounter', 'source') if gpio_available else "sim", "cps": self.cps, "cps_dtc": self.cps_dtc, "cpm": self.cpm, "cpm_dtc": self.cpm_dtc, "totalcount": self.totalcount, "totalcount_dtc": self.totalcount_dtc, "edr": self.edr }, "annotation": "" } return msg
def start(g,gl): global geiger, geigerlog, clients_handler geiger = g geigerlog = gl clients_handler = geigerclient.ClientsHandler(geiger,geigerlog) ip = cfg.get('server','ip') port = cfg.getint('server','port') log.info("listening on %s:%d" % (ip, port)) server = WSGIServer((ip, port), app, handler_class=WebSocketHandler) server.serve_forever()
def start(g, gl): global geiger, geigerlog, clients_handler geiger = g geigerlog = gl clients_handler = geigerclient.ClientsHandler(geiger, geigerlog) ip = cfg.get('server', 'ip') port = cfg.getint('server', 'port') log.info("listening on %s:%d" % (ip, port)) server = WSGIServer((ip, port), app, handler_class=WebSocketHandler) server.serve_forever()
def __init__(self,total=0,total_dtc=0): log.info("Starting geigercounter") threading.Thread.__init__(self) self.daemon = True self.socket = None self.totalcount=total self.totalcount_dtc=total_dtc if cfg.getboolean('entropy','enable'): self.entropygenerator = EntropyGenerator(cfg.get('entropy','filename')) else: self.entropygenerator = None self.reset() self.start()
def dummy_entry(timestamp,total,total_dtc): msg = { "type": "geigerjson", "node_uuid": cfg.get('node','uuid'), "timestamp": timestamp, "data": { "source": "off", "cps": 0, "cps_dtc": 0, "cpm": 0, "cpm_dtc": 0, "totalcount": total, "totalcount_dtc": total_dtc, "edr": 0 }, "annotation": "" } return msg
def dummy_entry(timestamp, total, total_dtc): msg = { "type": "geigerjson", "node_uuid": cfg.get('node', 'uuid'), "timestamp": timestamp, "data": { "source": "off", "cps": 0, "cps_dtc": 0, "cpm": 0, "cpm_dtc": 0, "totalcount": total, "totalcount_dtc": total_dtc, "edr": 0 }, "annotation": "" } return msg
def receive_commands(self, handler): #FIXME: some commands need clearer names #FIXME: more robust error/edgecase handling while True: try: message = self.ws.receive() if message is None: raise WebSocketError log.info("Received : %s" % message) msg = json.loads(message) cmd = msg.get("cmd") if not cmd: log.error("Received something, but not a command: %s" % msg) #Ticks if cmd == "send_ticks": if msg.get("state") == "on": self.send_ticks = True elif msg.get("state") == "off": self.send_ticks = False else: log.error("Invalid set_ticks command: %s" % msg) #Log elif cmd == "read": age_seconds = int(msg.get("age", 60 * 60)) if msg.get("hd"): handler.send_log(self, age=age_seconds, amount=None) else: handler.send_log(self, age=age_seconds, amount=1000) elif cmd == "history": age_from = msg.get("from") age_to = msg.get("to") #log.info("From %s to %s"%(str(age_from),str(age_to))) handler.send_log(self, start=age_from, end=age_to, amount=1000, static=True) elif cmd == "annotation": ts = msg.get("timestamp") text = msg.get("text") handler.geigerlog.set_annotation(ts, text) handler.send_log(start=age_from, end=age_to, amount=1000, static=True) #Config elif cmd == "get": try: entropy_pool = os.path.getsize( cfg.get('entropy', 'filename')) except (IOError, OSError): entropy_pool = 0 conf = { "type": "geigerconf", "uuid": cfg.get('node', 'uuid'), "name": cfg.get('node', 'name'), "opmode": cfg.get('node', 'opmode'), "lat": cfg.getfloat('node', 'lat'), "lon": cfg.getfloat('node', 'lon'), "alt": cfg.getfloat('node', 'alt'), "sim_dose_rate": cfg.getfloat('geigercounter', 'sim_dose_rate'), "window": cfg.get('geigercounter', 'window'), "source": cfg.get('geigercounter', 'source') if geigercounter.gpio_available else "sim", "entropy": cfg.getboolean('entropy', 'enable'), "entropy_pool": entropy_pool } self.send(conf) elif cmd == "save": for field in ["lat", "lon", "alt", "opmode"]: val = msg["conf"].get(field) if not val is None: cfg.set('node', field, str(val)) for field in ["window", "source", "sim_dose_rate"]: val = msg["conf"].get(field) if not val is None: cfg.set('geigercounter', field, str(val)) entropy_enable = msg["conf"].get("entropy") if not entropy_enable is None: cfg.set('entropy', 'enable', str(entropy_enable)) cfg.write_dynamic() cfg.read_dynamic() elif cmd == "resetEntropy": log.info("Resetting entropy file") os.remove( os.path.join(script_dir, cfg.get('entropy', 'filename'))) elif cmd == "resetDynamicCfg": log.info("Resetting client config") cfg.clear_dynamic() except WebSocketError: break log.info("websocket closed %s (client %s)" % (self.ws.path, self.session_id))
def receive_commands(self,handler): #FIXME: some commands need clearer names #FIXME: more robust error/edgecase handling while True: try: message = self.ws.receive() if message is None: raise WebSocketError log.info("Received : %s" % message) msg = json.loads(message) cmd = msg.get("cmd") if not cmd: log.error("Received something, but not a command: %s"%msg) #Ticks if cmd == "send_ticks": if msg.get("state") == "on": self.send_ticks = True elif msg.get("state") == "off": self.send_ticks = False else: log.error("Invalid set_ticks command: %s"%msg) #Log elif cmd == "read": age_seconds = int(msg.get("age",60*60)); if msg.get("hd"): handler.send_log(self,age=age_seconds,amount=None) else: handler.send_log(self,age=age_seconds,amount=1000) elif cmd == "history": age_from = msg.get("from") age_to = msg.get("to") #log.info("From %s to %s"%(str(age_from),str(age_to))) handler.send_log(self,start=age_from,end=age_to,amount=1000,static=True) elif cmd == "annotation": ts = msg.get("timestamp") text = msg.get("text") handler.geigerlog.set_annotation(ts,text) handler.send_log(start=age_from,end=age_to,amount=1000,static=True) #Config elif cmd == "get": try: entropy_pool = os.path.getsize(cfg.get('entropy','filename')) except (IOError, OSError): entropy_pool = 0 conf = { "type": "geigerconf", "uuid": cfg.get('node','uuid'), "name": cfg.get('node','name'), "opmode": cfg.get('node','opmode'), "lat": cfg.getfloat('node','lat'), "lon": cfg.getfloat('node','lon'), "alt": cfg.getfloat('node','alt'), "sim_dose_rate": cfg.getfloat('geigercounter','sim_dose_rate'), "window": cfg.get('geigercounter','window'), "source": cfg.get('geigercounter','source') if geigercounter.gpio_available else "sim", "entropy": cfg.getboolean('entropy','enable'), "entropy_pool": entropy_pool } self.send(conf) elif cmd == "save": for field in ["lat","lon","alt","opmode"]: val = msg["conf"].get(field) if not val is None: cfg.set('node',field,str(val)) for field in ["window","source","sim_dose_rate"]: val = msg["conf"].get(field) if not val is None: cfg.set('geigercounter',field,str(val)) entropy_enable = msg["conf"].get("entropy") if not entropy_enable is None: cfg.set('entropy','enable',str(entropy_enable)) cfg.write_dynamic() cfg.read_dynamic() elif cmd == "resetEntropy": log.info("Resetting entropy file") os.remove(os.path.join(script_dir,cfg.get('entropy','filename'))) elif cmd == "resetDynamicCfg": log.info("Resetting client config") cfg.clear_dynamic() except WebSocketError: break log.info("websocket closed %s (client %s)"%(self.ws.path,self.session_id))