wetation_protocol/__init__.py

72 lines
2.4 KiB
Python
Raw Normal View History

2020-09-03 19:41:20 +02:00
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import config
import logging
import socket_protocol
2020-09-12 20:11:08 +02:00
import time
2020-09-03 19:41:20 +02:00
2020-09-12 20:11:08 +02:00
from rpi_envsens.dht import dht_22
from rpi_envsens.bmp import bmp_180
2020-09-03 19:41:20 +02:00
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):
2020-09-12 20:11:08 +02:00
def __init__(self, comm_instance, dht_data, bmp_data, secret=None):
2020-09-03 19:41:20 +02:00
socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret)
2020-09-12 20:11:08 +02:00
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
2020-09-03 19:41:20 +02:00
2020-09-07 12:32:08 +02:00
class my_client_protocol_tcp(my_base_protocol_tcp):
2020-09-03 19:41:20 +02:00
START_ROUTINE_DATA_IDS = []
2020-09-07 12:32:08 +02:00
def __init__(self, comm_instance, secret=None):
2020-09-03 19:41:20 +02:00
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()
2020-09-12 20:11:08 +02:00
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])
2020-09-03 19:41:20 +02:00
return self.STATUS_OKAY, None
else:
print('No environmental data received!')
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None