|
@@ -9,7 +9,7 @@ import time
|
|
|
|
|
|
from django.views.generic.base import View
|
|
|
|
|
|
-from Model.models import SensorRecord, GatewaySubDevice, GatewayPush, Device_Info
|
|
|
+from Model.models import SensorRecord, GatewaySubDevice, GatewayPush, Device_Info, SceneLog, SmartScene
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.utils import LocalDateTimeUtil
|
|
|
from Service.CommonService import CommonService
|
|
@@ -32,6 +32,8 @@ class GatewayView(View):
|
|
|
response = ResponseObject()
|
|
|
if operation == 'gatewayPush': # 网关推送
|
|
|
return self.gateway_push(request_dict, response)
|
|
|
+ elif operation == 'sceneLogPush': # 网关推送
|
|
|
+ return self.scene_log_push(request_dict, response)
|
|
|
else:
|
|
|
return response.json(414)
|
|
|
|
|
@@ -163,3 +165,117 @@ class GatewayView(View):
|
|
|
except Exception as e:
|
|
|
logger.info('---网关推送接口异常--- {}'.format(repr(e)))
|
|
|
return response.json(500, repr(e))
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def scene_log_push(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: 消息内容
|
|
|
+ @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))
|
|
|
+ if not all([scene_id, status]):
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+ smart_scene_qs = SmartScene.objects.filter(id=scene_id).values('scene_name', 'conditions', 'tasks', 'device_id',
|
|
|
+ 'sub_device_id', 'user_id')
|
|
|
+ if not smart_scene_qs.exists():
|
|
|
+ return response.json(174)
|
|
|
+
|
|
|
+ nickname = ''
|
|
|
+ scene_name = smart_scene_qs[0]['scene_name']
|
|
|
+ tasks = smart_scene_qs[0]['tasks']
|
|
|
+ device_id = smart_scene_qs[0]['device_id']
|
|
|
+ sub_device_id = smart_scene_qs[0]['sub_device_id']
|
|
|
+ n_time = int(time.time())
|
|
|
+ user_id = smart_scene_qs[0]['user_id']
|
|
|
+ log_dict = {
|
|
|
+ 'scene_id': scene_id,
|
|
|
+ 'scene_name': scene_name,
|
|
|
+ 'tasks': tasks,
|
|
|
+ 'status': status,
|
|
|
+ 'device_id': device_id,
|
|
|
+ 'sub_device_id': sub_device_id,
|
|
|
+ 'created_time': n_time,
|
|
|
+ }
|
|
|
+ tasks = eval(tasks)
|
|
|
+
|
|
|
+ # if sub_device_id:
|
|
|
+ # sub_device_info_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('device_id', 'nickname')
|
|
|
+ # nickname = sub_device_info_qs[0]['nickname']
|
|
|
+ # device_id = sub_device_info_qs[0]['device_id']
|
|
|
+ #
|
|
|
+ # device_info_qs = Device_Info.objects.filter(id=device_id).values('userID_id', 'NickName')
|
|
|
+ # if not device_info_qs.exists():
|
|
|
+ # return response.json(173)
|
|
|
+ # if not nickname:
|
|
|
+ # nickname = device_info_qs[0]['NickName']
|
|
|
+ #
|
|
|
+ # user_id = device_info_qs[0]['userID_id']
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 存储日志
|
|
|
+ # scene_log = {
|
|
|
+ # 'conditions': conditions,
|
|
|
+ # 'tasks': tasks
|
|
|
+ # }
|
|
|
+ # log_dict['scene_log'] = json.dumps(scene_log)
|
|
|
+ SceneLog.objects.create(**log_dict)
|
|
|
+
|
|
|
+ # 推送日志
|
|
|
+ gateway_push_qs = GatewayPush.objects.filter(user_id=user_id, logout=False). \
|
|
|
+ values('user_id', 'app_bundle_id', 'app_type', 'push_type', 'token_val', 'm_code', 'lang', 'm_code',
|
|
|
+ 'tz')
|
|
|
+ if not gateway_push_qs.exists():
|
|
|
+ return response.json(174)
|
|
|
+ for task in tasks:
|
|
|
+ event_type = task['event_type']
|
|
|
+ if event_type == '1001':
|
|
|
+ kwargs = {
|
|
|
+ 'n_time': n_time,
|
|
|
+ 'event_type': event_type,
|
|
|
+ 'nickname': nickname,
|
|
|
+ }
|
|
|
+ event_info = task['value']
|
|
|
+ # 推送到每台登录账号的手机
|
|
|
+ for gateway_push in gateway_push_qs:
|
|
|
+ app_bundle_id = gateway_push['app_bundle_id']
|
|
|
+ push_type = gateway_push['push_type']
|
|
|
+ token_val = gateway_push['token_val']
|
|
|
+ lang = gateway_push['lang']
|
|
|
+ tz = gateway_push['tz'] if gateway_push['tz'] else 0
|
|
|
+
|
|
|
+ # 获取推送所需数据
|
|
|
+ # msg_title = GatewayPushService.get_msg_title(app_bundle_id, nickname)
|
|
|
+ # msg_text = GatewayPushService.get_msg_text(n_time, tz, lang, event_info)
|
|
|
+
|
|
|
+ kwargs['msg_title'] = nickname
|
|
|
+ kwargs['msg_text'] = event_info
|
|
|
+ kwargs['app_bundle_id'] = app_bundle_id
|
|
|
+ kwargs['token_val'] = token_val
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 推送消息
|
|
|
+ if push_type == 0: # ios apns
|
|
|
+ GatewayPushService.ios_apns_push(**kwargs)
|
|
|
+ elif push_type == 1: # android gcm
|
|
|
+ GatewayPushService.android_fcm_push(**kwargs)
|
|
|
+ elif push_type == 2: # android 极光推送
|
|
|
+ GatewayPushService.android_jpush(**kwargs)
|
|
|
+ except Exception as 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))
|