Exemplo n.º 1
0
    async def post(self):
        try:
            room = self.get_body_argument("room")
            status = int(self.get_body_argument("status"))
            await self.application.objects.get(ClassRoom, name=room)
            airs = await self.application.objects.execute(
                WifiDevice.select().where((WifiDevice.device_name % 'air%')
                                          & (WifiDevice.class_number == room)))
            devices = await self.application.objects.execute(
                WifiDevice.select().where((WifiDevice.device_name % 'fan%')
                                          & (WifiDevice.class_number == room)))
            dead_devices = []

            for air in airs:
                if air.is_alive:
                    # 创建控制器执行命令,空调在单独的板,需要单独设置
                    air_contorller = DeviceController(air.device_name,
                                                      air.class_number)
                    if status:
                        air_contorller.turn_on_air()
                    else:
                        air_contorller.turn_off_air()
                    await air_contorller.send()
                else:
                    dead_devices.append(air.device_number)

            # 控制风扇,灯,窗帘
            contorller = DeviceController(devices[0].device_name,
                                          devices[0].class_number)
            for device in devices:
                if device.is_alive:
                    if status:
                        contorller.turn_on(device.device_number)
                    else:
                        contorller.turn_off(device.device_number)
                else:
                    dead_devices.append(device.device_number)
            await contorller.send()

            self.write({"code": code["success"], "DeadDevices": dead_devices})
        except DeviceNotConnectException:
            self.write({"code": code["fail"], "msg": "控制板掉线,请稍候再试"})
        except DeviceNotExistException:
            self.write({"code": code["fail"], "msg": "设备全掉线"})
        except ClassRoom.DoesNotExist:
            self.write({"code": code["fail"], "msg": "不存在该教室"})
        except MissingArgumentError:
            self.write({"code": code["fail"], "msg": "缺少参数"})
        except ValueError:
            self.write({"code": code["fail"], "msg": "status参数错误"})
Exemplo n.º 2
0
 async def get(self):
     try:
         room=self.get_query_argument("room")
         await self.application.objects.get(ClassRoom,name=room)
         devices=await self.application.objects.execute(WifiDevice.select().where(WifiDevice.class_number==room))
         devices_list=[]
         type_dic={
             "fan":"1",
             "lamp":"2",
             "air":"3",
             "curtain":"4"
         }
         for device in devices:
             device_dic={}
             device_dic["device_name"] = device.device_number
             #获取设备类型
             device_dic["type"]=type_dic[device.device_number.split("-")[0]]
             device_dic["status"]=device.status
             device_dic["is_alive"]=device.is_alive
             devices_list.append(device_dic)
         self.write({
             "code": code["success"],
             "devices": devices_list
         })
     except ClassRoom.DoesNotExist:
         self.write({
             "code": code["fail"],
             "msg": "不存在该教室"
         })
     except MissingArgumentError:
         self.write({
             "code": code["fail"],
             "msg": "缺少参数"
          })
Exemplo n.º 3
0
    async def session_start(self, session):
        sc_tables = Scheduler_Table.select().where(
            (Scheduler_Table.date == self.date)
            & (Scheduler_Table.session == session)).execute()
        # 上课做的事
        for sc_table in sc_tables:
            room = ClassRoom.get(id=sc_table.room_id)
            airs = WifiDevice.select().where(
                (WifiDevice.device_name % 'air%')
                & (WifiDevice.class_number == room.name)).execute()
            devices = WifiDevice.select().where(
                (WifiDevice.device_name % 'fan%')
                & (WifiDevice.class_number == room.name)).execute()
            for air in airs:
                # 创建控制器执行命令,空调在单独的板,需要单独设置
                air_contorller = DeviceController(air.device_name,
                                                  air.class_number)
                air_contorller.turn_on_air()
                await air_contorller.send()

            # 控制风扇,灯,窗帘
            contorller = DeviceController(devices[0].device_name,
                                          devices[0].class_number)
            for device in devices:
                contorller.turn_on(device.device_number)
            await contorller.send()

            # 开始下课任务
            end_session_schedule = TornadoScheduler()
            end_session_schedule.add_job(
                self.session_end,
                'cron', (
                    end_session_schedule,
                    airs,
                    devices,
                    session,
                ),
                hour=session_time["end"][session][0],
                minute=session_time["end"][session][1])
            end_session_schedule.start()
            logging.info("room_id: " + str(sc_table.room_id) + " open date: " +
                         self.date + " session: " + str(session))
