瀏覽代碼

添加网关传感器配置文件

locky 3 年之前
父節點
當前提交
b4f44d7b00

+ 0 - 3
Ansjer/config.py

@@ -82,9 +82,6 @@ AWS_ACCESS_KEY_ID = ['AKIA2MMWBR4DSFG67DTG','AKIA2E67UIMD45Y3HL53']  #0国内,
 AWS_SECRET_ACCESS_KEY = ['aI9gxcAKPmiGgPy9axrtFKzjYGbvpuytEX4xWweL','ckYLg4Lo9ZXJIcJEAKkzf2rWvs8Xth1FCjqiAqUw']
 AWS_ARN = ['arn:aws-cn:s3','arn:aws:s3']
 
-# MQTT主题名
-SMART_SCENE_TOPIC_NAME = 'loocam/gateway_sensor/smart_scene/{}'
-GET_SCENE_TOPIC_NAME = 'loocam/gateway_sensor/get_scene/{}'
 
 #尚云服务器CRCKey
 CRCKey = {'AUS':'CRCKey', 'ACN':'CRCKey'}   #{平台名:CRCKey值}

+ 6 - 0
Ansjer/config/__init__.py

@@ -0,0 +1,6 @@
+# -*- coding: utf-8 -*-
+"""
+@Author : Rocky
+@Time : 2022/8/8 11:24
+@File :__init__.py
+"""

+ 57 - 0
Ansjer/config/gatewaySensorConfig.py

@@ -0,0 +1,57 @@
+# -*- coding: utf-8 -*-
+"""
+@Author : Rocky
+@Time : 2022/8/8 11:20
+@File :gatewaySensorConfig.py
+"""
+
+# MQTT主题名
+SMART_SCENE_TOPIC = 'loocam/gateway_sensor/smart_scene/{}'
+GET_SCENE_TOPIC = 'loocam/gateway_sensor/get_scene/{}'
+
+# 传感器类型
+SENSOR_TYPE = {
+    'door_magnet': 215,
+    'smart_button': 216,
+    'water_flood_sensor': 217,
+    'smoke_sensor': 218,
+    'body_sensor': 219,
+    'tem_hum_sensor': 220,
+}
+
+# 事件类型
+EVENT_TYPE = {
+    # 门磁
+    'door_magnet_opening': 2150,
+    'door_magnet_closure': 2151,
+    'door_magnet_removal': 2152,
+    'door_magnet_low_power': 2153,
+    'door_magnet_start_detection': 2154,
+    'door_magnet_closure_detection': 2155,
+    # 智能按钮
+    'smart_button_press': 2160,
+    'smart_button_click': 2161,
+    'smart_button_double_click': 2162,
+    'smart_button_three_click': 2163,
+    'smart_button_low_power': 2164,
+    # 水浸
+    'flood_trigger': 2170,
+    'flood_recovery': 2171,
+    'flood_low_power': 2172,
+    # 烟雾
+    'smoke_trigger': 2180,
+    'smoke_recovery': 2181,
+    'smoke_removal': 2182,
+    'smoke_low_power': 2183,
+    # 人体红外
+    'someone_moves': 2190,
+    'no_one_moves': 2191,
+    'body_sensor_removal': 2192,
+    'body_sensor_low_power': 2193,
+    'body_sensor_start_detection': 2194,
+    'body_sensor_closure_detection': 2195,
+    # 温湿度
+    'temperature': 2200,
+    'humidity': 2201,
+    'tem_hum_sensor_low_power': 2202,
+}

+ 21 - 1
Ansjer/urls.py

@@ -410,4 +410,24 @@ urlpatterns = [
     re_path('(?P<path>.*)', LogManager.errorPath),
 
 ]
