wether station protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 2.3KB

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