""" Treatment Adjustment Pressure Limits UI Loader """ # Python import os import can import struct # Qt from PySide2 import QtCore, QtWidgets from PySide2.QtCore import Slot # parent from engine.dynamicloader import DynamicLoader # plugin specific from leahi_dialin.common.msg_defs import MsgIds, MsgFieldPositions from leahi_dialin.protocols import CAN from leahi_dialin.utils import conversions # TD simulator from leahi_dialin.ui.td_messaging import TD_Messaging class Loader(DynamicLoader): """ Treatment Adjustment Pressure Limits UI Loader """ def __init__(self): self.td_interface = TD_Messaging() self.can_interface = self.td_interface.can_interface super().__init__(os.path.dirname(__file__)) if self.can_interface is not None: self.can_interface.register_receiving_publication_function( CAN.DenaliChannels.ui_to_td_ch_id, MsgIds.MSG_ID_UI_DURATION_VALIDATE_REQUEST.value, self.handle_duration_edit_request) self.can_interface.register_receiving_publication_function( CAN.DenaliChannels.ui_to_td_ch_id, MsgIds.MSG_ID_UI_DURATION_CONFIRM_REQUEST.value, self.handle_duration_confirm_request) def _init_loader(self): """ finds and creates widgets :return: none """ self.tbReset = self.find_widget(QtWidgets.QToolButton , 'tbReset' ) self.lbEditReqDuration = self.find_widget(QtWidgets.QLabel , 'lbEditReqDuration' ) self.tbEditRspSend = self.find_widget(QtWidgets.QToolButton , 'tbEditRspSend' ) self.tbEditRspCopy = self.find_widget(QtWidgets.QToolButton , 'tbEditRspCopy' ) self.tbEditRspReset = self.find_widget(QtWidgets.QToolButton , 'tbEditRspReset' ) self.sbEditRspRejectionReason = self.find_widget(QtWidgets.QSpinBox , 'sbEditRspRejectionReason' ) self.slEditRspDuration = self.find_widget(QtWidgets.QSlider , 'slEditRspDuration' ) self.lbEditRspDuration = self.find_widget(QtWidgets.QLabel , 'lbEditRspDuration' ) self.slEditRspUfVolumeGoal = self.find_widget(QtWidgets.QSlider , 'slEditRspUfVolumeGoal' ) self.lbEditRspUfVolumeGoal = self.find_widget(QtWidgets.QLabel , 'lbEditRspUfVolumeGoal' ) self.slEditRspUfRate = self.find_widget(QtWidgets.QSlider , 'slEditRspUfRate' ) self.lbEditRspUfRate = self.find_widget(QtWidgets.QLabel , 'lbEditRspUfRate' ) self.tbConfirmRspSend = self.find_widget(QtWidgets.QToolButton , 'tbConfirmRspSend' ) self.tbConfirmRspReset = self.find_widget(QtWidgets.QToolButton , 'tbConfirmRspReset' ) self.lbConfirmReqDuration = self.find_widget(QtWidgets.QLabel , 'lbConfirmReqDuration' ) self.sbConfirmRspRejectionReason = self.find_widget(QtWidgets.QSpinBox , 'sbConfirmRspRejectionReason' ) def _init_connections(self): """ initializes the widgets connections :return: none """ self.tbReset .clicked .connect(self._init_widgets ) self.tbEditRspSend .clicked .connect(self.do_send_edit_response ) self.tbEditRspCopy .clicked .connect(self.do_copy_edit_response ) self.tbEditRspReset .clicked .connect(self.init_edit_response ) self.slEditRspDuration .valueChanged .connect(lambda value: self.lbEditRspDuration.setText(f"{value}") ) self.slEditRspUfVolumeGoal .valueChanged .connect(lambda value: self.lbEditRspUfVolumeGoal.setText(f"{value/1000:.2f}") ) self.slEditRspUfRate .valueChanged .connect(lambda value: self.lbEditRspUfRate.setText(f"{value/1000:.2f}") ) self.tbConfirmRspSend .clicked .connect(self.do_send_confirm_response ) self.tbConfirmRspReset .clicked .connect(self.init_confirm_response ) def _init_widgets(self): """ initializes the widgets' properties :return: none """ self.init_edit_response() self.init_confirm_response() @Slot() def handle_duration_edit_request(self, message, timestamp = 0.0): """ Called when the user requests duration edit from UI @return: None """ message = message['message'] index = MsgFieldPositions.START_POS_FIELD_1 value,index = conversions.bytearray_to_integer(message, index) self.lbEditReqDuration.setText(f"{value}") @Slot() def init_edit_response(self): self.lbEditReqDuration.setText("--") self.sbEditRspRejectionReason .setValue(0) self.slEditRspDuration .setValue(0) self.lbEditRspDuration .setText("{0}".format(self.slEditRspDuration.value())) self.slEditRspUfVolumeGoal .setValue(0) self.lbEditRspUfVolumeGoal .setText("{0:.2f}".format(self.slEditRspUfVolumeGoal.value() / 1000)) self.slEditRspUfRate .setValue(0) self.lbEditRspUfRate .setText("{0:.2f}".format(self.slEditRspUfRate.value() / 1000)) @Slot() def do_send_edit_response(self): """ the slot for sending duration edit response :return: none """ self.td_interface.td_duration_validate_response( self.sbEditRspRejectionReason .value(), self.slEditRspDuration .value(), self.slEditRspUfVolumeGoal .value(), self.slEditRspUfRate .value() / 1000 ) @Slot() def do_copy_edit_response(self): """ the slot for copying duration edit request data to edit response :return: none """ convert_to_int = lambda value : int(value) if value.isdigit() else 0 self.slEditRspDuration.setValue(convert_to_int(self.lbEditReqDuration.text())) @Slot() def handle_duration_confirm_request(self, message, timestamp = 0.0): """ Called when the user requests duration confirm from UI @return: None """ message = message['message'] index = MsgFieldPositions.START_POS_FIELD_1 value,index = conversions.bytearray_to_integer(message, index) self.lbConfirmReqDuration.setText(f"{value}") @Slot() def init_confirm_response(self): self.lbConfirmReqDuration .setText("--") self.sbConfirmRspRejectionReason .setValue(0) @Slot() def do_send_confirm_response(self): """ the slot for sending duration confirm response :return: none """ self.td_interface.cmd_send_general_response( message_id = MsgIds.MSG_ID_TD_DURATION_CONFIRM_RESPONSE.value, accepted = 1 if self.sbConfirmRspRejectionReason.value() == 0 else 0, reason = self.sbConfirmRspRejectionReason.value(), is_pure_data = False, has_parameters = False, )