123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- #
-
- import config
- import rpi_envsens as envsens
- import garage_protocol
- import gui
- import logging
- import os
- import report
- import task
- import tcp_socket
- import time
- import wetation_protocol
- import wx
-
- logger = logging.getLogger('APP')
-
-
- class WetationFrameProt(gui.Wetation):
- def __init__(self, *args, **kwds):
- gui.Wetation.__init__(self, *args, **kwds)
- self.__task_data_request__ = task.periodic(10, self.__initiate_data_request__)
- self.__init__communication__()
-
- def __init__communication__(self):
- #
- # Start TCP-Clients
- c_tcp = tcp_socket.tcp_client_stp(config.server_out_ip, config.server_out_port, rx_log_lvl=logging.DEBUG)
- self.prot_out = wetation_protocol.my_base_protocol_tcp(c_tcp, config.server_out_secret)
- 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)
- if config.server_out_secret is not None:
- self.prot_out.authentificate()
-
- c_tcp = tcp_socket.tcp_client_stp(config.server_in_ip, config.server_in_port, rx_log_lvl=logging.DEBUG)
- self.prot_in = wetation_protocol.my_base_protocol_tcp(c_tcp, config.server_in_secret)
- 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)
- if config.server_in_secret is not None:
- self.prot_in.authentificate()
-
- c_tcp = tcp_socket.tcp_client_stp(config.server_garage_ip, config.server_garage_port, rx_log_lvl=logging.DEBUG)
- self.prot_garage = garage_protocol.my_base_protocol_tcp(c_tcp, config.server_garage_secret)
- 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)
- if config.server_garage_secret is not None:
- self.prot_garage.authentificate()
-
- def __update_current_envdata__(self, msg, temperature, humidity, pressure):
- if msg.get_status() == wetation_protocol.my_base_protocol_tcp.STATUS_OKAY:
- env_data = msg.get_data()
- wx.CallAfter(temperature.SetLabel, "%.1f °C" % env_data[envsens.KEY_TEMPERATURE])
- wx.CallAfter(humidity.SetLabel, "%.1f %%" % env_data[envsens.KEY_HUMIDITY])
- wx.CallAfter(pressure.SetLabel, "%.0f mbar" % env_data[envsens.KEY_PRESSURE])
- wx.CallAfter(self.Layout)
- return wetation_protocol.my_base_protocol_tcp.STATUS_OKAY, None
- else:
- 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())))
- return wetation_protocol.my_base_protocol_tcp.STATUS_SERVICE_OR_DATA_UNKNOWN, None
-
- def update_current_env_data_out(self, msg):
- return self.__update_current_envdata__(msg, self.out_temperature, self.out_humidity, self.out_pressure)
-
- def update_current_env_data_in(self, msg):
- return self.__update_current_envdata__(msg, self.in_temperature, self.in_humidity, self.in_pressure)
-
- def update_gate_position(self, msg):
- if msg.get_status() == garage_protocol.my_base_protocol_tcp.STATUS_OKAY:
- #
- # show gate section in GUI
- #
- self.heading_garage.Show(True)
- self.gate_oc.Show(True)
- self.gate_open.Show(True)
- self.gate_position.Show(True)
- self.gate_close.Show(True)
- #
- # update gate position
- #
- wx.CallAfter(self.gate_position.SetValue, msg.get_data() * 100)
- wx.CallAfter(self.Layout)
- return garage_protocol.my_base_protocol_tcp.STATUS_OKAY, None
- else:
- 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())))
- return garage_protocol.my_base_protocol_tcp.STATUS_SERVICE_OR_DATA_UNKNOWN, None
-
- def garage_oc_evt(self, event): # wxGlade: Wetation.<event_handler>
- self.prot_garage.send(garage_protocol.my_base_protocol_tcp.SID_EXECUTE_REQUEST, garage_protocol.my_base_protocol_tcp.OPEN_CLOSE_GATE, None)
- event.Skip()
-
- def __initiate_data_request__(self, rt):
- self.prot_out.send(wetation_protocol.my_base_protocol_tcp.SID_READ_REQUEST, wetation_protocol.my_base_protocol_tcp.CURRENT_ENVDATA, None)
- self.prot_in.send(wetation_protocol.my_base_protocol_tcp.SID_READ_REQUEST, wetation_protocol.my_base_protocol_tcp.CURRENT_ENVDATA, None)
- self.prot_garage.send(garage_protocol.my_base_protocol_tcp.SID_READ_REQUEST, garage_protocol.my_base_protocol_tcp.GATE_POSITION, None)
- # TODO: Move the following three lines to the wx idle task
- wx.CallAfter(self.time.SetLabel, time.strftime("%H:%M"))
- wx.CallAfter(self.date.SetLabel, time.strftime("%d.%m.%Y"))
- wx.CallAfter(self.Layout)
-
- def run(self):
- self.__task_data_request__.run()
-
- def close(self):
- self.__task_data_request__.stop()
- self.__task_data_request__.join()
-
- def __del__(self):
- self.close()
-
-
- class MyApp(wx.App):
- def OnInit(self):
- self.frame = WetationFrameProt(None, wx.ID_ANY, "")
- self.SetTopWindow(self.frame)
- self.frame.Show()
- return True
-
-
-
-
- if __name__ == "__main__":
- report.appLoggingConfigure(os.path.dirname(__file__), config.LOGTARGET, config.loggers)
- #
- app = MyApp(0)
- app.frame.run()
- app.MainLoop()
- app.frame.close()
|