-# 鹏提交测试代码
+
+#                                _(\_/)
+#                              ,((((^`\
+#                             ((((  (6 \
+#                           ,((((( ,    \
+#       ,,,_              ,(((((  /"._  ,`,
+#      ((((\\ ,...       ,((((   /    `-.-'
+#      )))  ;'    `"'"'""((((   (
+#     (((  /            (((      \
+#      )) |                      |
+#     ((  |        .       '     |
+#     ))  \     _ '      `t   ,.')
+#     (   |   y;- -,-""'"-.\   \/
+#     )   / ./  ) /         `\  \
+#        |./   ( (           / /'
+#        ||     \\          //'|
+#        ||      \\       _//'||
+#        ||       ))     |_/  ||
+#        \_\     |_/          ||
+#        `'"                  \_\
+#                             `'"

+ 3 - 3
Controller/SensorGateway/GatewayDeviceController.py

@@ -12,7 +12,7 @@ from django.db import transaction
 from django.db.models import Q
 from django.views.generic.base import View
 
-from Ansjer.config import SMART_SCENE_TOPIC_NAME
+from Ansjer.Config.gatewaySensorConfig import SMART_SCENE_TOPIC
 from Controller.SensorGateway.EquipmentFamilyController import EquipmentFamilyView
 from Model.models import FamilyRoomDevice, FamilyRoom, GatewaySubDevice, Device_Info, UserFamily, FamilyMember, \
     UidSetModel, iotdeviceInfoModel, SmartScene, SceneLog
@@ -167,7 +167,7 @@ class GatewayDeviceView(View):
                         if smart_scene_qs.exists():
                             # 通知设备删除场景id
                             serial_number = device_qs.first().serial_number
-                            topic_name = SMART_SCENE_TOPIC_NAME.format(serial_number)
+                            topic_name = SMART_SCENE_TOPIC.format(serial_number)
                             smart_scene_info = smart_scene_qs.values('id')
                             for smart_scene in smart_scene_info:
                                 msg = {
@@ -193,7 +193,7 @@ class GatewayDeviceView(View):
                         sub_device_qs = GatewaySubDevice.objects.filter(id=sub_id).values('device_type', 'src_addr',
                                                                                           'device__serial_number')
                         serial_number = sub_device_qs[0]['device__serial_number']
-                        topic_name = SMART_SCENE_TOPIC_NAME.format(serial_number)
+                        topic_name = SMART_SCENE_TOPIC.format(serial_number)
                         device_type = sub_device_qs[0]['device_type']
                         if device_type == 216:  # 删除智能按钮通知设备
                             msg = {

+ 32 - 29
Controller/SensorGateway/SmartSceneController.py

@@ -12,7 +12,7 @@ from django.db import transaction
 from django.db.models import F, Q, Count
 from django.views import View
 
-from Ansjer.config import SMART_SCENE_TOPIC_NAME
+from Ansjer.Config.gatewaySensorConfig import SMART_SCENE_TOPIC, SENSOR_TYPE, EVENT_TYPE
 from Model.models import FamilyRoomDevice, GatewaySubDevice, FamilyRoom, SmartScene, EffectiveTime, Device_Info, \
     SceneLog
 from Object.ResponseObject import ResponseObject
@@ -109,14 +109,13 @@ class SmartSceneView(View):
                 device_id = sub_device_qs[0]['device_id']
                 device_type = sub_device_qs[0]['device_type']
 
-                if device_type != 216:  # 非智能按钮只返回网关
+                if device_type != SENSOR_TYPE['smart_button']:  # 非智能按钮只返回网关
                     res = [cls.get_gateway_data(device_id)]
                 else:
                     gateway_data = cls.get_gateway_data(device_id)
                     sub_device_qs = GatewaySubDevice.objects.filter(
-                        Q(device_id=device_id) & Q(device_type=215) | Q(device_type=219)).values('id', 'nickname',
-                                                                                                 'status',
-                                                                                                 'device_type')
+                        Q(device_id=device_id) & Q(device_type=SENSOR_TYPE['door_magnet']) | Q(
+                            device_type=SENSOR_TYPE['body_sensor'])).values('id', 'nickname', 'status', 'device_type')
                     if not sub_device_qs.exists():
                         return response.json(173)
                     res = cls.get_sub_device_room_name(sub_device_qs, gateway_data)
@@ -243,9 +242,9 @@ class SmartSceneView(View):
                 if not sub_device_id:
                     return response.json(444, {'error param': 'subDeviceId'})
 
-                device_type = conditions_dict['sensor']['device_type']
+                device_type = int(conditions_dict['sensor']['device_type'])
                 # 智能按钮不能创建触发条件相同的场景
-                if device_type == '216':
+                if device_type == SENSOR_TYPE['smart_button']:
                     event_type = conditions_dict['sensor']['eventValues'][0]['event_type']
                     smart_scene_qs = SmartScene.objects.filter(sub_device_id=sub_device_id,
                                                                conditions__contains=event_type)
@@ -253,7 +252,7 @@ class SmartSceneView(View):
                         return response.json(180)
 
                 # 温湿度传感器返回温湿度
-                elif device_type == '220':
+                elif device_type == SENSOR_TYPE['tem_hum_sensor']:
                     event_values = conditions_dict['sensor']['eventValues'][0]
                     if '≥' in event_values['value']:
                         replace_str = '≥ '
@@ -332,7 +331,7 @@ class SmartSceneView(View):
                 smart_scene_qs.save()
                 # 发布MQTT消息通知网关设备
                 thing_name = serial_number
-                topic_name = SMART_SCENE_TOPIC_NAME.format(serial_number)
+                topic_name = SMART_SCENE_TOPIC.format(serial_number)
 
                 success = CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg, 0)
                 try:
