EquipmentManagerV3.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 Object.RedisObject import RedisObject
  11. from Ansjer.config import OSS_STS_ACCESS_SECRET, OSS_STS_ACCESS_KEY, BASE_DIR
  12. from Model.models import Device_Info, UID_Bucket, UID_Preview, UidSetModel, UidPushModel, UidChannelSetModel, \
  13. Device_User
  14. from Object.ResponseObject import ResponseObject
  15. from Object.TokenObject import TokenObject
  16. from Service.CommonService import CommonService
  17. from Service.ModelService import ModelService
  18. import time, json
  19. class EquipmentManagerV3(View):
  20. def get(self, request, *args, **kwargs):
  21. request.encoding = 'utf-8'
  22. operation = kwargs.get('operation')
  23. return self.validation(request.GET, request, operation)
  24. def post(self, request, *args, **kwargs):
  25. request.encoding = 'utf-8'
  26. operation = kwargs.get('operation')
  27. return self.validation(request.POST, request, operation)
  28. def validation(self, request_dict, request, operation):
  29. response = ResponseObject()
  30. token = request_dict.get('token', None)
  31. # 设备主键uid
  32. tko = TokenObject(token)
  33. if tko.code == 0:
  34. response.lang = tko.lang
  35. userID = tko.userID
  36. # 手机端添加设备,查询,修改
  37. if operation == 'add':
  38. return self.do_add(userID, request_dict, response, request)
  39. elif operation == 'query':
  40. return self.do_query(userID, request_dict, response)
  41. elif operation == 'modify':
  42. return self.do_modify(userID, request_dict, response, request)
  43. elif operation == 'fuzzyQuery':
  44. return self.do_fuzzy_query(userID, request_dict, response)
  45. elif operation == 'mainUserDevice':
  46. return self.do_mainUserDevice( request_dict, response)
  47. else:
  48. return response.json(414)
  49. else:
  50. return response.json(tko.code)
  51. def do_add(self, userID, request_dict, response, request):
  52. token = request_dict.get('token', None)
  53. UID = request_dict.get('UID', None)
  54. NickName = request_dict.get('NickName', None)
  55. View_Account = request_dict.get('View_Account', None)
  56. View_Password = request_dict.get('View_Password', '')
  57. print("准备解密")
  58. View_Password = self.decode_pwd(View_Password)
  59. Type = request_dict.get('Type', None)
  60. ChannelIndex = request_dict.get('ChannelIndex', None)
  61. isCheckMainUser = request_dict.get('isCheckMainUser', None)
  62. isMainUserExists = False
  63. if all([UID, NickName, View_Account, Type, ChannelIndex]):
  64. tko = TokenObject(token)
  65. response.lang = tko.lang
  66. if tko.code == 0:
  67. userID = tko.userID
  68. re_uid = re.compile(r'^[A-Za-z0-9]{20}$')
  69. if re_uid.match(UID):
  70. is_exist = Device_Info.objects.filter(UID=UID, userID_id=userID)
  71. if is_exist:
  72. # 判断设备是否已存在
  73. if is_exist[0].isExist == 1:
  74. return response.json(174)
  75. else:
  76. is_exist.delete()
  77. if UID == '98UXAA8BRPA35VAL111A':
  78. asy = threading.Thread(target=ModelService.add_log,
  79. args=(CommonService.get_ip_address(request), userID, 'addV3'))
  80. asy.start()
  81. return response.json(10, 'illegal uid: {uid}'.format(uid=UID))
  82. pk = CommonService.getUserID(getUser=False)
  83. userName = Device_User.objects.get(userID=userID).username
  84. main_exist = Device_Info.objects.filter(UID=UID)
  85. main_exist = main_exist.filter(~Q(vodPrimaryUserID='')).values('vodPrimaryUserID', 'vodPrimaryMaster')
  86. vodPrimaryUserID = userID
  87. vodPrimaryMaster = userName
  88. if main_exist.exists():
  89. vodPrimaryUserID = main_exist[0]['vodPrimaryUserID']
  90. vodPrimaryMaster = main_exist[0]['vodPrimaryMaster']
  91. is_bind = Device_Info.objects.filter(UID=UID, isShare=False).values('userID__userID')
  92. # 判断是否有已绑定用户
  93. if is_bind and isCheckMainUser == '1':
  94. res = {
  95. 'id': pk,
  96. 'userID': userID,
  97. 'NickName': NickName,
  98. 'UID': UID,
  99. 'View_Account': View_Account,
  100. 'View_Password': View_Password,
  101. 'ChannelIndex': ChannelIndex,
  102. 'Type': Type,
  103. 'isShare': False,
  104. 'primaryUserID': '',
  105. 'primaryMaster': '',
  106. 'vodPrimaryUserID': vodPrimaryUserID,
  107. 'vodPrimaryMaster': vodPrimaryMaster,
  108. 'data_joined': '',
  109. 'version': '',
  110. 'isVod': 0,
  111. 'isExist': 1,
  112. 'userID__userEmail': ''
  113. }
  114. res['vod'] = [
  115. {
  116. "status": 1,
  117. "channel": ChannelIndex,
  118. "endTime": '',
  119. "bucket__content": '',
  120. "uid": UID
  121. }
  122. ]
  123. res['isMainUserExists'] = 1
  124. return response.json(0, res)
  125. try:
  126. # 判断是否有用户绑定
  127. nowTime = int(time.time())
  128. us_qs = UidSetModel.objects.filter(uid=UID)
  129. if not us_qs.exists():
  130. uid_set_create_dict = {
  131. 'uid': UID,
  132. 'addTime': nowTime,
  133. 'updTime': nowTime,
  134. 'ip': CommonService.get_ip_address(request_dict),
  135. 'channel': ChannelIndex,
  136. 'nickname': NickName,
  137. }
  138. UidSetModel.objects.create(**uid_set_create_dict)
  139. else:
  140. us_qs.update(nickname=NickName)
  141. userDevice = Device_Info(id=pk, userID_id=userID, UID=UID,
  142. NickName=NickName, View_Account=View_Account,
  143. View_Password=View_Password, Type=Type, ChannelIndex=ChannelIndex,
  144. vodPrimaryUserID=vodPrimaryUserID, vodPrimaryMaster=vodPrimaryMaster)
  145. userDevice.save()
  146. Device_Info.objects.filter(UID=UID).update(vodPrimaryUserID=vodPrimaryUserID,
  147. vodPrimaryMaster=vodPrimaryMaster)
  148. if not us_qs.exists():
  149. us_qs = UidSetModel.objects.filter(uid=UID)
  150. if us_qs.exists() and us_qs[0].is_alexa == 1:
  151. asy = threading.Thread(target=ModelService.notify_alexa_add, args=(UID, userID, NickName))
  152. asy.start()
  153. # redisObj = RedisObject(db=8)
  154. # redisObj.del_data(key='uid_qs_' + userID)
  155. except Exception as e:
  156. return response.json(10, repr(e))
  157. else:
  158. dvqs = Device_Info.objects.filter(id=pk).values('id', 'userID', 'NickName', 'UID',
  159. 'View_Account',
  160. 'View_Password', 'ChannelIndex', 'Type',
  161. 'isShare',
  162. 'primaryUserID', 'primaryMaster',
  163. 'vodPrimaryUserID', 'vodPrimaryMaster',
  164. 'userID__userEmail',
  165. 'data_joined', 'version',
  166. 'isVod', 'isExist')
  167. dvql = CommonService.qs_to_list(dvqs)
  168. ubqs = UID_Bucket.objects.filter(uid=UID). \
  169. values('bucket__content', 'status', 'channel', 'endTime', 'uid')
  170. res = dvql[0]
  171. res['vod'] = list(ubqs)
  172. return response.json(0, res)
  173. else:
  174. return response.json(444, {'param': 'UID'})
  175. else:
  176. return response.json(tko.code)
  177. else:
  178. return response.json(444, {'param': 'UID,NickName,View_Account,View_Password,Type,ChannelIndex'})
  179. def do_modify(self, userID, request_dict, response, request):
  180. token = request_dict.get('token', None)
  181. deviceContent = request_dict.get('content', None)
  182. id = request_dict.get('id', None)
  183. if not deviceContent or not id:
  184. return response.json(444, 'content,id')
  185. tko = TokenObject(token)
  186. response.lang = tko.lang
  187. if tko.code != 0:
  188. return response.json(tko.code)
  189. userID = tko.userID
  190. if userID is None:
  191. return response.json(309)
  192. deviceData = None
  193. dev_info_qs = None
  194. try:
  195. # deviceData = json.loads(deviceContent)
  196. deviceData = eval(deviceContent)
  197. if deviceData.__contains__('userID_id'):
  198. asy = threading.Thread(target=ModelService.update_log,
  199. args=(CommonService.get_ip_address(request), userID, 'modifyV3', deviceData, id))
  200. asy.start()
  201. return response.json(10, '110')
  202. if deviceData.__contains__('UID'):
  203. del deviceData['UID']
  204. # print(deviceData['View_Password'])
  205. if deviceData.__contains__('View_Password'):
  206. deviceData['View_Password'] = self.decode_pwd(deviceData['View_Password'])
  207. dev_info_qs = Device_Info.objects.filter(userID_id=userID, id=id)
  208. dev_info_qs.update(**deviceData)
  209. except Exception as e:
  210. print(e)
  211. return response.json(177, repr(e))
  212. else:
  213. qs = Device_Info.objects.filter(userID_id=userID, id=id)
  214. res = CommonService.qs_to_dict(qs)
  215. if qs.exists():
  216. uid = qs[0].UID
  217. nickname = qs[0].NickName
  218. # 增加设备影子信息修改昵称 start
  219. us_qs = UidSetModel.objects.filter(uid=uid)
  220. if us_qs.exists():
  221. if deviceData.__contains__('NickName'):
  222. us_qs.update(nickname=nickname)
  223. else:
  224. ChannelIndex = qs[0].ChannelIndex
  225. nowTime = int(time.time())
  226. uid_set_create_dict = {
  227. 'uid': uid,
  228. 'addTime': nowTime,
  229. 'updTime': nowTime,
  230. # 'ip': CommonService.get_ip_address(request),
  231. 'channel': ChannelIndex,
  232. 'nickname': nickname,
  233. }
  234. UidSetModel.objects.create(**uid_set_create_dict)
  235. di_qs = Device_Info.objects.filter(UID=uid)
  236. di_qs.update(NickName=nickname)
  237. if deviceData is not None and deviceData.__contains__('NickName') and us_qs[0].is_alexa == 1:
  238. asy = threading.Thread(target=ModelService.notify_alexa_add, args=(uid, userID, nickname))
  239. asy.start()
  240. # redisObj = RedisObject(db=8)
  241. # redisObj.del_data(key='uid_qs_' + userID)
  242. return response.json(0, res)
  243. # 新查询设备字段
  244. def do_query(self, userID, request_dict, response):
  245. token = request_dict.get('token', None)
  246. page = request_dict.get('page', None)
  247. line = request_dict.get('line', None)
  248. NickName = request_dict.get('NickName', None)
  249. if not token or not page or not line:
  250. return response.json(444)
  251. page = int(page)
  252. line = int(line)
  253. uid = request_dict.get('uid', None)
  254. tko = TokenObject(token)
  255. response.lang = tko.lang
  256. if page <= 0:
  257. return response.json(0)
  258. if tko.code == 0:
  259. userID = tko.userID
  260. dvqs = Device_Info.objects.filter(userID_id=userID)
  261. # # 过滤已重置的设备
  262. dvqs = dvqs.filter(~Q(isExist=2))
  263. dvql = dvqs.values('id', 'userID', 'NickName', 'UID', 'View_Account',
  264. 'View_Password', 'ChannelIndex', 'Type', 'isShare',
  265. 'primaryUserID', 'primaryMaster', 'data_joined', 'vodPrimaryUserID', 'vodPrimaryMaster',
  266. 'userID__userEmail',
  267. 'version', 'isVod', 'isExist', 'NotificationMode')
  268. dvls = CommonService.qs_to_list(dvql)
  269. uid_list = []
  270. for dvl in dvls:
  271. if dvl['primaryUserID'] and dvl['id'] == dvl['primaryUserID']:
  272. dvl['isPrimaryUser'] = 1
  273. else:
  274. dvl['isPrimaryUser'] = 0
  275. uid_list.append(dvl['UID'])
  276. ubqs = UID_Bucket.objects.filter(uid__in=uid_list). \
  277. values('bucket__content', 'status', 'channel', 'endTime', 'uid')
  278. upqs = UID_Preview.objects.filter(uid__in=uid_list).order_by('channel').values('id', 'uid', 'channel')
  279. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  280. bucket = oss2.Bucket(auth, 'oss-cn-hongkong.aliyuncs.com', 'statres')
  281. nowTime = int(time.time())
  282. data = []
  283. # 设备拓展信息表
  284. us_qs = UidSetModel.objects.filter(uid__in=uid_list).values('id', 'uid', 'version', 'nickname', 'ucode',
  285. 'detect_status', 'detect_group',
  286. 'detect_interval',
  287. 'region_alexa', 'is_alexa', 'deviceModel',
  288. 'TimeZone', 'TimeStatus', 'SpaceUsable',
  289. 'SpaceSum', 'MirrorType', 'RecordType',
  290. 'OutdoorModel', 'WIFIName', 'isDetector',
  291. 'DetectorRank')
  292. uv_dict = {}
  293. for us in us_qs:
  294. uv_dict[us['uid']] = {
  295. 'version': us['version'],
  296. 'nickname': us['nickname'],
  297. 'ucode': us['ucode'],
  298. 'detect_interval': us['detect_interval'],
  299. 'detect_group': us['detect_group'],
  300. 'detect_status': us['detect_status'],
  301. 'region_alexa': us['region_alexa'],
  302. 'is_alexa': us['is_alexa'],
  303. 'deviceModel': us['deviceModel'],
  304. 'TimeZone': us['TimeZone'],
  305. 'TimeStatus': us['TimeStatus'],
  306. 'SpaceUsable': us['SpaceUsable'],
  307. 'SpaceSum': us['SpaceSum'],
  308. 'MirrorType': us['MirrorType'],
  309. 'RecordType': us['RecordType'],
  310. 'OutdoorModel': us['OutdoorModel'],
  311. 'WIFIName': us['WIFIName'],
  312. 'isDetector': us['isDetector'],
  313. 'DetectorRank': us['DetectorRank']
  314. }
  315. # 从uid_channel里面取出通道配置信息
  316. ucs_qs = UidChannelSetModel.objects.filter(uid__id=us['id']).values('channel', 'pir_audio', 'mic_audio',
  317. 'battery_status', 'battery_level',
  318. 'sleep_status', 'sleep_time',
  319. 'light_night_model',
  320. 'light_alarm_type',
  321. 'light_alarm_level',
  322. 'light_alarm_man_en',
  323. 'light_alarm_vol',
  324. 'light_long_light'
  325. )
  326. channels = []
  327. for ucs in ucs_qs:
  328. channels_dict = {
  329. 'channel': ucs['channel'],
  330. 'pir_audio': ucs['pir_audio'],
  331. 'mic_audio': ucs['mic_audio'],
  332. 'battery_status': ucs['battery_status'],
  333. 'battery_level': ucs['battery_level'],
  334. 'sleep_status': ucs['sleep_status'],
  335. 'sleep_time': ucs['sleep_time'],
  336. 'light_night_model': ucs['light_night_model'],
  337. 'light_alarm_type': ucs['light_alarm_type'],
  338. 'light_alarm_level': ucs['light_alarm_level'],
  339. 'light_alarm_man_en': ucs['light_alarm_man_en'],
  340. 'light_alarm_vol': ucs['light_alarm_vol'],
  341. 'light_long_light': ucs['light_long_light']
  342. }
  343. channels.append(channels_dict)
  344. uv_dict[us['uid']]['channels'] = channels
  345. for p in dvls:
  346. p['vod'] = []
  347. for dm in ubqs:
  348. if p['UID'] == dm['uid']:
  349. if dm['endTime'] > nowTime:
  350. p['vod'].append(dm)
  351. p['preview'] = []
  352. for up in upqs:
  353. if p['UID'] == up['uid']:
  354. obj = 'uid_preview/{uid}/channel_{channel}.png'.format(uid=up['uid'], channel=up['channel'])
  355. img_sign = bucket.sign_url('GET', obj, 300)
  356. p['preview'].append(img_sign)
  357. p_uid = p['UID']
  358. if p_uid in uv_dict:
  359. # 设备版本号
  360. p['uid_version'] = uv_dict[p_uid]['version']
  361. p['ucode'] = uv_dict[p_uid]['ucode']
  362. p['detect_interval'] = uv_dict[p_uid]['detect_interval']
  363. p['detect_status'] = uv_dict[p_uid]['detect_status']
  364. p['detect_group'] = uv_dict[p_uid]['detect_group']
  365. p['region_alexa'] = uv_dict[p_uid]['region_alexa']
  366. p['is_alexa'] = uv_dict[p_uid]['is_alexa']
  367. p['deviceModel'] = uv_dict[p_uid]['deviceModel']
  368. p['TimeZone'] = uv_dict[p_uid]['TimeZone']
  369. p['TimeStatus'] = uv_dict[p_uid]['TimeStatus']
  370. p['SpaceUsable'] = uv_dict[p_uid]['SpaceUsable']
  371. p['SpaceSum'] = uv_dict[p_uid]['SpaceSum']
  372. p['MirrorType'] = uv_dict[p_uid]['MirrorType']
  373. p['RecordType'] = uv_dict[p_uid]['RecordType']
  374. p['OutdoorModel'] = uv_dict[p_uid]['OutdoorModel']
  375. p['WIFIName'] = uv_dict[p_uid]['WIFIName']
  376. p['isDetector'] = uv_dict[p_uid]['isDetector']
  377. p['DetectorRank'] = uv_dict[p_uid]['DetectorRank']
  378. p['channels'] = uv_dict[p_uid]['channels']
  379. # 设备昵称 调用影子信息昵称,先阶段不可
  380. if uv_dict[p_uid]['nickname']:
  381. p['NickName'] = uv_dict[p_uid]['nickname']
  382. else:
  383. # 设备版本号
  384. p['uid_version'] = ''
  385. p['ucode'] = ''
  386. data.append(p)
  387. result = data
  388. if NickName:
  389. # print('NickName搜索缓存')
  390. data = []
  391. for index, item in enumerate(result):
  392. if NickName == item['NickName']:
  393. # 加密
  394. item['View_Password'] = self.encrypt_pwd(item['View_Password'])
  395. data.append(item)
  396. return response.json(0, data)
  397. if uid:
  398. # print('uid搜索缓存')
  399. data = []
  400. for index, item in enumerate(result):
  401. if uid == item['UID']:
  402. # 加密
  403. item['View_Password'] = self.encrypt_pwd(item['View_Password'])
  404. data.append(item)
  405. return response.json(0, data)
  406. items = []
  407. # print('缓存分页')
  408. for index, item in enumerate(result):
  409. if (page - 1) * line <= index:
  410. if index < page * line:
  411. # 加密
  412. item['View_Password'] = self.encrypt_pwd(item['View_Password'])
  413. print(item)
  414. items.append(item)
  415. print(items)
  416. return response.json(0, items)
  417. else:
  418. return response.json(tko.code)
  419. def do_fuzzy_query(self, userID, request_dict, response):
  420. fuzzy = request_dict.get('fuzzy', None)
  421. page = request_dict.get('page', None)
  422. line = request_dict.get('line', None)
  423. if page and line:
  424. page = int(page)
  425. line = int(line)
  426. device_qs = Device_Info.objects.filter(userID=userID)
  427. device_qs = device_qs.filter(~Q(isExist=2))
  428. if device_qs.exists():
  429. if fuzzy:
  430. device_qs = device_qs.filter(Q(UID__contains=fuzzy) | Q(NickName__contains=fuzzy))
  431. device_qs = device_qs.values('id', 'userID', 'NickName', 'UID', 'View_Account',
  432. 'View_Password', 'ChannelIndex', 'Type', 'isShare',
  433. 'primaryUserID', 'primaryMaster', 'data_joined', 'vodPrimaryUserID',
  434. 'vodPrimaryMaster', 'userID__userEmail',
  435. 'version', 'isVod', 'isExist', 'NotificationMode')
  436. dvls = CommonService.qs_to_list(device_qs)
  437. uid_list = []
  438. for dvl in dvls:
  439. uid_list.append(dvl['UID'])
  440. ubqs = UID_Bucket.objects.filter(uid__in=uid_list). \
  441. values('bucket__content', 'status', 'channel', 'endTime', 'uid')
  442. upqs = UID_Preview.objects.filter(uid__in=uid_list).order_by('channel').values('id', 'uid', 'channel')
  443. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  444. bucket = oss2.Bucket(auth, 'oss-cn-hongkong.aliyuncs.com', 'statres')
  445. nowTime = int(time.time())
  446. data = []
  447. # 设备拓展信息表
  448. us_qs = UidSetModel.objects.filter(uid__in=uid_list).values('id', 'uid', 'version', 'nickname', 'ucode',
  449. 'detect_status', 'detect_group',
  450. 'detect_interval',
  451. 'region_alexa', 'is_alexa', 'deviceModel',
  452. 'TimeZone', 'TimeStatus', 'SpaceUsable',
  453. 'SpaceSum', 'MirrorType', 'RecordType',
  454. 'OutdoorModel', 'WIFIName', 'isDetector',
  455. 'DetectorRank')
  456. uv_dict = {}
  457. for us in us_qs:
  458. uv_dict[us['uid']] = {
  459. 'version': us['version'],
  460. 'nickname': us['nickname'],
  461. 'ucode': us['ucode'],
  462. 'detect_interval': us['detect_interval'],
  463. 'detect_group': us['detect_group'],
  464. 'detect_status': us['detect_status'],
  465. 'region_alexa': us['region_alexa'],
  466. 'is_alexa': us['is_alexa'],
  467. 'deviceModel': us['deviceModel'],
  468. 'TimeZone': us['TimeZone'],
  469. 'TimeStatus': us['TimeStatus'],
  470. 'SpaceUsable': us['SpaceUsable'],
  471. 'SpaceSum': us['SpaceSum'],
  472. 'MirrorType': us['MirrorType'],
  473. 'RecordType': us['RecordType'],
  474. 'OutdoorModel': us['OutdoorModel'],
  475. 'WIFIName': us['WIFIName'],
  476. 'isDetector': us['isDetector'],
  477. 'DetectorRank': us['DetectorRank']
  478. }
  479. # 从uid_channel里面取出通道配置信息
  480. ucs_qs = UidChannelSetModel.objects.filter(uid__id=us['id']).values()
  481. channels = []
  482. for ucs in ucs_qs:
  483. channel = {
  484. 'channel': ucs['channel'],
  485. 'pir_audio': ucs['pir_audio'],
  486. 'mic_audio': ucs['mic_audio'],
  487. 'battery_status': ucs['battery_status'],
  488. 'battery_level': ucs['battery_level'],
  489. 'sleep_status': ucs['sleep_status'],
  490. 'sleep_time': ucs['sleep_time'],
  491. 'light_night_model': ucs['light_night_model'],
  492. 'light_alarm_type': ucs['light_alarm_type'],
  493. 'light_alarm_level': ucs['light_alarm_level'],
  494. 'light_alarm_man_en': ucs['light_alarm_man_en'],
  495. 'light_alarm_vol': ucs['light_alarm_vol'],
  496. 'light_long_light': ucs['light_long_light']
  497. }
  498. channels.append(channel)
  499. uv_dict[us['uid']]['channels'] = channels
  500. for p in dvls:
  501. p['vod'] = []
  502. for dm in ubqs:
  503. if p['UID'] == dm['uid']:
  504. if dm['endTime'] > nowTime:
  505. p['vod'].append(dm)
  506. p['preview'] = []
  507. for up in upqs:
  508. if p['UID'] == up['uid']:
  509. obj = 'uid_preview/{uid}/channel_{channel}.png'.format(uid=up['uid'], channel=up['channel'])
  510. img_sign = bucket.sign_url('GET', obj, 300)
  511. p['preview'].append(img_sign)
  512. p_uid = p['UID']
  513. if p_uid in uv_dict:
  514. # 设备版本号
  515. p['uid_version'] = uv_dict[p_uid]['version']
  516. p['ucode'] = uv_dict[p_uid]['ucode']
  517. p['detect_interval'] = uv_dict[p_uid]['detect_interval']
  518. p['detect_status'] = uv_dict[p_uid]['detect_status']
  519. p['detect_group'] = uv_dict[p_uid]['detect_group']
  520. p['region_alexa'] = uv_dict[p_uid]['region_alexa']
  521. p['is_alexa'] = uv_dict[p_uid]['is_alexa']
  522. p['deviceModel'] = uv_dict[p_uid]['deviceModel']
  523. p['TimeZone'] = uv_dict[p_uid]['TimeZone']
  524. p['TimeStatus'] = uv_dict[p_uid]['TimeStatus']
  525. p['SpaceUsable'] = uv_dict[p_uid]['SpaceUsable']
  526. p['SpaceSum'] = uv_dict[p_uid]['SpaceSum']
  527. p['MirrorType'] = uv_dict[p_uid]['MirrorType']
  528. p['RecordType'] = uv_dict[p_uid]['RecordType']
  529. p['OutdoorModel'] = uv_dict[p_uid]['OutdoorModel']
  530. p['WIFIName'] = uv_dict[p_uid]['WIFIName']
  531. p['isDetector'] = uv_dict[p_uid]['isDetector']
  532. p['DetectorRank'] = uv_dict[p_uid]['DetectorRank']
  533. p['channels'] = uv_dict[p_uid]['channels']
  534. # 设备昵称 调用影子信息昵称,先阶段不可
  535. if uv_dict[p_uid]['nickname']:
  536. p['NickName'] = uv_dict[p_uid]['nickname']
  537. else:
  538. # 设备版本号
  539. p['uid_version'] = ''
  540. p['ucode'] = ''
  541. data.append(p)
  542. result = data
  543. items = []
  544. # print('缓存分页')
  545. for index, item in enumerate(result):
  546. if (page - 1) * line <= index:
  547. if index < page * line:
  548. # 加密
  549. item['View_Password'] = self.encrypt_pwd(item['View_Password'])
  550. print(item)
  551. items.append(item)
  552. print(items)
  553. return response.json(0, items)
  554. else:
  555. return response.json(0, [])
  556. else:
  557. return response.json(444)
  558. # 加密
  559. def encrypt_pwd(self, userPwd):
  560. for i in range(1, 4):
  561. if i == 1:
  562. userPwd = RandomStr(3, False) + userPwd + RandomStr(3, False)
  563. userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8')
  564. if i == 2:
  565. userPwd = RandomStr(2, False) + str(userPwd) + RandomStr(2, False)
  566. userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8')
  567. if i == 3:
  568. userPwd = RandomStr(1, False) + str(userPwd) + RandomStr(1, False)
  569. userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8')
  570. return userPwd
  571. # 解密
  572. def decode_pwd(self, password):
  573. for i in range(1, 4):
  574. if i == 1:
  575. # 第一次先解密
  576. password = base64.b64decode(password)
  577. password = password.decode('utf-8')
  578. # 截去第一位,最后一位
  579. password = password[1:-1]
  580. if i == 2:
  581. # 第2次先解密
  582. password = base64.b64decode(password)
  583. password = password.decode('utf-8')
  584. # 去前2位,后2位
  585. password = password[2:-2]
  586. if i == 3:
  587. # 第3次先解密
  588. password = base64.b64decode(password)
  589. password = password.decode('utf-8')
  590. # 去前3位,后3位
  591. password = password[3:-3]
  592. return password
  593. def do_mainUserDevice(self, request_dict, response):
  594. UID = request_dict.get('UID')
  595. dvq = Device_Info.objects.filter(UID=UID)
  596. dvq = dvq.filter(~Q(vodPrimaryUserID='')).values('vodPrimaryUserID')
  597. qs = {}
  598. if dvq.exists():
  599. qs = Device_User.objects.filter(userID=dvq[0]['vodPrimaryUserID']).values('userID', 'NickName', 'username', 'userEmail', 'phone')
  600. qs = CommonService.qs_to_list(qs)
  601. return response.json(0, qs)