ソースを参照

添加推送数据存储位置字段

lhq 4 年 前
コミット
139dda9757

+ 1 - 1
Ansjer/urls.py

@@ -140,9 +140,9 @@ urlpatterns = [
     url(r'^v3/account/loginByFingerprint$', UserController.v3LoginByFingerprintView.as_view()),
     url(r'^v3/account/setFingerprint$', UserController.v3SetFingerprintView.as_view()),
 
-    # 推送项目接口
     url(r'^detect/detect_group_push$',DetectController.NotificationView.detect_group_push),
     url(r'^detect/add$', DetectController.PushNotificationView.as_view()),
+    # 推送项目接口
     url(r'^detect/(?P<operation>.*)$', DetectController.DetectControllerView.as_view()),
     url(r'^notify/push$', DetectController.NotificationView.as_view()),
     url(r'^detectV2/(?P<operation>.*)$', DetectControllerV2.DetectControllerViewV2.as_view()),

+ 38 - 2
Controller/DetectController.py

@@ -545,7 +545,8 @@ class NotificationView(View):
                     alarm='Motion \tChannel:{channel}'.format(channel=channel),
                     is_st=int_is_st,
                     receiveTime=n_time,
-                    addTime=now_time
+                    addTime=now_time,
+                    storage_location=1
                 ))
                 if is_sys_msg:
                     sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
@@ -865,6 +866,40 @@ class NotificationView(View):
         else:
             return JsonResponse(status=200, data={'code': 404, 'msg': 'data is not exist'})
 
+    def do_bulk_create_info(self, uaqs, n_time, channel, event_type, is_st, uid):
+        #
+        qs_list = []
+        nowTime = int(time.time())
+        # 设备昵称
+        userID_ids = []
+        for dv in uaqs:
+            userID_id = dv["userID_id"]
+            if userID_id not in userID_ids:
+                if dv['uid_set__nickname']:
+                    uid_nickname = dv['uid_set__nickname']
+                else:
+                    uid_nickname = uid
+                add_data = {
+                    'userID_id': dv["userID_id"],
+                    'eventTime': n_time,
+                    'eventType': event_type,
+                    'devUid': uid,
+                    'devNickName': uid_nickname,
+                    'Channel': channel,
+                    'alarm': 'Motion \tChannel:{channel}'.format(channel=channel),
+                    'is_st': int(is_st),
+                    'receiveTime': n_time,
+                    'addTime': nowTime
+                }
+                qs_list.append(Equipment_Info(**add_data))
+                userID_ids.append(userID_id)
+        if qs_list:
+            print(1)
+            Equipment_Info.objects.bulk_create(qs_list)
+            return True
+        else:
+            return False
+
 
 # http://test.dvema.com/detect/add?uidToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJQMldOR0pSRDJFSEE1RVU5MTExQSJ9.xOCI5lerk8JOs5OcAzunrKCfCrtuPIZ3AnkMmnd-bPY&n_time=1526845794&channel=1&event_type=51&is_st=0
 # 移动侦测接口
@@ -973,7 +1008,8 @@ class PushNotificationView(View):
                     alarm='Motion \tChannel:{channel}'.format(channel=channel),
                     is_st=is_st,
                     receiveTime=n_time,
-                    addTime=now_time
+                    addTime=now_time,
+                    storage_location=1
                 ))
                 if is_sys_msg:
                     sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,

+ 51 - 35
Controller/DetectControllerV2.py

@@ -294,11 +294,13 @@ class DetectControllerViewV2(View):
         if not qs.exists():
             return response.json(0, {'datas': [], 'count': 0})
         qs = qs.values('id', 'devUid', 'devNickName', 'Channel', 'eventType', 'status', 'alarm', 'eventTime',
-                       'receiveTime', 'is_st', 'addTime')
+                       'receiveTime', 'is_st', 'addTime', 'storage_location')
 
         count = qs.count()
         qr = qs[(page - 1) * line:page * line]
         res = []