Exemplo n.º 4
0
    def __init__(self, device_name, class_room):
        if not device_name + '+' + class_room in TCP_CONNECTION.keys():
            logging.info(TCP_CONNECTION.keys())
            raise DeviceNotConnectException

        result = WifiDevice.select() \
            .where(WifiDevice.class_number == class_room).execute()
        self.devices = []
        for device in result:
            self.devices.append(device.device_number)
        self.device_name = device_name
        self.class_room = class_room
        self.send_data = {"device_name": device_name, "class": class_room}
Exemplo n.º 5
0
 async def get(self):
     # obj = await self.application.objects.create_or_get(LoraDevice, device_name='3634374710300059')
     # #             temperature = str(struct.unpack('!f', DATA[0:4])[0])[:5]
     # #             humidity = str(struct.unpack('!f', DATA[4:8])[0])[:5]
     # obj2 = await self.application.objects.create_or_get(LoraDevice, device_name='36343747104a002e')
     devices = WifiDevice.select()
     # print(obj2[0].data)
     await self.render(
         "indextest.html",
         # temperature=obj[0].data[0:5],
         # date2=obj2[0].date, humidity=obj[0].data[5:10], date=obj[0].date,
         devices=devices,
         # temperature2=int(obj2[0].data[:4], 16)/10, humidity2=int(obj2[0].data[5:], 16)/10
     )
Exemplo n.º 6
0
 async def get(self):
     try:
         room = self.get_query_argument("room")
         await self.application.objects.get(ClassRoom, name=room)
         devices = await self.application.objects.execute(
             WifiDevice.select().where(WifiDevice.class_number == room))
         devices_list = []
         for device in devices:
             device_dic = {}
             device_dic["device_name"] = device.device_number
             if device.device_number == "0":
                 device_dic["device_name"] = device.device_name
                 device_dic["degree"] = device.degree
             device_dic["status"] = device.status
             device_dic["is_alive"] = device.is_alive
             devices_list.append(device_dic)
         self.write({"code": code["success"], "devices": devices_list})
     except ClassRoom.DoesNotExist:
         self.write({"code": code["fail"], "msg": "不存在该教室"})
     except MissingArgumentError:
         self.write({"code": code["fail"], "msg": "缺少参数"})
