def on_voltage(mosq, userdata, msg): if (skip_retained and msg.retain == 1) or len(msg.payload) == 0: return if not storage: return save_rawdata(msg.topic, msg.payload) watcher(mosq, msg.topic, msg.payload) basetopic, suffix = tsplit(msg.topic) key = basetopic.split('/')[-1] # IMEI or TID vext = vbatt = None if suffix.endswith('voltage/ext'): try: vext = float(msg.payload) if basetopic in devices: devices[basetopic].update(dict(vext=vext)) except: return if suffix.endswith('voltage/batt'): try: vbatt = float(msg.payload) if basetopic in devices: devices[basetopic].update(dict(vbatt=vbatt)) except: return try: inv = Inventory.select().where( (Inventory.imei == key) | (Inventory.tid == key) ).get() try: if vext is not None: inv.vext = vext if vbatt is not None: inv.vbatt = vbatt inv.tstamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) inv.save() except Exception, e: raise log.error("DB error on UPDATE Inventory in voltage: {0}".format(str(e))) except Inventory.DoesNotExist: pass except Exception, e: raise log.error("DB error on GET Inventory: {0}".format(str(e))) return
def on_voltage(mosq, userdata, msg): if (skip_retained and msg.retain == 1) or len(msg.payload) == 0: return if not storage: return save_rawdata(msg.topic, msg.payload) watcher(mosq, msg.topic, msg.payload) basetopic, suffix = tsplit(msg.topic) key = basetopic.split('/')[-1] # IMEI or TID vext = vbatt = None if suffix.endswith('voltage/ext'): try: vext = float(msg.payload) if basetopic in devices: devices[basetopic].update(dict(vext=vext)) except: return if suffix.endswith('voltage/batt'): try: vbatt = float(msg.payload) if basetopic in devices: devices[basetopic].update(dict(vbatt=vbatt)) except: return try: inv = Inventory.select().where((Inventory.imei == key) | (Inventory.tid == key)).get() try: if vext is not None: inv.vext = vext if vbatt is not None: inv.vbatt = vbatt inv.tstamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) inv.save() except Exception, e: raise log.error("DB error on UPDATE Inventory in voltage: {0}".format( str(e))) except Inventory.DoesNotExist: pass except Exception, e: raise log.error("DB error on GET Inventory: {0}".format(str(e))) return
def flotbatt(voltage): ''' Find all user's devices and add vbatt levels into list for HW page ''' current_user = request.auth[0] usertids = getusertids(current_user) battlevels = [] try: query = (Inventory.select(Inventory).where( (Inventory.tid << usertids))) for q in query.naive(): try: battlevels.append([q.tid, float(q.vbatt)]) except: pass except Exception, e: log.warn("User {0} failed Inventory query on flotbatt: {1}".format( current_user, str(e))) pass
def page_hw(): current_user = request.auth[0] usertids = getusertids(current_user) device_list = [] # Find all devices in Inventory and extract their info into a list # of objects. Ensure values of hash keys are None if unset or # the template will bail out. try: query = (Inventory.select(Inventory).where( (Inventory.tid << usertids))) query = query.order_by(Inventory.tid.asc()) for q in query.naive(): try: device_list.append({ 'tid': q.tid, 'imei': q.imei, 'version': q.version, 'tstamp': q.startup, 'topic': q.topic, }) except: pass except: pass # device_list will be empty => no data, which is OK here device_list.sort(key=lambda x: x['tid'], reverse=False) params = { 'devices': device_list, 'pistapages': cf.g('pista', 'pages'), } return template('hw', params)
if not superuser: query = (Acl.select(Acl). where( (Acl.username == username) )) sublist = [ q.topic for q in query.naive() ] else: sublist.append('#') # Find distinct topic, tid combinations in Inventory table and # let Paho check if subscription matches topiclist = [] query = (Inventory.select(Inventory.tid, Inventory.topic) .distinct() .order_by(Inventory.tid) ) for q in query: for sub in sublist: if paho.topic_matches_sub(sub, q.topic): topiclist.append( dict(tid=q.tid, topic=q.topic) ) return topiclist #----------------- @app.hook('after_request') def enable_cors(): response.headers['Access-Control-Allow-Origin'] = '*'
return [] except Exception, e: raise if not superuser: query = (Acl.select(Acl).where((Acl.username == username))) sublist = [q.topic for q in query.naive()] else: sublist.append('#') # Find distinct topic, tid combinations in Inventory table and # let Paho check if subscription matches topiclist = [] query = (Inventory.select( Inventory.tid, Inventory.topic).distinct().order_by(Inventory.tid)) for q in query: for sub in sublist: if paho.topic_matches_sub(sub, q.topic): topiclist.append(dict(tid=q.tid, topic=q.topic)) return topiclist #----------------- @app.hook('after_request') def enable_cors(): response.headers['Access-Control-Allow-Origin'] = '*'