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.4KB

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 rpi_envsens.dht import dht_22
  8. from rpi_envsens.bmp import bmp_180
  9. logger = logging.getLogger('APP')
  10. class my_base_protocol_tcp(socket_protocol.pure_json_protocol):
  11. # EXECUTE_REQUESTs
  12. CURRENT_ENVDATA = 0
  13. class my_server_protocol_tcp(my_base_protocol_tcp):
  14. def __init__(self, comm_instance, dht_data, bmp_data, secret=None):
  15. socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret)
  16. self.dht_data = dht_data
  17. self.bmp_data = bmp_data
  18. self.register_callback(self.SID_READ_REQUEST, self.CURRENT_ENVDATA, self.current_envdata)
  19. def data_age_okay(self, dd, bd):
  20. MAX_AGE = 60.
  21. tm = time.time()
  22. return dd[dht_22.KEY_TIME] + MAX_AGE >= tm and bd[bmp_180.KEY_TIME] + MAX_AGE >= tm
  23. def current_envdata(self, msg):
  24. dd = self.dht_data.current
  25. bd = self.bmp_data.current
  26. if self.data_age_okay(bd, dd):
  27. rv = {}
  28. rv[dht_22.KEY_TEMPERATURE] = dd[dht_22.KEY_TEMPERATURE]
  29. rv[dht_22.KEY_HUMIDITY] = dd[dht_22.KEY_HUMIDITY]
  30. rv[bmp_180.KEY_PRESSURE] = bd[bmp_180.KEY_PRESSURE]
  31. return self.STATUS_OKAY, rv
  32. else:
  33. return self.STATUS_OPERATION_NOT_PERMITTED, None
  34. class my_client_protocol_tcp(my_base_protocol_tcp):
  35. START_ROUTINE_DATA_IDS = []
  36. def __init__(self, comm_instance, secret=None):
  37. socket_protocol.pure_json_protocol.__init__(self, comm_instance, secret)
  38. self.register_callback(self.SID_READ_RESPONSE, self.CURRENT_ENVDATA, self.read_current_envdata)
  39. def __authentificate_process_feedback__(self, msg):
  40. if msg.get_data() is True:
  41. print("The client is authentificated.")
  42. else:
  43. print("AUTHENTIFICATION ERROR")
  44. return my_base_protocol_tcp.__authentificate_process_feedback__(self, msg)
  45. def read_current_envdata(self, msg):
  46. if msg.get_status() == self.STATUS_OKAY:
  47. env_data = msg.get_data()
  48. print("Temperature: %6.1f °C" % env_data[dht_22.KEY_TEMPERATURE])
  49. print("Humidity: %6.1f %%" % env_data[dht_22.KEY_HUMIDITY])
  50. print("Pressure: %6.1f mbar" % env_data[bmp_180.KEY_PRESSURE])
  51. return self.STATUS_OKAY, None
  52. else:
  53. print('No environmental data received!')
  54. return self.STATUS_SERVICE_OR_DATA_UNKNOWN, None