123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- # -*- encoding: utf-8 -*-
- """
- @File : EquipmentFamilyManager.py
- @Time : 2022/5/13 15:50
- @Author : stephen
- @Email : zhangdongming@asj6.wecom.work
- @Software: PyCharm
- """
- import time
- import oss2
- from django.db import transaction
- from django.db.models import Q
- from django.views.generic.base import View
- from Ansjer.config import OSS_STS_ACCESS_SECRET, OSS_STS_ACCESS_KEY
- from Controller.DeviceConfirmRegion import Device_Region
- from Model.models import Device_Info, UID_Bucket, UID_Preview, UidSetModel, UidChannelSetModel, \
- iotdeviceInfoModel, UIDModel
- from Object.ResponseObject import ResponseObject
- from Object.TokenObject import TokenObject
- from Service.CommonService import CommonService
- class EquipmentFamilyManager(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):
- token = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
- lang = request_dict.get('lang', None)
- if lang:
- response = ResponseObject(lang)
- else:
- response = ResponseObject(token.lang) if token.lang else ResponseObject()
- if token.code != 0:
- return response.json(token.code)
- user_id = token.userID
- # 手机端添加设备,查询,修改
- if operation == 'add':
- return self.do_save(user_id, request_dict, response, request)
- if operation == 'page':
- return self.do_device_page(user_id, request_dict, response)
- else:
- return response.json(414)
- @classmethod
- def do_save(cls, user_id, request_dict, response, request):
- """
- 添加设备
- @param request:
- @param user_id:
- @param request_dict:
- @param response:
- @return:
- """
- nick_name = request_dict.get('nickName', None)
- serial_number = request_dict.get('serialNumber', None)
- device_type = request_dict.get('deviceType', None)
- version = request_dict.get('version', None)
- # type 可能为0
- if not all([nick_name, serial_number, device_type]):
- return response.json(444, {'param': 'nick_name, serial_number, device_type'})
- device_info_qs = Device_Info.objects.filter(serial_number=serial_number, userID_id=user_id)
- if device_info_qs:
- # 判断设备是否已存在
- if device_info_qs[0].isExist == 1:
- return response.json(174)
- else:
- device_info_qs.delete()
- try:
- with transaction.atomic():
- # 格式化后的日期时间
- now_time = CommonService.timestamp_to_str(int(time.time()))
- device_id = CommonService.getUserID(getUser=False)
- Device_Info.objects.create(id=device_id, userID_id=user_id, NickName=nick_name,
- Type=device_type,
- serial_number=serial_number, data_joined=now_time,
- update_time=now_time)
- # 判断是否有用户绑定
- us_qs = UidSetModel.objects.filter(uid=serial_number)
- if not us_qs:
- n_time = int(time.time())
- ip = CommonService.get_ip_address(request)
- region_id = Device_Region().get_device_region(ip)
- region_alexa = 'CN' if region_id == 1 else 'ALL'
- uid_set_create_dict = {
- 'uid': serial_number,
- 'addTime': n_time,
- 'updTime': n_time,
- 'ip': CommonService.get_ip_address(request_dict),
- 'nickname': nick_name,
- 'version': version,
- 'region_alexa': region_alexa,
- }
- UidSetModel.objects.create(**uid_set_create_dict)
- return response.json(0)
- except Exception as e:
- print(e)
- return response.json(177, repr(e))
- @classmethod
- def do_device_page(cls, user_id, request_dict, response):
- """
- 查询设备列表
- @param user_id:
- @param request_dict:
- @param response:
- @return:
- """
- page_no = request_dict.get('pageNo', None)
- page_size = request_dict.get('pageSize', None)
- if not all([page_no, page_size]):
- return response.json(444)
- page = int(page_no)
- line = int(page_size)
- device_info_qs = Device_Info.objects.filter(userID=user_id)
- device_info_qs = device_info_qs.filter(~Q(isExist=0))
- total = device_info_qs.count()
- device_info_qs = device_info_qs.values("id", "userID_id", "NickName", "Type", "serial_number",
- "data_joined",
- "update_time")[(page - 1) * line: page * line]
- data = []
- for item in device_info_qs:
- data.append({
- 'id': item['id'],
- 'userId': item['userID_id'],
- 'nickName': item['NickName'],
- 'type': item['Type'],
- 'serialNumber': item['serial_number'],
- 'dataJoined': item['data_joined'].strftime("%Y-%m-%d %H:%M:%S"),
- 'updateTime': item['update_time'].strftime("%Y-%m-%d %H:%M:%S")
- })
- return response.json(0, {'list': data, 'total': total})
- # @classmethod
- # def family_save(cls, user_id, save_type, lang):
- # # 0:默认创建家庭,1:新增,2:修改
- # n_time = int(time.time())
- # if save_type == 0:
- # device_qs = Device_User.objects.filter(userID=user_id)
- # if device_qs.exists():
- # device_user = device_qs.first()
- # family_name = device_user.username if device_user.username else device_user.userEmail
- # family_name = family_name + "的家" if lang == 'cn' else family_name + " home"
- # user_family = LoocamUserFamily.objects.create(name=family_name, updated_time=n_time,
- # created_time=n_time)
- # if user_family.id:
- # pass
- #
- # return ''
- # @classmethod
- # def family_query(cls, user_id):
- # family_qs = LoocamFamilyMember.objects.filter(user_id=user_id)
- # if family_qs.exists():
- # return True
- # else:
- # return False
|