1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
-
- import config
- import logging
- import socket_protocol
- import time
-
- from rpi_envsens.dht import dht_22
- from rpi_envsens.bmp import bmp_180
-
- logger = logging.getLogger('APP')
-
-
-
-
- class my_base_protocol_tcp(socket_protocol.pure_json_protocol):
- # EXECUTE_REQUESTs
- CURRENT_ENVDATA = 0
-
-
- class my_server_protocol_tcp(my_base_protocol_tcp):
- def __init__(self, comm_instance, dht_data, bmp_data, secret=None):
- 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, self.CURRENT_ENVDATA, self.current_envdata)
-
- 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 current_envdata(self, msg):
- dd = self.dht_data.current
- bd = self.bmp_data.current
- 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
- else:
- return self.STATUS_OPERATION_NOT_PERMITTED, None
-
-
- class my_client_protocol_tcp(my_base_protocol_tcp):
- START_ROUTINE_DATA_IDS = []
-
- def __init__(self, comm_instance, secret=None):
- socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret)
- self.register_callback(self.SID_READ_RESPONSE, self.CURRENT_ENVDATA, self.read_current_envdata)
-
- def __authentificate_process_feedback__(self, msg):
- if msg.get_data() is True:
- print("The client is authentificated.")
- else:
- print("AUTHENTIFICATION ERROR")
- return my_base_protocol_tcp.__authentificate_process_feedback__(self, msg)
-
- def read_current_envdata(self, msg):
- if msg.get_status() == self.STATUS_OKAY:
- env_data = msg.get_data()
- print("Temperature: %6.1f °C" % env_data[dht_22.KEY_TEMPERATURE])
- print("Humidity: %6.1f %%" % env_data[dht_22.KEY_HUMIDITY])
- print("Pressure: %6.1f mbar" % env_data[bmp_180.KEY_PRESSURE])
- return self.STATUS_OKAY, None
- else:
- print('No environmental data received!')
- return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
|