@@ -390,14 +389,17 @@ class SmartSceneView(View):
             return response.json(444, {'error param': 'subDeviceId'})
         try:
             click_scene_qs = SmartScene.objects.filter(user_id=user_id, sub_device_id=sub_device_id,
-                                                       conditions__contains='2161').values('id', 'scene_name',
-                                                                                           'is_enable')
+                                                       conditions__contains=str(
+                                                           EVENT_TYPE['smart_button_click'])).values('id', 'scene_name',
+                                                                                                     'is_enable')
             double_click_scene_qs = SmartScene.objects.filter(user_id=user_id, sub_device_id=sub_device_id,
-                                                              conditions__contains='2162').values('id', 'scene_name',
-                                                                                                  'is_enable')
-            press_scene_qs = SmartScene.objects.filter(user_id=user_id, sub_device_id=sub_device_id,
-                                                       conditions__contains='2163').values('id', 'scene_name',
-                                                                                           'is_enable')
+                                                              conditions__contains=str(
+                                                                  EVENT_TYPE['smart_button_double_click'])). \
+                values('id', 'scene_name', 'is_enable')
+            three_click_scene_qs = SmartScene.objects.filter(user_id=user_id, sub_device_id=sub_device_id,
+                                                             conditions__contains=str(
+                                                                 EVENT_TYPE['smart_button_three_click'])). \
+                values('id', 'scene_name', 'is_enable')
             scene_list = []
             if click_scene_qs.exists():
                 scene_list.append({
@@ -413,12 +415,12 @@ class SmartSceneView(View):
                     'scene_name': double_click_scene_qs[0]['scene_name'],
                     'is_enable': double_click_scene_qs[0]['is_enable']
                 })
-            if press_scene_qs.exists():
+            if three_click_scene_qs.exists():
                 scene_list.append({
                     'trigger_type': 3,
-                    'id': press_scene_qs[0]['id'],
-                    'scene_name': press_scene_qs[0]['scene_name'],
-                    'is_enable': press_scene_qs[0]['is_enable']
+                    'id': three_click_scene_qs[0]['id'],
+                    'scene_name': three_click_scene_qs[0]['scene_name'],
+                    'is_enable': three_click_scene_qs[0]['is_enable']
                 })
             return response.json(0, scene_list)
         except Exception as e:
@@ -456,7 +458,7 @@ class SmartSceneView(View):
                 serial_number = GatewaySubDevice.objects.filter(id=sub_device_id).values('device__serial_number')[0][
                     'device__serial_number']
 
-            topic_name = SMART_SCENE_TOPIC_NAME.format(serial_number)
+            topic_name = SMART_SCENE_TOPIC.format(serial_number)
 
             with transaction.atomic():
                 SmartScene.objects.filter(id=smart_scene_id).update(is_enable=is_enable)
