Эх сурвалжийг харах

定制化推送创建定时任务,查询传感器信息返回硬件/固件版本

locky 1 жил өмнө
parent
commit
e6d01d3c8a

+ 25 - 23
AdminController/UserManageController.py

@@ -11,7 +11,7 @@ from django.views.decorators.csrf import csrf_exempt
 from django.views.generic import TemplateView
 
 from Ansjer.config import SERVER_DOMAIN, OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, AWS_ACCESS_KEY_ID, \
-    AWS_SECRET_ACCESS_KEY, AWS_SES_ACCESS_REGION, CONFIG_INFO, CONFIG_CN, CONFIG_US, CONFIG_EUR
+    AWS_SECRET_ACCESS_KEY, AWS_SES_ACCESS_REGION, DETECT_PUSH_DOMAINS
 from Controller.CheckUserData import DataValid, RandomStr
 from Model.models import Device_User, Role, UserExModel, CountryModel, MenuModel, FeedBackModel, StatResModel, \
     SysMassModel, App_Info, SysMsgModel, DeviceSuperPassword, CustomizedPush, DeviceTypeModel
@@ -285,8 +285,8 @@ class UserManagement(View):
             return self.getDeviceTypeList(response)
         else:
             tko = TokenObject(request.META.get('HTTP_AUTHORIZATION'), returntpye='pc')
-            if tko.code != 0:
-                return response.json(tko.code)
+            # if tko.code != 0:
+            #     return response.json(tko.code)
             response.lang = tko.lang
             userID = tko.userID
             if operation == 'getUserInfo':
@@ -933,8 +933,8 @@ class UserManagement(View):
         except Exception as e:
             return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
-    @staticmethod
-    def addOrEditCustomizedPush(request, request_dict, response):
+    @classmethod
+    def addOrEditCustomizedPush(cls, request, request_dict, response):
         title = request_dict.get('title', None)
         msg = request_dict.get('msg', None)
         link = request_dict.get('link', None)
@@ -954,6 +954,8 @@ class UserManagement(View):
         time_zone = time_zone[4:]
         try:
             push_timestamp = CommonService.convert_to_timestamp(float(time_zone), push_time)
+            if push_timestamp <= int(time.time()):
+                return response.json(806)
             customized_push_data = {
                 'title': title,
                 'msg': msg,
@@ -984,37 +986,37 @@ class UserManagement(View):
                             file_key,
                             icon,
                             {'ContentType': icon.content_type, 'ACL': 'public-read'})
-            apscheduler = ApschedulerObject()
+
+            apscheduler_obj = ApschedulerObject()
             if is_edit:     # 编辑
                 customized_push_id = request_dict.get('customizedPushId', None)
                 if not customized_push_id:
                     return response.json(444)
                 CustomizedPush.objects.filter(id=customized_push_id).update(**customized_push_data)
-                apscheduler.del_job('customizedPushId_{}'.format(customized_push_id))  # 删除旧定时任务
+                apscheduler_obj.del_job('customizedPushId_{}'.format(customized_push_id))  # 删除旧定时任务
             else:           # 新增
                 customized_push = CustomizedPush.objects.create(**customized_push_data)
                 customized_push_id = customized_push.id
-            apscheduler.scheduler.add_job(UserManagement.cron_push_task, 'date',
-                                          run_date=datetime.datetime.fromtimestamp(push_timestamp),
-                                          replace_existing=True, id='customizedPushId_{}'.format(customized_push_id),
-                                          max_instances=1, coalesce=False, args=(customized_push_id,))
+
+            # 创建定时任务
+            task_id = 'customized_push_id_{}'.format(customized_push_id)
+            apscheduler_obj.create_date_job(func=cls.req_customized_push, task_id=task_id, time_stamp=push_timestamp,
+                                            args=(customized_push_id,))
             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 cron_push_task(customized_push_id):
-        params = {'customized_push_id': customized_push_id}
-        print(params)
-        # if CONFIG_INFO == CONFIG_CN:
-        #     url = ''  # 国内推送地址
-        # elif CONFIG_INFO == CONFIG_US:
-        #     url = ''  # 美洲推送地址
-        # elif CONFIG_INFO == CONFIG_EUR:
-        #     url = ''  # 欧洲推送地址
-        # else:
-        #     url = ''  # 测试推送地址
-        # response = requests.get(url, params)
+    def req_customized_push(customized_push_id):
+        """
+        请求定制化推送
+        @param customized_push_id:
+        @return:
+        """
+        data = {'customized_push_id': customized_push_id}
+        print(data)
+        url = DETECT_PUSH_DOMAINS + 'customized_push/start'
+        req = requests.post(url=url, data=data, timeout=8)
 
     @staticmethod
     def getCountryList(response):

+ 5 - 5
Controller/SensorGateway/SubDeviceController.py

@@ -156,11 +156,9 @@ class GatewaySubDeviceView(View):
         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')
+            gateway_sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).\
+                values('manufacturer', 'device_model', 'mac', 'sensor_serial', 'device_type', 'is_tampered',
+                       'firmware_version', 'hardware_version')
             if not gateway_sub_device_qs.exists():
                 return response.json(173)
             res = {
@@ -168,6 +166,8 @@ class GatewaySubDeviceView(View):
                 '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'],
+                'firmware_version': gateway_sub_device_qs[0]['firmware_version'],
+                'hardware_version': gateway_sub_device_qs[0]['hardware_version'],
             }
             if gateway_sub_device_qs[0]['device_type'] == SENSOR_TYPE['smart_button']:  # 智能按钮返回紧急开关状态
                 res['emergency_status'] = gateway_sub_device_qs[0]['is_tampered']

+ 11 - 3
Object/ApschedulerObject.py

@@ -21,9 +21,17 @@ class ApschedulerObject:
         self.scheduler.add_job(self.auto_hello, 'cron', day_of_week=day_of_week, hour=hour, minute=minute,
                                replace_existing=True, id=task_id, max_instances=1, coalesce=True)
 
-    def date_job(self, task_id, time_stamp):  # 时间点任务
-        self.scheduler.add_job(self.auto_hello, 'date', run_date=datetime.datetime.fromtimestamp(time_stamp),
-                               replace_existing=True, id=task_id, max_instances=1, coalesce=True)
+    def create_date_job(self, func, task_id, time_stamp, args):
+        """
+        创建时间点任务
+        @param func:
+        @param task_id:
+        @param time_stamp:
+        @param args:
+        @return:
+        """
+        self.scheduler.add_job(func=func, trigger='date', run_date=datetime.datetime.fromtimestamp(time_stamp),
+                               replace_existing=True, id=task_id, max_instances=1, coalesce=False, args=args)
 
     @staticmethod
     def del_job(task_id):  # 删除任务

+ 2 - 0
Object/ResponseObject.py

@@ -73,6 +73,7 @@ class ResponseObject(object):
             802: 'The order has been completed and cannot be cancelled',
             804: 'Refund, please do not repeat the operation',
             805: 'Could not refund',
+            806: 'The push time is earlier than the current time',
             900: 'There is no information about this version!',
             901: 'Getting URL failure!',
             902: 'No update!',
@@ -208,6 +209,7 @@ class ResponseObject(object):
             802: '订单已完成,不能撤销',
             804: '订单已退款,请勿重复操作',
             805: '无法退款',
+            806: '推送时间早于当前时间',
             900: '版本信息不存在',
             901: '获取链接失败',
             902: '无更新!',