# -*- coding: utf-8 -*-
"""
@Author ： Rocky
@Time ： 2022/5/25 15:17
@File ：SubDeviceController.py
"""
import time
from collections import OrderedDict

from django.db import transaction
from django.db.models import Count, Q
from django.views import View

from Ansjer.Config.gatewaySensorConfig import SMART_SCENE_TOPIC, SENSOR_TYPE, EVENT_TYPE, SCENE_EVENT_SOS
from Model.models import Device_Info, GatewaySubDevice, FamilyRoomDevice, SensorRecord, SmartScene, SceneLog, FamilyRoom
from Service.CommonService import CommonService
from Object.ResponseObject import ResponseObject


class GatewaySubDeviceView(View):
    def get(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation')
        return self.validation(request.GET, request, operation)

    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation')
        return self.validation(request.POST, request, operation)

    def validation(self, request_dict, request, operation):

        if operation == 'update-online-status':  # 更新子设备在线状态
            response = ResponseObject()
            return self.update_online_status(request_dict, response)

        token_code, user_id, response = CommonService.verify_token_get_user_id(request_dict, request)
        if token_code != 0:
            return response.json(token_code)

        if operation == 'home':  # 查询传感器主页信息
            return self.sensor_home_info(request_dict, response)
        elif operation == 'device-list':  # 获取设备信息
            return self.get_device_list(request_dict, response)
        elif operation == 'list':  # 获取子设备信息
            return self.get_sensor_list(request_dict, response)
        elif operation == 'detail':  # 查询子设备信息
            return self.query_sub_device(request_dict, response)
        elif operation == 'add':  # 添加子设备
            return self.add_sub_device(request_dict, user_id, response)
        elif operation == 'update':  # 更新子设备信息
            return self.sensor_update(request_dict, response)
        elif operation == 'delete':  # 删除子设备
            return self.delete_sub_device(request_dict, response)
        elif operation == 'records/tem-hum':  # 查询温湿度传感器记录
            return self.records_tem_hum(request_dict, response)
        elif operation == 'records':  # 查询其他传感器记录
            return self.records(request_dict, response)
        elif operation == 'records-date':  # 查询传感器记录日期
            return self.records_date(request_dict, response)
        elif operation == 'update-emergency-status':  # 更新智能按钮紧急开关状态
            return self.update_emergency_status(request_dict, response)
        else:
            return response.json(414)

    @staticmethod
    def add_sub_device(request_dict, user_id, response):
        """
        添加子设备
        @param request_dict: 请求参数
        @request_dict serialNumber: 序列号
        @request_dict deviceType: 设备类型
        @request_dict nickName: 设备名
        @request_dict ieeeAddr: 长地址
        @request_dict mac: mac地址
        @request_dict deviceModel: 设备型号
        @request_dict manufacturer: 制造商
        @request_dict sensorSerial: 传感器序列号
        @request_dict firmwareVersion: 固件版本
        @request_dict hardwareVersion: 硬件版本
        @request_dict familyId: 家庭id
        @request_dict roomId: 房间id
        @param user_id: 用户id
        @param response: 响应对象
        @return: response 响应对象
        """
        serial_number = request_dict.get('serialNumber', None)
        device_type = int(request_dict.get('deviceType', None))
        nickname = request_dict.get('nickName', None)
        ieee_addr = request_dict.get('ieeeAddr', None)
        mac = request_dict.get('mac', '')
        device_model = request_dict.get('deviceModel', '')
        manufacturer = request_dict.get('manufacturer', '')
        sensor_serial = request_dict.get('sensorSerial', '')
        firmware_version = request_dict.get('firmwareVersion', '')
        hardware_version = request_dict.get('hardwareVersion', '')

        family_id = request_dict.get('familyId', None)
        room_id = request_dict.get('roomId', 0)

        if not all([serial_number, device_type, nickname, family_id]):
            return response.json(444)
        now_time = int(time.time())

        try:
            # 查询网关设备数据
            device_info_qs = Device_Info.objects.filter(userID_id=user_id, serial_number=serial_number).values('id')
            if not device_info_qs.exists():
                return response.json(14)
            device_id = device_info_qs[0]['id']
            # 查询是否已添加过该子设备
            sub_device_qs = GatewaySubDevice.objects.filter(device_id=device_id, ieee_addr=ieee_addr)
            if sub_device_qs.exists():
                return response.json(174)
            if device_type == SENSOR_TYPE['smart_button']:
                sub_device_qs = GatewaySubDevice.objects.filter(device_id=device_id, device_type=device_type)
                if sub_device_qs.count() >= 3:
                    return response.json(181)

            with transaction.atomic():
                sub_device = GatewaySubDevice.objects.create(device_id=device_id, device_type=device_type,
                                                             nickname=nickname, ieee_addr=ieee_addr,
                                                             mac=mac, device_model=device_model,
                                                             manufacturer=manufacturer, sensor_serial=sensor_serial,
                                                             firmware_version=firmware_version,
                                                             hardware_version=hardware_version,
                                                             created_time=now_time, updated_time=now_time)

                family_room_device_qs = FamilyRoomDevice.objects.filter(family_id=family_id, category=0).values(
                    'category_sort')
                if family_room_device_qs.exists():
                    FamilyRoomDevice.objects.create(family_id=family_id, room_id=room_id, device_id=device_id,
                                                    sub_device=sub_device.id, created_time=now_time,
                                                    updated_time=now_time,
                                                    category_sort=family_room_device_qs[0]['category_sort'])
                else:
                    FamilyRoomDevice.objects.create(family_id=family_id, room_id=room_id, device_id=device_id,
                                                    sub_device=sub_device.id, created_time=now_time,
                                                    updated_time=now_time)

            return response.json(0)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def query_sub_device(request_dict, response):
        """
        查询子设备信息
        @param request_dict: 请求参数
        @request_dict gatewaySubId: 子设备id
        @param response: 响应对象
        @return: response
        """
        sub_device_id = request_dict.get('gatewaySubId', None)

        if not all([sub_device_id]):
            return response.json(444)
        try:
            gateway_sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('manufacturer',
                                                                                             'device_model',
                                                                                             'mac', 'sensor_serial',
                                                                                             'device_type',
                                                                                             'is_tampered')
            if not gateway_sub_device_qs.exists():
                return response.json(173)
            res = {
                'manufacturer': gateway_sub_device_qs[0]['manufacturer'],
                'device_model': gateway_sub_device_qs[0]['device_model'],
                'mac': gateway_sub_device_qs[0]['mac'],
                'sensor_serial': gateway_sub_device_qs[0]['sensor_serial'],
            }
            if gateway_sub_device_qs[0]['device_type'] == SENSOR_TYPE['smart_button']:  # 智能按钮返回紧急开关状态
                res['emergency_status'] = gateway_sub_device_qs[0]['is_tampered']
            return response.json(0, res)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @classmethod
    def sensor_update(cls, request_dict, response):
        """
        传感器修改
        @param request_dict: 请求参数
        @request_dict gateway_sub_id: 子设备id
        @request_dict nickName: 设备名
        @request_dict room_id: 房间id
        @param response: 响应结果
        @return: list
        """
        gateway_sub_id = int(request_dict.get('gatewaySubId', None))
        nickName = request_dict.get('nickName', None)
        room_id = request_dict.get('roomId', None)
        if not all([gateway_sub_id]):
            return response.json(444)
        try:
            gateway_sub_qs = GatewaySubDevice.objects.filter(id=gateway_sub_id)
            if not gateway_sub_qs.exists():
                return response.json(173)
            if nickName:
                gateway_sub_qs.update(nickname=nickName)
            if room_id:
                FamilyRoomDevice.objects.filter(sub_device=gateway_sub_id).update(room_id=room_id)
            return response.json(0)
        except Exception as e:
            print(e.args)
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def delete_sub_device(request_dict, response):
        """
        更新子设备信息
        @param request_dict: 请求参数
        @request_dict sub_device_id: 子设备id
        @param response: 响应对象
        @return: response
        """
        sub_device_id = request_dict.get('subDeviceId', None)

        if not all([sub_device_id]):
            return response.json(444)
        try:
            with transaction.atomic():
                GatewaySubDevice.objects.filter(id=sub_device_id).delete()
                smart_scene_qs = SmartScene.objects.filter(sub_device_id=sub_device_id)
                if smart_scene_qs.exists():
                    smart_scene_qs.delete()
                scene_log_qs = SceneLog.objects.filter(sub_device_id=sub_device_id)
                if scene_log_qs.exists():
                    scene_log_qs.delete()
            return response.json(0)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @classmethod
    def records_tem_hum(cls, request_dict, response):
        """
        查询温湿度传感器记录
        @param request_dict: 请求参数
        @request_dict gatewaySubId: 子设备id
        @request_dict cycle: 时间周期
        @request_dict eventType: 事件类型, 2200:温度,2201:湿度
        @param response: 响应对象
        @return: response
        """
        sub_device_id = request_dict.get('gatewaySubId', None)
        cycle = request_dict.get('cycle', None)
        event_type = request_dict.get('eventType', None)
        if not all([sub_device_id, cycle, event_type]):
            return response.json(444, {'error param': 'gatewaySubId or cycle or eventType'})

        now_time = int(time.time())
        # 判断event_type
        event_type = int(event_type)
        if event_type != EVENT_TYPE['temperature'] and event_type != EVENT_TYPE['humidity']:
            return response.json(444, {'invalid eventType': event_type})

        try:
            record_dict = cls.get_record_dict(cycle, now_time, sub_device_id, event_type)
            record_list = []
            if record_dict:
                # 组织响应数据列表,value为每 小时/天 的平均值
                for k, v in record_dict.items():
                    record_list.append({'key': k, 'value': round(sum(v) / len(v), 1)})

            res = {
                'records': record_list,
                'time': now_time
            }
            return response.json(0, res)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def get_record_dict(cycle, now_time, sub_device_id, event_type):
        """
        获取记录数据
        @param cycle: 时间周期
        @param now_time: 当前时间
        @param sub_device_id: 子设备id
        @param event_type: 事件类型, 2200:温度,2201:湿度
        @return: record_dict: 记录数据
        """
        record_dict = OrderedDict()
        if cycle == 'Hours':
            start_time = now_time - 24 * 60 * 60
            sensor_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
                                                           event_type=event_type,
                                                           created_time__range=(start_time, now_time)). \
                values('alarm', 'created_time').order_by('created_time')
            if not sensor_record_qs.exists():
                return record_dict

            for sensor_record in sensor_record_qs:
                created_time = time.strftime('%m/%d %H:%M %w', time.localtime(sensor_record['created_time']))
                hour = int(created_time[-7:-5])
                minute = int(created_time[-4:-2])
                if hour != 23 and minute > 30:  # 不为23时且分钟大于30,hour+1
                    hour += 1
                alarm = float(sensor_record['alarm'])
                # 组织数据,record_dict:{"0": [1.0, 2.0, 3.0], "1": [1.0, 2.0, 3.0]...}
                if str(hour) in record_dict:
                    record_dict[str(hour)].append(alarm)
                else:
                    record_dict[str(hour)] = [alarm]

        elif cycle == 'Week':
            start_time = now_time - 24 * 60 * 60 * 7
            sensor_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
                                                           event_type=event_type,
                                                           created_time__range=(start_time, now_time)). \
                values('alarm', 'created_time').order_by('created_time')
            if not sensor_record_qs.exists():
                return record_dict

            for sensor_record in sensor_record_qs:
                created_time = time.strftime('%m/%d %H:%M %w', time.localtime(sensor_record['created_time']))
                week = int(created_time[-1:])
                alarm = float(sensor_record['alarm'])
                # 组织数据,record_dict:{"0": [1.0, 2.0, 3.0], "1": [1.0, 2.0, 3.0]...}
                if str(week) in record_dict:
                    record_dict[str(week)].append(alarm)
                else:
                    record_dict[str(week)] = [alarm]

        elif cycle == 'Month':
            start_time = now_time - 24 * 60 * 60 * 30
            sensor_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
                                                           event_type=event_type,
                                                           created_time__range=(start_time, now_time)). \
                values('alarm', 'created_time').order_by('created_time')
            if not sensor_record_qs.exists():
                return record_dict

            for sensor_record in sensor_record_qs:
                created_time = time.strftime('%m/%d %H:%M %w', time.localtime(sensor_record['created_time']))
                month = int(created_time[:2])
                day = int(created_time[3:5])
                date = str(month) + '/' + str(day)
                alarm = float(sensor_record['alarm'])
                # 组织数据,record_dict:{"1/1": [1.0, 2.0, 3.0], "1/2": [1.0, 2.0, 3.0]...}
                if date in record_dict:
                    record_dict[date].append(alarm)
                else:
                    record_dict[date] = [alarm]

        return record_dict

    @staticmethod
    def records(request_dict, response):
        """
        查询其他传感器记录
        @param request_dict: 请求参数
        @request_dict gatewaySubId: 子设备id
        @request_dict page: 页数
        @request_dict size: 条数
        @request_dict startTime: 开始时间
        @request_dict endTime: 结束时间
        @param response: 响应对象
        @return: response
        """
        sub_device_id = request_dict.get('gatewaySubId', None)
        page = request_dict.get('page', None)
        size = request_dict.get('size', None)
        if not all([sub_device_id, page, size]):
            return response.json(444, {'error param': 'gatewaySubId or page or size'})

        start_time = request_dict.get('startTime', None)
        end_time = request_dict.get('endTime', None)

        try:
            page, size = int(page), int(size)
            if start_time and end_time:
                sensor_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
                                                               created_time__range=(start_time, end_time)). \
                                       values('alarm', 'event_type', 'created_time').order_by('-created_time')[
                                   (page - 1) * size:page * size]
            else:
                sensor_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id). \
                                       values('alarm', 'event_type', 'created_time').order_by('-created_time')[
                                   (page - 1) * size:page * size]
            if not sensor_record_qs.exists():
                return response.json(0, [])
            return response.json(0, list(sensor_record_qs))
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def records_date(request_dict, response):
        """
        查询传感器记录日期
        @param request_dict: 请求参数
        @request_dict gatewaySubId: 子设备id
        @request_dict startTime: 开始时间
        @request_dict endTime: 结束时间
        @param response: 响应对象
        @return: response
        """
        sub_device_id = request_dict.get('gatewaySubId', None)
        if not sub_device_id:
            return response.json(444, {'error param': 'gatewaySubId'})

        try:
            sensor_record_qs = SensorRecord.objects.extra(
                select={'date': "FROM_UNIXTIME(created_time,'%%Y-%%m-%%d')"}).values('date'). \
                                   filter(gateway_sub_device_id=sub_device_id). \
                                   annotate(count=Count('created_time')). \
                                   order_by('-date')[:31]
            record_date_list = []
            for sensor_record in sensor_record_qs:
                record_date_list.append({
                    'timestamp': CommonService.str_to_timestamp(sensor_record['date'], '%Y-%m-%d'),
                    'count': sensor_record['count'],
                    'format': sensor_record['date'],
                })
            return response.json(0, record_date_list)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def sensor_home_info(request_dict, response):
        """
        查询子设备首页信息
        @param request_dict: 请求参数
        @request_dict gatewaySubId: 子设备id
        @param response: 响应对象
        @return: response
        """
        sub_device_id = request_dict.get('gatewaySubId', None)
        if not sub_device_id:
            return response.json(444, {'error param': 'gatewaySubId'})

        try:
            sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('device_type', 'status',
                                                                                     'is_tampered')
            if not sub_device_qs.exists():
                return response.json(173)
            scene_count = SmartScene.objects.filter(sub_device_id=sub_device_id).count()
            res = {
                'scene_count': scene_count,
                'status': sub_device_qs[0]['status']
            }

            device_type = sub_device_qs[0]['device_type']
            # 门磁,烟雾,人体传感器返回拆动状态
            if device_type == SENSOR_TYPE['door_magnet'] or device_type == SENSOR_TYPE['smoke_sensor'] or \
                    device_type == SENSOR_TYPE['body_sensor']:
                res['is_tampered'] = sub_device_qs[0]['is_tampered']
            # 温湿度传感器返回温湿度数据
            elif device_type == SENSOR_TYPE['tem_hum_sensor']:
                tem_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
                                                            event_type=EVENT_TYPE['temperature']).order_by(
                    '-created_time').values('alarm')[:1]
                hum_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
                                                            event_type=EVENT_TYPE['humidity']).order_by(
                    '-created_time').values('alarm')[:1]
                temperature = tem_record_qs[0]['alarm'] if tem_record_qs.exists() else ''
                humidity = hum_record_qs[0]['alarm'] if tem_record_qs.exists() else ''
                res['temperature'] = temperature
                res['humidity'] = humidity

            return response.json(0, res)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def update_emergency_status(request_dict, response):
        """
        更新智能按钮紧急开关状态
        @param request_dict: 请求参数
        @request_dict gatewaySubId: 子设备id
        @request_dict emergencyStatus: 紧急开关状态,0:关,1:开
        @param response: 响应对象
        @return: response
        """
        sub_device_id = request_dict.get('gatewaySubId', None)
        emergency_status = request_dict.get('emergencyStatus', None)
        if not all([sub_device_id, emergency_status]):
            return response.json(444, {'error param': 'gatewaySubId or emergency_status'})

        try:
            emergency_status = int(emergency_status)
            sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id)
            if not sub_device_qs.exists():
                response.json(173)
            with transaction.atomic():
                # 更新智能按钮紧急开关状态
                sub_device_qs.update(is_tampered=emergency_status)
                # 获取序列号
                sub_device_qs = sub_device_qs.values('device_id', 'ieee_addr', 'device__serial_number')
                serial_number = sub_device_qs[0]['device__serial_number']
                topic_name = SMART_SCENE_TOPIC.format(serial_number)

                # 通知设备修改sos状态
                msg = {
                    'scene_event': SCENE_EVENT_SOS,
                    'ieee_addr': sub_device_qs[0]['ieee_addr'],
                    'sos': emergency_status
                }
                success = CommonService.req_publish_mqtt_msg(serial_number, topic_name, msg)
                try:
                    assert success
                except AssertionError:
                    return response.json(10044)

                # 如果打开紧急开关,关闭已开启的场景
                if emergency_status == 1:
                    SmartScene.objects.filter(sub_device_id=sub_device_id, is_enable=True).update(is_enable=False)
            return response.json(0)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def get_sensor_list(request_dict, response):
        """
        获取子设备信息
        @param request_dict: 请求参数
        @request_dict familyId: 家庭id
        @param response: 响应对象
        @return: response
        """
        family_id = request_dict.get('familyId', None)
        room_id = request_dict.get('roomId', None)
        if not family_id:
            return response.json(444, {'error param': 'familyId'})

        try:
            family_room_device_qs = FamilyRoomDevice.objects.filter(Q(family_id=family_id), ~Q(sub_device=0)).values(
                'sub_device', 'room_id').order_by('sort', '-device__data_joined')
            if room_id:
                family_room_device_qs = family_room_device_qs.filter(room_id=room_id)
            device_list = []
            for item in family_room_device_qs:
                room_qs = FamilyRoom.objects.filter(id=item['room_id']).values('name')
                room_name = room_qs[0]['name'] if room_qs.exists() else ''
                device_qs = GatewaySubDevice.objects.filter(id=item['sub_device']).values('id', 'nickname',
                                                                                          'device_type', 'ieee_addr',
                                                                                          'status', 'created_time')
                if device_qs.exists():
                    res = {
                        'gatewaySubId': device_qs[0]['id'],
                        'nickName': device_qs[0]['nickname'],
                        'deviceType': device_qs[0]['device_type'],
                        'srcAddr': device_qs[0]['ieee_addr'],
                        'status': device_qs[0]['status'],
                        'createdTime': device_qs[0]['created_time'],
                        'roomName': room_name,
                        'familyId': family_id
                    }
                    device_list.append(res)
            return response.json(0, device_list)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def get_device_list(request_dict, response):
        """
        获取设备信息
        @param request_dict: 请求参数
        @request_dict familyId: 家庭id
        @param response: 响应对象
        @return: response
        """
        family_id = request_dict.get('familyId', None)
        room_id = request_dict.get('roomId', None)
        if not family_id:
            return response.json(444, {'error param': 'familyId'})

        try:
            family_room_device_qs = FamilyRoomDevice.objects.filter(family_id=family_id, sub_device=0,
                                                                    device__Type=200).values(
                'device_id', 'device__NickName', 'device__serial_number').order_by('-device__data_joined')
            if room_id:
                family_room_device_qs = family_room_device_qs.filter(room_id=room_id)
            device_list = []
            for item in family_room_device_qs:
                device_id = item['device_id']
                serial_number = item['device__serial_number']
                device_dict = {
                    'deviceType': 200,
                    'deviceId': device_id,
                    'deviceNickName': item['device__NickName']
                }
                sub_device = []
                family_room_sub_device_qs = FamilyRoomDevice.objects.filter(Q(family_id=family_id), ~Q(sub_device=0),
                                                                            Q(device_id=device_id)).values(
                    'sub_device', 'room_id').order_by('sort')
                if family_room_sub_device_qs.exists():
                    for each in family_room_sub_device_qs:
                        room_qs = FamilyRoom.objects.filter(id=each['room_id']).values('name')
                        room_name = room_qs[0]['name'] if room_qs.exists() else ''
                        device_qs = GatewaySubDevice.objects.filter(id=each['sub_device']).values('id', 'nickname',
                                                                                                  'device_type',
                                                                                                  'ieee_addr',
                                                                                                  'status',
                                                                                                  'created_time')
                        if device_qs.exists():
                            sub_device.append({
                                'gatewaySubId': device_qs[0]['id'],
                                'nickName': device_qs[0]['nickname'],
                                'deviceType': device_qs[0]['device_type'],
                                'srcAddr': device_qs[0]['ieee_addr'],
                                'status': device_qs[0]['status'],
                                'createdTime': device_qs[0]['created_time'],
                                'roomName': room_name,
                                'familyId': family_id,
                                'serialNumber': serial_number
                            })
                    device_dict['subDevice'] = sub_device
                    device_list.append(device_dict)
            return response.json(0, device_list)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def update_online_status(request_dict, response):
        """
        更新子设备在线状态
        @param request_dict: 请求参数
        @request_dict online: 在线状态, 0:离线, 1:在线, 2:复位
        @request_dict ieee_addr: 长地址
        @param response: 响应对象
        @return: response
        """
        online = request_dict.get('online', None)
        ieee_addr = request_dict.get('ieee_addr', None)

        if not all([online, ieee_addr]):
            return response.json(444, {'error param': 'online or ieee_addr'})
        online = int(online)
        try:
            if online == 2:     # 复位删除设备
                gateway_sub_device_qs = GatewaySubDevice.objects.filter(ieee_addr=ieee_addr)
                if not gateway_sub_device_qs.exists():
                    return response.json(0)
                sub_device_id = gateway_sub_device_qs.values('id')[0]['id']
                # 删除设备信息,场景信息,场景日志
                with transaction.atomic():
                    gateway_sub_device_qs.delete()
                    SmartScene.objects.filter(sub_device_id=sub_device_id).delete()
                    SceneLog.objects.filter(sub_device_id=sub_device_id).delete()
            else:
                GatewaySubDevice.objects.filter(ieee_addr=ieee_addr).update(status=online)
            return response.json(0)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))


#
#                   ___====-_  _-====___
#             _--^^^#####//      \\#####^^^--_
#          _-^##########// (    ) \\##########^-_
#         -############//  |\^^/|  \\############-
#       _/############//   (@::@)   \\############\_
#      /#############((     \\//     ))#############\
#     -###############\\    (oo)    //###############-
#    -#################\\  / VV \  //#################-
#   -###################\\/      \//###################-
#  _#/|##########/\######(   /\   )######/\##########|\#_
#  |/ |#/\#/\#/\/  \#/\##\  |  |  /##/\#/  \/\#/\#/\#| \|
#  `  |/  V  V  `   V  \#\| |  | |/#/  V   '  V  V  \|  '
#     `   `  `      `   / | |  | | \   '      '  '   '
#                      (  | |  | |  )
#                     __\ | |  | | /__
#                    (vvv(VVV)(VVV)vvv)
#                         神兽保佑
#                        代码无BUG!
#
