def getHistory(p, m): global Metrics global Measures global Config output = "" db = dbutil.dbutil(Config) p = auth.decodeUrlToken(p) if p: fields = p.split('/') if len(fields) == 5: if fields[3] in Measures.keys(): if fields[4] == 'null': avg, rng = db.GetSensorHistory(fields[3], False) else: avg, rng = db.GetSensorLastdays(fields[3], True, days=int(fields[4])) ylabel = Metrics[fields[3]]['YLabel'] unit = Metrics[fields[3]]['Unit'] mclass = Metrics[fields[3]]['Class'] return '{"status":"ok","avg":%s,"rng":%s,"ylabel":"%s","unit":"%s","mclass":"%s"}' % ( avg, rng, ylabel, unit, mclass) else: return '{"status":"error","value":"sensor not exist"}' else: return '{"status":"error","value":"missing sensor name"}' else: return '{"status":"error","value":"token authorization failure"}'
def getDaily(p, m): global Metrics global Measures global Config output = "" db = dbutil.dbutil(Config) p = auth.decodeUrlToken(p) if p: fields = p.split('/') if len(fields) == 4: if fields[3] in Measures.keys(): data = db.GetSensorDaily(fields[3]) ylabel = Metrics[fields[3]]['YLabel'] unit = Metrics[fields[3]]['Unit'] mclass = Metrics[fields[3]]['Class'] return '{"status":"ok","value":%s,"ylabel":"%s","unit":"%s","mclass":"%s"}' % ( data, ylabel, unit, mclass) else: return '{"status":"error","value":"sensor not exist"}' else: return '{"status":"error","value":"missing sensor name"}' else: return '{"status":"error","value":"token authorization failure"}'
def callSensor(p, m, b=None): global Metrics global Callbacks p = auth.decodeUrlToken(p) if p: if b is not None: try: req = json.loads(b) fields = p.split('/') if len(fields) == 4: if fields[3] in Metrics.keys(): sensorname = Metrics[fields[3]]['SensorName'] if sensorname in Callbacks.keys(): if Callbacks[sensorname] is not None: return Callbacks[sensorname](fields[3], b) else: return '{"status":"error","value":"callback is none"}' else: return '{"status":"error","value":"callback not registered"}' else: return '{"status":"error","value":"sensor not exist"}' else: return '{"status":"error","value":"missing sensor name"}' except ValueError: # includes simplejson.decoder.JSONDecodeError return '{"status":"error","value":"json decoding error","body":"%s"}' % b else: return '{"status":"error","value":"missing body"}' else: return '{"status":"error","value":"token authorization failure"}'
def getSensorConfig(p, m): p = auth.decodeUrlToken(p) if p: return '{"status":"ok","value":%s}' % json.dumps(Sensors, sort_keys=True) else: return '{"status":"error","value":"token authorization failure"}'
def getSensor(p, m): global Metrics global Measures global LastRead p = auth.decodeUrlToken(p) if p: fields = p.split('/') if len(fields) == 4: if fields[3] in LastRead.keys(): value = LastRead[fields[3]][0] ylabel = Metrics[fields[3]]['YLabel'] unit = Metrics[fields[3]]['Unit'] mclass = Metrics[fields[3]]['Class'] return '{"status":"ok","value":%s,"ylabel":"%s","unit":"%s","mclass":"%s"}' % ( value, ylabel, unit, mclass) else: return '{"status":"error","value":"sensor not exist"}' else: return '{"status":"error","value":"missing sensor name"}' else: return '{"status":"error","value":"token authorization failure"}'