From e0645a2b9dfc2f0de36e75cdbbec831aa7178535 Mon Sep 17 00:00:00 2001 From: Pim van Gennip Date: Thu, 11 May 2017 22:16:19 +0200 Subject: [PATCH] Added hap sensor readout --- laradock/mqttwarn/services/influxdb_akvo.py | 25 +++++++-- laradock/mqttwarn/services/test.py | 51 +++++++++++++------ .../app/Http/Controllers/SensorController.php | 11 ++-- 3 files changed, 65 insertions(+), 22 deletions(-) diff --git a/laradock/mqttwarn/services/influxdb_akvo.py b/laradock/mqttwarn/services/influxdb_akvo.py index 82cecd2..4ccc9e6 100644 --- a/laradock/mqttwarn/services/influxdb_akvo.py +++ b/laradock/mqttwarn/services/influxdb_akvo.py @@ -27,10 +27,27 @@ def split_ssu_wap_for_influx(item): # 0 1 2 3 4 5 # HAP formatting: Timestamp, CO, CO2, P1, P2, BatteryLevel def split_hap_sum_for_influx(item, sensor_id): - out = item.payload.strip().split(",") - tpc = "topic=" + item.topic.replace('/'+sensor_id, '').replace('/', '_') - tst = int(out[0]) if int(out[0]) > 0 else int(time.time()) - pyl = "%s,sensor_id=%s,type=hap_sum co=%s,co2=%s,p1=%s,p2=%s,bat_v=%s %s" % (tpc, sensor_id, float(out[1]), float(out[2]), float(out[3]), float(out[4]), float(out[5]), tst) + pl = item.payload + tpc= "topic=" + item.topic.replace('/'+sensor_id, '').replace('/', '_') + if len(pl) == 26: # HAP payload + out = [] + out.append(int(pl[0:8], 16)) #ts + out.append(int(pl[8:12], 16)) + out.append(int(pl[12:16], 16)) + out.append(int(pl[16:20], 16)) + out.append(int(pl[20:24], 16)) + out.append(int(pl[24:26], 16)) + tst = int(time.time()) + pyl = "%s,sensor_id=%s,type=hap_sum co=%s,co2=%s,p1=%s,p2=%s,hap_bat_v=%s %s" % (tpc, sensor_id, float(out[1]), float(out[2]), float(out[3]), float(out[4]), (float(out[5])+200)/100, tst) + elif len(pl) == 16: # HAP payload + out = [] + out.append(int(pl[0:8], 16)) # ts + out.append(int(pl[8:10], 16)) # Dur + out.append(int(pl[10:14], 16)) # T max + out.append(int(pl[14:16], 16)) # Bat Level + tst = int(out[0]) if int(out[0]) > 0 and int(out[0]) < int(time.time()) else int(time.time()) + pyl = "%s,sensor_id=%s,type=hap_sum duration=%s,t_max=%s,sum_bat_v=%s %s" % (tpc, sensor_id, float(out[1]), float(out[2]), (float(out[3])+200)/100, tst) + return pyl diff --git a/laradock/mqttwarn/services/test.py b/laradock/mqttwarn/services/test.py index 1edb9cf..077b880 100644 --- a/laradock/mqttwarn/services/test.py +++ b/laradock/mqttwarn/services/test.py @@ -11,29 +11,50 @@ def split_ssu_wap_for_influx(tpc, pl): return pyl -def split_hap_sum_for_influx(tpc, pl): - sensor_id = 1 - out = pl.split(",") - tpc = "topic=" + tpc.replace('/', '_') - tst = int(out[0]) if int(out[0]) > 0 else int(time.time()) - pyl = "%s,sensor_id=%s,type=hap_sum co=%s,co2=%s,p1=%s,p2=%s,bat_v=%s %s" % (tpc, sensor_id, float(out[1]), float(out[2]), float(out[3]), float(out[4]), float(out[5]), tst) - return pyl +# HAP Message: 1494335973, 98, 0, 65140, 52924, 168 +# 0 1 2 3 4 5 +# HAP formatting: Timestamp, CO, CO2, P1, P2, BatteryLevel +def split_hap_sum_for_influx(pl, sensor_id, tp): - -tp = "ssu_wap" -pl = "6d70a5d273205cae,203,1024,1017,199,3677" -tag = "" + tpc= "topic=" + tp.replace('/'+sensor_id, '').replace('/', '_') + print 'topic: ' + tpc + print 'payload length: ' + str(len(pl)) + if len(pl) == 26: # HAP payload + out = [] + out.append(int(pl[0:8], 16)) #ts + out.append(int(pl[8:12], 16)) + out.append(int(pl[12:16], 16)) + out.append(int(pl[16:20], 16)) + out.append(int(pl[20:24], 16)) + out.append(int(pl[24:26], 16)) + tst = int(out[0]) if int(out[0]) > 0 else int(time.time()) + pyl = "%s,sensor_id=%s,type=hap_sum co=%s,co2=%s,p1=%s,p2=%s,hap_bat_v=%s %s" % (tpc, sensor_id, float(out[1]), float(out[2]), float(out[3]), float(out[4]), (float(out[5])+200)/100, tst) + elif len(pl) == 16: # HAP payload + out = [] + out.append(int(pl[0:8], 16)) # ts + out.append(int(pl[8:10], 16)) # Dur + out.append(int(pl[10:14], 16)) # T max + out.append(int(pl[14:16], 16)) # Bat Level + tst = int(out[0]) if int(out[0]) > 0 else int(time.time()) + pyl = "%s,sensor_id=%s,type=hap_sum duration=%s,t_max=%s,sum_bat_v=%s %s" % (tpc, sensor_id, float(out[1]), float(out[2]), (float(out[3])+200)/100, tst) -# tp = "hap_sum" -# pl = "1494335973, 98, 0, 65140, 52924, 168" + return pyl + +# tp = "ssu_wap" +# pl = "6d70a5d273205cae,203,1024,1017,199,3677" # tag = "" +tp = "ITAY/HAP/0000E0DB40604500" +pl = "81ff0b6e0000000000000000e2" +tag = "" +tpc = tp.split('/') + if tp == "ssu_wap": tag = "" payload = split_ssu_wap_for_influx(tp, pl) -elif tp == "hap_sum": +elif tpc[0] == "ITAY" and tpc[1] == "HAP": tag = "" - payload = split_hap_sum_for_influx(tp, pl) + payload = split_hap_sum_for_influx(pl, tpc[2], tp) else: payload = " value="+item.payload diff --git a/portal/app/Http/Controllers/SensorController.php b/portal/app/Http/Controllers/SensorController.php index 13e3a0f..b5e0ab4 100644 --- a/portal/app/Http/Controllers/SensorController.php +++ b/portal/app/Http/Controllers/SensorController.php @@ -31,7 +31,7 @@ protected function allowedSensors($request, $id=null) protected function convertSensorTypeToInfluxSeries($type) { - return substr(strtolower($type), 0, 3); + return substr(strtolower($type), 0, 3); // convert hap_sum -> hap, ssu_wap -> ssu } @@ -63,12 +63,17 @@ protected function addLastSensorData($sensors) { //die(print_r($sensordata)); $sensors[$id]->date = $sensordata[0]['time']; - if ($sensor->type == "ssu_wap") + if ($sensor->type == "ssu_wap" && isset($sensordata[0]['pressure_wap']) && isset($sensordata[0]['pressure_ssu'])) { // Depth = Depth(m)=(WAP pressure- BME280 pressure)/98.1 $sensors[$id]->value = round( (($sensordata[0]['pressure_wap'] - $sensordata[0]['pressure_ssu'])/98.1), 2)." m"; } - else + else if ($sensor->type == "hap_sum" && isset($sensordata[0]['p1'])) + { + // Depth = Depth(m)=(WAP pressure- BME280 pressure)/98.1 + $sensors[$id]->value = $sensordata[0]['p1']." pcs/ft³"; + } + else if (isset($sensordata[0]['value'])) { $sensors[$id]->value = $sensordata[0]['value']; }