Exemplo n.º 7
0
    async def handle_stream(self, stream, address):
        info_dict = {}
        try:
            while True:
                msg = await stream.read_bytes(2048, partial=True)
                # print(msg)
                # logging.info('记录tcp连接:{}'.format(msg))
                msg = msg.decode('utf-8')
                # logging.info('msg1{}'.format(msg))
                for msg in re.findall('({.*?})', msg):
                    print(msg)
                    # logging.info(msg, '-----------------------------msg----------------------')
                    # print(msg, '------------------------------msg----------------------')
                    msg = eval(msg)
                    # logging.info('msg{}'.format(msg))
                    # 掉线处理要用到的port
                    msg['port'] = address[1]
                    # 判断是否为初次建立连接,发送heartbeat
                    # 用规定的指令设备名和课室创建key
                    # 处理返回指令
                    if (
                            msg['device_name'] + msg['class']
                            in info_dict.keys()
                    ):  # 判断是否已经连接,这里匹配,应该为 {'device_name': 'fan-lamp-curtain', 'class': 'G511', 'fan': '3', 'lamp': '3', 'curtain': '1'}
                        # info_dict根据device_name和class来存放整一个msg
                        if (msg == info_dict[msg['device_name'] +
                                             msg['class']]):  # 判断是否心跳包
                            # print(info_dict[msg['device_name']])
                            print('状态相同,心跳包')
                            # logging.info('心跳包')
                            continue
                        else:
                            # 下发指令或者android更改设备状态返回信息
                            # logging.info('更新操作{}'.format(msg))

                            # if (msg['device_name'] == 'fan-lamp-curtain'):
                            if (re.findall('lamp|fan|curtain', str(msg))):
                                print('风扇更新操作')
                                match_list = re.findall('\w*-\d', str(msg))
                                # 匹配lamp-1,fan-1...
                                for i in match_list:
                                    # i: ['lamp-1', 'curtain-1'.....]
                                    update = WifiDevice.update(status=msg[i]). \
                                        where(
                                        (WifiDevice.device_number == i) & (WifiDevice.class_number == msg['class']))
                                    update.execute()
                                    # 更新info_dict信息里面单个信息
                                    info_dict[
                                        msg['device_name'] +
                                        msg['class']][i] = msg[
                                            i]  # msg[i] 为lamp-1的 ‘1’ or ‘0’                            print('更新操作')
                            # elif (msg['device_name'] == 'fan-lamp'):
                            #
                            #     match_list = re.findall('\w*-\d', str(msg))
                            #     # 匹配lamp-1,fan-1...
                            #     for i in match_list:
                            #         update = WifiDevice.update(status=msg[i]). \
                            #             where(
                            #             (WifiDevice.device_number == i) & (WifiDevice.class_number == msg['class']))
                            #         update.execute()
                            #         # 更新info_dict信息里面单个信息
                            #         info_dict[msg['device_name']+msg['class']][i] = msg[i] # msg[i] 为lamp-1的 ‘1’ or ‘0’
                            elif (re.findall('air', msg['device_name'])):
                                print('空调更新')
                                # logging.info('air{}'.format(msg))
                                # logging.info('info_dict{}'.format(info_dict[msg['device_name']]))
                                if ('degree' in msg.keys()
                                        and 'status' in msg.keys()
                                        and 'gear' in msg.keys()
                                        and 'model' in msg.keys()):
                                    # logging.info('更新全部')
                                    # logging.info(msg)
                                    update = WifiDevice.update(degree=msg['degree'], status=msg['status'], gear=msg['gear'], model=msg['model']). \
                                        where(
                                        (WifiDevice.device_name == msg['device_name']) & (WifiDevice.class_number == msg['class']))
                                    update.execute()
                                    info_dict[
                                        msg['device_name'] +
                                        msg['class']]['degree'] = msg['degree']
                                    info_dict[
                                        msg['device_name'] +
                                        msg['class']]['status'] = msg['status']
                                    info_dict[
                                        msg['device_name'] +
                                        msg['class']]['gear'] = msg['gear']
                                    info_dict[
                                        msg['device_name'] +
                                        msg['class']]['model'] = msg['model']
                                # 单独更新
                                elif ('degree' in msg.keys()):
                                    update = WifiDevice.update(degree=msg['degree']). \
                                        where(
                                        (WifiDevice.device_name == msg['device_name']) & (WifiDevice.class_number == msg['class']))
                                    update.execute()
                                    # logging.info('更新degree')
                                    info_dict[
                                        msg['device_name'] +
                                        msg['class']]['degree'] = msg['degree']
                                elif ('status' in msg.keys()):
                                    update = WifiDevice.update(status=msg['status']). \
                                        where(
                                        (WifiDevice.device_name == msg['device_name']) & (WifiDevice.class_number == msg['class']))
                                    update.execute()
                                    # logging.info('更新status')
                                    info_dict[
                                        msg['device_name'] +
                                        msg['class']]['status'] = msg['status']
                                elif ('gear' in msg.keys()):
                                    update = WifiDevice.update(gear=msg['gear']). \
                                        where(
                                        (WifiDevice.device_name == msg['device_name']) & (WifiDevice.class_number == msg['class']))
                                    update.execute()
                                    # logging.info('更新gear')
                                    info_dict[
                                        msg['device_name'] +
                                        msg['class']]['gear'] = msg['gear']
                                elif ('model' in msg.keys()):
                                    update = WifiDevice.update(gear=msg['model']). \
                                        where(
                                        (WifiDevice.device_name == msg['device_name']) & (WifiDevice.class_number == msg['class']))
                                    update.execute()
                                    # logging.info('更新model')
                                    info_dict[
                                        msg['device_name'] +
                                        msg['class']]['model'] = msg['model']
                            elif (re.findall('controller',
                                             msg['device_name'])):
                                match_list = re.findall('console-\d', str(msg))
                                # 匹配lamp-1,fan-1...
                                for i in match_list:
                                    update = WifiDevice.update(status=msg[i]). \
                                        where(
                                        (WifiDevice.device_number == i) & (WifiDevice.class_number == msg['class']))
                                    update.execute()
                                    # 更新info_dict信息里面单个信息
                                    info_dict[msg['device_name'] +
                                              msg['class']][i] = msg[i]
                            elif (re.findall('switch', msg['device_name'])):
                                # match_list = re.findall('switch-\d', str(msg))
                                print('开关动作')
                                # 匹配lamp-1,fan-1...
                                devices = WifiDevice.select() \
                                    .where((WifiDevice.class_number == msg['class']) & (
                                            WifiDevice.device_name == msg['device_name'])).execute()
                                list_ = [
                                    device.device_number for device in devices
                                ]
                                if msg['status'] == '1':
                                    print('action ==1 ')
                                    await TCP_CONNECTION[
                                        msg['device_name'] + msg['class']
                                    ].write(
                                        b'''{'devices_name': 'fan-lamp-curtain', 'class': 'C1004', 'lamp-1': '0', 'lamp-2': '0','lamp-3': '0', 'fan-1': '0', 'fan-2': '0', 'curtain-1':'0'}'''
                                    )
                                    # await device_controller.turn_devices_on(list_)
                                    # for i in list_:
                                    #     pass
                                    # await DeviceController(i, msg['class']).turn_devices_on(list_)
                                elif msg['status'] == '0':
                                    await TCP_CONNECTION[
                                        msg['device_name'] + msg['class']
                                    ].write(
                                        b'''{'device_name': 'fan-lamp-curtain', 'class': 'C1004', 'lamp-1': '0', 'lamp-2': '0','lamp-3': '0', 'fan-1': '0', 'fan-2': '0', 'curtain-1':'0'}'''
                                    )
                                    print('0')
                                    # for i in list_:
                                    #     pass
                                    # await DeviceController(i, msg['class']).turn_devices_off(list_)
                                update = WifiDevice.update(status=msg['status']). \
                                    where(
                                    (WifiDevice.device_name == msg['device_name']) & (
                                                WifiDevice.class_number == msg['class']))
                                update.execute()
                                # 更新info_dict信息里面单个信息
                                info_dict[
                                    msg['device_name'] +
                                    msg['class']]['status'] = msg['status']
                    else:
                        print('第一次连接')
                        # logging.info('第一次连接')
                        return_dict = {}
                        TCP_CONNECTION[msg['device_name'] +
                                       msg['class']] = stream
                        # stream.write(bytes(msg, encoding='utf-8'))
                        # 根据初次连接上发的指令,记录到数据库
                        if ('lamp' in msg.keys() and 'fan' in msg.keys()
                                and 'curtain' in msg.keys()):
                            print('同时存在')
                            logging.info('三个设备同时存在')
                            for i in range(1, int(msg['fan']) + 1):
                                fan = WifiDevice.get_or_create(
                                    device_number='fan-{}'.format(i),
                                    class_number=msg['class'],
                                    defaults={
                                        'device_name': msg['device_name'],
                                        'is_alive': 1,
                                        'port': address[1]
                                    })
                                return_dict['fan-{}'.format(i)] = fan[0].status
                            for j in range(1, int(msg['lamp']) + 1):
                                lamp = WifiDevice.get_or_create(
                                    device_number='lamp-{}'.format(j),
                                    class_number=msg['class'],
                                    defaults={
                                        'device_name': msg['device_name'],
                                        'is_alive': 1,
                                        'port': address[1]
                                    })
                                return_dict['lamp-{}'.format(
                                    j)] = lamp[0].status
                            for k in range(1, int(msg['curtain']) + 1):
                                curtain = WifiDevice.get_or_create(
                                    device_number='curtain-{}'.format(k),
                                    class_number=msg['class'],
                                    defaults={
                                        'device_name': msg['device_name'],
                                        'is_alive': 1,
                                        'port': address[1]
                                    })
                                return_dict['curtain-{}'.format(
                                    k)] = curtain[0].status
                        elif ('lamp' in msg.keys() and 'fan' in msg.keys()):
                            print('lamp和fan同时存在')
                            logging.info('风扇,灯同时存在')
                            for i in range(1, int(msg['fan']) + 1):
                                fan = WifiDevice.get_or_create(
                                    device_number='fan-{}'.format(i),
                                    class_number=msg['class'],
                                    defaults={
                                        'device_name': msg['device_name'],
                                        'is_alive': 1,
                                        'port': address[1]
                                    })
                                return_dict['fan-{}'.format(i)] = fan[0].status
                            for j in range(1, int(msg['lamp']) + 1):
                                lamp = WifiDevice.get_or_create(
                                    device_number='lamp-{}'.format(j),
                                    class_number=msg['class'],
                                    defaults={
                                        'device_name': msg['device_name'],
                                        'is_alive': 1,
                                        'port': address[1]
                                    })
                                return_dict['lamp-{}'.format(
                                    j)] = lamp[0].status
                        elif ('fan' in msg.keys()):
                            print('fan存入数据库操作')
                            for i in range(1, int(msg['fan']) + 1):
                                fan = WifiDevice.get_or_create(
                                    device_number='fan-{}'.format(i),
                                    class_number=msg['class'],
                                    defaults={
                                        'device_name': msg['device_name'],
                                        'is_alive': 1,
                                        'port': address[1]
                                    })
                                return_dict['fan-{}'.format(i)] = fan[0].status
                                print(fan[0].status)
                                print(msg.keys())
                        elif ('lamp' in msg.keys()):
                            print('lamp存入数据库')
                            for j in range(1, int(msg['lamp']) + 1):
                                lamp = WifiDevice.get_or_create(
                                    device_number='lamp-{}'.format(j),
                                    class_number=msg['class'],
                                    defaults={
                                        'device_name': msg['device_name'],
                                        'is_alive': 1,
                                        'port': address[1]
                                    })
                                return_dict['lamp-{}'.format(
                                    j)] = lamp[0].status

                        elif ('curtain' in msg.keys()):
                            print('curtain存入数据库')
                            for j in range(1, int(msg['curtain']) + 1):
                                curtain = WifiDevice.get_or_create(
                                    device_number='curtain-{}'.format(j),
                                    class_number=msg['class'],
                                    device_name=msg['device_name'],
                                    defaults={
                                        'is_alive': 1,
                                        'port': address[1]
                                    })
                                return_dict['curtain-{}'.format(
                                    j)] = curtain[0].status
                                print(curtain[0].port)

                        elif (re.findall('air', msg['device_name'])):
                            print('空调')
                            # number = re.findall('\d', msg['device_name'])[0]
                            # for k in msg['air']:
                            air = WifiDevice.get_or_create(
                                device_name=msg['device_name'],
                                class_number=msg['class'],
                                device_number=msg['device_name'],
                                defaults={
                                    'degree': '25',
                                    'is_alive': 1,
                                    'gear': '1',
                                    'model': '1',
                                    'port': address[1]
                                })
                            return_dict['status'] = air[0].status
                            return_dict['degree'] = str(air[0].degree)
                            return_dict['gear'] = air[0].gear
                            return_dict['model'] = air[0].model
                        elif (re.findall('controller', msg['device_name'])):
                            for i in range(1, int(msg['console']) + 1):
                                controller = WifiDevice.get_or_create(
                                    device_number='console-{}'.format(i),
                                    class_number=msg['class'],
                                    defaults={
                                        'device_name': msg['device_name'],
                                        'is_alive': 1,
                                        'port': address[1]
                                    })
                                return_dict['console-{}'.format(
                                    i)] = controller[0].status
                        # 开关
                        elif (re.findall('switch', msg['device_name'])):
                            print('开关')
                            switch = WifiDevice.get_or_create(
                                device_name=msg['device_name'],
                                class_number=msg['class'],
                                device_number=msg['device_name'],
                                defaults={
                                    'is_alive': 1,
                                    'port': address[1]
                                })
                            return_dict['status'] = switch[0].status
                        ClassRoom.get_or_create(name=msg['class'])
                        return_dict['device_name'] = msg['device_name']
                        return_dict['class'] = msg['class']
                        # logging.info('info_dict{}'.format(info_dict.values()))
                        # 硬件死机重连返回必要信息,取消,硬件找到存储信息的方法了.
                        # print('return_dict:{}'.format(return_dict))
                        await stream.write(b"{'heartBeat'}")
                        # logging.info('return_dict{}'.format(return_dict))
                        # await stream.write(bytes(str(return_dict), encoding='utf-8'))
                        return_dict['port'] = msg['port']
                        info_dict[msg['device_name'] +
                                  msg['class']] = return_dict
                        del return_dict
                        # 重连修改连接端口号
                        update = WifiDevice.update(
                            port=address[1], is_alive=1).where(
                                (WifiDevice.device_name == msg['device_name'])
                                & (WifiDevice.class_number == msg['class']))
                        update.execute()
                    # else:
                    #     # 处理单片机返回非json数据
                    #     logging.info("处理单片机返回非json数据:{}".format(msg))
                    #     print(msg)
        except Exception as e:
            print('错误信息{}'.format(e))
            # 掉线处理

            for value in info_dict.values():
                # value = eval(value)
                print(value, '掉线')
                if (address[1] in value.values()):
                    print('处理')
                    # print(value['device_name'], value['class'])
                    # wifi设备掉线,数据更改数据库设备状态
                    update = WifiDevice.update(is_alive=0).where(
                        WifiDevice.port == address[1])
                    update.execute()
