|
@@ -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):
|