#!/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__) ENVDATA_STATISTIC_DHT = 0 ENVDATA_STATISTIC_BMP = 1 class my_base_protocol_tcp(socket_protocol.pure_json_protocol): def __init__(self, *args, **kwargs): socket_protocol.pure_json_protocol.__init__(self, *args, **kwargs) self.add_data((socket_protocol.SID_READ_REQUEST, socket_protocol.SID_READ_RESPONSE), ENVDATA_STATISTIC_DHT, 'dht_data') self.add_data((socket_protocol.SID_READ_REQUEST, socket_protocol.SID_READ_RESPONSE), ENVDATA_STATISTIC_BMP, 'bmp_data') class my_server_protocol_tcp(my_base_protocol_tcp): def __init__(self, comm_instance, dht_data, bmp_data, secret=None): my_base_protocol_tcp.__init__(self, comm_instance, secret=secret) self.dht_data = dht_data self.bmp_data = bmp_data self.register_callback(socket_protocol.SID_READ_REQUEST, ENVDATA_STATISTIC_BMP, self.envdata_statistic_request) self.register_callback(socket_protocol.SID_READ_REQUEST, ENVDATA_STATISTIC_DHT, self.envdata_statistic_request) def envdata_statistic_request(self, msg): did = msg.get_data_id() if did == ENVDATA_STATISTIC_BMP: return socket_protocol.STATUS_OKAY, dict(self.bmp_data.pop()) elif did == ENVDATA_STATISTIC_DHT: return socket_protocol.STATUS_OKAY, dict(self.dht_data.pop()) return socket_protocol.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): my_base_protocol_tcp.__init__(self, comm_instance, secret=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() == socket_protocol.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: print('No data received!')