wetation_protocol/__init__.py

71 líneas
2.5 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
import helpers
try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
class my_base_protocol_tcp(socket_protocol.pure_json_protocol):
ENVDATA_STATISTIC_DHT = 0
ENVDATA_STATISTIC_BMP = 1
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.ENVDATA_STATISTIC_BMP, self.envdata_statistic_request)
self.register_callback(self.SID_READ_REQUEST, self.ENVDATA_STATISTIC_DHT, self.envdata_statistic_request)
def envdata_statistic_request(self, msg):
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
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 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