def post(self): ''' { meeting_room_guid: channel_guid_list:[] } ''' # todo:获取参数 try: data = json.loads(self.request.body) except: result = {'ret': 1} return self.write(json_dumps(result)) meeting_room_guid = data.get('meeting_room_guid') channel_guid_list = data.get('channel_guid_list') meetingRoomObject = self.mosquittoObject.GuidToMeetingRoom.get( meeting_room_guid) if meetingRoomObject == None: result = {'ret': 1} else: info = {} for channel_guid in channel_guid_list: channel_object = meetingRoomObject.channel_dict.get( channel_guid) if channel_object == None: info[channel_guid] = '' else: info[channel_guid] = channel_object.getSelfStatus() else: result = {'ret': 0, 'msg': info} self.write(json_dumps(result))
def handleOnMessage(self, ReceiveMessageObject): topic_list = ReceiveMessageObject.topicList meeting_room_guid = topic_list[1] if topic_list[2] == 'receive': # 其他服务主动获取--会议室排程信息[当前会议,下次会议,会议室排程状态] if topic_list[6] == 'schedule_info': get_from = topic_list[7] topic = '/aaiot/{}/send/controlbus/event/schedule/schedule_info/{}'.format( meeting_room_guid, get_from) meeting_room_object = self.meetingRoomDict.get( meeting_room_guid) if meeting_room_object != None: info = yield meeting_room_object.getSelfScheduleInfo() yield logClient.tornadoInfoLog('会议室:{}.获取排程信息'.format( meeting_room_object.name)) if get_from == 0 or get_from == '0': yield self.myPublish(topic, json_dumps(info), qos=QOSLOCAL) elif get_from == 1 or get_from == '1': yield self.myPublish(topic, json_dumps(info), qos=QOS) else: return # 其他服务主动获取--会议室排程剩余时间 elif topic_list[6] == 'remaining_time': topic = '/aaiot/{}/send/controlbus/event/schedule/remaining_time/0'.format( meeting_room_guid) meeting_room_object = self.meetingRoomDict.get( meeting_room_guid) if meeting_room_object != None: yield logClient.tornadoInfoLog('会议室:{}.获取排程剩余时间信息'.format( meeting_room_object.name)) info = yield meeting_room_object.getSelfRemainingTime() yield self.myPublish(topic, json_dumps(info), qos=QOSLOCAL) # 取消排程 elif topic_list[6] == 'cancel': meeting_room_object = self.meetingRoomDict.get( meeting_room_guid) if meeting_room_object == None: return yield meeting_room_object.overNowSchedule() # 监控到会议室排程信息发生改变 elif topic_list[2] == 'send': print(ReceiveMessageObject.topic) if topic_list[6] == 'add' or topic_list[ 6] == 'change' or topic_list[6] == 'remove': meeting_room_object = self.meetingRoomDict.get( meeting_room_guid) if meeting_room_object != None: yield gen.sleep(5) yield meeting_room_object.handleSelfSchedule( update_schedule=1)
def cityWeather(self, clientObject, connection_token, user_info, msg): city_guid = user_info.get('city_guid') if city_guid == msg.get('guid'): msg_connection_token = msg.get('connection_token') if msg_connection_token: if msg_connection_token == connection_token: msg['connection_token'] = connection_token yield clientObject.sendToClient(json_dumps(msg)) else: msg['connection_token'] = connection_token yield clientObject.sendToClient(json_dumps(msg))
def handle_message(self): try: while self.mosquittoReceiveBuffer: try: msg = self.mosquittoReceiveBuffer.pop(0) except: return topic = msg.get('topic') topic_list = msg.get('topic_list') control_event = msg.get('data') data = { 'type': 'control', 'topic': topic, 'topic_list': topic_list, 'control_event': control_event } meeting_room_guid = topic_list[1] type_ = topic_list[5] if type_ == 'command': channel = topic_list[7] if channel == 'macro': #计算macro信息 try: macro_info = json.loads(control_event) except: macro_info = [] yield self.updateMeetingRoomMacro( meeting_room_guid, macro_info) data = { 'type': 'sync_device', 'meeting_room_guid': meeting_room_guid } yield logClient.tornadoDebugLog(json_dumps(data)) for websocketObject in self.websocketObject.clientObjectSet: if meeting_room_guid in websocketObject.meeting_room_list: websocketObject.sendToClientService( json_dumps(data)) else: yield logClient.tornadoDebugLog(json_dumps(data)) if topic_list[5] == 'macro': yield self.macroControlHandle(meeting_room_guid, topic, topic_list, control_event) else: yield self.transmitControlComand( meeting_room_guid, data) except Exception as e: yield logClient.tornadoErrorLog(str(e)) self.handle_message_task = None
def get_meeting_room_status(self, msg): ''' msg = { 'ret': 0, 'type': request_type, 'company_db':company_db, 'connection_token': connection_token, } ''' connection_token = msg.get('connection_token') company_db = msg.get('company_db') status_info_list = [] for meeting_room_object in self.meeting_room_list: if meeting_room_object.company_db == company_db: info = yield meeting_room_object.get_meeting_room_status() status_info_list.append(info) else: content = { 'ret': 0, 'type': 'meeting_room_status', 'meeting_room_status_info_list': status_info_list, 'errmsg': '', 'connection_token': connection_token } topic = '/aaiot/0/send/controlbus/event/websocket/0/meeting_room_status' yield self.my_publish(topic, json_dumps(content), QOS_LOCAL)
def sparateChange(self, clientObject, connection_token, user_info, msg): mode = msg.get('mode') meeting_room_info = msg.get('meeting_room_info') result = False use_room_list = user_info.get('use_room_list') if mode == 0: # 合房模式 separate_meeting_room_guid_list = meeting_room_info.get( 'separate_meeting_room_guid_list') for room_guid in use_room_list: if room_guid in separate_meeting_room_guid_list: result = True break elif mode == 1: # 分房模式 real_meeting_room_guid = meeting_room_info.get( 'real_meeting_room_guid') if real_meeting_room_guid in use_room_list: result = True else: return if result: data = { 'type': 'separate_out', 'mode': mode, 'connection_token': connection_token } yield clientObject.sendToClient(json_dumps(data))
async def post(self): data = await self.request.json() sql = data.get('sql') args = data.get('args') size = data.get('size') result = await self.sqlSelect(sql, args, size) self.write(json_dumps(result))
def get_device_info(self, msg): connection_token = msg.get('connection_token') company_db = msg.get('company_db') meeting_room_guid = msg.get('meeting_room_guid') device_info_list = [] for meeting_room_object in self.meeting_room_list: if meeting_room_guid: result = meeting_room_guid == meeting_room_object.guid elif company_db: result = meeting_room_object.company_db == company_db else: continue if result: try: info = yield meeting_room_object.get_meeting_room_device() device_info_list.append(info) except Exception as e: print(str(e)) else: content = { 'ret': 0, 'type': 'meeting_room_device', 'meeting_room_device_info_list': device_info_list, 'errmsg': '', 'connection_token': connection_token } topic = '/aaiot/0/send/controlbus/event/websocket/0/meeting_room_device' yield self.myPublish(topic, json_dumps(content), QOS_LOCAL)
def asyncTornadoRequest( url:str, params:dict=None, body:dict=None, method:str='GET', headers:dict=None, allow_nonstandard_methods:bool=False ): if body: body = json_dumps(body) if params: url = url_concat(url, params) request = httpclient.HTTPRequest( url=url, headers=headers, method=method, body=body, allow_nonstandard_methods=allow_nonstandard_methods, ) http = httpclient.AsyncHTTPClient() try: resp = yield http.fetch(request) result = json.loads(resp.body.decode()) except Exception as e: result = { "status": 501, "msg": e } return result
def get(self, *args, **kwargs): callback = self.get_argument('callback', None) method = self.get_argument('method', None) token_error = self.get_argument('token_error', None) if token_error != None: yield logClient.tornadoInfoLog('收到反馈,token失效') yield self.server.login() content = {'ret': 0, 'token': self.server.token} elif callback != None and method != None: info = {'callback': callback, 'method': method} self.server.callback_address_set.add(json_dumps(info)) content = {'ret': 0, 'token': self.server.token} else: content = { 'ret': 1, } self.write(json_dumps(content))
def on_close(self): #从server的websocket对象集合中移除自己 self.server.clientObjectSet.discard(self) yield logClient.tornadoInfoLog('用户 {} 退出登录,当前连接数量:{}'.format( self.user_name, len(self.server.clientObjectSet))) msg = {'type': 'logout', 'connection_token': self.connection_token} self.server.sendToService(json_dumps(msg)) self = None
async def delete(self): try: data = json.loads(self.request.body) transaction_point = data.get('transaction_point') transactionInfo = await self.rollbackTransaction(transaction_point) except: transactionInfo = {'ret': RET.PARAMERR} self.write(json_dumps(transactionInfo))
def reportSelfDevice(self,type_info,meeting_room_guid): device_info = yield self.getSelfDevice(meeting_room_guid) data = { 'type': type_info, 'meeting_room_device_list': device_info, 'meeting_room_guid':meeting_room_guid, } yield self.sendToService(json_dumps(data))
async def post(self, mode): # todo:获取参数 try: data = json.loads(self.request.body) result = await self.insertOne(mode, data) except: result = {'ret': RET.PARAMERR} self.write(json_dumps(result))
def baseErrorReturn(self,msg_type,return_msg,connection_token): msg = { 'ret':1, 'type':msg_type, 'errmsg':return_msg, 'connection_token':connection_token } yield self.sendToClient(json_dumps(msg))
async def post(self, mode): try: data = json.loads(self.request.body) result = await self.conditionInsertOne(mode, data) except: result = { 'ret': RET.PARAMERR, } self.write(json_dumps(result))
def sqlExecute(self, table, data): resp = requests.post('http://{}:{}/sqlExecute/{}'.format( self.host, self.port, table), data=json_dumps(data)) try: result = json.loads(resp.text) except JSONDecodeError as e: result = resp.text return result
def wsClientKeep_alive(self): if self.serviceObject == None: self.wsClientConnect() else: self.heartDelayCount += 1 if self.heartDelayCount >= 10: self.heartDelayCount = 0 msg = {'type': 'heartbeat'} self.sendToService(json_dumps(msg))
def controlHandle(self, msg_type, msg): connection_token = msg.get('connection_token') login_info = yield self.checkLoginState(msg_type, connection_token) if login_info == False: return # 矩阵控制前端需要的数据 # msg = { # 'guid':'', # 'meeting_room_guid':'', # 'control_flag':4, # 'event_type':'click', # 'input_port':'输入段guid', # 'status':'当前有为on,没有连接为off', # } user_type = login_info.get('user_type') user_id = login_info.get('user_id', '') channel_guid = msg.get('guid') meeting_room_guid = msg.get('meeting_room_guid') control_type = msg.get('control_flag', 1) event_type = msg.get('event_type') status = msg.get('status') # 添加兼容矩阵控制 input_port = msg.get('input_port') # 1 正常模式 # 0 不发送MQTT指令 # 2 不回control信息 # 3 不回control信息,不发送MQTT指令 # 判断控制方式是否正确 try: control_type = int(control_type) except: if control_type not in [0,1,2,3]: yield self.baseErrorReturn(msg_type,'control_type parameter error',connection_token) return if user_type == 'applet' or user_type == 'front-end': yield self.baseErrorReturn(msg_type, 'No control permission',connection_token) return elif user_type == 'back-end' or user_type == 'ipad': topic = '/aaiot/0/receive/controlbus/event/websocket/0/{}'.format(msg_type) msg = { 'ret': 0, 'type': msg_type, 'meeting_room_guid': meeting_room_guid, 'channel_guid': channel_guid, 'user_id': user_id, 'event_type': event_type, 'status': status, 'control_type': control_type, 'connection_token': connection_token, } # 添加兼容矩阵控制 if input_port != None: msg['input_port'] = input_port self.mosquittoClient.myPublish(topic, json_dumps(msg), QOS_LOCAL) return
def control_device(self, control_type, msg): ''' msg = { 'ret':0, 'type':'control', 'meeting_room_guid':'会议室guid', 'channel_guid':'通道guid', 'user_id':'用户id', 'event_type':'事件类型', 'status':'当前状态', 'control_type':'控制类型' ‘input_port’:'连接的输入源guid 'connection_token':'连接token‘ } ''' # msg = { # 'ret': 0, # 'type': 'control', # 'meeting_room_guid': '会议室guid', # 'channel_guid': '通道guid', # 'event_type': 'click', # 'status': '当前状态', # 'control_type': '控制类型', # 'input_port':'连接的输入源guid', # 'connection_token':'连接token', # } connection_token = msg.get('connection_token') meeting_room_guid = msg.get('meeting_room_guid') channel_guid = msg.get('channel_guid') meeting_room_object = self.GuidToMeetingRoom.get(meeting_room_guid) if meeting_room_object == None: yield logClient.tornadoWarningLog('控制未知会议室设备') return back_msg = yield meeting_room_object.userControl(msg) ret = back_msg.get('ret') if ret == 0: content = { 'ret': 0, 'type': control_type, 'guid': channel_guid, 'control_event': back_msg.get('event_type'), 'status': back_msg.get('status'), 'connection_token': connection_token } else: content = { 'ret': 1, 'type': control_type, 'guid': channel_guid, 'connection_token': connection_token } topic = '/aaiot/0/send/controlbus/event/websocket/0/control' yield self.myPublish(topic, json_dumps(content), QOS_LOCAL)
def statusFeedback(self, clientObject, connection_token, user_info, msg): authorization_status = user_info.get('authorization_status') if not authorization_status: return use_room_list = user_info.get('use_room_list') if not isinstance(use_room_list, list): return msg_meeting_room_guid = msg.get('meeting_room_guid') if msg_meeting_room_guid in use_room_list: msg['connection_token'] = connection_token yield clientObject.sendToClient(json_dumps(msg))
def deviceSwitch(self, clientObject, connection_token, user_info, msg): user_type = user_info['user_type'] if user_type == 'back-end': use_room_list = user_info.get('use_room_list') if not isinstance(use_room_list, list): return msg_meeting_room_guid = msg.get('meeting_room_guid') if msg_meeting_room_guid in use_room_list: # 添加一个connection_token以便代理推送 msg['connection_token'] = connection_token yield clientObject.sendToClient(json_dumps(msg))
def query_status(self, guid, meeting_room_guid): data = { 'type': 'query', 'guid': guid, 'meeting_room_guid': meeting_room_guid, 'connection_token': self.connection_token } # print('执行查询{}'.format(guid)) self.server.sendToService(json_dumps(data)) try: del self.query_task_dict[guid] except: pass
def reportDeviceInfo(self, meeting_room_guid, device_list, macro_list): topic = '/aaiot/{}/receive/controlbus/event/command/0/device'.format( meeting_room_guid) data = { "type": "device", "devices_list": device_list, "marco_list": macro_list, "ts": time.time(), "status": "ok", "processTime": 0 } self.my_publish(topic, json_dumps(data)) yield logClient.tornadoDebugLog('同步设备,macro 信息给后台')
def getMeetingRoomSchedule(self, msg): connection_token = msg.get('connection_token') meeting_room_guid = msg.get('meeting_room_guid') meeting_room_object = self.GuidToMeetingRoom.get(meeting_room_guid) if meeting_room_object is not None: schedule_info = yield meeting_room_object.getSelfScheduleInfo() content = { 'ret': 0, 'type': 'meeting_room_schedule', 'schedule_info': schedule_info, 'meeting_room_guid': meeting_room_guid, 'connection_token': connection_token } topic = '/aaiot/0/send/controlbus/event/websocket/0/meeting_room_schedule' yield self.myPublish(topic, json_dumps(content), QOS_LOCAL)
def wsClientLoopRun(self): '''接收websocket服务器的信息''' try: msg = yield self.serviceObject.read_message() except: msg = None # return if msg is None: yield logClient.tornadoInfoLog("connection closed") yield self.closeAllClient() else: msg = json.loads(msg) yield logClient.tornadoDebugLog('2---服务端信息:({})'.format(msg)) yield self.forwardInformation(json_dumps(msg)) self.ioloop.add_timeout(self.ioloop.time(), self.wsClientLoopRun)
def meetingRoomScheduleHandle(self,msg_type,msg): connection_token = msg.get('connection_token') login_info = yield self.checkLoginState(msg_type, connection_token) if login_info == False: return meeting_room_guid = msg.get('meeting_room_guid') topic = '/aaiot/0/receive/controlbus/event/websocket/0/meeting_room_schedule' msg = { 'ret': 0, 'type': msg_type, 'meeting_room_guid': meeting_room_guid, 'connection_token': connection_token, } self.mosquittoClient.myPublish(topic, json_dumps(msg), QOS_LOCAL) return
def updateMany(self, table, data): headers = { 'content-type': 'application/json', } resp = requests.post('http://{}:{}/updateMany/{}'.format( self.host, self.port, table), data=json_dumps(data), headers=headers) try: result = resp.json() except: result = { 'ret': 1, 'msg': [], } return result
def weatherInfoHandle(self, msg_type, msg): connection_token = msg.get('connection_token') login_info = yield self.checkLoginState(msg_type, connection_token) if login_info == False: return city_guid = msg.get('city_guid') login_info['city_guid'] = city_guid content = { 'type': msg_type, 'ret': 0, 'city_guid': city_guid, 'connection_token': connection_token } topic = '/aaiot/{}/receive/controlbus/event/websocket/city_weather/0'.format(city_guid) self.mosquittoClient.myPublish(topic, json_dumps(content), QOS_LOCAL) return
async def asyncioSqlExecute(self, table, data): url = 'http://{}:{}/sqlExecute/{}'.format(self.host, self.port, table) headers = { 'content-type': 'application/json', } async with aiohttp.ClientSession() as session: async with session.post(url, data=json_dumps(data), headers=headers) as resp: if resp.status == 200: result = await resp.json() else: result = { 'ret': 1, 'msg': [], 'errmsg': await resp.text(encoding='utf8') } return result