chenjunkai il y a 5 ans
Parent
commit
4a59ba0e9b
7 fichiers modifiés avec 163 ajouts et 112 suppressions
  1. 2 2
      AnsjerPush/settings.py
  2. 109 56
      Controller/DetectController.py
  3. 30 54
      Controller/ShadowController.py
  4. 1 0
      Model/models.py
  5. 7 0
      formal_manage.py
  6. 7 0
      local_manage.py
  7. 7 0
      test_manage.py

+ 2 - 2
AnsjerPush/settings.py

@@ -65,7 +65,7 @@ WSGI_APPLICATION = 'AnsjerPush.wsgi.application'
 DATABASE_DATA = 'AnsjerLocal'
 SERVER_HOST = '192.168.136.45'
 DATABASES_USER = 'ansjer'
-DATABASES_PASS = '1234'
+DATABASES_PASS = 'ansjer.x.x'
 
 DATABASES = {'default': {
     'ENGINE': 'django.db.backends.mysql',
@@ -142,4 +142,4 @@ CORS_ALLOW_HEADERS = (
     'user-agent',
     'x-csrftoken',
     'x-requested-with',
-)
+)

+ 109 - 56
Controller/DetectController.py

@@ -29,11 +29,12 @@ from Object.RedisObject import RedisObject
 from Object.UidTokenObject import UidTokenObject
 from Service.CommonService import CommonService
 
-
 '''
 http://push.dvema.com/notify/push?etk=Y2lTRXhMTjBWS01sWlpURTVJU0ZWTlJ6RXhNVUU9T3o=&n_time=1526845794&channel=1&event_type=704&is_st=0
 http://push.dvema.com/deviceShadow/generateUTK?username=debug_user&password=debug_password&uid=VVDHCVBYDKFMJRWA111A
 '''
+
+
 # 移动侦测接口
 class NotificationView(View):
 
@@ -82,7 +83,7 @@ class NotificationView(View):
                 # 从数据库查询出来
                 uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
                     values('token_val', 'app_type', 'appBundleId',
-                           'push_type', 'userID_id','lang',
+                           'push_type', 'userID_id', 'lang',
                            'tz', 'uid_set__nickname')
                 # 新建一个list接收数据
                 redis_list = []
@@ -112,6 +113,7 @@ class NotificationView(View):
             }
             eq_list = []
             sys_msg_list = []
+            userID_ids = []
             for up in redis_list:
                 push_type = up['push_type']
                 appBundleId = up['appBundleId']
@@ -133,31 +135,32 @@ class NotificationView(View):
                 elif push_type == 2:  # android jpush
                     self.do_jpush(**kwag_args)
                 userID_id = up["userID_id"]
-                eq_list.append(Equipment_Info(
-                    userID_id=userID_id,
-                    eventTime=n_time,
-                    eventType=event_type,
-                    devUid=uid,
-                    devNickName=nickname,
-                    Channel=channel,
-                    alarm='Motion \tChannel:{channel}'.format(channel=channel),
-                    is_st=int(is_st),
-                    receiveTime=n_time,
-                    addTime=now_time
-                ))
-
-                if is_sys_msg:
-                    sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
-                                                 event_type=event_type,is_sys=1)
-                    sys_msg_list.append(SysMsgModel(
+                int_is_st = int(is_st)
+                if userID_id not in userID_ids:
+                    eq_list.append(Equipment_Info(
                         userID_id=userID_id,
-                        msg=sys_msg_text,
-                        # msg='{msg_title} {msg_text}'.format(msg_title=msg_title, msg_text=msg_text),
-                        addTime=now_time,
-                        updTime=now_time,
-                        uid=uid,
-                        eventType=event_type
+                        eventTime=n_time,
+                        eventType=event_type,
+                        devUid=uid,
+                        devNickName=nickname,
+                        Channel=channel,
+                        alarm='Motion \tChannel:{channel}'.format(channel=channel),
+                        is_st=int_is_st,
+                        receiveTime=n_time,
+                        addTime=now_time
                     ))
+                    if is_sys_msg:
+                        sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
+                                                         event_type=event_type, is_sys=1)
+                        sys_msg_list.append(SysMsgModel(
+                            userID_id=userID_id,
+                            msg=sys_msg_text,
+                            addTime=now_time,
+                            updTime=now_time,
+                            uid=uid,
+                            eventType=event_type))
+                    userID_ids.append(userID_id)
+
             if is_sys_msg:
                 SysMsgModel.objects.bulk_create(sys_msg_list)
             Equipment_Info.objects.bulk_create(eq_list)
@@ -210,7 +213,7 @@ class NotificationView(View):
             return True
         return False
 
-    def get_msg_text(self, channel, n_time, lang, tz, event_type,is_sys=0):
+    def get_msg_text(self, channel, n_time, lang, tz, event_type, is_sys=0):
         n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz)
         etype = int(event_type)
         if lang == 'cn':
@@ -342,13 +345,12 @@ class PushNotificationView(View):
             redisObj = RedisObject(db=6)
             # pkey = '{uid}_{channel}_ptl'.format(uid=uid, channel=channel)
             pkey = '{uid}_ptl'.format(uid=uid)
