SmartSceneController.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. # -*- coding: utf-8 -*-
  2. """
  3. @Author : Rocky
  4. @Time : 2022/6/29 9:31
  5. @File :SmartSceneController.py
  6. """
  7. import json
  8. import time
  9. from django.core.exceptions import ObjectDoesNotExist
  10. from django.db import transaction
  11. from django.db.models import F, Q
  12. from django.views import View
  13. from Model.models import FamilyRoomDevice, GatewaySubDevice, FamilyRoom, SmartScene, EffectiveTime, Device_Info, \
  14. SceneLog
  15. from Service.CommonService import CommonService
  16. class SmartSceneView(View):
  17. def get(self, request, *args, **kwargs):
  18. request.encoding = 'utf-8'
  19. operation = kwargs.get('operation')
  20. return self.validation(request.GET, request, operation)
  21. def post(self, request, *args, **kwargs):
  22. request.encoding = 'utf-8'
  23. operation = kwargs.get('operation')
  24. return self.validation(request.POST, request, operation)
  25. def validation(self, request_dict, request, operation):
  26. token_code, user_id, response = CommonService.verify_token_get_user_id(request_dict, request)
  27. if token_code != 0:
  28. return response.json(token_code)
  29. if operation == 'condition-devices': # 添加条件-查询设备
  30. return self.condition_devices(request_dict, response)
  31. elif operation == 'task-devices': # 添加任务-查询设备
  32. return self.task_devices(request_dict, response)
  33. elif operation == 'create': # 创建智能场景
  34. return self.create_smart_scene(request_dict, user_id, response)
  35. elif operation == 'scene-list': # 查询智能场景列表
  36. return self.scene_list(request_dict, user_id, response)
  37. elif operation == 'update-status': # 更新智能场景状态
  38. return self.update_status(request_dict, response)
  39. elif operation == 'detail': # 查询智能场景详情
  40. return self.scene_detail(request_dict, response)
  41. elif operation == 'edit': # 编辑智能场景
  42. return self.edit_smart_scene(request_dict, user_id, response)
  43. elif operation == 'delete': # 删除智能场景
  44. return self.delete_smart_scene(request_dict, response)
  45. elif operation == 'log': # 查询智能场景日志
  46. return self.scene_log(request_dict, response)
  47. else:
  48. return response.json(414)
  49. @classmethod
  50. def condition_devices(cls, request_dict, response):
  51. """
  52. 添加条件-查询设备
  53. @param request_dict: 请求参数
  54. @request_dict deviceId: 网关设备id
  55. @request_dict subDeviceId: 子设备id
  56. @param response: 响应对象
  57. @return: response
  58. """
  59. device_id = request_dict.get('deviceId', None)
  60. sub_device_id = request_dict.get('subDeviceId', None)
  61. if not any([device_id, sub_device_id]):
  62. return response.json(444, {'error param': 'deviceId or subDeviceId'})
  63. try:
  64. if sub_device_id:
  65. device_id = GatewaySubDevice.objects.get(id=sub_device_id).device_id
  66. gateway_sub_device_qs = GatewaySubDevice.objects.filter(device_id=device_id)
  67. if not gateway_sub_device_qs.exists():
  68. return response.json(173)
  69. res = cls.get_sub_device_room_name(gateway_sub_device_qs)
  70. return response.json(0, res)
  71. except Exception as e:
  72. return response.json(500, repr(e))
  73. @classmethod
  74. def task_devices(cls, request_dict, response):
  75. """
  76. 添加任务-查询设备
  77. @param request_dict: 请求参数
  78. @request_dict deviceId: 网关设备id
  79. @param response: 响应对象
  80. @return: response
  81. """
  82. sub_device_id = request_dict.get('subDeviceId', None)
  83. device_id = request_dict.get('deviceId', None)
  84. if not any([device_id, sub_device_id]):
  85. return response.json(444, {'error param': 'deviceId or subDeviceId'})
  86. try:
  87. if device_id:
  88. res = [cls.get_gateway_data(device_id)]
  89. else:
  90. sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('device_id', 'device_type')
  91. device_id = sub_device_qs[0]['device_id']
  92. device_type = sub_device_qs[0]['device_type']
  93. if device_type != 216: # 非智能按钮只返回网关
  94. res = [cls.get_gateway_data(device_id)]
  95. else:
  96. gateway_data = cls.get_gateway_data(device_id)
  97. sub_device_qs = GatewaySubDevice.objects.filter(
  98. Q(device_id=device_id) & Q(device_type=215) | Q(device_type=219)).values('id', 'nickname',
  99. 'status',
  100. 'device_type')
  101. if not sub_device_qs.exists():
  102. return response.json(173)
  103. res = cls.get_sub_device_room_name(sub_device_qs, gateway_data)
  104. return response.json(0, res)
  105. except Exception as e:
  106. return response.json(500, repr(e))
  107. @staticmethod
  108. def get_sub_device_room_name(sub_device_qs, gateway_data=None):
  109. """
  110. 获取房间名称
  111. @param sub_device_qs: 子设备信息
  112. @param gateway_data: 网关参数
  113. @return: sub_device_list
  114. """
  115. sub_device_list = []
  116. if gateway_data:
  117. sub_device_list.append(gateway_data)
  118. sub_device_qs = sub_device_qs.annotate(gatewaySubId=F('id'),
  119. deviceType=F('device_type'),
  120. deviceNickName=F('nickname')). \
  121. values('gatewaySubId', 'deviceType', 'deviceNickName', 'status')
  122. for sub_device in sub_device_qs:
  123. family_room_device_qs = FamilyRoomDevice.objects.filter(sub_device=sub_device['gatewaySubId']). \
  124. values('room_id')
  125. if not family_room_device_qs.exists():
  126. sub_device['roomName'] = ''
  127. else:
  128. room_id = family_room_device_qs[0]['room_id']
  129. try:
  130. sub_device['roomName'] = FamilyRoom.objects.get(id=room_id).name
  131. except ObjectDoesNotExist:
  132. sub_device['roomName'] = ''
  133. sub_device_list.append(sub_device)
  134. return sub_device_list
  135. @staticmethod
  136. def get_gateway_data(device_id):
  137. """
  138. 获取网关数据
  139. @param device_id: 网关设备id
  140. @return: res
  141. """
  142. device_info_qs = Device_Info.objects.filter(id=device_id).values('NickName', 'Type')
  143. nickname = device_info_qs[0]['NickName']
  144. device_type = device_info_qs[0]['Type']
  145. room_id = FamilyRoomDevice.objects.filter(device_id=device_id).values('room_id')[0]['room_id']
  146. room_id_qs = FamilyRoom.objects.filter(id=room_id).values('name')
  147. room_name = room_id_qs.first()['name'] if room_id_qs.exists() else ''
  148. res = {
  149. 'deviceNickName': nickname,
  150. 'deviceType': device_type,
  151. 'roomName': room_name,
  152. 'status': 1,
  153. }
  154. return res
  155. @staticmethod
  156. def create_smart_scene(request_dict, user_id, response):
  157. """
  158. 创建智能场景
  159. @param request_dict: 请求参数
  160. @param user_id: 用户id
  161. @request_dict deviceId: 网关设备id
  162. @request_dict subDeviceId: 子设备id
  163. @request_dict sceneName: 场景名称
  164. @request_dict conditions: 条件
  165. @request_dict tasks: 任务
  166. @request_dict isAllDay: 是否全天执行
  167. @request_dict startTime: 开始时间
  168. @request_dict endTime: 结束时间
  169. @request_dict repeat: 重复周期
  170. @param response: 响应对象
  171. @return: response
  172. """
  173. device_id = request_dict.get('deviceId', None)
  174. sub_device_id = request_dict.get('subDeviceId', None)
  175. scene_name = request_dict.get('sceneName', None)
  176. conditions = request_dict.get('conditions', None)
  177. tasks = request_dict.get('tasks', None)
  178. is_all_day = request_dict.get('isAllDay', None)
  179. if not all([scene_name, conditions, tasks]):
  180. return response.json(444, {'error param': 'scene_name and conditions and tasks'})
  181. now_time = int(time.time())
  182. conditions_dict = eval(conditions)
  183. tasks_list = eval(tasks)
  184. try:
  185. # 判断是否已存在该场景名
  186. smart_scene_qs = SmartScene.objects.filter(user_id=user_id, scene_name=scene_name)
  187. if smart_scene_qs.exists():
  188. return response.json(174)
  189. smart_scene_dict = {
  190. 'user_id': user_id,
  191. 'scene_name': scene_name,
  192. 'conditions': conditions,
  193. 'tasks': tasks,
  194. 'created_time': now_time,
  195. 'updated_time': now_time,
  196. }
  197. msg = {}
  198. # 处理设置时间
  199. if is_all_day is not None:
  200. is_all_day = int(is_all_day)
  201. smart_scene_dict['is_all_day'] = is_all_day
  202. # 处理传网关设备id和子设备id的情况
  203. if conditions_dict['type'] == 1: # 网关设置时间
  204. if not device_id:
  205. return response.json(444, {'error param': 'deviceId'})
  206. smart_scene_dict['device_id'] = device_id
  207. device_info_qs = Device_Info.objects.filter(id=device_id).values('serial_number')
  208. if not device_info_qs.exists():
  209. return response.json(173)
  210. serial_number = device_info_qs[0]['serial_number']
  211. else: # 子设备设置场景
  212. if not sub_device_id:
  213. return response.json(444, {'error param': 'subDeviceId'})
  214. smart_scene_dict['sub_device_id'] = sub_device_id
  215. sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('device__serial_number',
  216. 'src_addr')
  217. if not sub_device_qs.exists():
  218. return response.json(173)
  219. serial_number = sub_device_qs[0]['device__serial_number']
  220. msg['scene_status'] = 1
  221. msg['sensor_type'] = conditions_dict['sensor']['device_type']
  222. msg['sensor_src'] = int(sub_device_qs[0]['src_addr'], 16)
  223. msg['sensor_status'] = int(conditions_dict['sensor']['eventValues'][0]['event_type'])
  224. with transaction.atomic():
  225. if is_all_day is None: # 不设置时间
  226. smart_scene_qs = SmartScene.objects.create(**smart_scene_dict)
  227. # 设备的time数据
  228. hour, minute = conditions_dict['time']['minutes'] / 60, conditions_dict['time']['minutes'] % 60
  229. start_time = int(hour * 3600 + minute * 60)
  230. time_dict = {
  231. 'start_time': start_time,
  232. 'repeat': conditions_dict['time']['repeat']
  233. }
  234. elif is_all_day == 1: # 全天
  235. smart_scene_qs = SmartScene.objects.create(**smart_scene_dict)
  236. # 设备的time数据
  237. time_dict = {
  238. 'is_all_day': is_all_day
  239. }
  240. elif is_all_day == 2: # 非全天
  241. start_time = int(request_dict.get('startTime', None))
  242. end_time = int(request_dict.get('endTime', None))
  243. repeat = int(request_dict.get('repeat', None))
  244. effective_time_qs = EffectiveTime.objects.filter(start_time=start_time, end_time=end_time,
  245. repeat=repeat).values('id')
  246. if effective_time_qs.exists():
  247. effective_time_id = effective_time_qs[0]['id']
  248. else:
  249. effective_time_id = EffectiveTime.objects.create(start_time=start_time, end_time=end_time,
  250. repeat=repeat).id
  251. smart_scene_dict['effective_time_id'] = effective_time_id
  252. smart_scene_qs = SmartScene.objects.create(**smart_scene_dict)
  253. # 设备的time数据
  254. start_hour, start_minute, end_hour, end_minute = \
  255. start_time / 60, start_time % 60, end_time / 60, end_time % 60
  256. start_time, end_time = \
  257. int(start_hour * 3600 + start_minute * 60), \
  258. int(end_hour * 3600 + end_minute * 60)
  259. time_dict = {
  260. 'is_all_day': is_all_day,
  261. 'start_time': start_time,
  262. 'end_time': end_time,
  263. 'repeat': repeat
  264. }
  265. else:
  266. return response.json(444, {'error param': 'invalid isAllDay'})
  267. msg['time'] = time_dict
  268. msg['smart_scene_id'] = smart_scene_qs.id
  269. task_list = []
  270. for task in tasks_list:
  271. task_temp = {
  272. 'sensor_type': task['device_type'],
  273. 'sensor_action': task['event_type']
  274. }
  275. sub_device_id = task.get('subDeviceId', None)
  276. if sub_device_id:
  277. sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('src_addr').first()
  278. task_temp['sensor_src'] = int(sub_device_qs['src_addr'], 16)
  279. task_list.append(task_temp)
  280. msg['task'] = task_list
  281. smart_scene_qs.device_data = json.dumps(msg)
  282. smart_scene_qs.save()
  283. # 发布MQTT消息通知网关设备
  284. thing_name = serial_number
  285. topic_name = 'loocam/gateway_sensor/smart_scene/{}'.format(serial_number)
  286. success = CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg)
  287. try:
  288. assert success
  289. except AssertionError:
  290. return response.json(10044)
  291. return response.json(0)
  292. except Exception as e:
  293. return response.json(500, repr(e))
  294. @staticmethod
  295. def scene_list(request_dict, user_id, response):
  296. """
  297. 查询智能场景列表
  298. @param request_dict: 请求参数
  299. @param user_id: 用户id
  300. @request_dict deviceId: 网关设备id
  301. @request_dict subDeviceId: 子设备id
  302. @param response: 响应对象
  303. @return: response
  304. """
  305. device_id = request_dict.get('deviceId', None)
  306. sub_device_id = request_dict.get('subDeviceId', None)
  307. if not any([device_id, sub_device_id]):
  308. return response.json(444, {'error param': 'deviceId or subDeviceId'})
  309. try:
  310. if device_id:
  311. sub_device_id = GatewaySubDevice.objects.filter(device_id=device_id).values('id')
  312. smart_scene_qs = SmartScene.objects.filter(
  313. Q(user_id=user_id) & Q(device_id=device_id) | Q(sub_device_id__in=sub_device_id))
  314. else:
  315. smart_scene_qs = SmartScene.objects.filter(user_id=user_id, sub_device_id=sub_device_id)
  316. if not smart_scene_qs.exists():
  317. return response.json(173)
  318. smart_scene_qs = smart_scene_qs.values('id', 'scene_name', 'is_enable')
  319. return response.json(0, list(smart_scene_qs))
  320. except Exception as e:
  321. return response.json(500, repr(e))
  322. @staticmethod
  323. def update_status(request_dict, response):
  324. """
  325. 更新智能场景状态
  326. @param request_dict: 请求参数
  327. @request_dict smartSceneId: 智能场景id
  328. @request_dict isEnable: 状态,True or False
  329. @param response: 响应对象
  330. @return: response
  331. """
  332. smart_scene_id = request_dict.get('smartSceneId', None)
  333. is_enable = request_dict.get('isEnable', None)
  334. if not all([smart_scene_id, is_enable]):
  335. return response.json(444, {'error param': 'smartSceneId and status'})
  336. try:
  337. SmartScene.objects.filter(id=smart_scene_id).update(is_enable=is_enable)
  338. return response.json(0)
  339. except Exception as e:
  340. return response.json(500, repr(e))
  341. @staticmethod
  342. def scene_detail(request_dict, response):
  343. """
  344. 查询智能场景详情
  345. @param request_dict: 请求参数
  346. @request_dict smartSceneId: 智能场景id
  347. @param response: 响应对象
  348. @return: response
  349. """
  350. smart_scene_id = request_dict.get('smartSceneId', None)
  351. if not smart_scene_id:
  352. return response.json(444, {'error param': 'smartSceneId'})
  353. try:
  354. smart_scene_qs = SmartScene.objects.filter(id=smart_scene_id).values('id', 'scene_name', 'conditions',
  355. 'tasks', 'effective_time_id',
  356. 'is_all_day')
  357. if not smart_scene_qs.exists():
  358. return response.json(173)
  359. res = {
  360. 'scene_name': smart_scene_qs[0]['scene_name'],
  361. 'condition': eval(smart_scene_qs[0]['conditions']),
  362. 'task': eval(smart_scene_qs[0]['tasks']),
  363. }
  364. # 如果存在关联的时间数据,组织时间数据
  365. is_all_day = smart_scene_qs[0]['is_all_day']
  366. effectiveTime = {}
  367. if is_all_day != 0:
  368. effectiveTime['isAllDay'] = is_all_day
  369. if is_all_day == 2:
  370. try:
  371. effective_time_qs = EffectiveTime.objects.get(id=smart_scene_qs[0]['effective_time_id'])
  372. effectiveTime['startTime'] = effective_time_qs.start_time
  373. effectiveTime['endTime'] = effective_time_qs.end_time
  374. effectiveTime['repeat'] = effective_time_qs.repeat
  375. except ObjectDoesNotExist:
  376. return response.json(0, res)
  377. res['effectiveTime'] = effectiveTime
  378. return response.json(0, res)
  379. except Exception as e:
  380. return response.json(500, repr(e))
  381. @staticmethod
  382. def edit_smart_scene(request_dict, user_id, response):
  383. """
  384. 编辑智能场景
  385. @param request_dict: 请求参数
  386. @request_dict smartSceneId: 智能场景id
  387. @param response: 响应对象
  388. @return: response
  389. """
  390. smart_scene_id = request_dict.get('smartSceneId', None)
  391. device_id = request_dict.get('deviceId', None)
  392. sub_device_id = request_dict.get('subDeviceId', None)
  393. scene_name = request_dict.get('sceneName', None)
  394. conditions = request_dict.get('conditions', None)
  395. tasks = request_dict.get('tasks', None)
  396. is_all_day = request_dict.get('isAllDay', None)
  397. conditions_dict = eval(conditions)
  398. tasks_list = eval(tasks)
  399. now_time = int(time.time())
  400. smart_scene_qs = SmartScene.objects.filter(user_id=user_id, scene_name=scene_name).filter(~Q(id=smart_scene_id))
  401. if smart_scene_qs.exists():
  402. return response.json(174)
  403. res = {
  404. 'scene_name': scene_name,
  405. 'conditions': conditions_dict,
  406. 'tasks': tasks_list
  407. }
  408. effective_time = {}
  409. if is_all_day:
  410. is_all_day = int(is_all_day)
  411. effective_time['is_all_day'] = is_all_day
  412. else:
  413. is_all_day = 0
  414. if not all([smart_scene_id, scene_name, conditions, tasks]):
  415. return response.json(444, {'error param': 'smartSceneId,sceneName,conditions or tasks'})
  416. try:
  417. smart_scene_qs = SmartScene.objects.filter(id=smart_scene_id)
  418. if not smart_scene_qs.exists():
  419. return response.json(173)
  420. msg = {}
  421. if conditions_dict['type'] == 2: # 条件为选择子设备
  422. if not sub_device_id:
  423. return response.json(444, {'error param': 'subDeviceId'})
  424. device_id = ''
  425. sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id).values('src_addr').first()
  426. msg['smart_scene_id'] = smart_scene_id
  427. msg['scene_status'] = 1
  428. msg['sensor_type'] = conditions_dict['sensor']['device_type']
  429. msg['sensor_src'] = int(sub_device_qs['src_addr'], 16)
  430. msg['sensor_status'] = int(conditions_dict['sensor']['eventValues'][0]['event_type'])
  431. else:
  432. if not device_id:
  433. return response.json(444, {'error param': 'deviceId'})
  434. sub_device_id = 0
  435. task_list = []
  436. for task in tasks_list:
  437. task_temp = {
  438. 'sensor_type': task['device_type'],
  439. 'sensor_action': task['event_type']
  440. }
  441. task_sub_device_id = task.get('subDeviceId', None)
  442. if task_sub_device_id:
  443. sub_device_qs = GatewaySubDevice.objects.filter(id=task_sub_device_id).values('src_addr').first()
  444. task_temp['sensor_src'] = int(sub_device_qs['src_addr'], 16)
  445. task_list.append(task_temp)
  446. msg['task'] = task_list
  447. with transaction.atomic():
  448. smart_scene_qs.update(scene_name=scene_name, conditions=conditions, tasks=tasks,
  449. device_data=json.dumps(msg), updated_time=now_time, device_id=device_id,
  450. sub_device_id=sub_device_id)
  451. if is_all_day == 0 or is_all_day == 1: # 不设置时间或全天
  452. smart_scene_qs.update(effective_time_id=0, is_all_day=is_all_day)
  453. else:
  454. start_time = int(request_dict.get('startTime', None))
  455. end_time = int(request_dict.get('endTime', None))
  456. repeat = int(request_dict.get('repeat', None))
  457. effective_time_qs = EffectiveTime.objects.filter(start_time=start_time, end_time=end_time,
  458. repeat=repeat).values('id')
  459. if effective_time_qs.exists():
  460. effective_time_id = effective_time_qs[0]['id']
  461. else:
  462. effective_time_id = EffectiveTime.objects.create(start_time=start_time, end_time=end_time,
  463. repeat=repeat).id
  464. smart_scene_qs.update(effective_time_id=effective_time_id, is_all_day=is_all_day)
  465. effective_time = {
  466. 'isAllDay': is_all_day,
  467. 'startTime': start_time,
  468. 'endTime': end_time,
  469. 'repeat': repeat
  470. }
  471. res['effectiveTime'] = effective_time
  472. return response.json(0, res)
  473. except Exception as e:
  474. return response.json(500, repr(e))
  475. @staticmethod
  476. def delete_smart_scene(request_dict, response):
  477. """
  478. 删除智能场景
  479. @param request_dict: 请求参数
  480. @request_dict smartSceneIds: 智能场景id
  481. @param response: 响应对象
  482. @return: response
  483. """
  484. smart_scene_ids = request_dict.get('smartSceneIds', None)
  485. if not smart_scene_ids:
  486. return response.json(444, {'error param': 'smartSceneIds'})
  487. try:
  488. SmartScene.objects.filter(id__in=smart_scene_ids.split(',')).delete()
  489. except Exception as e:
  490. return response.json(500, repr(e))
  491. else:
  492. return response.json(0)
  493. @staticmethod
  494. def scene_log(request_dict, response):
  495. """
  496. 查询场景日志
  497. @param request_dict: 请求参数
  498. @request_dict gatewaySubId: 子设备id
  499. @request_dict page: 页数
  500. @request_dict size: 条数
  501. @request_dict startTime: 开始时间
  502. @request_dict endTime: 结束时间
  503. @param response: 响应对象
  504. @return: response
  505. """
  506. device_id = request_dict.get('deviceId', None)
  507. sub_device_id = request_dict.get('subDeviceId', None)
  508. page = request_dict.get('page', None)
  509. size = request_dict.get('size', None)
  510. start_time = request_dict.get('startTime', None)
  511. end_time = request_dict.get('endTime', None)
  512. if not any([device_id, sub_device_id]):
  513. return response.json(444, {'error param': 'deviceId or subDeviceId'})
  514. if not all([page, size]):
  515. return response.json(444, {'error param': 'page or size'})
  516. device_dict = {}
  517. if device_id:
  518. device_dict['device_id'] = device_id
  519. else:
  520. device_dict['sub_device_id'] = sub_device_id
  521. try:
  522. page, size = int(page), int(size)
  523. if start_time and end_time:
  524. sensor_record_qs = SceneLog.objects.filter(**device_dict, created_time__range=(start_time, end_time)). \
  525. values('scene_id', 'scene_log', 'status', 'created_time').order_by(
  526. '-created_time')[(page - 1) * size:page * size]
  527. else:
  528. sensor_record_qs = SceneLog.objects.filter(**device_dict).values('scene_id', 'scene_log', 'status',
  529. 'created_time').order_by(
  530. '-created_time')[(page - 1) * size:page * size]
  531. if not sensor_record_qs.exists():
  532. return response.json(0, [])
  533. for item in sensor_record_qs:
  534. scene_id = item['scene_id']
  535. scene_name = SmartScene.objects.filter(id=scene_id)[0].scene_name
  536. item['scene_name'] = scene_name
  537. return response.json(0, list(sensor_record_qs))
  538. except Exception as e:
  539. return response.json(500, repr(e))