@@ -567,9 +569,9 @@ class SmartSceneView(View):
                 if not sub_device_id:
                     return response.json(444, {'error param': 'subDeviceId'})
 
-                device_type = conditions_dict['sensor']['device_type']
+                device_type = int(conditions_dict['sensor']['device_type'])
                 # 智能按钮不能创建触发条件相同的场景
-                if device_type == '216':
+                if device_type == SENSOR_TYPE['smart_button']:
                     event_type = conditions_dict['sensor']['eventValues'][0]['event_type']
                     smart_scene_temp_qs = SmartScene.objects.filter(Q(sub_device_id=sub_device_id),
                                                                     ~Q(id=smart_scene_id),
@@ -578,7 +580,7 @@ class SmartSceneView(View):
                         return response.json(180)
 
                 # 温湿度传感器返回温湿度
-                elif device_type == '220':
+                elif device_type == SENSOR_TYPE['tem_hum_sensor']:
                     event_values = conditions_dict['sensor']['eventValues'][0]
                     if '≥' in event_values['value']:
                         replace_str = '≥ '
@@ -665,7 +667,7 @@ class SmartSceneView(View):
 
             # 通过mqtt发送设备数据
             thing_name = serial_number
-            topic_name = SMART_SCENE_TOPIC_NAME.format(serial_number)
+            topic_name = SMART_SCENE_TOPIC.format(serial_number)
             success = CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg, 0)
             try:
                 assert success
@@ -702,7 +704,7 @@ class SmartSceneView(View):
             else:
                 serial_number = GatewaySubDevice.objects.filter(id=smart_scene_qs[0]['sub_device_id']). \
                     values('device__serial_number')[0]['device__serial_number']
-            topic_name = SMART_SCENE_TOPIC_NAME.format(serial_number)
+            topic_name = SMART_SCENE_TOPIC.format(serial_number)
             with transaction.atomic():
                 SmartScene.objects.filter(id__in=smart_scene_id_list).delete()
                 for smart_scene_id in smart_scene_id_list:
@@ -857,7 +859,7 @@ class SmartSceneView(View):
 
             # 下发智能场景数据
             smart_scene_qs = smart_scene_qs.values('device_data')
-            topic_name = SMART_SCENE_TOPIC_NAME.format(serial_number)
+            topic_name = SMART_SCENE_TOPIC.format(serial_number)
             for smart_scene in smart_scene_qs:
                 msg = eval(smart_scene['device_data'])
                 success = CommonService.req_publish_mqtt_msg(serial_number, topic_name, msg, 0)
@@ -868,8 +870,9 @@ class SmartSceneView(View):
                 time.sleep(1)
 
             # 下发智能按钮数据
-            smart_button_qs = GatewaySubDevice.objects.filter(device_id=device_id, device_type=216). \
-                values('src_addr', 'is_tampered')
+            smart_button_qs = GatewaySubDevice.objects.filter(device_id=device_id,
+                                                              device_type=SENSOR_TYPE['smart_button']).values(
+                'src_addr', 'is_tampered')
             if smart_button_qs.exists():
                 sos_count = smart_button_qs.count()
                 for index, smart_button in enumerate(smart_button_qs):

+ 17 - 16
Controller/SensorGateway/SubDeviceController.py

@@ -11,7 +11,7 @@ from django.db import transaction
 from django.db.models import Count
 from django.views import View
 
-from Ansjer.config import SMART_SCENE_TOPIC_NAME
+from Ansjer.Config.gatewaySensorConfig import SMART_SCENE_TOPIC, SENSOR_TYPE, EVENT_TYPE
 from Model.models import Device_Info, GatewaySubDevice, FamilyRoomDevice, SensorRecord, SmartScene, SceneLog
 from Service.CommonService import CommonService
 
@@ -92,10 +92,7 @@ class GatewaySubDeviceView(View):
         if not all([serial_number, device_type, nickname, src_addr, family_id]):
             return response.json(444)
         now_time = int(time.time())
