|
@@ -4,13 +4,13 @@
|
|
|
@Time : 2022/5/9 10:51
|
|
|
@File :gatewayController.py
|
|
|
"""
|
|
|
-import logging
|
|
|
import time
|
|
|
|
|
|
from django.views.generic.base import View
|
|
|
|
|
|
from AnsjerPush.Config.gatewaySensorConfig import SENSOR_TYPE, EVENT_TYPE
|
|
|
-from Model.models import SensorRecord, GatewaySubDevice, GatewayPush, Device_Info, SceneLog, SmartScene
|
|
|
+from AnsjerPush.config import LOGGER
|
|
|
+from Model.models import SensorRecord, GatewaySubDevice, GatewayPush, Device_Info, SceneLog, SmartScene, CountryModel
|
|
|
from Object.RedisObject import RedisObject
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.utils import LocalDateTimeUtil
|
|
@@ -19,8 +19,6 @@ from Service.EquipmentInfoService import EquipmentInfoService
|
|
|
from Service.HuaweiPushService.HuaweiPushService import HuaweiPushObject
|
|
|
from Service.PushService import PushObject
|
|
|
|
|
|
-LOGGER = logging.getLogger('info')
|
|
|
-
|
|
|
|
|
|
class GatewayView(View):
|
|
|
def get(self, request, *args, **kwargs):
|
|
@@ -44,33 +42,27 @@ class GatewayView(View):
|
|
|
else:
|
|
|
return response.json(414)
|
|
|
|
|
|
- @staticmethod
|
|
|
- def gateway_push(request_dict, response):
|
|
|
+ @classmethod
|
|
|
+ def gateway_push(cls, request_dict, response):
|
|
|
"""
|
|
|
网关推送
|
|
|
@param request_dict: 请求参数
|
|
|
@request_dict serial_number: 序列号
|
|
|
@request_dict ieee_addr: 长地址
|
|
|
- @request_dict src_addr: 短地址
|
|
|
@request_dict sensor_type: 传感器类型
|
|
|
@request_dict event_type: 事件类型
|
|
|
- @request_dict alarm: 消息内容
|
|
|
@request_dict defense: 防御状态,0:撤防,1:防御
|
|
|
@request_dict sensor_status: 拆动状态,拆动时传参
|
|
|
@param response: 响应对象
|
|
|
@return: response
|
|
|
"""
|
|
|
- logger = logging.getLogger('info')
|
|
|
serial_number = request_dict.get('serial_number', None)
|
|
|
ieee_addr = request_dict.get('ieee_addr', None)
|
|
|
- src_addr = request_dict.get('src_addr', None)
|
|
|
sensor_type = int(request_dict.get('sensor_type', None))
|
|
|
event_type = int(request_dict.get('event_type', None))
|
|
|
- alarm = request_dict.get('alarm', None)
|
|
|
defense = int(request_dict.get('defense', None))
|
|
|
- sensor_status = request_dict.get('sensor_status', None)
|
|
|
- logger.info('---调用网关推送接口--- request_dict:{}'.format(request_dict))
|
|
|
- if not all([serial_number, ieee_addr, src_addr, sensor_type, event_type, alarm]):
|
|
|
+ LOGGER.info('---调用网关推送接口--- request_dict:{}'.format(request_dict))
|
|
|
+ if not all([serial_number, ieee_addr, sensor_type, event_type]):
|
|
|
return response.json(444)
|
|
|
|
|
|
n_time = int(time.time())
|
|
@@ -78,37 +70,39 @@ class GatewayView(View):
|
|
|
try:
|
|
|
# 查询子设备表id
|
|
|
gateway_sub_device_qs = GatewaySubDevice.objects.filter(device__serial_number=serial_number,
|
|
|
- device_type=sensor_type, ieee_addr=ieee_addr,
|
|
|
- src_addr=src_addr).values('id', 'nickname')
|
|
|
+ device_type=sensor_type, ieee_addr=ieee_addr). \
|
|
|
+ values('id', 'nickname', 'device__userID__region_country')
|
|
|
if not gateway_sub_device_qs.exists():
|
|
|
return response.json(173)
|
|
|
- else:
|
|
|
- gateway_sub_device_id = gateway_sub_device_qs[0]['id']
|
|
|
- nickname = gateway_sub_device_qs[0]['nickname']
|
|
|
- sensor_record_dict = {
|
|
|
- 'gateway_sub_device_id': gateway_sub_device_id,
|
|
|
- 'alarm': alarm,
|
|
|
- 'event_type': event_type,
|
|
|
- 'created_time': n_time,
|
|
|
- }
|
|
|
- # 处理温湿度,不推送
|
|
|
- if sensor_type == SENSOR_TYPE['tem_hum_sensor'] and (
|
|
|
- event_type == EVENT_TYPE['temperature'] or event_type == EVENT_TYPE['humidity']):
|
|
|
- num = request_dict.get('num', None)
|
|
|
- num = str(int(num) / 100)
|
|
|
- sensor_record_dict['alarm'] = num
|
|
|
- SensorRecord.objects.create(**sensor_record_dict)
|
|
|
- return response.json(0)
|
|
|
|
|
|
+ country_id = gateway_sub_device_qs[0]['device__userID__region_country']
|
|
|
+ lang = cls.confirm_lang(country_id)
|
|
|
+ alarm = cls.get_alarm(lang, event_type)
|
|
|
+
|
|
|
+ gateway_sub_device_id = gateway_sub_device_qs[0]['id']
|
|
|
+ nickname = gateway_sub_device_qs[0]['nickname']
|
|
|
+ sensor_record_dict = {
|
|
|
+ 'gateway_sub_device_id': gateway_sub_device_id,
|
|
|
+ 'alarm': alarm,
|
|
|
+ 'event_type': event_type,
|
|
|
+ 'created_time': n_time,
|
|
|
+ }
|
|
|
+ # 处理温湿度,不推送
|
|
|
+ if sensor_type == SENSOR_TYPE['tem_hum_sensor'] and (
|
|
|
+ event_type == EVENT_TYPE['temperature'] or event_type == EVENT_TYPE['humidity']):
|
|
|
+ num = request_dict.get('num', None)
|
|
|
+ num = str(int(num) / 100)
|
|
|
+ sensor_record_dict['alarm'] = num
|
|
|
SensorRecord.objects.create(**sensor_record_dict)
|
|
|
+ return response.json(0)
|
|
|
+
|
|
|
+ SensorRecord.objects.create(**sensor_record_dict)
|
|
|
|
|
|
- # (门磁,烟雾,人体)传感器被拆动/拆动恢复,修改拆动状态
|
|
|
- if sensor_status:
|
|
|
- if sensor_type != SENSOR_TYPE['smart_button']: # 智能按钮不更新
|
|
|
- gateway_sub_device_qs.update(is_tampered=1)
|
|
|
- elif sensor_type == SENSOR_TYPE['door_magnet'] or sensor_type == SENSOR_TYPE['smoke_sensor'] or \
|
|
|
- sensor_type == SENSOR_TYPE['body_sensor']:
|
|
|
- gateway_sub_device_qs.update(is_tampered=0)
|
|
|
+ # 门磁被拆动/拆动恢复,修改拆动状态
|
|
|
+ if event_type == 2152:
|
|
|
+ gateway_sub_device_qs.update(is_tampered=1)
|
|
|
+ elif event_type == 2156:
|
|
|
+ gateway_sub_device_qs.update(is_tampered=0)
|
|
|
|
|
|
# 撤防状态不推送
|
|
|
if defense == 0:
|
|
@@ -172,38 +166,128 @@ class GatewayView(View):
|
|
|
kwargs['token_val'] = token_val
|
|
|
|
|
|
try:
|
|
|
- if 'loocam' in app_bundle_id:
|
|
|
- # 推送消息
|
|
|
- if push_type == 0: # ios apns
|
|
|
- PushObject.ios_apns_push(**kwargs)
|
|
|
- elif push_type == 1: # android gcm
|
|
|
- PushObject.android_fcm_push(**kwargs)
|
|
|
- elif push_type == 2: # android 极光推送
|
|
|
- PushObject.android_jpush(**kwargs)
|
|
|
- elif push_type == 3:
|
|
|
- huawei_push_object = HuaweiPushObject()
|
|
|
- huawei_push_object.send_push_notify_message(**kwargs)
|
|
|
- elif push_type == 4: # android 小米推送
|
|
|
- channel_id = 104551
|
|
|
- PushObject.android_xmpush(channel_id=channel_id, **kwargs)
|
|
|
- elif push_type == 5: # android vivo推送
|
|
|
- PushObject.android_vivopush(**kwargs)
|
|
|
- elif push_type == 6: # android oppo推送
|
|
|
- channel_id = 'DEVICE_REMINDER'
|
|
|
- PushObject.android_oppopush(channel_id=channel_id, **kwargs)
|
|
|
- elif push_type == 7: # android 魅族推送
|
|
|
- PushObject.android_meizupush(**kwargs)
|
|
|
+ # 推送消息
|
|
|
+ cls.push_msg(push_type, **kwargs)
|
|
|
except Exception as e:
|
|
|
- logger.info('网关推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+ LOGGER.info('网关推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
continue
|
|
|
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
- logger.info('---网关推送接口异常--- {}'.format(repr(e)))
|
|
|
- return response.json(500, repr(e))
|
|
|
+ LOGGER.info('---网关推送接口异常--- {}'.format(repr(e)))
|
|
|
+ return response.json(500, 'error_ine:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@staticmethod
|
|
|
- def scene_log_push(request_dict, response):
|
|
|
+ def confirm_lang(country_id):
|
|
|
+ """
|
|
|
+ 根据country_id确定语言
|
|
|
+ @param country_id: 国家id
|
|
|
+ @return lang: 语言
|
|
|
+ """
|
|
|
+ country_qs = CountryModel.objects.filter(id=country_id).values('country_code')
|
|
|
+ if not country_qs.exists():
|
|
|
+ lang = 'NA'
|
|
|
+ else:
|
|
|
+ lang = country_qs[0]['country_code']
|
|
|
+ return lang
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_alarm(lang, event_type):
|
|
|
+ """
|
|
|
+ 根据语言和事件类型确定警报内容
|
|
|
+ @param lang: 语言
|
|
|
+ @param event_type: 事件类型
|
|
|
+ @return alarm: 警报内容
|
|
|
+ """
|
|
|
+ alarm = ''
|
|
|
+ if lang == 'CN':
|
|
|
+ # 门磁
|
|
|
+ if event_type == 2150:
|
|
|
+ alarm = '门磁开'
|
|
|
+ elif event_type == 2151:
|
|
|
+ alarm = '门磁关'
|
|
|
+ elif event_type == 2152:
|
|
|
+ alarm = '被拆动'
|
|
|
+ elif event_type == 2156:
|
|
|
+ alarm = '拆动恢复'
|
|
|
+
|
|
|
+ # 智能按钮
|
|
|
+ elif event_type == 2160:
|
|
|
+ alarm = '紧急按钮按下'
|
|
|
+ elif event_type == 2161:
|
|
|
+ alarm = '单击'
|
|
|
+ elif event_type == 2162:
|
|
|
+ alarm = '双击'
|
|
|
+ elif event_type == 2163:
|
|
|
+ alarm = '长按'
|
|
|
+
|
|
|
+ # 水浸
|
|
|
+ elif event_type == 2170:
|
|
|
+ alarm = '水浸触发'
|
|
|
+ elif event_type == 2171:
|
|
|
+ alarm = '水浸恢复'
|
|
|
+
|
|
|
+ # 烟雾
|
|
|
+ elif event_type == 2180:
|
|
|
+ alarm = '烟雾触发'
|
|
|
+ elif event_type == 2181:
|
|
|
+ alarm = '烟雾恢复'
|
|
|
+
|
|
|
+ # 人体红外
|
|
|
+ elif event_type == 2190:
|
|
|
+ alarm = '有人移动'
|
|
|
+ elif event_type == 2191:
|
|
|
+ alarm = '无人移动'
|
|
|
+
|
|
|
+ # 低电量
|
|
|
+ elif event_type in (2153, 2164, 2172, 2182, 2193):
|
|
|
+ alarm = '低电量'
|
|
|
+ else:
|
|
|
+ # 门磁
|
|
|
+ if event_type == 2150:
|
|
|
+ alarm = 'Door magnetic opening'
|
|
|
+ elif event_type == 2151:
|
|
|
+ alarm = 'Door magnetic closing'
|
|
|
+ elif event_type == 2152:
|
|
|
+ alarm = 'Be dismantled'
|
|
|
+ elif event_type == 2156:
|
|
|
+ alarm = 'Dismantling recovery'
|
|
|
+
|
|
|
+ # 智能按钮
|
|
|
+ elif event_type == 2160:
|
|
|
+ alarm = 'Emergency button pressed'
|
|
|
+ elif event_type == 2161:
|
|
|
+ alarm = 'Single click'
|
|
|
+ elif event_type == 2162:
|
|
|
+ alarm = 'Double click'
|
|
|
+ elif event_type == 2163:
|
|
|
+ alarm = 'Long press'
|
|
|
+
|
|
|
+ # 水浸
|
|
|
+ elif event_type == 2170:
|
|
|
+ alarm = 'Water immersion trigger'
|
|
|
+ elif event_type == 2171:
|
|
|
+ alarm = 'Water immersion recovery'
|
|
|
+
|
|
|
+ # 烟雾
|
|
|
+ elif event_type == 2180:
|
|
|
+ alarm = 'Smoke triggering'
|
|
|
+ elif event_type == 2181:
|
|
|
+ alarm = 'Smoke recovery'
|
|
|
+
|
|
|
+ # 人体红外
|
|
|
+ elif event_type == 2190:
|
|
|
+ alarm = 'Someone moving'
|
|
|
+ elif event_type == 2191:
|
|
|
+ alarm = 'Unmanned movement'
|
|
|
+
|
|
|
+ # 低电量
|
|
|
+ elif event_type in (2153, 2164, 2172, 2182, 2193):
|
|
|
+ alarm = 'LOW BATTERY'
|
|
|
+ return alarm
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def scene_log_push(cls, request_dict, response):
|
|
|
"""
|
|
|
网关智能场景日志推送
|
|
|
@param request_dict: 请求参数
|
|
@@ -212,10 +296,9 @@ class GatewayView(View):
|
|
|
@param response: 响应对象
|
|
|
@return: response
|
|
|
"""
|
|
|
- logger = logging.getLogger('info')
|
|
|
scene_id = request_dict.get('sceneId', None)
|
|
|
status = request_dict.get('status', None)
|
|
|
- logger.info('---场景日志推送接口--- request_dict:{}'.format(request_dict))
|
|
|
+ LOGGER.info('---场景日志推送接口--- request_dict:{}'.format(request_dict))
|
|
|
if not all([scene_id, status]):
|
|
|
return response.json(444)
|
|
|
|
|
@@ -277,32 +360,43 @@ class GatewayView(View):
|
|
|
|
|
|
try:
|
|
|
# 推送消息
|
|
|
- if push_type == 0: # ios apns
|
|
|
- PushObject.ios_apns_push(**kwargs)
|
|
|
- elif push_type == 1: # android gcm
|
|
|
- PushObject.android_fcm_push(**kwargs)
|
|
|
- elif push_type == 2: # android 极光推送
|
|
|
- PushObject.android_jpush(**kwargs)
|
|
|
- elif push_type == 3:
|
|
|
- huawei_push_object = HuaweiPushObject()
|
|
|
- huawei_push_object.send_push_notify_message(**kwargs)
|
|
|
- elif push_type == 4: # android 小米推送
|
|
|
- channel_id = 104551
|
|
|
- PushObject.android_xmpush(channel_id=channel_id, **kwargs)
|
|
|
- elif push_type == 5: # android vivo推送
|
|
|
- PushObject.android_vivopush(**kwargs)
|
|
|
- elif push_type == 6: # android oppo推送
|
|
|
- channel_id = 'DEVICE_REMINDER'
|
|
|
- PushObject.android_oppopush(channel_id=channel_id, **kwargs)
|
|
|
- elif push_type == 7: # android 魅族推送
|
|
|
- PushObject.android_meizupush(**kwargs)
|
|
|
+ cls.push_msg(push_type, **kwargs)
|
|
|
except Exception as e:
|
|
|
- logger.info('场景日志推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+ LOGGER.info(
|
|
|
+ '场景日志推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
continue
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
- logger.info('---场景日志推送接口异常--- {}'.format(repr(e)))
|
|
|
- return response.json(500, repr(e))
|
|
|
+ LOGGER.info('---场景日志推送接口异常--- {}'.format(repr(e)))
|
|
|
+ return response.json(500, 'error_ine:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def push_msg(push_type, **kwargs):
|
|
|
+ """
|
|
|
+ 发送推送消息
|
|
|
+ @param push_type: 推送类型
|
|
|
+ @param kwargs: 推送参数
|
|
|
+ @return: None
|
|
|
+ """
|
|
|
+ if push_type == 0: # ios apns
|
|
|
+ PushObject.ios_apns_push(**kwargs)
|
|
|
+ elif push_type == 1: # android gcm
|
|
|
+ PushObject.android_fcm_push(**kwargs)
|
|
|
+ elif push_type == 2: # android 极光推送
|
|
|
+ PushObject.android_jpush(**kwargs)
|
|
|
+ elif push_type == 3:
|
|
|
+ huawei_push_object = HuaweiPushObject()
|
|
|
+ huawei_push_object.send_push_notify_message(**kwargs)
|
|
|
+ elif push_type == 4: # android 小米推送
|
|
|
+ channel_id = 104551
|
|
|
+ PushObject.android_xmpush(channel_id=channel_id, **kwargs)
|
|
|
+ elif push_type == 5: # android vivo推送
|
|
|
+ PushObject.android_vivopush(**kwargs)
|
|
|
+ elif push_type == 6: # android oppo推送
|
|
|
+ channel_id = 'DEVICE_REMINDER'
|
|
|
+ PushObject.android_oppopush(channel_id=channel_id, **kwargs)
|
|
|
+ elif push_type == 7: # android 魅族推送
|
|
|
+ PushObject.android_meizupush(**kwargs)
|
|
|
|
|
|
@classmethod
|
|
|
def socket_msg_push(cls, request_dict, response):
|