EquipmentFamilyManager.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : EquipmentFamilyManager.py
  4. @Time : 2022/5/13 15:50
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. import time
  10. import oss2
  11. from django.db import transaction
  12. from django.db.models import Q
  13. from django.views.generic.base import View
  14. from Ansjer.config import OSS_STS_ACCESS_SECRET, OSS_STS_ACCESS_KEY
  15. from Controller.DeviceConfirmRegion import Device_Region
  16. from Model.models import Device_Info, UID_Bucket, UID_Preview, UidSetModel, UidChannelSetModel, \
  17. iotdeviceInfoModel, UIDModel
  18. from Object.ResponseObject import ResponseObject
  19. from Object.TokenObject import TokenObject
  20. from Service.CommonService import CommonService
  21. class EquipmentFamilyManager(View):
  22. def get(self, request, *args, **kwargs):
  23. request.encoding = 'utf-8'
  24. operation = kwargs.get('operation')
  25. return self.validation(request.GET, request, operation)
  26. def post(self, request, *args, **kwargs):
  27. request.encoding = 'utf-8'
  28. operation = kwargs.get('operation')
  29. return self.validation(request.POST, request, operation)
  30. def validation(self, request_dict, request, operation):
  31. token = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
  32. lang = request_dict.get('lang', None)
  33. if lang:
  34. response = ResponseObject(lang)
  35. else:
  36. response = ResponseObject(token.lang) if token.lang else ResponseObject()
  37. if token.code != 0:
  38. return response.json(token.code)
  39. user_id = token.userID
  40. # 手机端添加设备,查询,修改
  41. if operation == 'add':
  42. return self.do_save(user_id, request_dict, response, request)
  43. if operation == 'page':
  44. return self.do_device_page(user_id, request_dict, response)
  45. else:
  46. return response.json(414)
  47. @classmethod
  48. def do_save(cls, user_id, request_dict, response, request):
  49. """
  50. 添加设备
  51. @param request:
  52. @param user_id:
  53. @param request_dict:
  54. @param response:
  55. @return:
  56. """
  57. nick_name = request_dict.get('nickName', None)
  58. serial_number = request_dict.get('serialNumber', None)
  59. device_type = request_dict.get('deviceType', None)
  60. version = request_dict.get('version', None)
  61. # type 可能为0
  62. if not all([nick_name, serial_number, device_type]):
  63. return response.json(444, {'param': 'nick_name, serial_number, device_type'})
  64. device_info_qs = Device_Info.objects.filter(serial_number=serial_number, userID_id=user_id)
  65. if device_info_qs:
  66. # 判断设备是否已存在
  67. if device_info_qs[0].isExist == 1:
  68. return response.json(174)
  69. else:
  70. device_info_qs.delete()
  71. try:
  72. with transaction.atomic():
  73. # 格式化后的日期时间
  74. now_time = CommonService.timestamp_to_str(int(time.time()))
  75. device_id = CommonService.getUserID(getUser=False)
  76. Device_Info.objects.create(id=device_id, userID_id=user_id, NickName=nick_name,
  77. Type=device_type,
  78. serial_number=serial_number, data_joined=now_time,
  79. update_time=now_time)
  80. # 判断是否有用户绑定
  81. us_qs = UidSetModel.objects.filter(uid=serial_number)
  82. if not us_qs:
  83. n_time = int(time.time())
  84. ip = CommonService.get_ip_address(request)
  85. region_id = Device_Region().get_device_region(ip)
  86. region_alexa = 'CN' if region_id == 1 else 'ALL'
  87. uid_set_create_dict = {
  88. 'uid': serial_number,
  89. 'addTime': n_time,
  90. 'updTime': n_time,
  91. 'ip': CommonService.get_ip_address(request_dict),
  92. 'nickname': nick_name,
  93. 'version': version,
  94. 'region_alexa': region_alexa,
  95. }
  96. UidSetModel.objects.create(**uid_set_create_dict)
  97. return response.json(0)
  98. except Exception as e:
  99. print(e)
  100. return response.json(177, repr(e))
  101. @classmethod
  102. def do_device_page(cls, user_id, request_dict, response):
  103. """
  104. 查询设备列表
  105. @param user_id:
  106. @param request_dict:
  107. @param response:
  108. @return:
  109. """
  110. page_no = request_dict.get('pageNo', None)
  111. page_size = request_dict.get('pageSize', None)
  112. if not all([page_no, page_size]):
  113. return response.json(444)
  114. page = int(page_no)
  115. line = int(page_size)
  116. device_info_qs = Device_Info.objects.filter(userID=user_id)
  117. device_info_qs = device_info_qs.filter(~Q(isExist=0))
  118. total = device_info_qs.count()
  119. device_info_qs = device_info_qs.values("id", "userID_id", "NickName", "Type", "serial_number",
  120. "data_joined",
  121. "update_time")[(page - 1) * line: page * line]
  122. data = []
  123. for item in device_info_qs:
  124. data.append({
  125. 'id': item['id'],
  126. 'userId': item['userID_id'],
  127. 'nickName': item['NickName'],
  128. 'type': item['Type'],
  129. 'serialNumber': item['serial_number'],
  130. 'dataJoined': item['data_joined'].strftime("%Y-%m-%d %H:%M:%S"),
  131. 'updateTime': item['update_time'].strftime("%Y-%m-%d %H:%M:%S")
  132. })
  133. return response.json(0, {'list': data, 'total': total})
  134. # @classmethod
  135. # def family_save(cls, user_id, save_type, lang):
  136. # # 0:默认创建家庭,1:新增,2:修改
  137. # n_time = int(time.time())
  138. # if save_type == 0:
  139. # device_qs = Device_User.objects.filter(userID=user_id)
  140. # if device_qs.exists():
  141. # device_user = device_qs.first()
  142. # family_name = device_user.username if device_user.username else device_user.userEmail
  143. # family_name = family_name + "的家" if lang == 'cn' else family_name + " home"
  144. # user_family = LoocamUserFamily.objects.create(name=family_name, updated_time=n_time,
  145. # created_time=n_time)
  146. # if user_family.id:
  147. # pass
  148. #
  149. # return ''
  150. # @classmethod
  151. # def family_query(cls, user_id):
  152. # family_qs = LoocamFamilyMember.objects.filter(user_id=user_id)
  153. # if family_qs.exists():
  154. # return True
  155. # else:
  156. # return False