def statusNotify(msg): statusType = msg[16:18] print "statusType:",statusType if statusType == '01':#温湿度 curTemp = int(msg[22:24],16) print "curTemp:",curTemp ret = db.select("ALIVE_DEVICE", where={"aliveRcuId": msg[4:10],"clearDevType": 'alive_airCondition'}).first() if ret is not None: st = ret.get("devStatus", None) if st is not None: stJson = json.loads(ret['devStatus']) stJson['currentTemp'] = curTemp else: stJson = {'currentTemp' : curTemp} print "stJson:", stJson devInfo = {'devStatus': json.dumps(stJson)} db.update("ALIVE_DEVICE", where={"aliveRcuId": msg[4:10],"clearDevType": 'alive_airCondition'}, **devInfo) elif statusType == '02':#服务状态 pass elif statusType == '03':#空调状态 pass elif statusType == '04':#主机ID查询 rcuInfo = {'aliveRcuId': msg[4:10], 'last_timestamp': time.time()} db_replace("ROOM", {"aliveRcuId": msg[4:10]}, rcuInfo) send_cmd(msg[4:10])
def update_room_devices(data): authToken = data.get('authToken', None) devInfo = data.get('devInfos', None) if authToken is None or len(devInfo) < 1: print "no devices found" return 0 for d in devInfo: d['onLine'] = 1 - d['offLine'] d['authToken'] = authToken d.pop('offLine') if d.get('devStatus', None) is not None: d['devStatus'] = json.dumps(d.get('devStatus')) ret = db.select('HAIER_DEVICE', where={'devId': d['devId']}).first() if ret is None: db.insert('HAIER_DEVICE', **d) else: d.pop('devName') db.update('HAIER_DEVICE', where={'devId': d['devId']}, **d)
def revDevInfo(device, gw_mac=None): devInfo = { "id": device.get("id"), "ol": device.get("ol"), "ep": device.get("ep"), "pid": device.get("pid", None), "did": device.get("did", None), "st": json.dumps(device.get("st", {})), "onoff": device.get("st", {}).get("on", None), "time_last": time.time() } if gw_mac is not None: devInfo.update({"gw": gw_mac}) if device.get("did") == constant.SZ_CURTAIN_DID: devInfo['onoff'] = device.get("st", {}).get("pt", None) elif device.get("did") == constant.SZ_MENCI_DID: zsta = device.get("st", {}).get("zsta", None) if zsta is None: #光感照明 return 0 devInfo['onoff'] = zsta - 4 ret = db.select("DEVICE", where={ "id": devInfo["id"], "ep": devInfo["ep"] }).first() if ret is None: db.insert("DEVICE", **devInfo) else: st = ret.get("st", None) if st is not None: stJson = json.loads(ret['st']) for k, v in device.get("st", {}).items(): stJson[k] = v print "stJson:", stJson devInfo['st'] = json.dumps(stJson) db.update("DEVICE", where={ "id": devInfo["id"], "ep": devInfo["ep"] }, **devInfo)
def serviceUploadPacket(clinet_msg, msg): msgInfo = {'aliveRcuId':msg[4:10], 'onLine':1} devId = msg[4:10] + msg[14:20] msgInfo['devId'] = devId room = db.select("ROOM", where={"aliveRcuId": msg[4:10]}).first() if room is None: return 0 if msg[18:20] == '01': #插卡取电 msgInfo['devActionCode'] = int(msg[22:24], 16) db_replace("ALIVE_DEVICE", {"aliveRcuId": msg[4:10], "devId": devId}, msgInfo) if msgInfo['devActionCode'] == 0:#插卡 thread.start_new_thread(welcomeStrategy, (room, 1)) else:#拔卡 thread.start_new_thread(goodbyeStrategy, (room, 2)) thread.start_new_thread(goodbyeStrategy, (room, 4)) elif msg[18:20] == '02':#服务状态 for id in range(1, int(msg[20:22], 16), 2): step = id * 2 msgInfo['devId'] = msg[4:10] + msg[14:20] + msg[20+step:22+step] msgInfo['devActionCode'] = int(msg[22+step:24+step], 16) print "service id:", msg[20+step:22+step] if msg[20+step:22+step] not in {'1a', '1b', '1c'}:#除了service继电器,其它继电器不用记录 continue db_replace("ALIVE_DEVICE", {"aliveRcuId": msg[4:10], "devId": msgInfo['devId']}, msgInfo) mqttJson = { "wxCmd": "devStatus", "devName": constant.ALIVE_SERVICE[msg[20+step:22+step]], "onLine": 1, "actionCode": msgInfo['devActionCode'] } mqtt_client.publish_message(config.project_name + room['roomNo'], json.dumps(mqttJson)) elif msg[18:20] == '03':#空调状态 stringValue = msg[24:52] statusJson = {'stringValue': stringValue, 'mode':constant.ALVIE_2API_MODE_TUP[int(stringValue[0:2], 16)], 'setTemp': int(stringValue[2:4], 16) + 16, 'speed': constant.ALVIE_2API_SPEED_TUP[int(stringValue[4:6], 16)], 'switch': 2 - int(stringValue[20:22], 16)%2} ret = db.select("ALIVE_DEVICE", where={"aliveRcuId": msg[4:10], "clearDevType": 'alive_airCondition'}).first() if ret is not None: stJson = json.loads(ret['devStatus']) statusJson['currentTemp'] = stJson['currentTemp'] else: statusJson['currentTemp'] = 20 send_cmd(msg[4:10]) msgInfo['devActionCode'] = int(msg[44:46],16) msgInfo['devStatus'] = json.dumps(statusJson) db_replace("ALIVE_DEVICE", {"aliveRcuId": msg[4:10],"devId":devId}, msgInfo) mqttJson = { "wxCmd": "devStatus", "devName": '空调', "onLine": 1, "actionCode": msgInfo['devActionCode'], 'devStatus':statusJson } mqtt_client.publish_message(config.project_name + room['roomNo'], json.dumps(mqttJson)) elif msg[18:20] == '04':#当前温度上传 curTemp = int(msg[22:24], 16) print "curTemp:", curTemp ret = db.select("ALIVE_DEVICE", where={"aliveRcuId": msg[4:10],"clearDevType": 'alive_airCondition'}).first() if ret is not None: st = ret.get("devStatus", None) if st is not None: stJson = json.loads(ret['devStatus']) print "stjson:",stJson stJson['currentTemp'] = curTemp else: stJson = {'currentTemp': curTemp} print "stJson:", stJson devInfo = {'devStatus': json.dumps(stJson)} db.update("ALIVE_DEVICE", where={"aliveRcuId": msg[4:10], "clearDevType": 'alive_airCondition'},**devInfo)
def dev_status_notify(data): token = data.get('authToken', None) statusJson = data.get('info', None) roomInfo = db.query("select * from ROOM where authToken='%s'" % (token)) devId = statusJson['devId'] if len(roomInfo) < 1 or statusJson is None: raise Exception("room token: %s or status: %s not found" % (token, statusJson)) room = roomInfo[0] res = db.query("select * from HAIER_DEVICE where devId='%s'" % (devId)) devStatus = statusJson.get('devStatus', None) onLine = 0 if statusJson['offLine'] else 1 if len(res) < 1: # 设备不存在,插入 db.insert('HAIER_DEVICE', devId=statusJson['devId'], devName=statusJson['devName'], devType=statusJson['devType'], devSecretKey=statusJson['devSecretKey'], devActionCode=statusJson['devActionCode'], devStatus=json.dumps(devStatus), onLine=onLine, authToken=token) else: #更新rcu设备状态到数据库 db.update('HAIER_DEVICE', where="devId = $ID", vars={'ID': devId}, onLine=onLine, devStatus=json.dumps(devStatus), devActionCode=statusJson['devActionCode']) # mqtt发送状态更新 devInfo = db.query( "select * from ROOM r,HAIER_DEVICE d where r.authToken=d.authToken and d.devId='%s'" % (statusJson['devId'])) if len(devInfo) > 0: dev = devInfo[0] mqttJson = { "wxCmd": "devStatus", "devName": dev['devName'], "onLine": dev['onLine'], "actionCode": dev['devActionCode'] } if devStatus is not None: mqttJson['devStatus'] = devStatus mqtt_client.publish_message(config.project_name + room['roomNo'], json.dumps(mqttJson)) # 将状态变动发给websocket mqttJson['wsCmd'] = mqttJson.pop('wxCmd') mqttJson['roomNo'] = room['roomNo'] websocketServer.send_message_to_all(json.dumps(mqttJson)) #插卡取电 if statusJson['devType'] == 2 and devStatus is not None: if devStatus['cardStatus'] == 1: protocol.openWindow(room) thread.start_new_thread(welcomeStrategy, (room, 1)) get_room_devices(token) get_room_services(token) elif devStatus['cardStatus'] == 0: thread.start_new_thread(goodbyeStrategy, (room, 9)) thread.start_new_thread(goodbyeStrategy, (room, 13))