wether station protocol
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

__init__.py 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. class my_base_protocol_tcp(socket_protocol.pure_json_protocol):
  17. ENVDATA_STATISTIC_DHT = 0
  18. ENVDATA_STATISTIC_BMP = 1
  19. class my_server_protocol_tcp(my_base_protocol_tcp):
  20. def __init__(self, comm_instance, dht_data, bmp_data, secret=None):
  21. socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret)
  22. self.dht_data = dht_data
  23. self.bmp_data = bmp_data
  24. self.register_callback(self.SID_READ_REQUEST, self.ENVDATA_STATISTIC_BMP, self.envdata_statistic_request)
  25. self.register_callback(self.SID_READ_REQUEST, self.ENVDATA_STATISTIC_DHT, self.envdata_statistic_request)
  26. def envdata_statistic_request(self, msg):
  27. did = msg.get_data_id()
  28. if did == self.ENVDATA_STATISTIC_BMP:
  29. return self.STATUS_OKAY, dict(self.bmp_data.pop())
  30. elif did == self.ENVDATA_STATISTIC_DHT:
  31. return self.STATUS_OKAY, dict(self.dht_data.pop())
  32. return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
  33. class my_client_protocol_tcp(my_base_protocol_tcp):
  34. START_ROUTINE_DATA_IDS = []
  35. def __init__(self, comm_instance, secret=None):
  36. socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret)
  37. self.register_callback(self.SID_READ_RESPONSE, None, self.print_read_response)
  38. def __authentificate_process_feedback__(self, msg):
  39. if msg.get_data() is True:
  40. print("The client is authentificated.")
  41. else:
  42. print("AUTHENTIFICATION ERROR")
  43. return my_base_protocol_tcp.__authentificate_process_feedback__(self, msg)
  44. def print_read_response(self, msg):
  45. if msg.get_status() == self.STATUS_OKAY:
  46. did = msg.get_data_id()
  47. data = msg.get_data()
  48. if did in [self.ENVDATA_STATISTIC_BMP, self.ENVDATA_STATISTIC_DHT]:
  49. print(helpers.continues_statistic_multivalue(**data))
  50. else:
  51. return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None
  52. return self.STATUS_OKAY, None
  53. else:
  54. print('No data received!')
  55. return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None