Exemplo n.º 8
0
    async def post(self):
        return_dict = {
            "returnCode": "0",
            "returnErrorSolution": "",
            "returnMessage": "",
            "returnValue": {
                "reply":
                "好的",
                "resultType":
                "RESULT",
                "actions": [{
                    "name": "audioPlayGenieSource",
                    "properties": {
                        "audioGenieId": "123"
                    }
                }],
                "properties": {},
                "executeCode":
                "SUCCESS",
                "msgInfo":
                ""
            }
        }
        get_json = self.request.body.decode('utf-8')
        get_json = get_json.replace('true', '1')
        dicts = eval(get_json.replace('false', '0'))
        # print(/)
        # 这里由天猫精灵开发者平台定义请求头
        # headers = str(self.request.headers).lower()
        # headers = eval(headers)

        # command['action'] = self.request.headers['action']
        class_number = self.request.headers['class']
        try:
            if dicts['skillName'] == '全部设备':
                devices_ = self.request.headers['devices']
                devices_list = devices_.split('+')
                print(devices_)
                devices = WifiDevice.select() \
                    .where(WifiDevice.class_number == class_number).execute()
                air_list = []
                other_list = []
                for j in devices_list:
                    if re.findall('air', j):
                        air_list.append(j)
                    else:
                        other_list.append(j)
                # device_controller = [DeviceController(k, class_number) for k in other_list]
                list_ = [device.device_number for device in devices]
                if self.request.headers['action'] == 'on':
                    print('action ==1 ')
                    # await device_controller.turn_devices_on(list_)
                    for j in air_list:
                        await DeviceController(j, class_number).turn_air_on()
                        time.sleep(0.3)
                    for k in other_list:
                        await DeviceController(
                            k, class_number).turn_devices_on(list_)
                        # time.sleep(0.3)
                    # device_controller_air.turn_on_air()
                    # await device_controller_air.send()
                    return_dict['returnValue']['reply'] = "好的,又是元气满满的一天哦。"
                    self.write(return_dict)
                elif self.request.headers['action'] == 'off':
                    print('action ==0')
                    for k in other_list:
                        await DeviceController(
                            k, class_number).turn_devices_off(list_)
                        time.sleep(1)
                    for j in air_list:
                        await DeviceController(j, class_number).turn_air_off()
                        time.sleep(1)
                    # device_controller_air.turn_off_air()
                    # await device_controller_air.send()
                    return_dict['returnValue']['reply'] = "好的,祝您晚上有个好梦哦。"
                    self.write(return_dict)
            elif dicts['skillName'] == '领导来了':
                return_dict['returnValue']['actions'][0]['properties'][
                    'audioGenieId'] = "24976"
                return_dict['returnValue']['reply'] = '。'
                self.write(return_dict)
                # time.sleep(1)
                if ('lamp' + class_number in TCP_CONNECTION.keys()) and (
                        'curtain' + class_number in TCP_CONNECTION.keys()):
                    await TCP_CONNECTION['lamp' + class_number].write(
                        bytes(str(
                            '''{'device_name': 'lamp', 'class': 'D910', 'lamp-1': '1'}'''
                        ),
                              encoding='utf-8'))
                    await TCP_CONNECTION['curtain' + class_number].write(
                        bytes(str(
                            '''{'device_name': 'curtain', 'class': 'D910', 'curtain-1': '1'}'''
                        ),
                              encoding='utf-8'))

                else:
                    return_dict['returnValue']['reply'] = "设备未连接"
                    self.write(return_dict)
            else:
                return_dict['returnValue']['reply'] = "未识别意图"
                # logging.info('非{}'.format(return_dict))
                self.write(return_dict)
        except Exception as e:
            print('出错了{}'.format(e))
            self.write(return_dict)
