SmartSocketController.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 time
  10. from django.http import QueryDict
  11. from django.views import View
  12. from Model.models import SocketInfo
  13. from Object.ResponseObject import ResponseObject
  14. class SmartSocketView(View):
  15. def get(self, request, *args, **kwargs):
  16. request.encoding = 'utf-8'
  17. operation = kwargs.get('operation')
  18. return self.validation(request.GET, request, operation)
  19. def post(self, request, *args, **kwargs):
  20. request.encoding = 'utf-8'
  21. operation = kwargs.get('operation')
  22. return self.validation(request.POST, request, operation)
  23. def delete(self, request, *args, **kwargs):
  24. request.encoding = 'utf-8'
  25. operation = kwargs.get('operation')
  26. delete = QueryDict(request.body)
  27. if not delete:
  28. delete = request.GET
  29. return self.validation(delete, request, operation)
  30. def put(self, request, *args, **kwargs):
  31. request.encoding = 'utf-8'
  32. operation = kwargs.get('operation')
  33. put = QueryDict(request.body)
  34. return self.validation(put, request, operation)
  35. def validation(self, request_dict, request, operation):
  36. response = ResponseObject('cn')
  37. if operation == 'getSocketDetails':
  38. return self.getSocketDetails(request_dict, response)
  39. @classmethod
  40. def getSocketDetails(cls, request_dict, response):
  41. return response.json(0)
  42. @staticmethod
  43. def save_socket_switch(device_id, serial_number, status, type_switch=0):
  44. if not device_id:
  45. return False
  46. socket_info_qs = SocketInfo.objects.filter(device_id=device_id, type_switch=type_switch)
  47. now_time = int(time.time())
  48. if not socket_info_qs.exists():
  49. socket_dict = {"device_id": device_id,
  50. "serial_number": serial_number,
  51. "status": status,
  52. "type_switch": type_switch,
  53. "created_time": now_time,
  54. "updated_time": now_time}
  55. SocketInfo.objects.create(**socket_dict)
  56. if socket_info_qs.first().status == status:
  57. return True
  58. pass