|
@@ -0,0 +1,68 @@
|
|
|
+# -*- encoding: utf-8 -*-
|
|
|
+"""
|
|
|
+@File : SmartSocketController.py
|
|
|
+@Time : 2023/3/17 11:52
|
|
|
+@Author : stephen
|
|
|
+@Email : zhangdongming@asj6.wecom.work
|
|
|
+@Software: PyCharm
|
|
|
+"""
|
|
|
+import time
|
|
|
+
|
|
|
+from django.http import QueryDict
|
|
|
+from django.views import View
|
|
|
+
|
|
|
+from Model.models import SocketInfo
|
|
|
+from Object.ResponseObject import ResponseObject
|
|
|
+
|
|
|
+
|
|
|
+class SmartSocketView(View):
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ operation = kwargs.get('operation')
|
|
|
+ return self.validation(request.GET, request, operation)
|
|
|
+
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ operation = kwargs.get('operation')
|
|
|
+ return self.validation(request.POST, request, operation)
|
|
|
+
|
|
|
+ def delete(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ operation = kwargs.get('operation')
|
|
|
+ delete = QueryDict(request.body)
|
|
|
+ if not delete:
|
|
|
+ delete = request.GET
|
|
|
+ return self.validation(delete, request, operation)
|
|
|
+
|
|
|
+ def put(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ operation = kwargs.get('operation')
|
|
|
+ put = QueryDict(request.body)
|
|
|
+ return self.validation(put, request, operation)
|
|
|
+
|
|
|
+ def validation(self, request_dict, request, operation):
|
|
|
+ response = ResponseObject('cn')
|
|
|
+ if operation == 'getSocketDetails':
|
|
|
+ return self.getSocketDetails(request_dict, response)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def getSocketDetails(cls, request_dict, response):
|
|
|
+ return response.json(0)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def save_socket_switch(device_id, serial_number, status, type_switch=0):
|
|
|
+ if not device_id:
|
|
|
+ return False
|
|
|
+ socket_info_qs = SocketInfo.objects.filter(device_id=device_id, type_switch=type_switch)
|
|
|
+ now_time = int(time.time())
|
|
|
+ if not socket_info_qs.exists():
|
|
|
+ socket_dict = {"device_id": device_id,
|
|
|
+ "serial_number": serial_number,
|
|
|
+ "status": status,
|
|
|
+ "type_switch": type_switch,
|
|
|
+ "created_time": now_time,
|
|
|
+ "updated_time": now_time}
|
|
|
+ SocketInfo.objects.create(**socket_dict)
|
|
|
+ if socket_info_qs.first().status == status:
|
|
|
+ return True
|
|
|
+ pass
|