SensorGatewayController.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- coding: utf-8 -*-
  2. """
  3. @Time : 2022/4/26 9:01
  4. @Auth : Locky
  5. @File :SensorGatewayController.py
  6. @IDE :PyCharm
  7. """
  8. import random
  9. import string
  10. from django.views import View
  11. from Object.ResponseObject import ResponseObject
  12. class SensorGateway(View):
  13. def get(self, request, *args, **kwargs):
  14. request.encoding = 'utf-8'
  15. operation = kwargs.get('operation')
  16. return self.validation(request.GET, request, operation)
  17. def post(self, request, *args, **kwargs):
  18. request.encoding = 'utf-8'
  19. operation = kwargs.get('operation')
  20. return self.validation(request.POST, request, operation)
  21. def validation(self, request_dict, request, operation):
  22. response = ResponseObject()
  23. if operation == 'getSensorId': # 返回唯一标识id给设备
  24. return self.getSensorId(response)
  25. else:
  26. return response.json(404)
  27. @staticmethod
  28. def getSensorId(response):
  29. try:
  30. sensorId = ''.join(random.sample(string.ascii_letters + string.digits, 6))
  31. return response.json(0, {'sensorId': sensorId})
  32. except Exception as e:
  33. return response.json(500, repr(e))