Переглянути джерело

新增创建/更新IoT设备信息接口

locky 3 місяців тому
батько
коміт
0d0feb1568
2 змінених файлів з 99 додано та 3 видалено
  1. 4 3
      azoauth/urls.py
  2. 95 0
      controller/DeviceController.py

+ 4 - 3
azoauth/urls.py

@@ -16,7 +16,7 @@ Including another URLconf
 from django.conf.urls import url
 from django.contrib import admin
 from django.urls import path, re_path
-from controller import index, beian, AppToApp
+from controller import index, beian, AppToApp, DeviceController
 from controller import deviceStatus
 
 urlpatterns = [
@@ -34,10 +34,11 @@ urlpatterns = [
 
     path('oa2/rtspStart', index.oa2RtspStartView.as_view()),            # 通知摄像头设备推流
     path('oa2/powerController', index.powerController.as_view()),       # 控制智能插座开关
-    re_path('oa2/(?P<operation>.*)', index.RtcController.as_view()),                     # rtc
+    re_path('device/(?P<operation>.*)', DeviceController.DeviceView.as_view()),
+    re_path('oa2/(?P<operation>.*)', index.RtcController.as_view()),    # rtc
 
     url(r'^deviceStatus/(?P<operation>.*)$', deviceStatus.deviceStatus.as_view()),  # 更新设备信息等接口
-    url(r'^vseesTest/(?P<operation>.*)', index.VesseTest.as_view()),  # test
+    url(r'^vseesTest/(?P<operation>.*)', index.VesseTest.as_view()),    # test
 
     # app to app oauth2验证登录连接
     re_path('appToApp/oa2/(?P<operation>.*)', AppToApp.Oa2View.as_view()),

+ 95 - 0
controller/DeviceController.py

@@ -0,0 +1,95 @@
+# @Author    : Rocky
+# @File      : DeviceController.py
+# @Time      : 2025/5/16 14:56
+
+import logging
+from datetime import datetime
+
+from django.views import View
+
+from model.models import iotdeviceInfoModel
+from object.ResponseObject import ResponseObject
+
+
+class DeviceView(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 validation(self, request_dict, request, operation):
+        response = ResponseObject()
+        if operation == 'creatOrUpdateIotInfo':
+            return self.creatOrUpdateIotInfo(request_dict, response)
+        else:
+            return response.json(414)
+
+    @staticmethod
+    def creatOrUpdateIotInfo(request_dict, response):
+        logger = logging.getLogger('django')
+
+        # 获取必要参数
+        serial_number = request_dict.get('serial_number', '')
+        uid = request_dict.get('uid', '')
+        certificate_id = request_dict.get('certificate_id', '')
+        certificate_pem = request_dict.get('certificate_pem', '')
+        public_key = request_dict.get('public_key', '')
+        private_key = request_dict.get('private_key', '')
+        thing_name = request_dict.get('thing_name', '')
+        thing_groups = request_dict.get('thing_groups', '')
+        endpoint = request_dict.get('endpoint', '')
+        token_iot_number = request_dict.get('token_iot_number', '')
+        
+        # 检查参数有效性
+        if not (serial_number or uid):
+            return response.json(444, 'serial_number或uid必须有一个不为空')
+            
+        try:
+            # 查询设备是否已存在
+            if serial_number:
+                device_name = serial_number
+                device = iotdeviceInfoModel.objects.filter(serial_number=serial_number).first()
+            else:
+                device_name = uid
+                device = iotdeviceInfoModel.objects.filter(uid=uid).first()
+
+            logger.info('--------{}创建/更新IoT设备信息--------'.format(device_name))
+            if not device:
+                # 创建新设备
+                iotdeviceInfoModel.objects.create(
+                    serial_number=serial_number,
+                    uid=uid,
+                    certificate_id=certificate_id,
+                    certificate_pem=certificate_pem,
+                    public_key=public_key,
+                    private_key=private_key,
+                    thing_name=thing_name,
+                    thing_groups=thing_groups,
+                    endpoint=endpoint,
+                    token_iot_number=token_iot_number
+                )
+            else:
+                # 更新现有设备
+                if device.certificate_id != certificate_id:
+                    device.certificate_id = certificate_id
+                    device.certificate_pem = certificate_pem
+                    device.public_key = public_key
+                    device.private_key = private_key
+                    device.thing_name = thing_name
+                    device.thing_groups = thing_groups
+                    device.endpoint = endpoint
+                    device.token_iot_number = token_iot_number
+                    device.update_time = datetime.now()
+                    device.save()
+
+            return response.json(0)
+                    
+        except Exception as e:
+            logger.error('创建/更新IoT设备信息异常: %s', str(e))
+            return response.json(500, '服务器内部错误')
+