Exemplo n.º 9
0
 async def post(self):
     dicts = eval(self.request.body.decode('utf-8'))
     print(dicts)
     if dicts['header']['namespace'] == "AliGenie.Iot.Device.Discovery":
         if dicts['payload']['accessToken'] == 'D712':
             # from TmallDevices import devices
             self.write(D712_devices)
         elif dicts['payload']['accessToken'] == 'D910':
             self.write(D910_devices)
         elif dicts['payload']['accessToken'] == 'C1004':
             self.write(C1004_devices)
     elif dicts['header']['namespace'] == "AliGenie.Iot.Device.Control":
         name = dicts['header']['name']
         messageId = dicts['header']['messageId']
         deviceType = dicts['payload']['deviceType']
         deviceId = dicts['payload']['deviceId']
         class_number = dicts['payload']['accessToken']
         return_data = {
             "header": {
                 "namespace": "AliGenie.Iot.Device.Control",
                 "name": "TurnOnResponse",
                 "messageId": messageId,
                 "payLoadVersion": 1
             },
             "payload": {
                 "deviceId": deviceId
             }
         }
         device_name = deviceId[0:-1]
         devices = WifiDevice.select() \
             .where((WifiDevice.class_number == class_number) & (WifiDevice.device_name == device_name)).execute()
         # print()
         device_controller = DeviceController(device_name, class_number)
         if deviceType in ['fan', 'curtain', 'light']:
             device_list = [device.device_number for device in devices]
             print(device_list)
             if name == 'TurnOn':
                 for _ in range(2):
                     print('开')
                     if deviceType == 'fan':
                         await device_controller.turn_fan_on(device_list)
                     elif deviceType == 'light':
                         await device_controller.turn_lamp_on(device_list)
                     elif deviceType == 'curtain':
                         await device_controller.turn_curtain_on(device_list
                                                                 )
                     time.sleep(0.2)
             elif name == 'TurnOff':
                 if deviceType == 'fan':
                     print('关')
                     await device_controller.turn_fan_off(device_list)
                 elif deviceType == 'light':
                     await device_controller.turn_lamp_off(device_list)
                 elif deviceType == 'curtain':
                     await device_controller.turn_curtain_off(device_list)
                 return_data['header']['name'] = 'TurnOffResponse'
         elif deviceType == 'aircondition':
             if name == 'TurnOn':
                 # for _ in range(2):
                 device_controller.turn_on_air()
                 await device_controller.send()
                 # time.sleep(0.2)
             elif name == 'TurnOff':
                 device_controller.turn_off_air()
                 await device_controller.send()
                 return_data['header']['name'] = 'TurnOffResponse'
             elif name == 'SetTemperature':
                 device_controller.setDegree(str(dicts['payload']['value']))
                 await device_controller.send()
                 return_data['header']['name'] = 'SetTemperatureResponse'
             elif name == 'AdjustUpTemperature':
                 # degree = WifiDevice.select
                 # print(devices[0].degree)
                 # print(type(devices[0].degree))
                 # print(devices.degree)
                 device_controller.setDegree(
                     str(int(devices[0].degree) + 1))
                 await device_controller.send()
                 return_data['header'][
                     'name'] = 'AdjustUpTemperatureResponse'
             elif name == 'AdjustDownTemperature':
                 # degree = WifiDevice.select
                 # print(devices[0].degree)
                 # print(type(devices[0].degree))
                 # print(devices.degree)
                 device_controller.setDegree(
                     str(int(devices[0].degree) - 1))
                 await device_controller.send()
                 return_data['header'][
                     'name'] = 'AdjustDownTemperatureResponse'
         self.write(return_data)
