SmartSocketController.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : SmartSocketController.py
  4. @Time : 2023/3/17 11:52
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. import logging
  10. import time
  11. from django.db import transaction
  12. from django.http import QueryDict
  13. from django.views import View
  14. from Model.models import SocketInfo
  15. from Object.ResponseObject import ResponseObject
  16. from Service.CommonService import CommonService
  17. LOGGER = logging.getLogger('info')
  18. class SmartSocketView(View):
  19. def get(self, request, *args, **kwargs):
  20. request.encoding = 'utf-8'
  21. operation = kwargs.get('operation')
  22. return self.validation(request.GET, request, operation)
  23. def post(self, request, *args, **kwargs):
  24. request.encoding = 'utf-8'
  25. operation = kwargs.get('operation')
  26. return self.validation(request.POST, request, operation)
  27. def delete(self, request, *args, **kwargs):
  28. request.encoding = 'utf-8'
  29. operation = kwargs.get('operation')
  30. delete = QueryDict(request.body)
  31. if not delete:
  32. delete = request.GET
  33. return self.validation(delete, request, operation)
  34. def put(self, request, *args, **kwargs):
  35. request.encoding = 'utf-8'
  36. operation = kwargs.get('operation')
  37. put = QueryDict(request.body)
  38. return self.validation(put, request, operation)
  39. def validation(self, request_dict, request, operation):
  40. response = ResponseObject('cn')
  41. if operation == 'getSocketDetails':
  42. return self.getSocketDetails(request_dict, response)
  43. elif operation == 'publishSocketInfo':
  44. device_id = '123'
  45. serial_number = '0013UC11A'
  46. status = 1
  47. type_switch = 0
  48. # self.save_socket_switch(device_id, serial_number, status, type_switch)
  49. is_open = 0
  50. count_down_time = 1679131969
  51. self.save_socket_count_down(device_id, serial_number, status, is_open, count_down_time)
  52. return response.json(0)
  53. return response.json(404)
  54. @classmethod
  55. def getSocketDetails(cls, request_dict, response):
  56. return response.json(0)
  57. @staticmethod
  58. def save_socket_switch(device_id, serial_number, status, type_switch=0):
  59. """
  60. 保存插座开关信息
  61. @param device_id: 设备ID
  62. @param serial_number: 序列号
  63. @param status: 状态 0关,1开
  64. @param type_switch: 0:总开关,1倒计时开关
  65. @return:
  66. """
  67. if not device_id:
  68. return False
  69. socket_info_qs = SocketInfo.objects.filter(device_id=device_id, type_switch=type_switch)
  70. now_time = int(time.time())
  71. try:
  72. with transaction.atomic():
  73. # 创建插座开关信息
  74. if not socket_info_qs.exists():
  75. socket_dict = {"device_id": device_id,
  76. "serial_number": serial_number,
  77. "status": status,
  78. "type_switch": type_switch,
  79. "created_time": now_time,
  80. "updated_time": now_time,
  81. "online": True}
  82. SocketInfo.objects.create(**socket_dict)
  83. return True
  84. if socket_info_qs.first().status == status:
  85. return True
  86. socket_info_qs.update(status=status, updated_time=now_time)
  87. # 主题名称
  88. topic_name = 'loocam/smart-socket/{}'.format(serial_number)
  89. # 发布消息内容
  90. msg = {'type': 1, 'data': {'deviceSwitch': status}}
  91. result = CommonService.req_publish_mqtt_msg(serial_number, topic_name, msg)
  92. LOGGER.info('智能插座开关设置发布MQTT消息结果{}'.format(result))
  93. return True
  94. except Exception as e:
  95. LOGGER.info('智能插座异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  96. return False
  97. @staticmethod
  98. def save_socket_count_down(device_id, serial_number, status, is_open, count_down_time, type_switch=1):
  99. """
  100. 保存插座倒计时信息
  101. @param count_down_time: 倒计时时间戳
  102. @param is_open: 是否开始 0:关停,1:开始
  103. @param device_id: 设备ID
  104. @param serial_number: 序列号
  105. @param status: 电源状态 0关,1开
  106. @param type_switch: 0:总开关,1倒计时开关
  107. @return:
  108. """
  109. if not device_id:
  110. return False
  111. socket_info_qs = SocketInfo.objects.filter(device_id=device_id, type_switch=type_switch)
  112. now_time = int(time.time())
  113. try:
  114. with transaction.atomic():
  115. # 创建插座倒计时信息
  116. if not socket_info_qs.exists():
  117. socket_dict = {"device_id": device_id,
  118. "serial_number": serial_number,
  119. "status": status,
  120. "type_switch": type_switch,
  121. "created_time": now_time,
  122. "updated_time": now_time,
  123. "online": True,
  124. "count_down_time": int(count_down_time)}
  125. socket_info_qs = SocketInfo.objects.create(**socket_dict)
  126. else:
  127. socket_info_qs.update(status=status, count_down_time=int(count_down_time),
  128. updated_time=now_time)
  129. count_down_id = socket_info_qs.first().id
  130. # 主题名称
  131. topic_name = 'loocam/smart-socket/{}'.format(serial_number)
  132. # 发布消息内容
  133. msg = {'type': 2,
  134. 'data': {'powerType': status,
  135. 'countDownId': count_down_id,
  136. 'time': int(count_down_time),
  137. 'start': is_open}}
  138. result = CommonService.req_publish_mqtt_msg(serial_number, topic_name, msg)
  139. LOGGER.info('智能插座倒计时发布MQTT消息结果{}'.format(result))
  140. return True
  141. except Exception as e:
  142. LOGGER.info('智能插座异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  143. return False