Browse Source

发送mqtt时使用redis锁

peng 1 year ago
parent
commit
a957b2551c
1 changed files with 9 additions and 1 deletions
  1. 9 1
      Controller/SensorGateway/SmartSwitchController.py

+ 9 - 1
Controller/SensorGateway/SmartSwitchController.py

@@ -13,6 +13,7 @@ from django.views import View
 
 from Model.models import SwitchDimmingSettings, SwitchChronopher, Device_Info, SceneLog, FamilyRoomDevice, \
     SwitchOperateLog
+from Object.RedisObject import RedisObject
 from Service.CommonService import CommonService
 from Object.ApschedulerObject import ApschedulerObject
 from django.db import transaction
@@ -61,7 +62,6 @@ class SmartSwitchView(View):
             else:
                 return response.json(414)
 
-
     @staticmethod
     def get_dimming_setting(request_dict, response):
         """
@@ -363,10 +363,18 @@ class SmartSwitchView(View):
         """
         now_time = int(time.time())
         msg['implementTime'] = now_time
+        redis_obj = RedisObject()
+        is_lock = redis_obj.CONN.setnx(task_id + 'do_notify', 1)
+        redis_obj.CONN.expire(task_id + 'do_notify', 60)
+        if not is_lock:
+            LOGGER.info('定时发送mqtt结果:{},参数:{},{},{},{},{},线程:{},进程:{}'.format(False, serial_number, topic_name, msg,
+                                                                             now_time, task_id,
+                                                                             threading.get_ident(), os.getpid()))
         result = CommonService.req_publish_mqtt_msg(serial_number, topic_name, msg)
         LOGGER.info('定时发送mqtt结果:{},参数:{},{},{},{},{},线程:{},进程:{}'.format(result, serial_number, topic_name, msg,
                                                                          now_time, task_id,
                                                                          threading.get_ident(), os.getpid()))
+        redis_obj.del_data(key=task_id + 'do_notify')
 
     @staticmethod
     def create_chronopher_log(request_dict, response):