Exemplo n.º 10
0
    async def post(self):
        # global TCP_CONNECTION
        # data = self.get_arguments("data")
        offlamp = self.get_arguments("off-lamp")
        off1 = self.get_arguments("off-fan-1")
        off2 = self.get_arguments("off-fan-2")
        off3 = self.get_arguments("off-fan-3")
        off4 = self.get_arguments("off-curtain-1")

        stop = self.get_arguments("stop-curtain-1")

        on = self.get_arguments("on-lamp")
        on1 = self.get_arguments("on-fan-1")
        on2 = self.get_arguments("on-fan-2")
        on3 = self.get_arguments("on-fan-3")
        on4 = self.get_arguments("on-curtain-1")
        on5 = self.get_arguments("on-7-lamp")
        off5 = self.get_arguments("off-7-lamp")
        on6 = self.get_arguments("on-7-fan")
        off6 = self.get_arguments("off-7-fan")
        off7 = self.get_arguments("off-7-air")
        on7 = self.get_arguments("on-7-air")
        on8 = self.get_arguments("on-10-air")
        off8 = self.get_arguments("off-10-air")

        on9 = self.get_arguments("on-all")
        on10 = self.get_arguments("on-09-air")
        on11 = self.get_arguments("on-09-lamp")
        on12 = self.get_arguments("on-09-curtain")

        off9 = self.get_arguments("off-all")
        off10 = self.get_arguments("off-09-air")
        off11 = self.get_arguments("off-09-lamp")
        off12 = self.get_arguments("off-09-curtain")
        if (offlamp):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'C1004', 'lamp-1': '0', 'lamp-2': '0','lamp-3': '0'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():

                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(return_data)
        elif (off1):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'C1004', 'fan-1': '0'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (off4):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'C1004', 'curtain-1': '0'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (stop):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'C1004', 'curtain-1': '2'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (on4):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'C1004', 'curtain-1': '1'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():

                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (off2):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'C1004', 'fan-2': '0'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (off3):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'C1004', 'fan-3': '0'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (off5):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'D712', 'lamp-1': '0', 'lamp-2': '0'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (on7):
            send_data = '''{'device_name': 'air-1', 'class': 'D712', 'status' :'1' , 'gear': '5', 'degree': '25', 'model': '1'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (on8):
            send_data = '''{'device_name': 'air-1', 'class': 'C1004', 'status': '1', 'gear': '1', 'degree': '25', 'model': '1' }'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (off7):
            send_data = '''{'device_name': 'air-1', 'class': 'D712', 'status': '0' }'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (off8):
            send_data = '''{'device_name': 'air-1', 'class': 'C1004', 'status': '0' }'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (on):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'C1004', 'lamp-1': '1', 'lamp-2': '1','lamp-3': '1'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (on1):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'C1004', 'fan-1': '2'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (on2):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'C1004', 'fan-2': '2'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (on3):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'C1004', 'fan-3': '2'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (on5):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'D712', 'lamp-1': '1', 'lamp-2': '1'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (on6):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'D712', 'fan-1': '1', 'fan-2': '1', 'fan-3': '1', 'fan-4': '1'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (off6):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'D712', 'fan-1': '0', 'fan-2': '0', 'fan-3': '0', 'fan-4': '0'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))

        elif (on9):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'D910'}'''
            # try:
            send_data = eval(send_data)
            if isinstance(send_data, dict):
                # if send_data['device_name'] + send_data['class'] in TCP_CONNECTION.keys():
                devices = WifiDevice.select() \
                    .where(WifiDevice.class_number == send_data['class']).execute()
                list_ = [device.device_number for device in devices]
                # await device_controller.turn_devices_on(list_)
                for j in list_:
                    if re.findall('air', j):
                        await DeviceController(
                            j, send_data['class']).turn_air_on()
                    else:
                        # for k in other_list:
                        await DeviceController(
                            j[0:-2], send_data['class']).turn_devices_on(list_)
                return_data = {'status': 200, 'message': 'success'}
                self.write(json.dumps(return_data))
                # else:
                #     return_data = {'status': 500, 'message': 'failure, 设备TCP未连接'}
                #     self.write(json.dumps(return_data))
            else:
                return_data = {'status': 500, 'message': 'failure, 命令有误'}
                self.write(json.dumps(return_data))
            # except Exception as e:
            #     return_data = {'status': 500, 'message': 'failure, track:{}'.format(e)}
            #
            # del TCP_CONNECTION[send_data['device_name']]
            # self.write(json.dumps(return_data))

        elif (on10):
            send_data = '''{'device_name': 'air-1', 'class': 'D910',  'status': '1', 'gear': '1', 'degree': '25', 'model': '1'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))

        elif (on11):
            send_data = '''{'device_name': 'lamp', 'class': 'D910', 'lamp-1': '1'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))

        elif (on12):
            send_data = '''{'device_name': 'curtain', 'class': 'D910', 'curtain-1': '1'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (off9):
            send_data = '''{'device_name': 'fan-lamp-curtain', 'class': 'D910'}'''
            # try:
            send_data = eval(send_data)
            if isinstance(send_data, dict):
                # if send_data['device_name'] + send_data['class'] in TCP_CONNECTION.keys():
                devices = WifiDevice.select() \
                    .where(WifiDevice.class_number == send_data['class']).execute()
                list_ = [device.device_number for device in devices]
                # await device_controller.turn_devices_on(list_)
                for j in list_:
                    if re.findall('air', j):
                        await DeviceController(
                            j, send_data['class']).turn_air_off()
                    else:
                        # for k in other_list:
                        await DeviceController(
                            j[0:-2],
                            send_data['class']).turn_devices_off(list_)
                return_data = {'status': 200, 'message': 'success'}
                self.write(json.dumps(return_data))
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (off10):
            send_data = '''{'device_name': 'air-1', 'class': 'D910', 'status': '0'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (off11):
            send_data = '''{'device_name': 'lamp', 'class': 'D910', 'lamp-1': '0'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))
        elif (off12):
            send_data = '''{'device_name': 'curtain', 'class': 'D910', 'curtain-1': '0'}'''
            try:
                send_data = eval(send_data)
                if isinstance(send_data, dict):
                    if send_data['device_name'] + send_data[
                            'class'] in TCP_CONNECTION.keys():
                        await TCP_CONNECTION[send_data['device_name'] +
                                             send_data['class']].write(
                                                 bytes(str(send_data),
                                                       encoding='utf-8'))
                        return_data = {'status': 200, 'message': 'success'}
                        self.write(json.dumps(return_data))
                    else:
                        return_data = {
                            'status': 500,
                            'message': 'failure, 设备TCP未连接'
                        }
                        self.write(json.dumps(return_data))
                else:
                    return_data = {'status': 500, 'message': 'failure, 命令有误'}
                    self.write(json.dumps(return_data))
            except Exception as e:
                return_data = {
                    'status': 500,
                    'message': 'failure, track:{}'.format(e)
                }
                #
                # del TCP_CONNECTION[send_data['device_name']]
                self.write(json.dumps(return_data))