+        auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
+        oss_img_bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
         aws_s3_guonei = boto3.client(
             's3',
             aws_access_key_id=AWS_ACCESS_KEY_ID[0],
@@ -318,18 +320,23 @@ class DetectControllerViewV2(View):
             devUid = p['devUid']
             eventTime = p['eventTime']
             channel = p['Channel']
+            storage_location = p['storage_location']
             if p['is_st'] == 1:
                 thumbspng = '{uid}/{channel}/{time}.jpeg'.format(uid=devUid, channel=p['Channel'], time=eventTime)
-                if region == 2:  # 2:国内
-                    response_url = aws_s3_guonei.generate_presigned_url(
-                        'get_object',
-                        Params={'Bucket': 'push', 'Key': thumbspng}, ExpiresIn=3600)
+                if storage_location == 1:   # oss
+                    response_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
                     p['img'] = response_url
                     p['img_list'] = [response_url]
-                else:   # 1:国外
-                    response_url = aws_s3_guowai.generate_presigned_url(
-                        'get_object',
-                        Params={'Bucket': 'foreignpush', 'Key': thumbspng}, ExpiresIn=3600)
+                if region == 2 and storage_location == 2:  # 2:国内,aws
+                    response_url = aws_s3_guonei.generate_presigned_url('get_object',
+                                                                        Params={'Bucket': 'push', 'Key': thumbspng},
+                                                                        ExpiresIn=300)
+                    p['img'] = response_url
+                    p['img_list'] = [response_url]
+                if region == 1 and storage_location == 2:   # 1:国外,aws
+                    response_url = aws_s3_guowai.generate_presigned_url('get_object',
+                                                                        Params={'Bucket': 'foreignpush', 'Key': thumbspng},
+                                                                        ExpiresIn=300)
                     p['img'] = response_url
                     p['img_list'] = [response_url]
 
@@ -341,38 +348,44 @@ class DetectControllerViewV2(View):
                 if vodqs.exists():
                     bucket_name = vodqs[0]['bucket__bucket']
                     endpoint = vodqs[0]['bucket__endpoint']
-                    # bucket = oss2.Bucket(auth, endpoint, bucket_name)
+                    bucket = oss2.Bucket(auth, endpoint, bucket_name)
                     ts = '{uid}/vod{channel}/{etime}/ts0.ts'.format(uid=devUid, channel=p['Channel'], etime=eventTime)
-                    if region == 2:     # 2:国内
-                        thumb = aws_s3_guonei.generate_presigned_url(
-                            'get_object',
-                            Params={'Bucket': 'push', 'Key': ts}, ExpiresIn=3600)
-                    else:   # 1:国外
-                        thumb = aws_s3_guowai.generate_presigned_url(
-                            'get_object',
-                            Params={'Bucket': 'foreignpush', 'Key': ts}, ExpiresIn=3600)
-                    p['img_list'] = [thumb]
-                    # thumb0 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_0000,w_700'})
-                    # thumb1 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_1000,w_700'})
-                    # thumb2 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_2000,w_700'})
-                    # thumb3 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_3000,w_700'})
-                    # p['img_list'] = [thumb0, thumb1, thumb2]
+                    if storage_location == 1:   # oss
+                        thumb0 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_0000,w_700'})
+                        thumb1 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_1000,w_700'})
+                        thumb2 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_2000,w_700'})
+                        # thumb3 = bucket.sign_url('GET', ts, 3600, params={'x-oss-process': 'video/snapshot,t_3000,w_700'})
+                        p['img_list'] = [thumb0, thumb1, thumb2]
+                    elif region == 2 and storage_location == 2:     # 2:国内,aws
+                        thumb = aws_s3_guonei.generate_presigned_url('get_object',
+                                                                     Params={'Bucket': 'push', 'Key': ts},
+                                                                     ExpiresIn=3600)
+                        p['img_list'] = [thumb]
+                    elif region == 1 and storage_location == 2:   # 1:国外,aws
+                        thumb = aws_s3_guowai.generate_presigned_url('get_object',
+                                                                     Params={'Bucket': 'foreignpush', 'Key': ts},
+                                                                     ExpiresIn=3600)
+                        p['img_list'] = [thumb]
             elif p['is_st'] == 3:
                 # 列表装载回放时间戳标记
                 p['img_list'] = []
                 for i in range(p['is_st']):
                     thumbspng = '{uid}/{channel}/{time}_{st}.jpeg'.format(uid=devUid, channel=p['Channel'], time=eventTime, st=i)
-                    if region == 2:  # 2:国内
-                        response_url = aws_s3_guonei.generate_presigned_url(
-                            'get_object',
-                            Params={'Bucket': 'push', 'Key': thumbspng}, ExpiresIn=3600)
+                    if storage_location == 1:   # oss
+                        img = oss_img_bucket.sign_url('GET', thumbspng, 300)
+                        p['img_list'].append(img)
+                    elif region == 2 and storage_location == 2:  # 2:国内,aws
+                        response_url = aws_s3_guonei.generate_presigned_url('get_object',
+                                                                            Params={'Bucket': 'push', 'Key': thumbspng},
+                                                                            ExpiresIn=300)
                         img = response_url
-                    else:   # 1:国外
-                        response_url = aws_s3_guowai.generate_presigned_url(
-                            'get_object',
-                            Params={'Bucket': 'foreignpush', 'Key': thumbspng}, ExpiresIn=3600)
+                        p['img_list'].append(img)
+                    elif region == 1 and storage_location == 2:   # 1:国外,aws
+                        response_url = aws_s3_guowai.generate_presigned_url('get_object',
+                                                                            Params={'Bucket': 'foreignpush', 'Key': thumbspng},
+                                                                            ExpiresIn=300)
                         img = response_url
-                    p['img_list'].append(img)
+                        p['img_list'].append(img)
             if devUid in uid_type_dict.keys():
                 p['uid_type'] = uid_type_dict[devUid]['type']
                 p['devNickName'] = uid_type_dict[devUid]['NickName']
@@ -643,7 +656,8 @@ class NotificationView(View):
                     alarm='Motion \tChannel:{channel}'.format(channel=channel),
                     is_st=int_is_st,
                     receiveTime=n_time,
-                    addTime=now_time
+                    addTime=now_time,
+                    storage_location=2
                 ))
                 if is_sys_msg:
                     sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
