class TopRightPanel(wx.Panel): def __init__(self, parent=None): super().__init__(parent) self.app_data = AppData() self.modbus = ModbusConnectionThreadSingleton() self.modbus = self.modbus.thread_instance_get() self.input_matrix = InputArray(parent=self, title='Inputs state:', interface=DISPLAY_INTERFACE, dimension=(3, 5), col_titles=['1', '2', '3', '4', '5'], row_titles=['X1', 'X2', 'X3']) inner_panel_sizer = wx.BoxSizer(wx.VERTICAL) inner_panel_sizer.Add(self.input_matrix, 0, wx.ALL | wx.CENTER, 15) self.app_data.iface_output_handler_register(self._inputs_state_update) self.SetSizer(inner_panel_sizer) def _inputs_state_update(self): for i in range(15): self.input_matrix.value_set_by_index( i, self.app_data.input_state_get(i))
class TopLeftPanel(wx.Panel): def __init__(self, parent=None): super().__init__(parent) self.app_data = AppData() self._input_matrix_enabled = False self._in_matrix_update_stage = 0 self.modbus = ModbusConnectionThreadSingleton() self.modbus = self.modbus.thread_instance_get() self.input_matrix = InputArray(parent=self, title='Inputs mode toggle:', interface=INPUT_INTERFACE, dimension=(3, 5), col_titles=['1', '2', '3', '4', '5'], row_titles=['X1', 'X2', 'X3']) inner_panel_sizer = wx.BoxSizer(wx.VERTICAL) inner_panel_sizer.Add(self.input_matrix, 0, wx.ALL | wx.CENTER, 15) self.input_matrix.disable() self.app_data.iface_conn_handler_register( self._matrix_initial_data_setup) self.app_data.iface_output_handler_register( self._matrix_visibility_update) self.app_data.iface_input_handler_register(self._in_matrix_data_gather) self.SetSizer(inner_panel_sizer) for instance in self.input_matrix.instance_array: self.Bind(wx.EVT_CHECKBOX, self._checkbox_pressed_handle, instance.cell_instance.checkbox) def _checkbox_pressed_handle(self, event): self.app_data.user_interaction = True def _matrix_initial_data_setup(self): for i in range(15): toggle = self.app_data.input_trigger_type_is_toggle_get(i) self.input_matrix.value_set_by_index(i, toggle) def _matrix_visibility_update(self): if self.modbus.is_connected: if self._input_matrix_enabled is False: self.input_matrix.enable() self._input_matrix_enabled = True else: if self._input_matrix_enabled is True: self.input_matrix.disable() self._input_matrix_enabled = False def _in_matrix_data_gather(self): val_list = [] for in_id in range(15): value = self.input_matrix.value_get_by_index(in_id) val_list.append(value) self.app_data.input_trigger_type_is_toggle_set_mask(val_list)
class StatusPanel(wx.Panel): STATUSES = {0: 'Connected', 1: 'Disconnected', 2: 'Connection timeout'} def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.main_sizer = wx.BoxSizer(wx.VERTICAL) inner_sizer = wx.BoxSizer(wx.HORIZONTAL) self.modbus_label = ' ' self.conn_static_text = wx.StaticText(parent=self, label='Connection status') self.modbus_status_text = wx.StaticText(parent=self, label='') inner_sizer.Add(self.conn_static_text, 2, wx.LEFT | wx.ALIGN_LEFT, 10) inner_sizer.Add(self.modbus_status_text, 4, wx.LEFT | wx.ALIGN_LEFT, 120) self.main_sizer.Add(inner_sizer, 1, wx.ALL | wx.EXPAND, 2) self.appdata = AppData() self.modbus = ModbusConnectionThreadSingleton().modbus_comm_instance self.SetSizer(self.main_sizer) self.appdata.iface_output_handler_register(self._conn_status_update) self.appdata.iface_output_handler_register(self._modbus_data_update) self.status = 1 self.conn_static_text.SetLabel(self.STATUSES[self.status]) def _conn_status_update(self): if self.modbus.is_connected: status = 0 if self.modbus.exception_state_get(): status = 2 else: status = 1 if status != self.status: self.status = status try: self.conn_static_text.SetLabel(self.STATUSES[self.status]) except RuntimeError: pass def _modbus_data_update(self): label = 'Port: ' + str(self.modbus.port) + '; ' + str(self.modbus.baudrate) + '-' + \ str(self.modbus.bytesize) + '-' + str(self.modbus.parity) + '-' + \ str(self.modbus.stopbits) if label != self.modbus_label: self.modbus_label = label try: self.modbus_status_text.SetLabel(self.modbus_label) except RuntimeError: pass
class BtmLeftPanel(wx.Panel): def __init__(self, parent): # Basic Construction stuff super().__init__(parent) self.app_data = AppData() self._output_garbage_collector = 0 self._input_matrix_enabled = False # Create modbus instance, to have access to its props instance = ModbusConnectionThreadSingleton() self.modbus = instance.thread_instance_get() # Create content for self.input_matrix = InputArray(parent=self, title='Associated inputs:', dimension=(3, 5), col_titles=['1', '2', '3', '4', '5'], row_titles=['X1', 'X2', 'X3']) self.inner_panel_sizer = wx.BoxSizer(wx.VERTICAL) self.inner_panel_sizer.Add(self.input_matrix, 0, wx.ALL | wx.ALIGN_CENTER, 10) self.SetSizer(self.inner_panel_sizer) self.input_matrix.disable() self.app_data.iface_output_handler_register(self._matrix_visibility_update) for instance in self.input_matrix.instance_array: self.Bind(wx.EVT_CHECKBOX, self._checkbox_pressed_handle, instance.cell_instance.checkbox) def _checkbox_pressed_handle(self, event): self.app_data.user_interaction = True def _matrix_visibility_update(self): if self.modbus.is_connected: if self._input_matrix_enabled is False: self.input_matrix.enable() self._input_matrix_enabled = True else: if self._input_matrix_enabled is True: self.input_matrix.disable() self._input_matrix_enabled = False
class _BtmSubPanel(wx.Panel): def __init__(self, parent): # Basic Construction procedure super().__init__(parent) self.left_panel = None self.right_panel = None self.app_data = AppData() self.rb_value = 0 # Create modbus instance, to have access to its props instance = ModbusConnectionThreadSingleton() self.modbus = instance.thread_instance_get() # Create sizers self.panel_sizer = wx.BoxSizer(wx.VERTICAL) self.upper_sizer = wx.BoxSizer(wx.HORIZONTAL) self.bottom_sizer = wx.BoxSizer(wx.HORIZONTAL) # Create upper sequence items self.output_mode_label = wx.StaticText(parent=self, label='Mode:') self.output_mode_rb_auto = wx.RadioButton(parent=self, id=0, label='Auto', style=wx.RB_GROUP) self.output_mode_rb_off = wx.RadioButton(parent=self, id=1, label='Off') self.output_mode_rb_on = wx.RadioButton(parent=self, id=2, label='On') # self.output_mode_rb_change = wx.RadioButton(parent=self, id=3, label='Change') self.output_state_label = wx.StaticText(parent=self, label='State:') self.output_led = InputArray(parent=self, dimension=(1, 1), interface=DISPLAY_INTERFACE, outlined=False, is_input_indication=False) # Create both panels self._left_panel_create(self) self._right_panel_create(self) # Prepare led state sizer self.led_state_sizer = wx.BoxSizer(wx.HORIZONTAL) led_inter_sizer = wx.BoxSizer(wx.VERTICAL) led_inter_sizer.Add(self.output_led, 1, wx.RIGHT | wx.ALIGN_RIGHT, 20) label_inter_sizer = wx.BoxSizer(wx.VERTICAL) label_inter_sizer.Add(self.output_state_label, 1, wx.LEFT, 85) self.led_state_sizer.Add(label_inter_sizer, 1, wx.ALL) self.led_state_sizer.Add(led_inter_sizer, 1, wx.ALL) # Wrap data into upper sizer self.upper_radio_sizer = wx.BoxSizer(wx.HORIZONTAL) self.upper_radio_sizer.Add(self.output_mode_label, 0, wx.LEFT, 15) self.upper_radio_sizer.Add(self.output_mode_rb_auto, 0, wx.LEFT, 5) self.upper_radio_sizer.Add(self.output_mode_rb_off, 0, wx.LEFT, 5) self.upper_radio_sizer.Add(self.output_mode_rb_on, 0, wx.LEFT, 5) # self.upper_radio_sizer.Add(self.output_mode_rb_change, 0, wx.LEFT, 5) self.upper_sizer.Add(self.upper_radio_sizer, 1, wx.TOP | wx.BOTTOM, 15) self.upper_sizer.Add(self.led_state_sizer, 5, wx.ALL, 15) # Wrap data into bottom sizer self.bottom_sizer.Add(self.left_panel, 1, wx.EXPAND | wx.LEFT, 2) self.bottom_sizer.Add(self.right_panel, 1, wx.EXPAND | wx.RIGHT, 2) # Wrap data into self sizer self.panel_sizer.Add(self.upper_sizer, 1, wx.EXPAND | wx.TOP, 5) self.panel_sizer.Add(self.bottom_sizer, 3, wx.EXPAND) # Apply self sizer self.SetSizer(self.panel_sizer) # Beautify self.output_mode_rb_auto.Disable() self.output_mode_rb_off.Disable() self.output_mode_rb_on.Disable() # self.output_mode_rb_change.Disable() # Bind Callbacks self.Bind(wx.EVT_RADIOBUTTON, self._radio_button_callback, self.output_mode_rb_auto) self.Bind(wx.EVT_RADIOBUTTON, self._radio_button_callback, self.output_mode_rb_off) self.Bind(wx.EVT_RADIOBUTTON, self._radio_button_callback, self.output_mode_rb_on) # self.Bind(wx.EVT_RADIOBUTTON, self._radio_button_callback, self.output_mode_rb_change) self.app_data.iface_output_handler_register( self.radio_buttons_visibility_handler) def radio_buttons_visibility_handler(self): if self.modbus.is_connected: try: if self.output_mode_rb_auto: self.output_mode_rb_auto.Enable() if self.output_mode_rb_off: self.output_mode_rb_off.Enable() if self.output_mode_rb_on: self.output_mode_rb_on.Enable() # if self.output_mode_rb_change: # self.output_mode_rb_change.Enable() except Exception as e: print(e) else: try: if self.output_mode_rb_auto: self.output_mode_rb_auto.Disable() if self.output_mode_rb_off: self.output_mode_rb_off.Disable() if self.output_mode_rb_on: self.output_mode_rb_on.Disable() # if self.output_mode_rb_change: # self.output_mode_rb_change.Disable() except Exception as e: print(e) self.rb_value = -1 def rb_layout(self): if self.rb_value == 0: if self.output_mode_rb_auto: self.output_mode_rb_auto.SetValue(True) self.output_mode_rb_auto.Layout() elif self.rb_value == 1: if self.output_mode_rb_off: self.output_mode_rb_off.SetValue(True) self.output_mode_rb_off.Layout() elif self.rb_value == 2: if self.output_mode_rb_on: self.output_mode_rb_on.SetValue(True) self.output_mode_rb_on.Layout() # elif self.rb_value == 3: # if self.output_mode_rb_change: # self.output_mode_rb_change.SetValue(True) # self.output_mode_rb_change.Layout() def _left_panel_create(self, parent): self.left_panel = BtmLeftPanel(parent) def _right_panel_create(self, parent): self.right_panel = BtmRightPanel(parent) def _radio_button_callback(self, event): self.app_data.user_interaction = True if self.modbus.is_connected and self.app_data.modbus_data is not None: self.rb_value = event.GetId()
class _BtmPanel(wx.Panel): def __init__(self, parent=None): super().__init__(parent=parent) self.app_data = AppData() self.modbus = ModbusConnectionThreadSingleton() self.modbus = self.modbus.thread_instance_get() self.btm_page_sizer = wx.BoxSizer(wx.HORIZONTAL) self.notebook_sizer = wx.BoxSizer(wx.HORIZONTAL) self.notebook = wx.Notebook(self) self.sub_canvases = list() for i in range(8): self.sub_canvases.append(_BtmSubPanel(parent=self.notebook)) self.notebook.AddPage(self.sub_canvases[i], "Relay " + str(1 + i)) ''' Here goes a very important line of a code, setting notebook item's color to default window color instead of white color, chosen by default, when creating widget ''' self.notebook.SetOwnBackgroundColour(self.GetBackgroundColour()) self.notebook_sizer.Add(self.notebook, 0, wx.EXPAND) self.btm_page_sizer.Add(self.notebook_sizer, 0, wx.EXPAND) self.SetSizer(self.btm_page_sizer) # Apply data on connection self.app_data.iface_conn_handler_register( self._input_left_matrix_update_on_conn) self.app_data.iface_conn_handler_register( self._input_radio_data_set_on_conn) # Interface handlers, always called to update data on screen self.app_data.iface_output_handler_register(self._out_led_update) self.app_data.iface_output_handler_register( self._matrix_visibility_update) self.app_data.iface_output_handler_register( self._input_right_matrix_update) # Gather input data handlers self.app_data.iface_input_handler_register( self._input_left_matrix_data_gather) self.app_data.iface_input_handler_register( self._input_radio_data_gather) self._input_matrix_enabled = 0 self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.on_page_change) def on_page_change(self, event): page = event.GetSelection() if self.modbus.is_connected: self.sub_canvases[page].rb_value = self.app_data.output_mode_get( page) self.sub_canvases[page].rb_layout() def _input_left_matrix_data_gather(self): for out_id in range(8): val_list = [] left_panel = self.sub_canvases[out_id].left_panel for in_id in range(15): value = left_panel.input_matrix.value_get_by_index(in_id) val_list.append(value) self.app_data.output_associated_input_set_mask(out_id, val_list) def _input_left_matrix_update_on_conn(self): # Update left matrix when we acquire connection for out_id in range(8): left_panel = self.sub_canvases[out_id].left_panel for in_id in range(15): value = self.app_data.output_associated_input_get( out_id, in_id) left_panel.input_matrix.value_set_by_index(in_id, value) def _input_right_matrix_update(self): # Update right matrix when we acquire connection for out_id in range(8): right_panel = self.sub_canvases[out_id].right_panel for in_id in range(15): is_visible = self.app_data.output_associated_input_get( out_id, in_id) right_panel.input_matrix.visibility_set_by_index( in_id, is_visible) value = self.app_data.input_state_get(in_id) right_panel.input_matrix.value_set_by_index(in_id, value) def _input_radio_data_set_on_conn(self): val_list = list() for out_id, sub_cvs in enumerate(self.sub_canvases): sub_cvs.rb_value = self.app_data.output_mode_get(out_id) val_list.append(sub_cvs.rb_value) sub_cvs.rb_layout() def _input_radio_data_gather(self): val_list = list() for out_id, sub_cvs in enumerate(self.sub_canvases): val_list.append(sub_cvs.rb_value) self.app_data.output_mode_set(out_id, sub_cvs.rb_value) def _matrix_visibility_update(self): if self.modbus.is_connected: for out_id in range(8): left_panel = self.sub_canvases[out_id].left_panel if self._input_matrix_enabled is False: left_panel.input_matrix.enable() self._input_matrix_enabled = True else: for out_id in range(8): left_panel = self.sub_canvases[out_id].left_panel if self._input_matrix_enabled is True: left_panel.input_matrix.disable() self._input_matrix_enabled = False def _out_led_update(self): for i in range(8): self.sub_canvases[i].output_led.values = [ self.app_data.output_state_get(i), ]
class _MidPanelContent(wx.Panel): def __init__(self, parent=None): super().__init__(parent=parent) self.conn_blink_state = 0 self.settings = Settings() self.app_data = AppData() self.sizer = wx.BoxSizer(wx.HORIZONTAL) self._output_garbage_collector = 0 self.act_indicator_wrapper = wx.BoxSizer(wx.VERTICAL) self.slave_id_sequence_wrapper = wx.BoxSizer(wx.VERTICAL) # Create display matrix for Relays self.output_matrix = InputArray( parent=self, title='Outputs state:', dimension=(1, 8), col_titles=['K1', 'K2', 'K3', 'K4', 'K5', 'K6', 'K7', 'K8'], interface=DISPLAY_INTERFACE, secret_ids=[1, 2, 3, 4, 5, 6, 7, 8], is_input_indication=False) self.output_matrix_wrapper = wx.BoxSizer(wx.VERTICAL) self.output_matrix_wrapper.Add(self.output_matrix, 0, wx.RIGHT | wx.ALIGN_RIGHT, 18) # Assign instance of modbus singleton, to have access to its props instance = ModbusConnectionThreadSingleton() self.modbus = instance.thread_instance_get() # Create an activity Led self.act_label = wx.StaticText(parent=self, label='Activity') self.activity_led = InputArray(parent=self, dimension=(1, 1), is_conn=True, interface=DISPLAY_INTERFACE, outlined=False) # Create Slave id sequence self.slave_id_label = wx.StaticText(parent=self, label='Slave ID') self.slave_id_control = wx.SpinCtrl(parent=self, size=(60, -1), style=wx.TE_LEFT, max=999) # Wrap indicator act_label_wrapper = wx.BoxSizer(wx.HORIZONTAL) led_activity_wrapper = wx.BoxSizer(wx.HORIZONTAL) act_label_wrapper.Add(self.act_label, 1, wx.LEFT, 8) led_activity_wrapper.Add(self.activity_led, 1, wx.LEFT, 21) self.act_indicator_wrapper.Add(act_label_wrapper, 2, wx.TOP, 12) self.act_indicator_wrapper.Add(led_activity_wrapper, 2, wx.TOP, 14) # Wrap slave id slave_id_label_wrapper = wx.BoxSizer(wx.HORIZONTAL) slave_id_control_wrapper = wx.BoxSizer(wx.HORIZONTAL) slave_id_label_wrapper.Add(self.slave_id_label, 1, wx.LEFT, 3) slave_id_control_wrapper.Add(self.slave_id_control, 1, wx.LEFT, 8) self.slave_id_sequence_wrapper.Add(slave_id_label_wrapper, 3, wx.TOP, 18) self.slave_id_sequence_wrapper.Add(slave_id_control_wrapper, 2, wx.TOP, 4) # Bind events self.Bind(wx.EVT_SPINCTRL, self.slave_id_update, self.slave_id_control) self.app_data.iface_output_handler_register(self._slave_id_update) # Assemble panel sizer self.sizer.Add(self.act_indicator_wrapper, 1, wx.LEFT | wx.ALIGN_CENTER_VERTICAL, 20) self.sizer.Add(self.slave_id_sequence_wrapper, 1, wx.LEFT | wx.ALIGN_CENTER_VERTICAL, 20) self.sizer.Add(self.output_matrix_wrapper, 6, wx.EXPAND, 0) # Apply panel sizer self.SetSizer(self.sizer) # Register handler to update output state self.app_data.iface_output_handler_register( self._output_indication_update) # Insert a certain delay for refresh button self.timer = wx.Timer() self.timer_evt_handler = wx.EvtHandler() self.timer.SetOwner(self.timer_evt_handler, id=228) self.timer_evt_handler.Bind(wx.EVT_TIMER, self._refresh_conn_activity, self.timer, id=228) self.timer.Start(100, True) def _slave_id_update(self): self.value = self.settings.slave_id def slave_id_update(self, event): self._output_garbage_collector = event self.settings.slave_id = self.value self.modbus.slave_id_update(self.settings.slave_id) def _output_indication_update(self): for i in range(8): self.output_matrix.value_set_by_index( i, self.app_data.output_state_get(i)) # Refresh callback, which is called every 100 def _refresh_conn_activity(self, event): self._garbage_evt_collector = event self.timer.Start(100, True) self._can_be_refreshed = 1 self._conn_indication() def _conn_indication(self): modbus_conn_state = self.modbus.is_connected self.activity_led.visible_instances = (modbus_conn_state, ) if modbus_conn_state: if self._can_be_refreshed == 1: self._can_be_refreshed = 0 self.conn_blink_state = not self.conn_blink_state self.activity_led.values = (self.conn_blink_state, ) else: self.activity_led.values = (0, ) self.conn_blink_state = 0 @property def value(self): if self.slave_id_control: try: value = self.slave_id_control.GetValue() except Exception as e: print(e) value = None return value @value.setter def value(self, new_value): if self.slave_id_control: self.slave_id_control.SetValue(new_value)