Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 committed Aug 11, 2024
1 parent 228806a commit 1df72ff
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 62 deletions.
4 changes: 0 additions & 4 deletions ph4_sense/mods/aht21.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def __init__(
):
super().__init__(base, sensor_helper, *args, **kwargs)
self.i2c = i2c
self.has_aht21 = True
self.temp = 0
self.humd = 0
self.aht21 = None
Expand All @@ -29,9 +28,6 @@ def load_config(self, js):
pass

def connect(self):
if not self.has_aht21:
return

self.print("\n - Connecting AHT21")
from ph4_sense.sensors.athx0 import ahtx0_factory

Expand Down
4 changes: 0 additions & 4 deletions ph4_sense/mods/ccs811.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def __init__(
):
super().__init__(base, sensor_helper, *args, **kwargs)
self.i2c = i2c
self.has_ccs811 = True
self.ccs_co2 = 0
self.ccs_tvoc = 0
self.eavg_css811_co2 = SensorFilter(median_window=9, alpha=0.2)
Expand All @@ -30,9 +29,6 @@ def load_config(self, js):
pass

def connect(self):
if not self.has_ccs811:
return

self.print("\n - Connecting CCS811")
from ph4_sense.sensors.ccs811 import css811_factory

Expand Down
3 changes: 0 additions & 3 deletions ph4_sense/mods/hdc1080.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def __init__(
super().__init__(base, sensor_helper, *args, **kwargs)
self.i2c = i2c

self.has_hdc1080 = True
self.temp = 0
self.humd = 0
self.hdc1080 = None
Expand All @@ -25,8 +24,6 @@ def load_config(self, js):
pass

def connect(self):
if not self.has_hdc1080:
return
self.print("\n - Connecting HDC1080")
from ph4_sense.sensors.hdc1080 import hdc1080_factory

Expand Down
4 changes: 0 additions & 4 deletions ph4_sense/mods/scd4x.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def __init__(
super().__init__(base, sensor_helper, *args, **kwargs)
self.i2c = i2c

self.has_scd4x = True
self.scd40_co2 = None
self.scd40_temp = None
self.scd40_hum = None
Expand All @@ -30,9 +29,6 @@ def load_config(self, js):
pass

def connect(self):
if not self.has_scd4x:
return

self.print("\n - Connecting SCD40")
from ph4_sense.sensors.scd4x import scd4x_factory

Expand Down
4 changes: 0 additions & 4 deletions ph4_sense/mods/sgp30.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def __init__(
super().__init__(base, sensor_helper, *args, **kwargs)
self.i2c = i2c

self.has_sgp30 = True
self.sgp30_co2eq = 0
self.sgp30_tvoc = 0
self.eth = 0
Expand All @@ -32,9 +31,6 @@ def load_config(self, js):
pass

def connect(self):
if not self.has_sgp30:
return

self.print(" - Connecting SGP30")
from ph4_sense.sensors.sgp30 import sgp30_factory

Expand Down
4 changes: 0 additions & 4 deletions ph4_sense/mods/sgp41.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def __init__(
self.i2c = i2c
self.measure_loop_ms = measure_loop_ms

self.has_sgp41 = True
self.sgp41_filter_voc = None
self.sgp41_filter_nox = None
self.sgp41_sraw_voc = None
Expand All @@ -33,9 +32,6 @@ def load_config(self, js):
pass

def connect(self):
if not self.has_sgp41:
return

self.print(" - Connecting SGP41")
from ph4_sense.sensirion import NoxGasIndexAlgorithm, VocGasIndexAlgorithm
from ph4_sense.sensors.sgp41 import sgp41_factory
Expand Down
6 changes: 1 addition & 5 deletions ph4_sense/mods/sps30.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def __init__(
self.i2c = i2c
self.uart = None

self.has_sps30 = True
self.sps30_data = None
self.sps30 = None

Expand All @@ -32,9 +31,6 @@ def load_config(self, js):
self.uart = cfg.get("uart", None)

def connect(self):
if not self.has_sps30:
return

self.print("\n - Connecting SPS30")
if self.uart:
from ph4_sense_py.sensors.sps30_uart_ada import SPS30AdaUart
Expand All @@ -55,7 +51,7 @@ def calibrate_temps(self, cal_temp, cal_hum):
pass

def measure(self):
if not self.has_sps30 or not self.sps30:
if not self.sps30:
return

def sps30_measure_body():
Expand Down
29 changes: 18 additions & 11 deletions ph4_sense/mods/zh03b.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,31 @@ def calibrate_temps(self, cal_temp, cal_hum):
pass

def measure(self):
if not self.has_sps30 or not self.sps30:
if not self.has_zh03b or not self.zh03b:
return

def sps30_measure_body():
if self.sps30.data_available:
self.sps30_data = self.sps30.read()

try:
self.try_measure(sps30_measure_body)
reading = self.zh03b.qa_read_sample()
if reading is None:
return

self.zh03b_data = reading
self.print("ZH03b data {}".format(self.zh03b_data))

except Exception as e:
self.print("Err SPS30: ", e)
self.log_error("SPS30 err: {}".format(e))
self.log_info("SPS30 err: {}".format(e), exc_info=e)
self.print("Err ZH03b: ", e)
self.log_error("ZH03b err: {}".format(e))
self.log_info("ZH03b err: {}".format(e), exc_info=e)
return

def get_publish_data(self):
if not self.sps30:
if not self.zh03b or not self.zh03b_data or len(self.zh03b_data) < 3:
return

return {"sensors/sps30": self.sps30_data}
return {
"sensors/zh03b": {
"pm10": self.zh03b_data[0],
"pm25": self.zh03b_data[1],
"pm100": self.zh03b_data[2],
}
}
98 changes: 75 additions & 23 deletions ph4_sense/sense_mod.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
from ph4_sense_base.adapters import sleep_ms, time
from ph4_sense_base.mods.sensors import SensorsMod
from ph4_sense_base.sensei_iface import SenseiIface
from ph4_sense_base.support.sensor_helper import SensorHelper
from ph4_sense_base.support.typing import Optional
from ph4_sense_base.utils import try_fnc


class SensorsModMp(SensorsMod):
class SensorsModMp(SensorsMod, SenseiIface):
def __init__(
self, i2c, base: Optional[SenseiIface] = None, sensor_helper: Optional[SensorHelper] = None, *args, **kwargs
):
super().__init__(*args, **kwargs)
self.i2c = i2c
self.base = base
self.sensor_helper = sensor_helper

# self.has_aht = has_aht
# self.has_hdc1080 = has_hdc1080
# self.has_sgp30 = has_sgp30
# self.has_sgp41 = has_sgp41
# self.has_ccs811 = has_ccs811
# self.has_scd4x = has_scd4x
# self.has_sps30 = has_sps30
# self.has_zh03b = has_zh03b
self.last_tsync = 0
self.temp_sync_timeout = 180

self.aht21 = None
self.hdc1080 = None
Expand All @@ -31,21 +26,27 @@ def __init__(
self.sps30 = None
self.zh03b = None

def log_fnc(self, level, msg, *args, **kwargs):
return self.base.log_fnc(level, msg, *args, **kwargs)

def print(self, msg, *args):
return self.base.log_fnc(msg, *args)

def print_cli(self, msg, *args):
return self.base.print_cli(msg, *args)

def get_uart_builder(self, desc):
return self.base.get_uart_builder(desc)

def get_temp_humd(self):
return None

def load_config(self, js):
super().load_config(js)
if "sensors" not in js:
return

kwargs = {"base": self.base, "sensor_helper": self.sensor_helper}

# self.has_aht = False
# self.has_hdc1080 = False
# self.has_sgp30 = False
# self.has_sgp41 = False
# self.has_ccs811 = False
# self.has_scd4x = False
# self.has_sps30 = False
# self.has_zh03b = False
kwargs = {"base": self, "sensor_helper": self.sensor_helper}

for sensor in js["sensors"]:
sensor = sensor.lower()
Expand Down Expand Up @@ -103,9 +104,60 @@ def get_all_sensors(self):
self.zh03b,
]

def get_sensors(self):
return [x for x in self.get_all_sensors() if x is not None]
def get_sensors(self, include_temp=True):
excl_list = [self.aht21, self.hdc1080] if include_temp else []
return [x for x in self.get_all_sensors() if x is not None and x not in excl_list]

def try_measure(self, fnc):
for attempt in range(self.measure_attempts):
try:
return fnc()
except Exception as e:
if attempt + 1 >= self.measure_attempts:
self.logger.error(f"Could not measure sensor {fnc}, attempt {attempt}: {e}")
raise
else:
self.logger.warn(f"Could not measure sensor {fnc}, attempt {attempt}: {e}")
sleep_ms(self.measure_timeout)

def try_connect_sensor(self, fnc):
for attempt in range(self.reconnect_attempts):
try:
return fnc()
except Exception as e:
if attempt + 1 >= self.reconnect_attempts:
self.logger.error(f"Could not connect sensor {fnc}, attempt {attempt}: {e}")
raise
else:
self.logger.warn(f"Could not connect sensor {fnc}, attempt {attempt}: {e}")
sleep_ms(self.reconnect_timeout)

def connect(self):
self.base.print("\nConnecting sensors")
for s in self.get_sensors():
s.connect()
self.try_connect_sensor(s.connect)

def measure_temperature(self):
if self.aht21:
return self.aht21.measure()
if self.hdc1080:
return self.hdc1080.measure()
return None, None

def calibrate_temps(self, cal_temp, cal_hum):
if cal_temp and cal_hum and time.time() - self.last_tsync > self.temp_sync_timeout:
for sensor in self.get_sensors():
try_fnc(lambda s=sensor: s.calibrate_temps(cal_temp, cal_hum))

self.last_tsync = time.time()
self.base.print("Temp sync", cal_temp, cal_hum)

def measure(self):
t, h = self.measure_temperature()
self.calibrate_temps(t, h)

for sensor in self.get_sensors(include_temp=False):
sensor.measure()

def publish(self):
pass

0 comments on commit 1df72ff

Please sign in to comment.