Explorar o código

优化定时器

peng hai 1 ano
pai
achega
f3cf110860
Modificáronse 1 ficheiros con 16 adicións e 7 borrados
  1. 16 7
      Controller/SensorGateway/SmartSwitchController.py

+ 16 - 7
Controller/SensorGateway/SmartSwitchController.py

@@ -451,6 +451,7 @@ class SmartSwitchView(View):
         @return: response
         """
         is_edit = request_dict.get('isEdit', None)
+        timer_id = request_dict.get('timerId', None)
         device_id = request_dict.get('deviceId', None)
         countdown_time = request_dict.get('countdownTime', None)
         actions = request_dict.get('actions', None)
@@ -464,6 +465,7 @@ class SmartSwitchView(View):
         try:
             now_time = int(time.time())
             countdown_time = int(countdown_time)
+            timer_status = int(timer_status)
             serial_number = device_qs[0]['serial_number']
             user_id = device_qs[0]['userID']
             tz = CommonService.get_user_tz(user_id)
@@ -473,21 +475,28 @@ class SmartSwitchView(View):
             topic_name = TIMER_TOPIC_NAME.format(serial_number)
             key = 'Switch-Timer-' + device_id
             implement_time = now_time + countdown_time
-            redis_dict = {'timePoint': implement_time,
+            redis_dict = {'timerId': 0,
+                          'timePoint': implement_time,
                           'countdownTime': countdown_time,
-                          'actions': actions,
+                          'actions': int(actions),
                           'timerStatus': timer_status}
             with transaction.atomic():
                 celery_obj.del_task(task_id)
                 if is_edit == '1':
                     if not timer_status:
                         return response.json(444, {'param': 'timerStatus'})
-                    timer_status = int(timer_status)
                     if timer_status == 0:  # 暂停计时器
-                        redis_dict['timePoint'] = -1
-                        redis_obj.set_hash_data(key, redis_dict)
-                        redis_obj.set_persist(key)
-                        return response.json(0, redis_dict)
+                        redis_obj.del_data(key)
+                        return response.json(0)
+                    else:
+                        if not timer_id:
+                            return response.json(444, {'param': 'timerId'})
+                        timer_exist = redis_obj.get_all_hash_data(key)
+                        if timer_exist:
+                            if int(timer_id) != int(timer_exist[b'timerId']):
+                                return response.json(174)
+                            else:
+                                redis_dict['timerId'] = int(timer_exist[b'timerId']) + 1
                 redis_obj.set_hash_data(key, redis_dict)
                 redis_obj.set_expire(key, countdown_time)
                 msg = {'device_switch': actions, 'task_id': task_id}