2020-09-06 13:38:47 +02:00
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
import config
2020-09-07 12:34:08 +02:00
import rpi_envsens as envsens
import garage_protocol
import gui
2020-09-06 13:38:47 +02:00
import logging
import os
import report
import task
2020-09-07 12:34:08 +02:00
import tcp_socket
import time
import wetation_protocol
import wx
2020-09-06 13:38:47 +02:00
2020-09-07 12:34:08 +02:00
logger = logging . getLogger ( ' APP ' )
2020-09-06 13:38:47 +02:00
2020-09-07 12:34:08 +02:00
class WetationFrameProt ( gui . Wetation ) :
2020-09-06 13:38:47 +02:00
def __init__ ( self , * args , * * kwds ) :
2020-09-07 12:34:08 +02:00
gui . Wetation . __init__ ( self , * args , * * kwds )
self . __task_data_request__ = task . periodic ( 10 , self . __initiate_data_request__ )
self . __init__communication__ ( )
2020-09-06 13:38:47 +02:00
2020-09-07 12:34:08 +02:00
def __init__communication__ ( self ) :
2020-09-06 13:38:47 +02:00
#
2020-09-07 12:34:08 +02:00
# 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
#
2020-09-07 12:41:19 +02:00
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 )
2020-09-07 12:34:08 +02:00
#
# 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
2020-09-06 13:38:47 +02:00
def garage_oc_evt ( self , event ) : # wxGlade: Wetation.<event_handler>
2020-09-07 12:34:08 +02:00
self . prot_garage . send ( garage_protocol . my_base_protocol_tcp . SID_EXECUTE_REQUEST , garage_protocol . my_base_protocol_tcp . OPEN_CLOSE_GATE , None )
2020-09-06 13:38:47 +02:00
event . Skip ( )
2020-09-07 12:34:08 +02:00
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 ( )
2020-09-06 13:38:47 +02:00
class MyApp ( wx . App ) :
def OnInit ( self ) :
2020-09-07 12:34:08 +02:00
self . frame = WetationFrameProt ( None , wx . ID_ANY , " " )
2020-09-06 13:38:47 +02:00
self . SetTopWindow ( self . frame )
self . frame . Show ( )
return True
if __name__ == " __main__ " :
2020-09-06 14:44:03 +02:00
report . appLoggingConfigure ( os . path . dirname ( __file__ ) , config . LOGTARGET , config . loggers )
2020-09-06 13:38:47 +02:00
#
app = MyApp ( 0 )
2020-09-07 12:34:08 +02:00
app . frame . run ( )
2020-09-06 13:38:47 +02:00
app . MainLoop ( )
2020-09-07 12:34:08 +02:00
app . frame . close ( )