Selaa lähdekoodia

修改智能场景状态通知设备

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

+ 26 - 1
Controller/SensorGateway/SmartSceneController.py

@@ -423,7 +423,32 @@ class SmartSceneView(View):
         if not all([smart_scene_id, is_enable]):
             return response.json(444, {'error param': 'smartSceneId and status'})
         try:
-            SmartScene.objects.filter(id=smart_scene_id).update(is_enable=is_enable)
+            smart_scene_id = int(smart_scene_id)
+            scene_status = 1 if is_enable == 'True' else 0
+            msg = {
+                'scene_id': smart_scene_id,
+                'scene_status': scene_status
+            }
+            # 查询序列号
+            smart_scene_qs = SmartScene.objects.filter(id=smart_scene_id).values('device_id', 'sub_device_id')
+            device_id = smart_scene_qs[0]['device_id']
+            if device_id:
+                serial_number = Device_Info.objects.filter(id=device_id).values('serial_number')[0]['serial_number']
+            else:
+                sub_device_id = smart_scene_qs[0]['sub_device_id']
+                serial_number = GatewaySubDevice.objects.filter(id=sub_device_id).values('device__serial_number')[0][
+                    'device__serial_number']
+
+            topic_name = 'loocam/gateway_sensor/smart_scene/{}'.format(serial_number)
+
+            with transaction.atomic():
+                SmartScene.objects.filter(id=smart_scene_id).update(is_enable=is_enable)
+                # 通过mqtt发送设备数据
+                success = CommonService.req_publish_mqtt_msg(serial_number, topic_name, msg)
+                try:
+                    assert success
+                except AssertionError:
+                    return response.json(10044)
             return response.json(0)
         except Exception as e:
             return response.json(500, repr(e))