SmartSceneController.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # -*- coding: utf-8 -*-
  2. """
  3. @Author : Rocky
  4. @Time : 2022/6/29 9:31
  5. @File :SmartSceneController.py
  6. """
  7. import time
  8. from django.core.exceptions import ObjectDoesNotExist
  9. from django.views import View
  10. from Model.models import FamilyRoomDevice, GatewaySubDevice, FamilyRoom, SmartScene, EffectiveTime
  11. from Object.ResponseObject import ResponseObject
  12. from Object.TokenObject import TokenObject
  13. class SmartSceneView(View):
  14. def get(self, request, *args, **kwargs):
  15. request.encoding = 'utf-8'
  16. operation = kwargs.get('operation')
  17. return self.validation(request.GET, request, operation)
  18. def post(self, request, *args, **kwargs):
  19. request.encoding = 'utf-8'
  20. operation = kwargs.get('operation')
  21. return self.validation(request.POST, request, operation)
  22. def validation(self, request_dict, request, operation):
  23. token_obj = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
  24. lang = request_dict.get('lang', None)
  25. response = ResponseObject(lang if lang else token_obj.lang)
  26. if token_obj.code != 0:
  27. return response.json(token_obj.code)
  28. user_id = token_obj.userID
  29. if operation == 'condition-devices': # 添加条件-查询设备
  30. return self.condition_devices(request_dict, response)
  31. if operation == 'task-devices': # 添加任务-查询设备
  32. return self.task_devices(request_dict, user_id, response)
  33. elif operation == 'get-device-information':
  34. return self.get_device_information(request_dict, response)
  35. elif operation == 'create': # 创建智能场景
  36. return self.create_smart_scene(request_dict, user_id, response)
  37. else:
  38. return response.json(414)
  39. @staticmethod
  40. def condition_devices(request_dict, response):
  41. """
  42. 添加条件-查询设备
  43. @param request_dict: 请求参数
  44. @request_dict deviceId: 网关设备id
  45. @request_dict subDeviceId: 子设备id
  46. @param response: 响应对象
  47. @return: response
  48. """
  49. device_id = request_dict.get('deviceId', None)
  50. sub_device_id = request_dict.get('subDeviceId', None)
  51. if not any([device_id, sub_device_id]):
  52. return response.json(444, {'error param': 'deviceId or subDeviceId'})
  53. try:
  54. if device_id:
  55. gateway_sub_device_qs = GatewaySubDevice.objects.filter(device_id=device_id)
  56. else:
  57. gateway_sub_device_qs = GatewaySubDevice.objects.filter(id=sub_device_id)
  58. if not gateway_sub_device_qs.exists():
  59. return response.json(173)
  60. gateway_sub_device_qs = gateway_sub_device_qs.values('id', 'device_type', 'nickname', 'status')
  61. sub_device_list = []
  62. for gateway_sub_device in gateway_sub_device_qs:
  63. room_id = FamilyRoomDevice.objects.filter(sub_device=gateway_sub_device['id']).values('room_id')[0][
  64. 'room_id']
  65. try:
  66. gateway_sub_device['room_name'] = FamilyRoom.objects.get(id=room_id).name
  67. except ObjectDoesNotExist:
  68. gateway_sub_device['room_name'] = ''
  69. gateway_sub_device.pop('id')
  70. sub_device_list.append(gateway_sub_device)
  71. return response.json(0, sub_device_list)
  72. except Exception as e:
  73. return response.json(500, repr(e))
  74. @staticmethod
  75. def task_devices(request_dict, user_id, response):
  76. """
  77. 添加任务-查询设备
  78. @param request_dict: 请求参数
  79. @param user_id: 用户id
  80. @request_dict deviceId: 网关设备id
  81. @param response: 响应对象
  82. @return: response
  83. """
  84. device_id = request_dict.get('deviceId', None)
  85. if not all([device_id]):
  86. return response.json(444)
  87. try:
  88. device_info_qs = GatewaySubDevice.objects.filter(device__userID_id=user_id)
  89. return response.json(0)
  90. except Exception as e:
  91. return response.json(500, repr(e))
  92. @staticmethod
  93. def create_smart_scene(request_dict, user_id, response):
  94. """
  95. 创建智能场景
  96. @param request_dict: 请求参数
  97. @param user_id: 用户id
  98. @request_dict deviceId: 网关设备id
  99. @request_dict gatewaySubId: 子设备id
  100. @request_dict sceneName: 场景名称
  101. @request_dict conditions: 条件
  102. @request_dict tasks: 任务
  103. @request_dict isAllDay: 是否全天执行
  104. @request_dict startTime: 开始时间
  105. @request_dict endTime: 结束时间
  106. @request_dict repeat: 重复周期
  107. @param response: 响应对象
  108. @return: response
  109. """
  110. device_id = request_dict.get('deviceId', None)
  111. sub_device_id = request_dict.get('gatewaySubId', None)
  112. scene_name = request_dict.get('sceneName', None)
  113. conditions = request_dict.get('conditions', None)
  114. tasks = request_dict.get('tasks', None)
  115. is_all_day = request_dict.get('isAllDay', None)
  116. if not any([device_id, sub_device_id]):
  117. return response.json(444, {'error param': 'deviceId or subDeviceId'})
  118. if not all([conditions, tasks]):
  119. return response.json(444, {'error param': 'conditions and tasks'})
  120. now_time = int(time.time())
  121. try:
  122. # 判断是否已存在该场景名
  123. smart_scene_qs = SmartScene.objects.filter(user_id=user_id, scene_name=scene_name)
  124. if smart_scene_qs.exists():
  125. return response.json(174)
  126. smart_scene_dict = {
  127. 'user_id': user_id,
  128. 'scene_name': scene_name,
  129. 'conditions': conditions,
  130. 'tasks': tasks,
  131. 'created_time': now_time,
  132. 'updated_time': now_time,
  133. }
  134. if device_id:
  135. smart_scene_dict['device_id'] = device_id
  136. else:
  137. smart_scene_dict['sub_device_id'] = sub_device_id
  138. if is_all_day:
  139. SmartScene.objects.create(**smart_scene_dict)
  140. else:
  141. start_time = request_dict.get('startTime', None)
  142. end_time = request_dict.get('endTime', None)
  143. repeat = request_dict.get('repeat', None)
  144. if not all([start_time, end_time, repeat]):
  145. return response.json(444, {'error param': 'startTime and endTime and repeat'})
  146. effective_time_qs = EffectiveTime.objects.filter(start_time=start_time, end_time=end_time,
  147. repeat=repeat).values('id')
  148. if effective_time_qs.exists():
  149. effective_time_id = effective_time_qs[0]['id']
  150. else:
  151. effective_time_id = EffectiveTime.objects.create(start_time=start_time, end_time=end_time,
  152. repeat=repeat).id
  153. smart_scene_dict['effective_time_id'] = effective_time_id
  154. SmartScene.objects.create(**smart_scene_dict)
  155. return response.json(0)
  156. except Exception as e:
  157. return response.json(500, repr(e))
  158. @staticmethod
  159. def get_device_information(request_dict, response):
  160. device_id = request_dict.get('deviceId', None)
  161. if not device_id:
  162. return response.json(444)
  163. device_qs = FamilyRoomDevice.objects.filter(device_id=device_id, sub_device=0)
  164. if not device_qs.exists():
  165. return response.json(173)
  166. gate_way = GatewaySubDevice.objects.filter(device_id=device_id)
  167. if not gate_way.exists():
  168. return response.json(173)
  169. try:
  170. gate_way = gate_way.values('id', 'device_type', 'nickname', 'status')
  171. gate_way_list = []
  172. for gate_int in gate_way:
  173. gate_way.exists()
  174. gate_way_list.append(gate_int)
  175. device_qs = device_qs.values('family_id', 'device_id', 'room_id', 'device__Type', 'device__NickName',
  176. 'device__UID')
  177. device_qs_list = []
  178. for device_int in device_qs:
  179. device_qs.exists()
  180. device_qs_list.append(device_int)
  181. device_qs = device_qs.first()
  182. family_id = device_qs['family_id']
  183. room_id = device_qs['room_id']
  184. room_qs = FamilyRoom.objects.filter(id=room_id).values('name')
  185. room_qs_list = []
  186. for room_int in room_qs:
  187. room_qs.exists()
  188. room_qs_list.append(room_int)
  189. repe = {'device_qs_list': device_qs_list, 'gate_way_list': gate_way_list, 'room_qs_list': room_qs_list}
  190. return response.json(0, repe)
  191. except Exception as e:
  192. print(e.args)
  193. return response.json(500)