wetation_protocol/__init__.py

66 lines
2.3 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-17 19:51:53 +02:00
from helpers import continues_statistic
2020-09-12 20:11:08 +02:00
from rpi_envsens.dht import dht_22
from rpi_envsens.bmp import bmp_180
import helpers
2020-09-03 19:41:20 +02:00
logger = logging.getLogger('APP')
class my_base_protocol_tcp(socket_protocol.pure_json_protocol):
ENVDATA_STATISTIC_DHT = 0
ENVDATA_STATISTIC_BMP = 1
2020-09-03 19:41:20 +02:00
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.ENVDATA_STATISTIC_BMP, self.envdata_statistic_request)
self.register_callback(self.SID_READ_REQUEST, self.ENVDATA_STATISTIC_DHT, self.envdata_statistic_request)
2020-09-12 20:11:08 +02:00
def envdata_statistic_request(self, msg):
2020-09-17 19:51:53 +02:00
did = msg.get_data_id()
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
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)
2020-09-17 19:51:53 +02:00
self.register_callback(self.SID_READ_RESPONSE, None, self.print_read_response)
2020-09-03 19:41:20 +02:00
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)
2020-09-17 19:51:53 +02:00
def print_read_response(self, msg):
2020-09-03 19:41:20 +02:00
if msg.get_status() == self.STATUS_OKAY:
2020-09-17 19:51:53 +02:00
did = msg.get_data_id()
data = msg.get_data()
if did in [self.ENVDATA_STATISTIC_BMP, self.ENVDATA_STATISTIC_DHT]:
print(helpers.continues_statistic_multivalue(**data))
2020-09-17 19:51:53 +02:00
else:
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
2020-09-03 19:41:20 +02:00
return self.STATUS_OKAY, None
else:
2020-09-17 19:51:53 +02:00
print('No data received!')
2020-09-03 19:41:20 +02:00
return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None