# -*- coding: utf-8 -*- # !/usr/bin/env python import time from apps.web.constant import DeviceOnlineStatus, ErrorCode, Const, DeviceCmdCode, DeviceErrorCodeDesc, MQTT_TIMEOUT from apps.web.core.adapter.base import fill_2_hexByte, SmartBox from apps.web.core.exceptions import ServiceException from apps.web.core.networking import MessageSender from apps.web.device.models import Device reason_dict = { 0x00: u'待机可用', 0x01: u'预约中', 0x02: u'消毒清洁中', 0x03: u'洗衣工作中', 0x04: u'洗衣机洗涤结束', 0x05: u'洗衣机自检中', 0x06: u'进水超时,请检查进水阀或者水龙头', 0x07: u'排水超时,请检查排水系统', 0x08: u'脱水时撞桶,请调整衣服位置,然后关上门盖', 0x09: u'脱水开盖,请关好门盖', 0x0a: u'水位传感器异常,请检查水位传感器', 0x0b: u'溢水报警,请检查进水阀或洗衣机控制器', 0x0c: u'电机故障,请检查电机或洗衣机控制器', 0x0d: u'通讯故障', 0xfd: u'IMEI未配置,请重启模块', 0xfe: u'投币金额不足' } class SimaierWasher(SmartBox): def get_port_status_from_dev(self): pass def check_dev_status(self, attachParas = None): if not self.device.need_fetch_online: raise ServiceException( {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL)}) if self.device.online == DeviceOnlineStatus.DEV_STATUS_ONLINE: retry = 3 timeout = 12 else: retry = 2 timeout = 10 operation_result = self.get_dev_info(retry = retry, timeout = timeout) if operation_result['rst'] != ErrorCode.DEVICE_SUCCESS: if operation_result['rst'] == ErrorCode.DEVICE_CONN_FAIL: raise ServiceException( { 'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL) }) elif operation_result['rst'] == ErrorCode.BOARD_UART_TIMEOUT: raise ServiceException( { 'result': 2, 'description': u'设备忙无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能' }) else: raise ServiceException( { 'result': 2, 'description': u'检查设备状态失败({})'.format(operation_result['rst']) }) _result = self.analyze_get_data(operation_result['data']) if _result['comm_result'] != 'SUCCESS': raise ServiceException( {'result': 2, 'description': _result['comm_desc']}) def __init__(self, device): super(SimaierWasher, self).__init__(device) # 足疗机的事件报文和get上来的报文一样,直接调用即可 def analyze_event_data(self, data): return self.analyze_get_data(data) # 解析get到的报文信息 def analyze_get_data(self, data): result = {'comm_result': 'SUCCESS', 'comm_desc': 'SUCCESS', 'status': Const.DEV_WORK_STATUS_IDLE, 'statusInfo': '', 'left_time': 0} crc_result = int(data[2:4], 16) if crc_result != 0x06: result['comm_result'] = 'FAILURE' result['comm_desc'] = u'CRC校验错,请联系平台' return result status = int(data[8:10], 16) if status in reason_dict: if status == 0x01: result['status'] = Const.DEV_WORK_STATUS_APPOINTMENT result['statusInfo'] = reason_dict[status] elif status == 0x03: result['status'] = Const.DEV_WORK_STATUS_WORKING result['statusInfo'] = reason_dict[status] elif status in [0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0xfd]: result['status'] = Const.DEV_WORK_STATUS_FAULT result['statusInfo'] = reason_dict[status] elif status in [0x00, 0x04]: result['status'] = Const.DEV_WORK_STATUS_IDLE result['statusInfo'] = reason_dict[status] elif status in [0x02, 0x05]: result['status'] = Const.DEV_WORK_STATUS_MAINTENANCE result['statusInfo'] = reason_dict[status] else: result['status'] = Const.DEV_WORK_STATUS_FAULT result['statusInfo'] = u'未知状态' result['left_time'] = int(data[10:12], 16) return result def get_dev_info(self, retry, timeout): result = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], 'funCode': '01', 'data': '00'}, timeout = timeout, retry = retry) if result['rst'] != 0: return {'rst': -1, 'isOffline': 1} result.update(self.analyze_get_data(result['data'])) return result def start_device(self, package, openId, attachParas): MODE = { u'加强洗': 0x1, u'标准洗': 0x2, u'快速洗': 0x3, u'单脱水': 0x4, u'桶消毒': 0x5, u'洗衣机自检': 0x6, } mode = hex(int(MODE[package['name']])) operation_result = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], 'funCode': '02', 'data': fill_2_hexByte(mode, 2)}, timeout = MQTT_TIMEOUT.START_DEVICE) if operation_result['rst'] != ErrorCode.DEVICE_SUCCESS: if operation_result['rst'] == ErrorCode.DEVICE_CONN_FAIL: raise ServiceException( {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_START_CONN_FAIL)}) elif operation_result['rst'] == ErrorCode.BOARD_UART_TIMEOUT: raise ServiceException( {'result': 2, 'description': u'设备忙无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'}) else: raise ServiceException( {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_START_FAILURE).format(operation_result['rst'])}) _result = self.analyze_get_data(operation_result['data']) _result['rst'] = ErrorCode.DEVICE_SUCCESS if _result['comm_result'] != 'SUCCESS': raise ServiceException( {'result': 2, 'description': _result['comm_desc']}) if _result['status'] != Const.DEV_WORK_STATUS_WORKING: raise ServiceException( {'result': 2, 'description': _result['statusInfo']}) if _result['left_time'] == 0: Device.invalid_device_control_cache(self._device['devNo']) return _result left_time = _result['left_time'] start_time = int(time.time()) finished_time = start_time + left_time * 60 Device.update_dev_control_cache( self._device['devNo'], { 'openId': openId, 'status': Const.DEV_WORK_STATUS_WORKING, 'startTime': start_time, 'finishedTime': finished_time }) return _result def support_count_down(self, openId = None, port = None): return False