Selaa lähdekoodia

创建场景接口添加设备的time数据

locky 3 vuotta sitten
vanhempi
commit
e7a1c76be8
1 muutettua tiedostoa jossa 37 lisäystä ja 9 poistoa
  1. 37 9
      Controller/SensorGateway/SmartSceneController.py

+ 37 - 9
Controller/SensorGateway/SmartSceneController.py

@@ -4,13 +4,13 @@
 @Time : 2022/6/29 9:31
 @File :SmartSceneController.py
 """
-import time
 import json
+import time
 
 from django.core.exceptions import ObjectDoesNotExist
+from django.db import transaction
 from django.db.models import F, Q
 from django.views import View
-from django.db import transaction
 
 from Model.models import FamilyRoomDevice, GatewaySubDevice, FamilyRoom, SmartScene, EffectiveTime, Device_Info
 from Service.CommonService import CommonService
@@ -213,7 +213,7 @@ class SmartSceneView(View):
             }
 
             # 处理设置时间
-            if is_all_day:
+            if is_all_day is not None:
                 is_all_day = int(is_all_day)
                 smart_scene_dict['is_all_day'] = is_all_day
 
@@ -232,9 +232,22 @@ class SmartSceneView(View):
                 serial_number = sub_device_qs[0]['device__serial_number']
 
             with transaction.atomic():
-                if not is_all_day or is_all_day == 1:  # 不设置时间或全天
+                if is_all_day is None:  # 不设置时间
+                    smart_scene_qs = SmartScene.objects.create(**smart_scene_dict)
+                    # 设备的time数据
+                    hour, minute = conditions_dict['time']['minutes'] / 60, conditions_dict['time']['minutes'] % 60
+                    start_time = int(hour * 3600 + minute * 60)
+                    time_dict = {
+                        'start_time': start_time,
+                        'repeat': conditions_dict['time']['repeat']
+                    }
+                elif is_all_day == 1:  # 全天
                     smart_scene_qs = SmartScene.objects.create(**smart_scene_dict)
-                elif is_all_day == 2:
+                    # 设备的time数据
+                    time_dict = {
+                        'is_all_day': is_all_day
+                    }
+                elif is_all_day == 2:  # 非全天
                     start_time = int(request_dict.get('startTime', None))
                     end_time = int(request_dict.get('endTime', None))
                     repeat = int(request_dict.get('repeat', None))
@@ -247,10 +260,25 @@ class SmartSceneView(View):
                                                                          repeat=repeat).id
                     smart_scene_dict['effective_time_id'] = effective_time_id
                     smart_scene_qs = SmartScene.objects.create(**smart_scene_dict)
+
+                    # 设备的time数据
+                    start_hour, start_minute, end_hour, end_minute = \
+                        start_time / 60, start_time % 60, end_time / 60, end_time % 60
+                    start_time, end_time = \
+                        int(start_hour * 3600 + start_minute * 60), \
+                        int(end_hour * 3600 + end_minute * 60)
+                    time_dict = {
+                        'is_all_day': is_all_day,
+                        'start_time': start_time,
+                        'end_time': end_time,
+                        'repeat': repeat
+                    }
                 else:
                     return response.json(444, {'error param': 'invalid isAllDay'})
 
-                msg = {}
+                msg = {
+                    'time': time_dict
+                }
                 if conditions_dict['type'] == 2:  # 条件为选择子设备
                     sub_device_id = conditions_dict['sensor']['subDeviceId']
                     sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('src_addr').first()
@@ -272,15 +300,15 @@ class SmartSceneView(View):
                         task_temp['sensor_src'] = int(sub_device_qs['src_addr'], 16)
                     task_list.append(task_temp)
                 msg['task'] = task_list
+
                 smart_scene_qs.device_data = json.dumps(msg)
                 smart_scene_qs.save()
                 # 发布MQTT消息通知网关设备
                 thing_name = serial_number
-                topic_name = 'loocam/gateway_sensor/{}/smart_scene'.format(serial_number)
+                topic_name = 'loocam/gateway_sensor/smart_scene/{}'.format(serial_number)
 
                 success = CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg)
-                if not success:
-                    return response.json(10044)
+                assert success
 
             return response.json(0)
         except Exception as e: