OctoPrint Plugin 만들기 #10 (Arduino로부터 전송된 설정값을 Base64로 디코딩)
옥토 프린트 플러그인 만들기 (챔버 매니저)
□ 필요 함수
[ __init__.py ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | from collections import namedtuple from struct import * import base64 deviceParams = { 'firstRun' : None, 'Temp_ControlMode' : None, 'Temp_Target' : None, 'Door_Ventil_Pos_Max' : None, 'Door_Ventil_Pos_Min' : None, 'Door_Filter_Pos_Max' : None, 'Door_Filter_Pos_Min' : None, 'Door_Ventil_Angle' : None, 'Door_Filter_Angle' : None, 'Door_Ventil_Speed' : None, 'Door_Filter_Speed' : None, 'Light_ControlMode' : None, 'Light_Sensitivity' : None, 'Light_InitailStatus' : None } def setDeviceParams(self) : try: p = pack('<bhhhhhhhhhhhhh', self.deviceParams['firstRun'], self.deviceParams['Temp_ControlMode'], self.deviceParams['Temp_Target'], self.deviceParams['Door_Ventil_Pos_Max'], self.deviceParams['Door_Ventil_Pos_Min'], self.deviceParams['Door_Filter_Pos_Max'], self.deviceParams['Door_Filter_Pos_Min'], self.deviceParams['Door_Ventil_Angle'], self.deviceParams['Door_Filter_Angle'], self.deviceParams['Door_Ventil_Speed'], self.deviceParams['Door_Filter_Speed'], self.deviceParams['Light_ControlMode'], self.deviceParams['Light_Sensitivity'], self.deviceParams['Light_InitailStatus']) params = base64.b64encode(p) msg = 'CMD O20 R%d %s\n' %(len(params), params) self.device.writelines(msg.encode('ascii','ignore')) ret = self.device.read_until('\r'); if ret.startswith('OK')==True : self._logger.info('Device parameter setting ok!') else : self._logger.info('Device parameter setting error!') except SerialException as e: self._logger.error(e) except Exception as e: self._logger.error(e) def getDeviceParams(self) : try: msg = 'CMD O20 R0\n' self.device.writelines(msg.encode('ascii','ignore')) ret = self.device.read_until('\r'); if ret.startswith('OK')==True : tok = ret.split(' ') decoded = base64.b64decode(tok[2]) deviceParams = namedtuple('params', 'firstRun Temp_ControlMode Temp_Target Door_Ventil_Pos_Max Door_Ventil_Pos_Min Door_Filter_Pos_Max Door_Filter_Pos_Min Door_Ventil_Angle Door_Filter_Angle Door_Ventil_Speed Door_Filter_Speed Light_ControlMode Light_Sensitivity Light_InitailStatus') params = deviceParams._make(unpack('<bhhhhhhhhhhhhh', decoded)) self.deviceParams['firstRun'] = params.firstRun self.deviceParams['Temp_ControlMode'] = params.Temp_ControlMode self.deviceParams['Temp_Target'] = params.Temp_Target self.deviceParams['Door_Ventil_Pos_Max'] = params.Door_Ventil_Pos_Max self.deviceParams['Door_Ventil_Pos_Min'] = params.Door_Ventil_Pos_Min self.deviceParams['Door_Filter_Pos_Max'] = params.Door_Filter_Pos_Max self.deviceParams['Door_Filter_Pos_Min'] = params.Door_Filter_Pos_Min self.deviceParams['Door_Ventil_Angle'] = params.Door_Ventil_Angle self.deviceParams['Door_Filter_Angle'] = params.Door_Filter_Angle self.deviceParams['Door_Ventil_Speed'] = params.Door_Ventil_Speed self.deviceParams['Door_Filter_Speed'] = params.Door_Filter_Speed self.deviceParams['Light_ControlMode'] = params.Light_ControlMode self.deviceParams['Light_Sensitivity'] = params.Light_Sensitivity self.deviceParams['Light_InitailStatus'] = params.Light_InitailStatus self._logger.info('Successfully received parameters from device.') else : self._logger.info('Error during receiving parameters from device') except SerialException as e: self._logger.error(e) except Exception as e: self._logger.error(e) | cs |