# -*- coding: utf-8 -*- """ @Time : 2022/4/26 9:01 @Auth : Locky @File :SensorGatewayController.py @IDE :PyCharm """ import random import string from django.views import View from Object.ResponseObject import ResponseObject class SensorGateway(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 == 'getSensorId': # 返回唯一标识id给设备 return self.getSensorId(response) else: return response.json(404) @staticmethod def getSensorId(response): try: sensorId = ''.join(random.sample(string.ascii_letters + string.digits, 6)) return response.json(0, {'sensorId': sensorId}) except Exception as e: return response.json(500, repr(e))