122 lines
4.8 KiB
Python
122 lines
4.8 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
import config
|
|
import logging
|
|
import socket_protocol
|
|
import time
|
|
|
|
from helpers import continues_statistic
|
|
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
|
|
TODAYS_STATISTIC_ENVDATA = 1
|
|
YESTERDAYS_STATISTIC_ENVDATA = 2
|
|
BUFFERED_TEMPERATURE = 3
|
|
BUFFERED_HUMIDITY = 4
|
|
BUFFERED_PRESSURE = 5
|
|
|
|
|
|
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, None, self.read_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):
|
|
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
|
|
|
|
|
|
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, None, self.print_read_response)
|
|
|
|
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 print_read_response(self, msg):
|
|
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)
|
|
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
|