wether station gui client
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

smarthome.py 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. import config
  5. import rpi_envsens as envsens
  6. import garage_protocol
  7. import gui
  8. import logging
  9. import os
  10. import report
  11. import task
  12. import tcp_socket
  13. import time
  14. import wetation_protocol
  15. import wx
  16. logger = logging.getLogger('APP')
  17. class WetationFrameProt(gui.Wetation):
  18. def __init__(self, *args, **kwds):
  19. gui.Wetation.__init__(self, *args, **kwds)
  20. self.__task_data_request__ = task.periodic(10, self.__initiate_data_request__)
  21. self.__init__communication__()
  22. def __init__communication__(self):
  23. #
  24. # Start TCP-Clients
  25. c_tcp = tcp_socket.tcp_client_stp(config.server_out_ip, config.server_out_port, rx_log_lvl=logging.DEBUG)
  26. self.prot_out = wetation_protocol.my_base_protocol_tcp(c_tcp, config.server_out_secret)
  27. self.prot_out.register_callback(wetation_protocol.my_base_protocol_tcp.SID_READ_RESPONSE, wetation_protocol.my_base_protocol_tcp.CURRENT_ENVDATA, self.update_current_env_data_out)
  28. if config.server_out_secret is not None:
  29. self.prot_out.authentificate()
  30. c_tcp = tcp_socket.tcp_client_stp(config.server_in_ip, config.server_in_port, rx_log_lvl=logging.DEBUG)
  31. self.prot_in = wetation_protocol.my_base_protocol_tcp(c_tcp, config.server_in_secret)
  32. self.prot_in.register_callback(wetation_protocol.my_base_protocol_tcp.SID_READ_RESPONSE, wetation_protocol.my_base_protocol_tcp.CURRENT_ENVDATA, self.update_current_env_data_in)
  33. if config.server_in_secret is not None:
  34. self.prot_in.authentificate()
  35. c_tcp = tcp_socket.tcp_client_stp(config.server_garage_ip, config.server_garage_port, rx_log_lvl=logging.DEBUG)
  36. self.prot_garage = garage_protocol.my_base_protocol_tcp(c_tcp, config.server_garage_secret)
  37. self.prot_garage.register_callback(garage_protocol.my_base_protocol_tcp.SID_READ_RESPONSE, garage_protocol.my_base_protocol_tcp.GATE_POSITION, self.update_gate_position)
  38. if config.server_garage_secret is not None:
  39. self.prot_garage.authentificate()
  40. def __update_current_envdata__(self, msg, temperature, humidity, pressure):
  41. if msg.get_status() == wetation_protocol.my_base_protocol_tcp.STATUS_OKAY:
  42. env_data = msg.get_data()
  43. wx.CallAfter(temperature.SetLabel, "%.1f °C" % env_data[envsens.KEY_TEMPERATURE])
  44. wx.CallAfter(humidity.SetLabel, "%.1f %%" % env_data[envsens.KEY_HUMIDITY])
  45. wx.CallAfter(pressure.SetLabel, "%.0f mbar" % env_data[envsens.KEY_PRESSURE])
  46. wx.CallAfter(self.Layout)
  47. return wetation_protocol.my_base_protocol_tcp.STATUS_OKAY, None
  48. else:
  49. logger.error('No environmental data received! MSG_STATUS was %s', wetation_protocol.my_base_protocol_tcp.STATUS_NAMES.get(msg.get_status(), repr(msg.get_status())))
  50. return wetation_protocol.my_base_protocol_tcp.STATUS_SERVICE_OR_DATA_UNKNOWN, None
  51. def update_current_env_data_out(self, msg):
  52. return self.__update_current_envdata__(msg, self.out_temperature, self.out_humidity, self.out_pressure)
  53. def update_current_env_data_in(self, msg):
  54. return self.__update_current_envdata__(msg, self.in_temperature, self.in_humidity, self.in_pressure)
  55. def update_gate_position(self, msg):
  56. if msg.get_status() == garage_protocol.my_base_protocol_tcp.STATUS_OKAY:
  57. #
  58. # show gate section in GUI
  59. #
  60. self.heading_garage.Show(True)
  61. self.gate_oc.Show(True)
  62. self.gate_open.Show(True)
  63. self.gate_position.Show(True)
  64. self.gate_close.Show(True)
  65. #
  66. # update gate position
  67. #
  68. wx.CallAfter(self.gate_position.SetValue, msg.get_data() * 100)
  69. wx.CallAfter(self.Layout)
  70. return garage_protocol.my_base_protocol_tcp.STATUS_OKAY, None
  71. else:
  72. logger.error('No gate position received! MSG_STATUS was %s', garage_protocol.my_base_protocol_tcp.STATUS_NAMES.get(msg.get_status(), repr(msg.get_status())))
  73. return garage_protocol.my_base_protocol_tcp.STATUS_SERVICE_OR_DATA_UNKNOWN, None
  74. def garage_oc_evt(self, event): # wxGlade: Wetation.<event_handler>
  75. self.prot_garage.send(garage_protocol.my_base_protocol_tcp.SID_EXECUTE_REQUEST, garage_protocol.my_base_protocol_tcp.OPEN_CLOSE_GATE, None)
  76. event.Skip()
  77. def __initiate_data_request__(self, rt):
  78. self.prot_out.send(wetation_protocol.my_base_protocol_tcp.SID_READ_REQUEST, wetation_protocol.my_base_protocol_tcp.CURRENT_ENVDATA, None)
  79. self.prot_in.send(wetation_protocol.my_base_protocol_tcp.SID_READ_REQUEST, wetation_protocol.my_base_protocol_tcp.CURRENT_ENVDATA, None)
  80. self.prot_garage.send(garage_protocol.my_base_protocol_tcp.SID_READ_REQUEST, garage_protocol.my_base_protocol_tcp.GATE_POSITION, None)
  81. # TODO: Move the following three lines to the wx idle task
  82. wx.CallAfter(self.time.SetLabel, time.strftime("%H:%M"))
  83. wx.CallAfter(self.date.SetLabel, time.strftime("%d.%m.%Y"))
  84. wx.CallAfter(self.Layout)
  85. def run(self):
  86. self.__task_data_request__.run()
  87. def close(self):
  88. self.__task_data_request__.stop()
  89. self.__task_data_request__.join()
  90. def __del__(self):
  91. self.close()
  92. class MyApp(wx.App):
  93. def OnInit(self):
  94. self.frame = WetationFrameProt(None, wx.ID_ANY, "")
  95. self.SetTopWindow(self.frame)
  96. self.frame.Show()
  97. return True
  98. if __name__ == "__main__":
  99. report.appLoggingConfigure(os.path.dirname(__file__), config.LOGTARGET, config.loggers)
  100. #
  101. app = MyApp(0)
  102. app.frame.run()
  103. app.MainLoop()
  104. app.frame.close()