-
+            ykey = '{uid}_redis_qs'.format(uid=uid)
             if redisObj.get_data(key=pkey):
                 res_data = {'code': 0, 'msg': 'success,!'}
                 return JsonResponse(status=200, data=res_data)
             else:
                 redisObj.set_data(key=pkey, val=1, expire=60)
-            ykey = '{uid}_redis_qs'.format(uid=uid)
             ##############
             redis_data = redisObj.get_data(key=ykey)
             if redis_data:
@@ -360,7 +362,7 @@ class PushNotificationView(View):
                 # 从数据库查询出来
                 uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
                     values('token_val', 'app_type', 'appBundleId',
-                           'push_type', 'userID_id','lang',
+                           'push_type', 'userID_id', 'lang',
                            'tz', 'uid_set__nickname')
                 # 新建一个list接收数据
                 redis_list = []
@@ -393,41 +395,92 @@ class PushNotificationView(View):
                         img_url_list.append(url)
                     res_data = {'code': 0, 'img_url_list': img_url_list, 'msg': 'success'}
                     return JsonResponse(status=200, data=res_data)
-
             else:
                 return JsonResponse(status=200, data={'code': 404, 'msg': 'data is not exist'})
         else:
             return JsonResponse(status=200, data={'code': 404, 'msg': 'wrong etk'})
 
     def do_bulk_create_info(self, uaqs, n_time, channel, event_type, is_st, uid):
-        #
-        qs_list = []
-        nowTime = int(time.time())
+        now_time = int(time.time())
         # 设备昵称
         userID_ids = []
-        for dv in uaqs:
-            userID_id = dv["userID_id"]
+        sys_msg_list = []
+        is_sys_msg = self.is_sys_msg(int(event_type))
+        is_st = int(is_st)
+        eq_list = []
+        nickname = uaqs[0]['uid_set__nickname']
+        if not nickname:
+            nickname = uid
+        for ua in uaqs:
+            lang = ua['lang']
+            tz = ua['tz']
+            userID_id = ua["userID_id"]
             if userID_id not in userID_ids:
-                uid_nickname = dv['uid_set__nickname']
-                if not uid_nickname:
-                    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)
+                eq_list.append(Equipment_Info(
+                    userID_id=userID_id,
+                    eventTime=n_time,
+                    eventType=event_type,
+                    devUid=uid,
+                    devNickName=nickname,
+                    Channel=channel,
+                    alarm='Motion \tChannel:{channel}'.format(channel=channel),
+                    is_st=is_st,
+                    receiveTime=n_time,
+                    addTime=now_time
+                ))
+                if is_sys_msg:
+                    sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
+                                                     event_type=event_type, is_sys=1)
+                    sys_msg_list.append(SysMsgModel(
+                        userID_id=userID_id,
+                        msg=sys_msg_text,
+                        addTime=now_time,
+                        updTime=now_time,
+                        uid=uid,
+                        eventType=event_type))
+        if eq_list:
+            print('eq_list')
+            Equipment_Info.objects.bulk_create(eq_list)
+        if is_sys_msg:
+            print('sys_msg')
+            SysMsgModel.objects.bulk_create(sys_msg_list)
+        return True
+
+    def is_sys_msg(self, event_type):
+        event_type_list = [702, 703, 704]
+        if event_type in event_type_list:
             return True
+        return False
+
+    def get_msg_text(self, channel, n_time, lang, tz, event_type, is_sys=0):
+        n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz)
+        etype = int(event_type)
+        if lang == 'cn':
+            if etype == 704:
+                msg_type = '电量过低'
+            elif etype == 702:
+                msg_type = '摄像头休眠'
+            elif etype == 703:
+                msg_type = '摄像头唤醒'
+            else:
+                msg_type = ''
+            if is_sys:
+                send_text = '{msg_type} 通道:{channel}'.format(msg_type=msg_type, channel=channel)
+            else:
+                send_text = '{msg_type} 通道:{channel} 日期:{date}'.format(msg_type=msg_type, channel=channel, date=n_date)
         else:
-            return False
+            if etype == 704:
+                msg_type = 'Low battery'
+            elif etype == 702:
+                msg_type = 'Camera sleep'
+            elif etype == 703:
+                msg_type = 'Camera wake'
+            else:
+                msg_type = ''
+            if is_sys:
+                send_text = '{msg_type} channel:{channel}'. \
+                    format(msg_type=msg_type, channel=channel)
+            else:
+                send_text = '{msg_type} channel:{channel} date:{date}'. \
+                    format(msg_type=msg_type, channel=channel, date=n_date)
+        return send_text

+ 30 - 54
Controller/ShadowController.py

@@ -50,7 +50,7 @@ def generate_utk(request):
         return response.json(444,'username password')
 
 
-
+# 设备影子更新
 def update_device_shadow(request):
     request.encoding = 'utf-8'
     response = ResponseObject()