@@ -950,6 +964,7 @@ class NotificationView(View):
                     redisObject.set_data(key=tmpDKey, val=1, expire=detect_interval)
 
 
+# 这个接口没有调用过,不敢动
 # http://test.dvema.com/detect/add?uidToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJQMldOR0pSRDJFSEE1RVU5MTExQSJ9.xOCI5lerk8JOs5OcAzunrKCfCrtuPIZ3AnkMmnd-bPY&n_time=1526845794&channel=1&event_type=51&is_st=0
 # 移动侦测接口
 class PushNotificationView(View):
@@ -1118,7 +1133,8 @@ class PushNotificationView(View):
                     alarm='Motion \tChannel:{channel}'.format(channel=channel),
                     is_st=is_st,
                     receiveTime=n_time,
-                    addTime=now_time
+                    addTime=now_time,
+                    storage_location=2
                 ))
                 if is_sys_msg:
                     sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,

+ 1 - 1
Controller/ShadowController.py

@@ -16,7 +16,7 @@
 # 生产环境
 # shadow.dvema.com
 # 设备影子更新
-# from Object.LogUtil import LogUtil
+from Object.LogUtil import LogUtil
 from Object.ResponseObject import ResponseObject
 from Object.ETkObject import ETkObject
 import time

+ 1 - 0
Model/models.py

@@ -296,6 +296,7 @@ class Equipment_Info(models.Model):
     receiveTime = models.CharField(blank=True, default='', max_length=16, verbose_name=u'接收到报警时间')
     userID_id = models.CharField(default='',  db_index=True, blank=True, max_length=32, verbose_name=u'用户ID')
     is_st = models.SmallIntegerField(default=0, verbose_name='是否截图')  # 0 否,1 是图,2,视频
+    storage_location = models.SmallIntegerField(default=1, verbose_name='数据信息存储位置。1:阿里云oss,2:aws')
     # message_id = models.CharField(blank=True, max_length=32, default='', verbose_name='第三方推送服务器返回的id')
     # push_type = models.SmallIntegerField(blank=True, default=0, verbose_name='第三方推送服务器标志。0:APNS推送,1:谷歌推送,2:极光推送')
     # push_server_status = models.IntegerField(blank=True, default=200, verbose_name='是否成功推送到第三方服务器。200:成功,other:失败')

+ 17 - 0
Object/LogUtil.py

@@ -0,0 +1,17 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+from Ansjer.config import BASE_DIR
+
+
+class LogUtil:
+
+    @staticmethod
+    def write_log(file_path=None, log=''):
+        if not file_path:
+            file_path = '/'.join((BASE_DIR, 'static/test.log'))
+
+        file = open(file_path, 'a+')
+        file.write(log)
+        file.write('\n')
+        file.flush()
+        file.close()

+ 9 - 6
Service/CommonService.py

@@ -207,12 +207,15 @@ class CommonService:
 
     # 获取当前时间
     @staticmethod
-    def get_now_time_str(n_time, tz):
-        n_time = int(n_time)
-        if tz:
-            n_time = n_time + 3600 * float(tz)
-        n_date = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(int(n_time)))
-        return n_date
+    def get_now_time_str(n_time, tz, lang):
+        print(n_time)
+        print(tz)
+        print(lang)
+        n_time = int(n_time) + 3600 * float(tz)
+        if lang == 'cn':
+            return time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(int(n_time)))
+        else:
+            return time.strftime('%m-%d-%Y %H:%M:%S', time.gmtime(int(n_time)))
 
     # 生成随机数
     @staticmethod