-        if device_type == 216:
-            is_tampered = 1
-        else:
-            is_tampered = 0
+        is_tampered = 1 if device_type == SENSOR_TYPE['smart_button'] else 0  # 智能按钮默认开启紧急开关
         try:
             # 查询网关设备数据
             device_info_qs = Device_Info.objects.filter(userID_id=user_id, serial_number=serial_number).values('id')
@@ -105,7 +102,7 @@ class GatewaySubDeviceView(View):
             # 查询是否已添加过该子设备
             sub_device_qs = GatewaySubDevice.objects.filter(device_id=device_id, ieee_addr=ieee_addr)
             if sub_device_qs.exists():
-                sub_device_qs.update(src_addr=src_addr)     # 更新短地址
+                sub_device_qs.update(src_addr=src_addr)  # 更新短地址
                 return response.json(174)
 
             with transaction.atomic():
@@ -149,7 +146,7 @@ class GatewaySubDeviceView(View):
                 'mac': gateway_sub_device_qs[0]['mac'],
                 'sensor_serial': gateway_sub_device_qs[0]['sensor_serial'],
             }
-            if gateway_sub_device_qs[0]['device_type'] == 216:  # 智能按钮返回紧急开关状态
+            if gateway_sub_device_qs[0]['device_type'] == SENSOR_TYPE['smart_button']:  # 智能按钮返回紧急开关状态
                 res['emergency_status'] = gateway_sub_device_qs[0]['is_tampered']
             return response.json(0, res)
         except Exception as e:
@@ -230,7 +227,7 @@ class GatewaySubDeviceView(View):
         now_time = int(time.time())
         # 判断event_type
         event_type = int(event_type)
-        if event_type != 2200 and event_type != 2201:
+        if event_type != EVENT_TYPE['temperature'] and event_type != EVENT_TYPE['humidity']:
             return response.json(444, {'invalid eventType': event_type})
 
         try:
@@ -420,15 +417,18 @@ class GatewaySubDeviceView(View):
             }
 
             device_type = sub_device_qs[0]['device_type']
-            if device_type == 215 or device_type == 218 or device_type == 219:  # 门磁,烟雾,人体传感器返回拆动状态
+            # 门磁,烟雾,人体传感器返回拆动状态
+            if device_type == SENSOR_TYPE['door_magnet'] or device_type == SENSOR_TYPE['smoke_sensor'] or \
+                    device_type == SENSOR_TYPE['body_sensor']:
                 res['is_tampered'] = sub_device_qs[0]['is_tampered']
-            elif device_type == 220:  # 温湿度传感器返回温湿度数据
+            # 温湿度传感器返回温湿度数据
+            elif device_type == SENSOR_TYPE['tem_hum_sensor']:
                 tem_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
-                                                            event_type=2200).order_by('-created_time').values('alarm')[
-                                :1]
+                                                            event_type=EVENT_TYPE['temperature']).order_by(
+                    '-created_time').values('alarm')[:1]
                 hum_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
-                                                            event_type=2201).order_by('-created_time').values('alarm')[
-                                :1]
+                                                            event_type=EVENT_TYPE['humidity']).order_by(
+                    '-created_time').values('alarm')[:1]
                 temperature = tem_record_qs[0]['alarm'] if tem_record_qs.exists() else ''
                 humidity = hum_record_qs[0]['alarm'] if tem_record_qs.exists() else ''
                 res['temperature'] = temperature
@@ -464,9 +464,10 @@ class GatewaySubDeviceView(View):
                 # 获取序列号
                 sub_device_qs = sub_device_qs.values('device_id', 'src_addr', 'device__serial_number')
                 serial_number = sub_device_qs[0]['device__serial_number']
-                topic_name = SMART_SCENE_TOPIC_NAME.format(serial_number)
+                topic_name = SMART_SCENE_TOPIC.format(serial_number)
                 gateway_sub_device_qs = GatewaySubDevice.objects.filter(device_id=sub_device_qs[0]['device_id'],
-                                                                        device_type=216).values('src_addr')
+                                                                        device_type=SENSOR_TYPE['smart_button']).values(
+                    'src_addr')
                 sos_count = gateway_sub_device_qs.count()
                 sos_num = 1
                 for index, gateway_sub_device in enumerate(gateway_sub_device_qs):