EquipmentManagerV3.py 32 KB

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