diff --git a/__init__.py b/__init__.py index 6fac78a..797167b 100644 --- a/__init__.py +++ b/__init__.py @@ -9,18 +9,14 @@ import time from helpers import continues_statistic from rpi_envsens.dht import dht_22 from rpi_envsens.bmp import bmp_180 +import helpers logger = logging.getLogger('APP') class my_base_protocol_tcp(socket_protocol.pure_json_protocol): - # EXECUTE_REQUESTs - CURRENT_ENVDATA = 0 - TODAYS_STATISTIC_ENVDATA = 1 - YESTERDAYS_STATISTIC_ENVDATA = 2 - BUFFERED_TEMPERATURE = 3 - BUFFERED_HUMIDITY = 4 - BUFFERED_PRESSURE = 5 + ENVDATA_STATISTIC_DHT = 0 + ENVDATA_STATISTIC_BMP = 1 class my_server_protocol_tcp(my_base_protocol_tcp): @@ -28,57 +24,16 @@ class my_server_protocol_tcp(my_base_protocol_tcp): socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret) self.dht_data = dht_data self.bmp_data = bmp_data - self.register_callback(self.SID_READ_REQUEST, None, self.read_request) + self.register_callback(self.SID_READ_REQUEST, self.ENVDATA_STATISTIC_BMP, self.envdata_statistic_request) + self.register_callback(self.SID_READ_REQUEST, self.ENVDATA_STATISTIC_DHT, self.envdata_statistic_request) - def data_age_okay(self, dd, bd): - MAX_AGE = 60. - tm = time.time() - return dd[dht_22.KEY_TIME] + MAX_AGE >= tm and bd[bmp_180.KEY_TIME] + MAX_AGE >= tm - - def read_request(self, msg): + def envdata_statistic_request(self, msg): did = msg.get_data_id() - if did == self.CURRENT_ENVDATA: - dd = self.dht_data.current - bd = self.bmp_data.current - if dd is not None and bd is not None: - if self.data_age_okay(bd, dd): - rv = {} - rv[dht_22.KEY_TEMPERATURE] = dd[dht_22.KEY_TEMPERATURE] - rv[dht_22.KEY_HUMIDITY] = dd[dht_22.KEY_HUMIDITY] - rv[bmp_180.KEY_PRESSURE] = bd[bmp_180.KEY_PRESSURE] - return self.STATUS_OKAY, rv - return self.STATUS_OPERATION_NOT_PERMITTED, None - elif did in [self.TODAYS_STATISTIC_ENVDATA, self.YESTERDAYS_STATISTIC_ENVDATA]: - if did == self.TODAYS_STATISTIC_ENVDATA: - dd = self.dht_data.todays_statistic - bd = self.bmp_data.todays_statistic - else: - dd = self.dht_data.yesterdays_statistic - bd = self.bmp_data.yesterdays_statistic - if dd is None or bd is None: - return self.STATUS_OPERATION_NOT_PERMITTED, None - else: - rv = {} - rv[dht_22.KEY_TEMPERATURE] = dd[dht_22.KEY_TEMPERATURE] - rv[dht_22.KEY_HUMIDITY] = dd[dht_22.KEY_HUMIDITY] - rv[bmp_180.KEY_PRESSURE] = bd[bmp_180.KEY_PRESSURE] - return self.STATUS_OKAY, rv - elif did in [self.BUFFERED_HUMIDITY, self.BUFFERED_PRESSURE, self.BUFFERED_TEMPERATURE]: - if did == self.BUFFERED_HUMIDITY: - db = self.dht_data.databuffer - key = dht_22.KEY_HUMIDITY - elif did == self.BUFFERED_PRESSURE: - db = self.bmp_data.databuffer - key = bmp_180.KEY_PRESSURE - else: - db = self.dht_data.databuffer - key = dht_22.KEY_TEMPERATURE - rv = [] - for entry in db: - rv.append((entry[dht_22.KEY_TIME], entry[key])) - return self.STATUS_OKAY, rv - else: - return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None + if did == self.ENVDATA_STATISTIC_BMP: + return self.STATUS_OKAY, dict(self.bmp_data.pop()) + elif did == self.ENVDATA_STATISTIC_DHT: + return self.STATUS_OKAY, dict(self.dht_data.pop()) + return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None class my_client_protocol_tcp(my_base_protocol_tcp): @@ -99,23 +54,12 @@ class my_client_protocol_tcp(my_base_protocol_tcp): if msg.get_status() == self.STATUS_OKAY: did = msg.get_data_id() data = msg.get_data() - if did == self.CURRENT_ENVDATA: - print("Temperature: %6.1f °C" % data[dht_22.KEY_TEMPERATURE]) - print("Humidity: %6.1f %%" % data[dht_22.KEY_HUMIDITY]) - print("Pressure: %6.1f mbar" % data[bmp_180.KEY_PRESSURE]) - elif did in [self.TODAYS_STATISTIC_ENVDATA, self.YESTERDAYS_STATISTIC_ENVDATA]: - print(continues_statistic(**data)) - elif did in [self.BUFFERED_HUMIDITY, self.BUFFERED_PRESSURE, self.BUFFERED_TEMPERATURE]: - if did == self.BUFFERED_HUMIDITY: - print("Humidity:") - elif did == self.BUFFERED_PRESSURE: - print("Pressure:") - else: - print("Temperature:") - print(data) + if did in [self.ENVDATA_STATISTIC_BMP, self.ENVDATA_STATISTIC_DHT]: + print(helpers.continues_statistic_multivalue(**data)) else: return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None return self.STATUS_OKAY, None else: print('No data received!') return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None +