Browse Source

优化定时器

peng 1 year ago
parent
commit
67737ad78a
2 changed files with 19 additions and 6 deletions
  1. 18 5
      Controller/SensorGateway/SmartSwitchController.py
  2. 1 1
      Object/ApschedulerObject.py

+ 18 - 5
Controller/SensorGateway/SmartSwitchController.py

@@ -15,6 +15,7 @@ from django.views import View
 from Model.models import SwitchInfo, SwitchDimmingSettings, SwitchChronopher, Device_Info, SceneLog, FamilyRoomDevice
 from Service.CommonService import CommonService
 from Object.ApschedulerObject import ApschedulerObject
+from Object.RedisObject import RedisObject
 from django.db import transaction
 from Ansjer.config import LOGGER
 
@@ -286,9 +287,13 @@ class SmartSwitchView(View):
                         **chronopher_data)
                     if not update_flag:
                         return response.json(173)
+                    redis_obj = RedisObject()
                     apscheduler_obj.del_job('switchchronopher_{}'.format(chronopher_id))
                     apscheduler_obj.del_job('switchchronopher_{}_1'.format(chronopher_id))
                     apscheduler_obj.del_job('switchchronopher_{}_2'.format(chronopher_id))
+                    redis_obj.del_data('switchchronopher_{}'.format(chronopher_id))
+                    redis_obj.del_data('switchchronopher_{}_1'.format(chronopher_id))
+                    redis_obj.del_data('switchchronopher_{}_2'.format(chronopher_id))
                 else:
                     switch_qs = SwitchChronopher.objects.create(**chronopher_data)
                     chronopher_id = switch_qs.id
@@ -368,10 +373,14 @@ class SmartSwitchView(View):
             delete_flag = SwitchChronopher.objects.filter(device_id=device_id, id=chronopher_id).delete()
             if not delete_flag[0]:
                 return response.json(173)
+            redis_obj = RedisObject()
             apscheduler_obj = ApschedulerObject()
             apscheduler_obj.del_job('switchchronopher_{}'.format(chronopher_id))  # 删除定时任务
             apscheduler_obj.del_job('switchchronopher_{}_1'.format(chronopher_id))
             apscheduler_obj.del_job('switchchronopher_{}_2'.format(chronopher_id))
+            redis_obj.del_data('switchchronopher_{}'.format(chronopher_id))
+            redis_obj.del_data('switchchronopher_{}_1'.format(chronopher_id))
+            redis_obj.del_data('switchchronopher_{}_2'.format(chronopher_id))
             return response.json(0)
         except Exception as e:
             print(e)
@@ -387,11 +396,15 @@ class SmartSwitchView(View):
         @param task_id: 任务id
         @return: response
         """
-        result = CommonService.req_publish_mqtt_msg(serial_number, topic_name, msg)
-        LOGGER.info(
-            '定时发送mqtt结果:{},参数:{},{},{},{},{},线程:{},进程:{}'.format(result, serial_number, topic_name, msg,
-                                                                 int(time.time()), task_id, threading.get_ident(),
-                                                                 os.getpid()))
+        redis_obj = RedisObject()
+        flag = redis_obj.get_data(task_id)
+        if not flag:
+            result = CommonService.req_publish_mqtt_msg(serial_number, topic_name, msg)
+            LOGGER.info('定时发送mqtt结果:{},参数:{},{},{},{},{},线程:{},进程:{}'.format(result, serial_number, topic_name, msg,
+                                                                             int(time.time()), task_id,
+                                                                             threading.get_ident(),
+                                                                             os.getpid()))
+            redis_obj.set_ex_data(task_id, 1, 5)
 
     @staticmethod
     def create_log(request_dict, response):

+ 1 - 1
Object/ApschedulerObject.py

@@ -8,7 +8,7 @@ import datetime
 
 class ApschedulerObject:
     def __init__(self):
-        self.scheduler = BackgroundScheduler({'process': 1, 'thread': 1})
+        self.scheduler = BackgroundScheduler()
         self.scheduler.add_jobstore(DjangoJobStore(), 'default')
 
     @staticmethod