EquipmentManagerV3.py 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. import re
  2. import threading
  3. import time
  4. import traceback
  5. import requests
  6. from Controller.CheckUserData import RandomStr
  7. import oss2, base64
  8. from django.db.models import Q
  9. from django.views.generic.base import View
  10. from Controller.DeviceConfirmRegion import Device_Region
  11. from Object.RedisObject import RedisObject
  12. from Ansjer.config import OSS_STS_ACCESS_SECRET, OSS_STS_ACCESS_KEY, BASE_DIR
  13. from Model.models import Device_Info, UID_Bucket, UID_Preview, UidSetModel, UidPushModel, UidChannelSetModel, \
  14. Device_User, iotdeviceInfoModel, UIDCompanySerialModel, UIDMainUser, UIDModel, UnicomDeviceInfo
  15. from Object.ResponseObject import ResponseObject
  16. from Object.TokenObject import TokenObject
  17. from Service.CommonService import CommonService
  18. from Service.ModelService import ModelService
  19. import time, json
  20. class EquipmentManagerV3(View):
  21. def get(self, request, *args, **kwargs):
  22. request.encoding = 'utf-8'
  23. operation = kwargs.get('operation')
  24. return self.validation(request.GET, request, operation)
  25. def post(self, request, *args, **kwargs):
  26. request.encoding = 'utf-8'
  27. operation = kwargs.get('operation')
  28. return self.validation(request.POST, request, operation)
  29. def validation(self, request_dict, request, operation):
  30. response = ResponseObject()
  31. token = request_dict.get('token', None)
  32. # 设备主键uid
  33. if operation == 'notLoginMainUserDevice':
  34. return self.not_login_do_mainUserDevice(request_dict, response)
  35. tko = TokenObject(token)
  36. if tko.code != 0:
  37. return response.json(tko.code)
  38. response.lang = tko.lang
  39. userID = tko.userID
  40. # 手机端添加设备,查询,修改
  41. if operation == 'add':
  42. return self.do_add(userID, request_dict, response, request)
  43. if operation == 'batchAdd':
  44. return self.do_batch_add(userID, request_dict, response, request)
  45. elif operation == 'query':
  46. return self.do_query(userID, request_dict, response)
  47. elif operation == 'modify':
  48. return self.do_modify(userID, request_dict, response, request)
  49. elif operation == 'modifyChannelName':
  50. return self.do_modify_channel_name(userID, request_dict, response, request)
  51. elif operation == 'fuzzyQuery':
  52. return self.do_fuzzy_query(userID, request_dict, response)
  53. elif operation == 'mainUserDevice':
  54. return self.do_mainUserDevice( request_dict, response)
  55. elif operation == 'getDeviceFeatures':
  56. return self.do_get_device_features(request_dict, response)
  57. else:
  58. return response.json(414)
  59. def do_add(self, userID, request_dict, response, request):
  60. UID = request_dict.get('UID', None)
  61. NickName = request_dict.get('NickName', None)
  62. View_Account = request_dict.get('View_Account', None)
  63. View_Password = request_dict.get('View_Password', '')
  64. encrypt_pass = View_Password
  65. print("准备解密")
  66. Type = request_dict.get('Type', None)
  67. ChannelIndex = request_dict.get('ChannelIndex', None)
  68. version = request_dict.get('version', '')
  69. isCheckMainUser = request_dict.get('isCheckMainUser', None)
  70. if not all([UID, NickName, View_Account, Type, ChannelIndex]): # Type和ChannelIndex可能为0
  71. return response.json(444, {'param': 'UID, NickName, View_Account, Type, ChannelIndex'})
  72. Type = int(Type)
  73. ChannelIndex = int(ChannelIndex)
  74. re_uid = re.compile(r'^[A-Za-z0-9]{14,20}$')
  75. if not re_uid.match(UID):
  76. return response.json(444, {'param': 'UID'})
  77. device_info_qs = Device_Info.objects.filter(UID=UID, userID_id=userID)
  78. if device_info_qs:
  79. # 判断设备是否已存在
  80. if device_info_qs[0].isExist == 1:
  81. return response.json(174)
  82. else:
  83. device_info_qs.delete()
  84. dvr_type_list = [1, 2, 3, 4, 10001]
  85. View_Password = '' if Type in dvr_type_list else 'admin'
  86. id = CommonService.getUserID(getUser=False)
  87. userName = Device_User.objects.get(userID=userID).username
  88. main_exist = Device_Info.objects.filter(UID=UID)
  89. main_exist = main_exist.filter(~Q(vodPrimaryUserID='')).values('vodPrimaryUserID', 'vodPrimaryMaster')
  90. vodPrimaryUserID = userID
  91. vodPrimaryMaster = userName
  92. primaryUserID = ''
  93. primaryMaster = ''
  94. isShare = False
  95. is_bind = Device_Info.objects.filter(UID=UID, isShare=False).values('userID__userID', 'primaryUserID', 'primaryMaster')
  96. if main_exist.exists():
  97. vodPrimaryUserID = main_exist[0]['vodPrimaryUserID']
  98. vodPrimaryMaster = main_exist[0]['vodPrimaryMaster']
  99. if is_bind.exists():
  100. primaryUserID = is_bind[0]['primaryUserID']
  101. primaryMaster = is_bind[0]['primaryMaster']
  102. isShare = True
  103. isusermain = False
  104. if (vodPrimaryUserID != userID and vodPrimaryUserID != '') or (primaryUserID != userID and primaryUserID != ''):
  105. isusermain = True
  106. # 判断是否有已绑定用户
  107. if isCheckMainUser == '1' and isusermain:
  108. res = {
  109. 'id': id,
  110. 'userID': userID,
  111. 'NickName': NickName,
  112. 'UID': UID,
  113. 'View_Account': View_Account,
  114. 'View_Password': View_Password,
  115. 'ChannelIndex': ChannelIndex,
  116. 'Type': Type,
  117. 'isShare': isShare,
  118. 'primaryUserID': primaryUserID,
  119. 'primaryMaster': primaryMaster,
  120. 'vodPrimaryUserID': vodPrimaryUserID,
  121. 'vodPrimaryMaster': vodPrimaryMaster,
  122. 'data_joined': '',
  123. 'version': version,
  124. 'isVod': 0,
  125. 'isExist': 1,
  126. 'userID__userEmail': ''
  127. }
  128. res['vod'] = [
  129. {
  130. "status": 1,
  131. "channel": ChannelIndex,
  132. "endTime": '',
  133. "bucket__content": '',
  134. "uid": UID
  135. }
  136. ]
  137. res['isMainUserExists'] = 1
  138. return response.json(0, res)
  139. try:
  140. # 判断是否有用户绑定
  141. nowTime = int(time.time())
  142. us_qs = UidSetModel.objects.filter(uid=UID)
  143. if us_qs.exists():
  144. us_qs.update(nickname=NickName)
  145. UidSet_id = us_qs.first().id
  146. else:
  147. ip = CommonService.get_ip_address(request)
  148. region_id = Device_Region().get_device_region(ip)
  149. region_alexa = 'CN' if region_id == 1 else 'ALL'
  150. uid_set_create_dict = {
  151. 'uid': UID,
  152. 'addTime': nowTime,
  153. 'updTime': nowTime,
  154. 'ip': CommonService.get_ip_address(request_dict),
  155. 'channel': ChannelIndex,
  156. 'nickname': NickName,
  157. 'version': version,
  158. 'region_alexa': region_alexa,
  159. }
  160. UidSet = UidSetModel.objects.create(**uid_set_create_dict)
  161. UidSet_id = UidSet.id
  162. # 查询uid_channel表有无该uid的数据
  163. uid_channel_set = UidChannelSetModel.objects.filter(uid_id=UidSet_id)
  164. if not uid_channel_set.exists():
  165. # 多通道设备设置通道名
  166. if Type in dvr_type_list:
  167. UidChannelSet_bulk = []
  168. for i in range(1, ChannelIndex+1):
  169. channel_name = 'channel'+str(i) # channel1,channel2...
  170. UidChannelSet = UidChannelSetModel(uid_id=UidSet_id, channel=i, channel_name=channel_name)
  171. UidChannelSet_bulk.append(UidChannelSet)
  172. UidChannelSetModel.objects.bulk_create(UidChannelSet_bulk)
  173. userDevice = Device_Info(id=id, userID_id=userID, UID=UID, NickName=NickName, View_Account=View_Account,
  174. View_Password=View_Password, Type=Type, ChannelIndex=ChannelIndex, version=version,
  175. vodPrimaryUserID=vodPrimaryUserID, vodPrimaryMaster=vodPrimaryMaster)
  176. userDevice.save()
  177. uid_serial_qs = UIDCompanySerialModel.objects.filter(uid__uid=UID)
  178. if uid_serial_qs.exists():
  179. uid_serial = uid_serial_qs[0]
  180. serial_number = uid_serial.company_serial.serial_number + uid_serial.company_serial.company.mark
  181. Device_Info.objects.filter(UID=UID).update(vodPrimaryUserID=vodPrimaryUserID,
  182. vodPrimaryMaster=vodPrimaryMaster,
  183. serial_number=serial_number)
  184. self.unicom_user_info_save(userID, serial_number)
  185. else:
  186. Device_Info.objects.filter(UID=UID).update(vodPrimaryUserID=vodPrimaryUserID,
  187. vodPrimaryMaster=vodPrimaryMaster)
  188. if not us_qs.exists():
  189. us_qs = UidSetModel.objects.filter(uid=UID)
  190. if us_qs.exists() and us_qs[0].is_alexa == 1:
  191. if us_qs[0].channel > 1:
  192. data_list = []
  193. uid_channel_set_qs = UidChannelSetModel.objects.filter(uid_id=us_qs[0].id).\
  194. values('channel', 'channel_name')
  195. if uid_channel_set_qs.exists():
  196. # 多通道设备名为 UidChannelSetModel 的 channel_name
  197. for uid_channel_set in uid_channel_set_qs:
  198. data_list.append({'userID': userID, 'UID': UID, 'uid_nick': uid_channel_set['channel_name'],
  199. 'channel': uid_channel_set['channel'], 'password': encrypt_pass})
  200. else:
  201. data_list = [{'userID': userID, 'UID': UID, 'uid_nick': NickName, 'password': encrypt_pass}]
  202. # 请求Alexa服务器更新事件网关
  203. data_list = json.dumps(data_list)
  204. data = {'data_list': data_list}
  205. url = 'https://www.zositech.xyz/deviceStatus/addOrUpdateV2'
  206. requests.post(url, data=data, timeout=2)
  207. except Exception as e:
  208. return response.json(10, repr(e))
  209. else:
  210. dvqs = Device_Info.objects.filter(id=id).values('id', 'userID', 'NickName', 'UID',
  211. 'View_Account',
  212. 'View_Password', 'ChannelIndex', 'Type',
  213. 'isShare',
  214. 'primaryUserID', 'primaryMaster',
  215. 'vodPrimaryUserID', 'vodPrimaryMaster',
  216. 'userID__userEmail',
  217. 'data_joined', 'version',
  218. 'isVod', 'isExist', 'isCameraOpenCloud', 'serial_number')
  219. dvql = CommonService.qs_to_list(dvqs)
  220. ubqs = UID_Bucket.objects.filter(uid=UID). \
  221. values('bucket__content', 'status', 'channel', 'endTime', 'uid')
  222. res = dvql[0]
  223. res['vod'] = list(ubqs)
  224. iotqs = iotdeviceInfoModel.objects.filter(serial_number=dvql[0]['serial_number'])
  225. if iotqs.exists():
  226. res['iot'] = {
  227. 'endpoint': iotqs[0].endpoint,
  228. 'token_iot_number': iotqs[0].endpoint
  229. }
  230. # 除C1的序列号暂时返回''
  231. if res['serial_number'] and Type == 101:
  232. res['serial_number'] = CommonService.get_full_serial_number(UID, res['serial_number'], Type)
  233. else:
  234. res['serial_number'] = ''
  235. return response.json(0, res)
  236. @classmethod
  237. def unicom_user_info_save(cls, user_id, serial_number):
  238. """
  239. 保存联通与用户信息绑定
  240. @param user_id: 用户id
  241. @param serial_number: 序列号
  242. @return: True | False
  243. """
  244. unicom_device_info_qs = UnicomDeviceInfo.objects.filter(serial_no=serial_number)
  245. if not unicom_device_info_qs.exists():
  246. return False
  247. if not unicom_device_info_qs[0].user_id:
  248. unicom_device_info_qs.update(user_id=user_id)
  249. return True
  250. def do_batch_add(self, userID, request_dict, response, request):
  251. # 批量添加设备
  252. uidContent = request_dict.get('uidContent', None)
  253. if not uidContent:
  254. return response.json(444, {'param': 'uidContent'})
  255. try:
  256. deviceNumber = 0 # 添加成功数量
  257. add_success_flag = False # 添加成功标识
  258. exception_flag = False # 异常标识
  259. exists_flag = False # 已存在标识
  260. uid_content_list = eval(uidContent)
  261. print('uidContent: ', uid_content_list)
  262. re_uid = re.compile(r'^[A-Za-z0-9]{14,20}$')
  263. for uid_content in uid_content_list:
  264. exception_flag = False # 重置异常标识
  265. exists_flag = False # 已存在标识
  266. UID = uid_content['uid']
  267. NickName = uid_content['nickName']
  268. Type = uid_content['type']
  269. ChannelIndex = uid_content['channelIndex']
  270. version = uid_content['version']
  271. isCheckMainUser = uid_content['isCheckMainUser']
  272. View_Account = uid_content['viewAccount']
  273. encryptPassword = uid_content['encryptPassword']
  274. View_Password = self.decode_pwd(encryptPassword)
  275. if not all([UID, NickName, View_Account]): # Type和ChannelIndex可能为0
  276. return response.json(444, {'param': 'UID, NickName, View_Account'})
  277. if not re_uid.match(UID): # 检查uid长度
  278. return response.json(444, {'error uid length': UID})
  279. device_info_qs = Device_Info.objects.filter(UID=UID, userID_id=userID)
  280. if device_info_qs:
  281. # 判断设备是否已存在
  282. if device_info_qs[0].isExist == 1:
  283. exists_res = {UID: 'device already exists!'}
  284. exists_flag = True
  285. continue
  286. else:
  287. device_info_qs.delete()
  288. id = CommonService.getUserID(getUser=False)
  289. userName = Device_User.objects.get(userID=userID).username
  290. main_exist = Device_Info.objects.filter(UID=UID)
  291. main_exist = main_exist.filter(~Q(vodPrimaryUserID='')).values('vodPrimaryUserID', 'vodPrimaryMaster')
  292. vodPrimaryUserID = userID
  293. vodPrimaryMaster = userName
  294. primaryUserID = ''
  295. primaryMaster = ''
  296. isShare = False
  297. is_bind = Device_Info.objects.filter(UID=UID, isShare=False).values('userID__userID', 'primaryUserID', 'primaryMaster')
  298. if main_exist.exists():
  299. vodPrimaryUserID = main_exist[0]['vodPrimaryUserID']
  300. vodPrimaryMaster = main_exist[0]['vodPrimaryMaster']
  301. if is_bind.exists():
  302. primaryUserID = is_bind[0]['primaryUserID']
  303. primaryMaster = is_bind[0]['primaryMaster']
  304. isShare = True
  305. isusermain = False
  306. if (vodPrimaryUserID != userID and vodPrimaryUserID != '') or (primaryUserID != userID and primaryUserID != ''):
  307. isusermain = True
  308. # 判断是否有已绑定用户
  309. if isCheckMainUser == 1 and isusermain:
  310. res = {
  311. 'id': id,
  312. 'userID': userID,
  313. 'NickName': NickName,
  314. 'UID': UID,
  315. 'View_Account': View_Account,
  316. 'View_Password': View_Password,
  317. 'ChannelIndex': ChannelIndex,
  318. 'Type': Type,
  319. 'isShare': isShare,
  320. 'primaryUserID': primaryUserID,
  321. 'primaryMaster': primaryMaster,
  322. 'vodPrimaryUserID': vodPrimaryUserID,
  323. 'vodPrimaryMaster': vodPrimaryMaster,
  324. 'data_joined': '',
  325. 'version': version,
  326. 'isVod': 0,
  327. 'isExist': 1,
  328. 'userID__userEmail': ''
  329. }
  330. res['vod'] = [
  331. {
  332. "status": 1,
  333. "channel": ChannelIndex,
  334. "endTime": '',
  335. "bucket__content": '',
  336. "uid": UID
  337. }
  338. ]
  339. res['isMainUserExists'] = 1
  340. continue
  341. # 判断是否有用户绑定
  342. nowTime = int(time.time())
  343. us_qs = UidSetModel.objects.filter(uid=UID)
  344. if us_qs.exists():
  345. us_qs.update(nickname=NickName)
  346. UidSet_id = us_qs.first().id
  347. else:
  348. ip = CommonService.get_ip_address(request)
  349. region_id = Device_Region().get_device_region(ip)
  350. region_alexa = 'CN' if region_id == 1 else 'ALL'
  351. uid_set_create_dict = {
  352. 'uid': UID,
  353. 'addTime': nowTime,
  354. 'updTime': nowTime,
  355. 'ip': CommonService.get_ip_address(request_dict),
  356. 'channel': ChannelIndex,
  357. 'nickname': NickName,
  358. 'version': version,
  359. 'region_alexa': region_alexa,
  360. }
  361. UidSet = UidSetModel.objects.create(**uid_set_create_dict)
  362. UidSet_id = UidSet.id
  363. # 查询uid_channel表有无该uid的数据
  364. uid_channel_set = UidChannelSetModel.objects.filter(uid_id=UidSet_id)
  365. if not uid_channel_set.exists():
  366. # 多通道设备设置通道名
  367. multi_channel_list = [1, 2, 3, 4, 10001]
  368. if Type in multi_channel_list:
  369. UidChannelSet_bulk = []
  370. for i in range(1, ChannelIndex+1):
  371. channel_name = 'channel'+str(i) # channel1,channel2...
  372. UidChannelSet = UidChannelSetModel(uid_id=UidSet_id, channel=i, channel_name=channel_name)
  373. UidChannelSet_bulk.append(UidChannelSet)
  374. UidChannelSetModel.objects.bulk_create(UidChannelSet_bulk)
  375. userDevice = Device_Info(id=id, userID_id=userID, UID=UID, NickName=NickName, View_Account=View_Account,
  376. View_Password=View_Password, Type=Type, ChannelIndex=ChannelIndex, version=version,
  377. vodPrimaryUserID=vodPrimaryUserID, vodPrimaryMaster=vodPrimaryMaster)
  378. userDevice.save()
  379. uid_serial_qs = UIDCompanySerialModel.objects.filter(uid__uid=UID)
  380. if uid_serial_qs.exists():
  381. uid_serial = uid_serial_qs[0]
  382. Device_Info.objects.filter(UID=UID).update(vodPrimaryUserID=vodPrimaryUserID,
  383. vodPrimaryMaster=vodPrimaryMaster,
  384. serial_number=uid_serial.company_serial.serial_number + uid_serial.company_serial.company.mark)
  385. else:
  386. Device_Info.objects.filter(UID=UID).update(vodPrimaryUserID=vodPrimaryUserID,
  387. vodPrimaryMaster=vodPrimaryMaster)
  388. if not us_qs.exists():
  389. us_qs = UidSetModel.objects.filter(uid=UID)
  390. if us_qs.exists() and us_qs[0].is_alexa == 1:
  391. if us_qs[0].channel > 1:
  392. data_list = []
  393. uid_channel_set_qs = UidChannelSetModel.objects.filter(uid_id=us_qs[0].id).\
  394. values('channel', 'channel_name')
  395. if uid_channel_set_qs.exists():
  396. # 多通道设备名为 UidChannelSetModel 的 channel_name
  397. for uid_channel_set in uid_channel_set_qs:
  398. data_list.append({'userID': userID, 'UID': UID, 'uid_nick': uid_channel_set['channel_name'],
  399. 'channel': uid_channel_set['channel'], 'password': encryptPassword})
  400. else:
  401. data_list = [{'userID': userID, 'UID': UID, 'uid_nick': NickName, 'password': encryptPassword}]
  402. # 请求Alexa服务器更新事件网关
  403. data_list = json.dumps(data_list)
  404. data = {'data_list': data_list}
  405. url = 'https://www.zositech.xyz/deviceStatus/addOrUpdateV2'
  406. requests.post(url, data=data, timeout=2)
  407. dvqs = Device_Info.objects.filter(id=id).values('id', 'userID', 'NickName', 'UID',
  408. 'View_Account',
  409. 'View_Password', 'ChannelIndex', 'Type',
  410. 'isShare',
  411. 'primaryUserID', 'primaryMaster',
  412. 'vodPrimaryUserID', 'vodPrimaryMaster',
  413. 'userID__userEmail',
  414. 'data_joined', 'version',
  415. 'isVod', 'isExist', 'isCameraOpenCloud', 'serial_number')
  416. dvql = CommonService.qs_to_list(dvqs)
  417. ubqs = UID_Bucket.objects.filter(uid=UID). \
  418. values('bucket__content', 'status', 'channel', 'endTime', 'uid')
  419. success_res = dvql[0]
  420. success_res['vod'] = list(ubqs)
  421. iotqs = iotdeviceInfoModel.objects.filter(serial_number=dvql[0]['serial_number'])
  422. if iotqs.exists():
  423. success_res['iot'] = {'endpoint': iotqs[0].endpoint, 'token_iot_number': iotqs[0].endpoint}
  424. deviceNumber += 1
  425. success_res['deviceNumber'] = deviceNumber
  426. add_success_flag = True
  427. except Exception as e:
  428. print(e)
  429. error_res = repr(e)
  430. exception_flag = True
  431. pass
  432. finally:
  433. if add_success_flag: # 有一台添加成功则返回成功
  434. return response.json(0, success_res)
  435. if exists_flag: # 全部设备已存在
  436. return response.json(174, exists_res)
  437. if exception_flag:
  438. return response.json(500, error_res)
  439. return response.json(0, res)
  440. def do_modify(self, userID, request_dict, response, request):
  441. token = request_dict.get('token', None)
  442. deviceContent = request_dict.get('content', None)
  443. id = request_dict.get('id', None)
  444. if not deviceContent or not id:
  445. return response.json(444, 'content,id')
  446. tko = TokenObject(token)
  447. response.lang = tko.lang
  448. if tko.code != 0:
  449. return response.json(tko.code)
  450. userID = tko.userID
  451. if userID is None:
  452. return response.json(309)
  453. try:
  454. deviceData = eval(deviceContent)
  455. if deviceData.__contains__('userID_id'):
  456. asy = threading.Thread(target=ModelService.update_log,
  457. args=(CommonService.get_ip_address(request), userID, 'modifyV3', deviceData, id))
  458. asy.start()
  459. return response.json(10, '110')
  460. if deviceData.__contains__('UID'):
  461. del deviceData['UID']
  462. # print(deviceData['View_Password'])
  463. if deviceData.__contains__('View_Password'):
  464. encrypt_pwd = deviceData['View_Password']
  465. deviceData['View_Password'] = self.decode_pwd(deviceData['View_Password'])
  466. dev_info_qs = Device_Info.objects.filter(userID_id=userID, id=id)
  467. dev_info_qs.update(**deviceData)
  468. except Exception as e:
  469. print(e)
  470. return response.json(177, repr(e))
  471. else:
  472. qs = Device_Info.objects.filter(userID_id=userID, id=id)
  473. res = CommonService.qs_to_dict(qs)
  474. if qs.exists():
  475. uid = qs[0].UID
  476. nickname = qs[0].NickName
  477. # 增加设备影子信息修改昵称 start
  478. us_qs = UidSetModel.objects.filter(uid=uid)
  479. if us_qs.exists():
  480. if deviceData.__contains__('NickName'):
  481. us_qs.update(nickname=nickname)
  482. else:
  483. ChannelIndex = qs[0].ChannelIndex
  484. nowTime = int(time.time())
  485. uid_set_create_dict = {
  486. 'uid': uid,
  487. 'addTime': nowTime,
  488. 'updTime': nowTime,
  489. # 'ip': CommonService.get_ip_address(request),
  490. 'channel': ChannelIndex,
  491. 'nickname': nickname,
  492. }
  493. UidSetModel.objects.create(**uid_set_create_dict)
  494. di_qs = Device_Info.objects.filter(UID=uid)
  495. di_qs.update(NickName=nickname)
  496. if deviceData is not None and deviceData.__contains__('NickName') and us_qs[0].is_alexa == 1:
  497. # 请求Alexa服务器更新事件网关
  498. url = 'https://www.zositech.xyz/deviceStatus/addOrUpdateV2'
  499. password = encrypt_pwd if deviceData.__contains__('View_Password') else ''
  500. data_list = [{'userID': userID, 'UID': uid, 'uid_nick': nickname, 'password': password}]
  501. data_list = json.dumps(data_list)
  502. data = {'data_list': data_list}
  503. requests.post(url, data=data, timeout=2)
  504. return response.json(0, res)
  505. # 编辑通道名
  506. def do_modify_channel_name(self, userID, request_dict, response):
  507. uid = request_dict.get('uid', None)
  508. channel = request_dict.get('channel', None)
  509. channel_name = request_dict.get('channel_name', None)
  510. if not all([uid, channel, channel_name]):
  511. return response.json(444)
  512. # 更新通道名
  513. UidChannelSetModel.objects.filter(uid__uid=uid).update(channel_name=channel_name)
  514. return response.json(0)
  515. # 新查询设备字段
  516. def do_query(self, userID, request_dict, response):
  517. token = request_dict.get('token', None)
  518. page = request_dict.get('page', None)
  519. line = request_dict.get('line', None)
  520. NickName = request_dict.get('NickName', None)
  521. if not token or not page or not line:
  522. return response.json(444)
  523. page = int(page)
  524. line = int(line)
  525. uid = request_dict.get('uid', None)
  526. tko = TokenObject(token)
  527. response.lang = tko.lang
  528. if page <= 0:
  529. return response.json(0)
  530. if tko.code != 0:
  531. return response.json(tko.code)
  532. userID = tko.userID
  533. dvqs = Device_Info.objects.filter(userID_id=userID)
  534. # # 过滤已重置的设备
  535. dvqs = dvqs.filter(~Q(isExist=2))
  536. dvql = dvqs.values('id', 'userID', 'NickName', 'UID', 'View_Account','View_Password', 'ChannelIndex',
  537. 'Type', 'isShare', 'primaryUserID', 'primaryMaster', 'data_joined', 'vodPrimaryUserID',
  538. 'vodPrimaryMaster', 'userID__userEmail', 'version', 'isVod', 'isExist', 'NotificationMode',
  539. 'isCameraOpenCloud', 'serial_number')
  540. dvls = CommonService.qs_to_list(dvql)
  541. uid_list = []
  542. for dvl in dvls:
  543. if dvl['primaryUserID'] and dvl['id'] == dvl['primaryUserID']:
  544. dvl['isPrimaryUser'] = 1
  545. else:
  546. dvl['isPrimaryUser'] = 0
  547. uid_list.append(dvl['UID'])
  548. ubqs = UID_Bucket.objects.filter(uid__in=uid_list). \
  549. values('bucket__content', 'status', 'channel', 'endTime', 'uid')
  550. upqs = UID_Preview.objects.filter(uid__in=uid_list).order_by('channel').values('id', 'uid', 'channel')
  551. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  552. bucket = oss2.Bucket(auth, 'oss-cn-hongkong.aliyuncs.com', 'statres')
  553. nowTime = int(time.time())
  554. data = []
  555. # 设备拓展信息表
  556. us_qs = UidSetModel.objects.filter(uid__in=uid_list).values('id', 'uid', 'version', 'nickname', 'ucode',
  557. 'detect_status', 'detect_group',
  558. 'detect_interval',
  559. 'region_alexa', 'is_alexa', 'deviceModel',
  560. 'TimeZone', 'TimeStatus', 'SpaceUsable',
  561. 'SpaceSum', 'MirrorType', 'RecordType',
  562. 'OutdoorModel', 'WIFIName', 'isDetector',
  563. 'DetectorRank', 'is_human', 'is_custom_voice',
  564. 'is_ptz', 'double_wifi', 'is_ai', 'mobile_4g')
  565. uv_dict = {}
  566. for us in us_qs:
  567. uv_dict[us['uid']] = {
  568. 'version': us['version'],
  569. 'nickname': us['nickname'],
  570. 'ucode': us['ucode'],
  571. 'detect_interval': us['detect_interval'],
  572. 'detect_group': us['detect_group'],
  573. 'detect_status': us['detect_status'],
  574. 'region_alexa': us['region_alexa'],
  575. 'is_alexa': us['is_alexa'],
  576. 'deviceModel': us['deviceModel'],
  577. 'TimeZone': us['TimeZone'],
  578. 'TimeStatus': us['TimeStatus'],
  579. 'SpaceUsable': us['SpaceUsable'],
  580. 'SpaceSum': us['SpaceSum'],
  581. 'MirrorType': us['MirrorType'],
  582. 'RecordType': us['RecordType'],
  583. 'OutdoorModel': us['OutdoorModel'],
  584. 'WIFIName': us['WIFIName'],
  585. 'isDetector': us['isDetector'],
  586. 'DetectorRank': us['DetectorRank'],
  587. 'is_human': us['is_human'],
  588. 'is_custom_voice': us['is_custom_voice'],
  589. 'is_ptz': us['is_ptz'],
  590. 'double_wifi': us['double_wifi'],
  591. 'mobile4G': us['mobile_4g'],
  592. 'is_ai': us['is_ai']
  593. }
  594. # 从uid_channel里面取出通道配置信息
  595. ucs_qs = UidChannelSetModel.objects.filter(uid__id=us['id']).values('channel', 'channel_name',
  596. 'pir_audio', 'mic_audio',
  597. 'battery_status', 'battery_level',
  598. 'sleep_status', 'sleep_time',
  599. 'light_night_model',
  600. 'light_alarm_type',
  601. 'light_alarm_level',
  602. 'light_alarm_man_en',
  603. 'light_alarm_vol',
  604. 'light_long_light'
  605. )
  606. channels = []
  607. for ucs in ucs_qs:
  608. channels_dict = {
  609. 'channel': ucs['channel'],
  610. 'channel_name': ucs['channel_name'],
  611. 'pir_audio': ucs['pir_audio'],
  612. 'mic_audio': ucs['mic_audio'],
  613. 'battery_status': ucs['battery_status'],
  614. 'battery_level': ucs['battery_level'],
  615. 'sleep_status': ucs['sleep_status'],
  616. 'sleep_time': ucs['sleep_time'],
  617. 'light_night_model': ucs['light_night_model'],
  618. 'light_alarm_type': ucs['light_alarm_type'],
  619. 'light_alarm_level': ucs['light_alarm_level'],
  620. 'light_alarm_man_en': ucs['light_alarm_man_en'],
  621. 'light_alarm_vol': ucs['light_alarm_vol'],
  622. 'light_long_light': ucs['light_long_light']
  623. }
  624. channels.append(channels_dict)
  625. uv_dict[us['uid']]['channels'] = channels
  626. for p in dvls:
  627. if p['serial_number']:
  628. u_device_info_qs = UnicomDeviceInfo.objects.filter(serial_no=p['serial_number'])
  629. if u_device_info_qs.exists():
  630. p['iccid'] = u_device_info_qs.first().iccid
  631. # 获取iot_deviceInfo表的endpoint和token_iot_number
  632. p['iot'] = []
  633. if p['serial_number']: # 存在序列号根据序列号查询
  634. iotdeviceInfo_qs = iotdeviceInfoModel.objects.filter(serial_number=p['serial_number'][0:6])
  635. else: # 根据uid查询
  636. iotdeviceInfo_qs = iotdeviceInfoModel.objects.filter(uid=p['UID'])
  637. if iotdeviceInfo_qs.exists():
  638. iotdeviceInfo = iotdeviceInfo_qs.values('endpoint', 'token_iot_number')
  639. p['iot'].append({
  640. 'endpoint': iotdeviceInfo[0]['endpoint'],
  641. 'token_iot_number': iotdeviceInfo[0]['token_iot_number']
  642. })
  643. # C1返回序列号
  644. if p['Type'] != 101:
  645. p['serial_number'] = ''
  646. p['vod'] = []
  647. for dm in ubqs:
  648. if p['UID'] == dm['uid']:
  649. if dm['endTime'] > nowTime:
  650. p['vod'].append(dm)
  651. p['preview'] = []
  652. for up in upqs:
  653. if p['UID'] == up['uid']:
  654. obj = 'uid_preview/{uid}/channel_{channel}.png'.format(uid=up['uid'], channel=up['channel'])
  655. img_sign = bucket.sign_url('GET', obj, 300)
  656. p['preview'].append(img_sign)
  657. p_uid = p['UID']
  658. # 返回设备初始化字符
  659. uid_qs = UIDModel.objects.filter(uid=p_uid).values('platform', 'init_string', 'init_string_app')
  660. if uid_qs.exists():
  661. p['platform'] = uid_qs[0]['platform']
  662. p['initString'] = uid_qs[0]['init_string']
  663. p['initStringApp'] = uid_qs[0]['init_string_app']
  664. if p_uid in uv_dict:
  665. # 设备版本号
  666. uidversion = uv_dict[p_uid]['version']
  667. if len(uidversion) >6:
  668. uidversion = uidversion[0 : uidversion.rfind('.')]
  669. p['uid_version'] = uidversion
  670. p['ucode'] = uv_dict[p_uid]['ucode']
  671. p['detect_interval'] = uv_dict[p_uid]['detect_interval']
  672. p['detect_status'] = uv_dict[p_uid]['detect_status']
  673. p['detect_group'] = uv_dict[p_uid]['detect_group']
  674. p['region_alexa'] = uv_dict[p_uid]['region_alexa']
  675. p['is_alexa'] = uv_dict[p_uid]['is_alexa']
  676. p['deviceModel'] = uv_dict[p_uid]['deviceModel']
  677. p['TimeZone'] = uv_dict[p_uid]['TimeZone']
  678. p['TimeStatus'] = uv_dict[p_uid]['TimeStatus']
  679. p['SpaceUsable'] = uv_dict[p_uid]['SpaceUsable']
  680. p['SpaceSum'] = uv_dict[p_uid]['SpaceSum']
  681. p['MirrorType'] = uv_dict[p_uid]['MirrorType']
  682. p['RecordType'] = uv_dict[p_uid]['RecordType']
  683. p['OutdoorModel'] = uv_dict[p_uid]['OutdoorModel']
  684. p['WIFIName'] = uv_dict[p_uid]['WIFIName']
  685. p['isDetector'] = uv_dict[p_uid]['isDetector']
  686. p['DetectorRank'] = uv_dict[p_uid]['DetectorRank']
  687. p['is_human'] = uv_dict[p_uid]['is_human']
  688. p['is_custom_voice'] = uv_dict[p_uid]['is_custom_voice']
  689. p['is_ptz'] = uv_dict[p_uid]['is_ptz']
  690. p['channels'] = uv_dict[p_uid]['channels']
  691. p['double_wifi'] = uv_dict[p_uid]['double_wifi']
  692. p['mobile4G'] = uv_dict[p_uid]['mobile4G']
  693. p['is_ai'] = uv_dict[p_uid]['is_ai']
  694. # 设备昵称 调用影子信息昵称,先阶段不可
  695. if uv_dict[p_uid]['nickname']:
  696. p['NickName'] = uv_dict[p_uid]['nickname']
  697. else:
  698. # 设备版本号
  699. p['uid_version'] = ''
  700. p['ucode'] = ''
  701. data.append(p)
  702. result = data
  703. if NickName:
  704. # print('NickName搜索缓存')
  705. data = []
  706. for index, item in enumerate(result):
  707. if NickName == item['NickName']:
  708. # 加密
  709. item['View_Password'] = self.encrypt_pwd(item['View_Password'])
  710. data.append(item)
  711. return response.json(0, data)
  712. if uid:
  713. # print('uid搜索缓存')
  714. data = []
  715. for index, item in enumerate(result):
  716. if uid != item['UID']:
  717. continue
  718. else:
  719. item['View_Password'] = self.encrypt_pwd(item['View_Password']) # 加密
  720. data.append(item)
  721. return response.json(0, data)
  722. return response.json(0, data)
  723. items = []
  724. # print('缓存分页')
  725. for index, item in enumerate(result):
  726. if (page - 1) * line <= index:
  727. if index < page * line:
  728. # 加密
  729. item['View_Password'] = self.encrypt_pwd(item['View_Password'])
  730. print(item)
  731. items.append(item)
  732. print(items)
  733. return response.json(0, items)
  734. def do_fuzzy_query(self, userID, request_dict, response):
  735. fuzzy = request_dict.get('fuzzy', None)
  736. page = request_dict.get('page', None)
  737. line = request_dict.get('line', None)
  738. if page and line:
  739. page = int(page)
  740. line = int(line)
  741. device_qs = Device_Info.objects.filter(userID=userID)
  742. device_qs = device_qs.filter(~Q(isExist=2))
  743. if device_qs.exists():
  744. if fuzzy:
  745. device_qs = device_qs.filter(Q(UID__icontains=fuzzy) | Q(NickName__icontains=fuzzy))
  746. device_qs = device_qs.values('id', 'userID', 'NickName', 'UID', 'View_Account',
  747. 'View_Password', 'ChannelIndex', 'Type', 'isShare',
  748. 'primaryUserID', 'primaryMaster', 'data_joined', 'vodPrimaryUserID',
  749. 'vodPrimaryMaster', 'userID__userEmail',
  750. 'version', 'isVod', 'isExist', 'NotificationMode', 'isCameraOpenCloud',
  751. 'serial_number')
  752. dvls = CommonService.qs_to_list(device_qs)
  753. uid_list = []
  754. for dvl in dvls:
  755. uid_list.append(dvl['UID'])
  756. ubqs = UID_Bucket.objects.filter(uid__in=uid_list). \
  757. values('bucket__content', 'status', 'channel', 'endTime', 'uid')
  758. upqs = UID_Preview.objects.filter(uid__in=uid_list).order_by('channel').values('id', 'uid', 'channel')
  759. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  760. bucket = oss2.Bucket(auth, 'oss-cn-hongkong.aliyuncs.com', 'statres')
  761. nowTime = int(time.time())
  762. data = []
  763. # 设备拓展信息表
  764. us_qs = UidSetModel.objects.filter(uid__in=uid_list).values('id', 'uid', 'version', 'nickname', 'ucode',
  765. 'detect_status', 'detect_group',
  766. 'detect_interval',
  767. 'region_alexa', 'is_alexa', 'deviceModel',
  768. 'TimeZone', 'TimeStatus', 'SpaceUsable',
  769. 'SpaceSum', 'MirrorType', 'RecordType',
  770. 'OutdoorModel', 'WIFIName', 'isDetector',
  771. 'DetectorRank')
  772. uv_dict = {}
  773. for us in us_qs:
  774. uv_dict[us['uid']] = {
  775. 'version': us['version'],
  776. 'nickname': us['nickname'],
  777. 'ucode': us['ucode'],
  778. 'detect_interval': us['detect_interval'],
  779. 'detect_group': us['detect_group'],
  780. 'detect_status': us['detect_status'],
  781. 'region_alexa': us['region_alexa'],
  782. 'is_alexa': us['is_alexa'],
  783. 'deviceModel': us['deviceModel'],
  784. 'TimeZone': us['TimeZone'],
  785. 'TimeStatus': us['TimeStatus'],
  786. 'SpaceUsable': us['SpaceUsable'],
  787. 'SpaceSum': us['SpaceSum'],
  788. 'MirrorType': us['MirrorType'],
  789. 'RecordType': us['RecordType'],
  790. 'OutdoorModel': us['OutdoorModel'],
  791. 'WIFIName': us['WIFIName'],
  792. 'isDetector': us['isDetector'],
  793. 'DetectorRank': us['DetectorRank']
  794. }
  795. # 从uid_channel里面取出通道配置信息
  796. ucs_qs = UidChannelSetModel.objects.filter(uid__id=us['id']).values()
  797. channels = []
  798. for ucs in ucs_qs:
  799. channel = {
  800. 'channel': ucs['channel'],
  801. 'channel_name': ucs['channel_name'],
  802. 'pir_audio': ucs['pir_audio'],
  803. 'mic_audio': ucs['mic_audio'],
  804. 'battery_status': ucs['battery_status'],
  805. 'battery_level': ucs['battery_level'],
  806. 'sleep_status': ucs['sleep_status'],
  807. 'sleep_time': ucs['sleep_time'],
  808. 'light_night_model': ucs['light_night_model'],
  809. 'light_alarm_type': ucs['light_alarm_type'],
  810. 'light_alarm_level': ucs['light_alarm_level'],
  811. 'light_alarm_man_en': ucs['light_alarm_man_en'],
  812. 'light_alarm_vol': ucs['light_alarm_vol'],
  813. 'light_long_light': ucs['light_long_light']
  814. }
  815. channels.append(channel)
  816. uv_dict[us['uid']]['channels'] = channels
  817. for p in dvls:
  818. # 获取iot_deviceInfo表的endpoint和token_iot_number
  819. p['iot'] = []
  820. if p['serial_number']: # 存在序列号根据序列号查询
  821. iotdeviceInfo_qs = iotdeviceInfoModel.objects.filter(serial_number=p['serial_number'][0:6])
  822. else: # 根据uid查询
  823. iotdeviceInfo_qs = iotdeviceInfoModel.objects.filter(uid=p['UID'])
  824. if iotdeviceInfo_qs.exists():
  825. iotdeviceInfo = iotdeviceInfo_qs.values('endpoint', 'token_iot_number')
  826. p['iot'].append({
  827. 'endpoint': iotdeviceInfo[0]['endpoint'],
  828. 'token_iot_number': iotdeviceInfo[0]['token_iot_number']
  829. })
  830. p['vod'] = []
  831. for dm in ubqs:
  832. if p['UID'] == dm['uid']:
  833. if dm['endTime'] > nowTime:
  834. p['vod'].append(dm)
  835. p['preview'] = []
  836. for up in upqs:
  837. if p['UID'] == up['uid']:
  838. obj = 'uid_preview/{uid}/channel_{channel}.png'.format(uid=up['uid'], channel=up['channel'])
  839. img_sign = bucket.sign_url('GET', obj, 300)
  840. p['preview'].append(img_sign)
  841. p_uid = p['UID']
  842. if p_uid in uv_dict:
  843. # 设备版本号
  844. p['uid_version'] = uv_dict[p_uid]['version']
  845. p['ucode'] = uv_dict[p_uid]['ucode']
  846. p['detect_interval'] = uv_dict[p_uid]['detect_interval']
  847. p['detect_status'] = uv_dict[p_uid]['detect_status']
  848. p['detect_group'] = uv_dict[p_uid]['detect_group']
  849. p['region_alexa'] = uv_dict[p_uid]['region_alexa']
  850. p['is_alexa'] = uv_dict[p_uid]['is_alexa']
  851. p['deviceModel'] = uv_dict[p_uid]['deviceModel']
  852. p['TimeZone'] = uv_dict[p_uid]['TimeZone']
  853. p['TimeStatus'] = uv_dict[p_uid]['TimeStatus']
  854. p['SpaceUsable'] = uv_dict[p_uid]['SpaceUsable']
  855. p['SpaceSum'] = uv_dict[p_uid]['SpaceSum']
  856. p['MirrorType'] = uv_dict[p_uid]['MirrorType']
  857. p['RecordType'] = uv_dict[p_uid]['RecordType']
  858. p['OutdoorModel'] = uv_dict[p_uid]['OutdoorModel']
  859. p['WIFIName'] = uv_dict[p_uid]['WIFIName']
  860. p['isDetector'] = uv_dict[p_uid]['isDetector']
  861. p['DetectorRank'] = uv_dict[p_uid]['DetectorRank']
  862. p['channels'] = uv_dict[p_uid]['channels']
  863. # 设备昵称 调用影子信息昵称,先阶段不可
  864. if uv_dict[p_uid]['nickname']:
  865. p['NickName'] = uv_dict[p_uid]['nickname']
  866. else:
  867. # 设备版本号
  868. p['uid_version'] = ''
  869. p['ucode'] = ''
  870. data.append(p)
  871. result = data
  872. items = []
  873. # print('缓存分页')
  874. for index, item in enumerate(result):
  875. if (page - 1) * line <= index:
  876. if index < page * line:
  877. # 加密
  878. item['View_Password'] = self.encrypt_pwd(item['View_Password'])
  879. print(item)
  880. items.append(item)
  881. print(items)
  882. return response.json(0, items)
  883. else:
  884. return response.json(0, [])
  885. else:
  886. return response.json(444)
  887. # 加密
  888. def encrypt_pwd(self, userPwd):
  889. for i in range(1, 4):
  890. if i == 1:
  891. userPwd = RandomStr(3, False) + userPwd + RandomStr(3, False)
  892. userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8')
  893. if i == 2:
  894. userPwd = RandomStr(2, False) + str(userPwd) + RandomStr(2, False)
  895. userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8')
  896. if i == 3:
  897. userPwd = RandomStr(1, False) + str(userPwd) + RandomStr(1, False)
  898. userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8')
  899. return userPwd
  900. # 解密
  901. def decode_pwd(self, password):
  902. for i in range(1, 4):
  903. if i == 1:
  904. # 第一次先解密
  905. password = base64.b64decode(password)
  906. password = password.decode('utf-8')
  907. # 截去第一位,最后一位
  908. password = password[1:-1]
  909. if i == 2:
  910. # 第2次先解密
  911. password = base64.b64decode(password)
  912. password = password.decode('utf-8')
  913. # 去前2位,后2位
  914. password = password[2:-2]
  915. if i == 3:
  916. # 第3次先解密
  917. password = base64.b64decode(password)
  918. password = password.decode('utf-8')
  919. # 去前3位,后3位
  920. password = password[3:-3]
  921. return password
  922. def do_mainUserDevice(self, request_dict, response):
  923. UID = request_dict.get('UID')
  924. dvq = Device_Info.objects.filter(UID=UID)
  925. dvq = dvq.filter(~Q(vodPrimaryUserID='')).values('vodPrimaryUserID')
  926. qs = {}
  927. if dvq.exists():
  928. qs = Device_User.objects.filter(userID=dvq[0]['vodPrimaryUserID']).values('userID', 'NickName', 'username',
  929. 'userEmail', 'phone')
  930. NickName = qs[0]['username']
  931. userEmail = qs[0]['userEmail']
  932. phone = qs[0]['phone']
  933. username = qs[0]['username']
  934. qs = CommonService.qs_to_list(qs)
  935. if NickName =='':
  936. qs[0]['NickName'] = username
  937. # if userEmail =='':
  938. # qs[0]['userEmail'] = NickName
  939. if phone =='':
  940. qs[0]['phone'] = NickName
  941. # if not qs:
  942. # uidq = UIDMainUser.objects.filter(UID=UID).values('user_id')
  943. # if uidq.exists():
  944. # qs = Device_User.objects.filter(userID=uidq[0]['user_id']).values('userID', 'NickName', 'username', 'userEmail', 'phone')
  945. # qs = CommonService.qs_to_list(qs)
  946. return response.json(0, qs)
  947. def not_login_do_mainUserDevice(self, request_dict, response):
  948. UID = request_dict.get('UID')
  949. token = request_dict.get('token', None)
  950. time_stamp = request_dict.get('time_stamp', None)
  951. if not all([token, time_stamp]):
  952. return response.json(444)
  953. # 时间戳token校验
  954. if not CommonService.check_time_stamp_token(token, time_stamp):
  955. return response.json(13)
  956. dvq = Device_Info.objects.filter(UID=UID)
  957. dvq = dvq.filter(~Q(vodPrimaryUserID='')).values('vodPrimaryUserID')
  958. qs = {}
  959. if dvq.exists():
  960. qs = Device_User.objects.filter(userID=dvq[0]['vodPrimaryUserID']).values('userID', 'NickName', 'username',
  961. 'userEmail', 'phone')
  962. NickName = qs[0]['username']
  963. phone = qs[0]['phone']
  964. username = qs[0]['username']
  965. qs = CommonService.qs_to_list(qs)
  966. if NickName =='':
  967. qs[0]['NickName'] = username
  968. if phone =='':
  969. qs[0]['phone'] = NickName
  970. return response.json(0, qs)
  971. def do_get_device_features(self, request_dict, response):
  972. uid = request_dict.get('uid', None)
  973. if uid:
  974. uid = CommonService.decode_data(uid)
  975. if uid:
  976. uid_qs = UidSetModel.objects.filter(uid=uid)
  977. if uid_qs.exists():
  978. uid_qs = uid_qs.values('is_alexa', 'is_human', 'is_custom_voice', 'double_wifi')
  979. return response.json(0, uid_qs[0])
  980. else:
  981. return response.json(173)
  982. else:
  983. return response.json(444)
  984. else:
  985. return response.json(444)