wether station protocol
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

__init__.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import config
  4. import logging
  5. import socket_protocol
  6. import time
  7. from helpers import continues_statistic
  8. from rpi_envsens.dht import dht_22
  9. from rpi_envsens.bmp import bmp_180
  10. import helpers
  11. try:
  12. from config import APP_NAME as ROOT_LOGGER_NAME
  13. except ImportError:
  14. ROOT_LOGGER_NAME = 'root'
  15. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  16. ENVDATA_STATISTIC_DHT = 0
  17. ENVDATA_STATISTIC_BMP = 1
  18. class my_base_protocol_tcp(socket_protocol.pure_json_protocol):
  19. def __init__(self, *args, **kwargs):
  20. socket_protocol.pure_json_protocol.__init__(self, *args, **kwargs)
  21. self.add_data((socket_protocol.SID_READ_REQUEST, socket_protocol.SID_READ_RESPONSE), ENVDATA_STATISTIC_DHT, 'dht_data')
  22. self.add_data((socket_protocol.SID_READ_REQUEST, socket_protocol.SID_READ_RESPONSE), ENVDATA_STATISTIC_BMP, 'bmp_data')
  23. class my_server_protocol_tcp(my_base_protocol_tcp):
  24. def __init__(self, comm_instance, dht_data, bmp_data, secret=None):
  25. my_base_protocol_tcp.__init__(self, comm_instance, secret=secret)
  26. self.dht_data = dht_data
  27. self.bmp_data = bmp_data
  28. self.register_callback(socket_protocol.SID_READ_REQUEST, ENVDATA_STATISTIC_BMP, self.envdata_statistic_request)
  29. self.register_callback(socket_protocol.SID_READ_REQUEST, ENVDATA_STATISTIC_DHT, self.envdata_statistic_request)
  30. def envdata_statistic_request(self, msg):
  31. did = msg.get_data_id()
  32. if did == ENVDATA_STATISTIC_BMP:
  33. return socket_protocol.STATUS_OKAY, dict(self.bmp_data.pop())
  34. elif did == ENVDATA_STATISTIC_DHT:
  35. return socket_protocol.STATUS_OKAY, dict(self.dht_data.pop())
  36. return socket_protocol.STATUS_SERVICE_OR_DATA_UNKNOWN, None
  37. class my_client_protocol_tcp(my_base_protocol_tcp):
  38. START_ROUTINE_DATA_IDS = []
  39. def __init__(self, comm_instance, secret=None):
  40. my_base_protocol_tcp.__init__(self, comm_instance, secret=secret)
  41. self.register_callback(self.SID_READ_RESPONSE, None, self.print_read_response)
  42. def __authentificate_process_feedback__(self, msg):
  43. if msg.get_data() is True:
  44. print("The client is authentificated.")
  45. else:
  46. print("AUTHENTIFICATION ERROR")
  47. return my_base_protocol_tcp.__authentificate_process_feedback__(self, msg)
  48. def print_read_response(self, msg):
  49. if msg.get_status() == socket_protocol.STATUS_OKAY:
  50. did = msg.get_data_id()
  51. data = msg.get_data()
  52. if did in [self.ENVDATA_STATISTIC_BMP, self.ENVDATA_STATISTIC_DHT]:
  53. print(helpers.continues_statistic_multivalue(**data))
  54. else:
  55. print('No data received!')