@@ -82,6 +82,7 @@ def update_device_shadow(request):
         push_status = request_dict.get('push_status', None)
         pwd = request_dict.get('pwd', None)
         resetTime = request_dict.get('resetTime', None)
+        is_alexa = request_dict.get('is_alexa', None)
 
         us_qs = UidSetModel.objects.filter(uid=uid)
         # 更新
@@ -90,64 +91,39 @@ def update_device_shadow(request):
         print('-------')
         print(resetTime)
         print('-------')
-
+        qs_dict = {
+            'updTime': nowTime,
+            'ip': ip
+        }
+        if channel:
+            qs_dict['channel'] = channel
+        if p2p_region:
+            qs_dict['p2p_region'] = p2p_region
+        if ucode:
+            qs_dict['ucode'] = ucode
+        if version:
+            qs_dict['version'] = version
+        if tz:
+            qs_dict['tz'] = tz
+        if video_code:
+            qs_dict['video'] = video_code
+        if cloud_vod:
+            qs_dict['cloud_vod'] = cloud_vod
+        if push_status:
+            qs_dict['detect_status'] = push_status
+        if pwd:
+            qs_dict['pwd'] = pwd
+        if is_alexa:
+            qs_dict['is_alexa'] = is_alexa
         if us_qs.exists():
-            update_dict = {
-                'updTime': nowTime,
-                'ip': ip
-            }
-            if channel:
-                update_dict['channel'] = channel
-            if p2p_region:
-                update_dict['p2p_region'] = p2p_region
-            if ucode:
-                update_dict['ucode'] = ucode
-            if version:
-                update_dict['version'] = version
-            if tz:
-                update_dict['tz'] = tz
-            if video_code:
-                update_dict['video'] = video_code
-            if cloud_vod:
-                update_dict['cloud_vod'] = cloud_vod
-            if push_status:
-                update_dict['detect_status'] = push_status
-            if pwd:
-                update_dict['pwd'] = pwd
-            us_qs.update(**update_dict)
+            us_qs.update(**qs_dict)
             # 如果推送状态开启,返回推送url
-            data = {}
-
             return JsonResponse(status=200, data={'code': 0, 'msg': 'success', 'data': {}})
         # 新增
         else:
-            create_dict = {
-                'uid': uid,
-                'addTime': nowTime,
-                'updTime': nowTime,
-                'ip': ip
-            }
-            if channel:
-                create_dict['channel'] = channel
-            if p2p_region:
-                create_dict['p2p_region'] = p2p_region
-            if ucode:
-                create_dict['ucode'] = ucode
-            if version:
-                create_dict['version'] = version
-            if tz:
-                create_dict['tz'] = tz
-            if video_code:
-                create_dict['video_code'] = video_code
-            if cloud_vod:
-                create_dict['cloud_vod'] = cloud_vod
-            if push_status:
-                create_dict['detect_status'] = push_status
-            if pwd:
-                create_dict['pwd'] = pwd
-            if resetTime:
-                create_dict['resetTime'] = resetTime
-            UidSetModel.objects.create(**create_dict)
+            qs_dict['uid'] = uid
+            qs_dict['addTime'] = nowTime
+            UidSetModel.objects.create(**qs_dict)
             # 如果推送状态开启,返回推送url
             return JsonResponse(status=200, data={'code': 0, 'msg': 'success', 'data': {}})
     else:

+ 1 - 0
Model/models.py

@@ -331,6 +331,7 @@ class UidSetModel(models.Model):
     pwd = models.CharField(max_length=32, default='', verbose_name=u'设备密码')
     # 设备重置后第一次启动时间
     resetTime = models.IntegerField(default=0, verbose_name='设备重置时间')
+    is_alexa = models.IntegerField(default=0, verbose_name='会否支持alexa') # 0:否,1:是
 
     class Meta:
         db_table = 'uid_set'

+ 7 - 0
formal_manage.py

@@ -3,6 +3,13 @@ import os
 import sys
 
 if __name__ == '__main__':
+    arg_m = sys.argv[1]
+    if arg_m == 'makemigrations':
+        print('do not makemigrations')
+        exit()
+    if arg_m == 'migrate':
+        print('do not migrate')
+        exit()
     os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'AnsjerPush.formal_settings')
     try:
         from django.core.management import execute_from_command_line

+ 7 - 0
local_manage.py

@@ -3,6 +3,13 @@ import os
 import sys
 
 if __name__ == '__main__':
+    arg_m = sys.argv[1]
+    if arg_m == 'makemigrations':
+        print('do not makemigrations')
+        exit()
+    if arg_m == 'migrate':
+        print('do not migrate')
+        exit()
     os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'AnsjerPush.local_settings')
     try:
         from django.core.management import execute_from_command_line

+ 7 - 0
test_manage.py

@@ -3,6 +3,13 @@ import os
 import sys
 
 if __name__ == '__main__':
+    arg_m = sys.argv[1]
+    if arg_m == 'makemigrations':
+        print('do not makemigrations')
+        exit()
+    if arg_m == 'migrate':
+        print('do not migrate')
+        exit()
     os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'AnsjerPush.test_settings')
     try:
         from django.core.management import execute_from_command_line