EquipmentManagerV2.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import re
  2. import threading
  3. import time
  4. import traceback
  5. import oss2
  6. from django.db.models import Q
  7. from django.views.generic.base import View
  8. from Ansjer.config import OSS_STS_ACCESS_SECRET, OSS_STS_ACCESS_KEY
  9. from Model.models import Device_Info, UID_Bucket, UID_Preview, UidSetModel, UidPushModel, UidChannelSetModel
  10. from Object.ResponseObject import ResponseObject
  11. from Object.TokenObject import TokenObject
  12. from Service.CommonService import CommonService
  13. from Service.ModelService import ModelService
  14. class EquipmentManagerV2(View):
  15. def get(self, request, *args, **kwargs):
  16. request.encoding = 'utf-8'
  17. operation = kwargs.get('operation')
  18. return self.validation(request.GET, request, operation)
  19. def post(self, request, *args, **kwargs):
  20. request.encoding = 'utf-8'
  21. operation = kwargs.get('operation')
  22. return self.validation(request.POST, request, operation)
  23. def validation(self, request_dict, request, operation):
  24. response = ResponseObject()
  25. token = request_dict.get('token', None)
  26. # 设备主键uid
  27. tko = TokenObject(token)
  28. if tko.code == 0:
  29. response.lang = tko.lang
  30. userID = tko.userID
  31. # if operation == 'add':
  32. # return self.do_add(userID, request_dict, response)
  33. if operation == 'query':
  34. return self.do_query(userID, request_dict, response)
  35. elif operation == 'query_reset':
  36. return self.do_query_reset(userID, request_dict, response)
  37. else:
  38. return response.json(414)
  39. else:
  40. return response.json(tko.code)
  41. def do_add(self, userID, request_dict, response, request):
  42. token = request_dict.get('token', None)
  43. UID = request_dict.get('UID', None)
  44. NickName = request_dict.get('NickName', None)
  45. View_Account = request_dict.get('View_Account', None)
  46. View_Password = request_dict.get('View_Password', '')
  47. Type = request_dict.get('Type', None)
  48. ChannelIndex = request_dict.get('ChannelIndex', None)
  49. if all([UID, NickName, View_Account, Type, ChannelIndex]):
  50. tko = TokenObject(token)
  51. response.lang = tko.lang
  52. if tko.code == 0:
  53. userID = tko.userID
  54. re_uid = re.compile(r'^[A-Za-z0-9]{20}$')
  55. if re_uid.match(UID):
  56. is_exist = Device_Info.objects.filter(UID=UID, userID_id=userID)
  57. if is_exist:
  58. # 判断设备是否已存在
  59. if is_exist[0].isExist == 1:
  60. return response.json(174)
  61. else:
  62. is_exist.delete()
  63. # is_bind = Device_Info.objects.filter(UID=UID, isShare=False)
  64. # # 判断是否有已绑定用户
  65. # if is_bind:
  66. # return response.json(15)
  67. try:
  68. # 判断是否有用户绑定
  69. nowTime = int(time.time())
  70. us_qs = UidSetModel.objects.filter(uid=UID)
  71. if not us_qs.exists():
  72. uid_set_create_dict = {
  73. 'uid': UID,
  74. 'addTime': nowTime,
  75. 'updTime': nowTime,
  76. 'ip': CommonService.get_ip_address(request),
  77. 'channel': ChannelIndex,
  78. 'nickname': NickName,
  79. }
  80. UidSetModel.objects.create(**uid_set_create_dict)
  81. else:
  82. us_qs.update(nickname=NickName)
  83. pk = CommonService.getUserID(getUser=False)
  84. userDevice = Device_Info(id=pk, userID_id=userID, UID=UID,
  85. NickName=NickName, View_Account=View_Account,
  86. View_Password=View_Password, Type=Type, ChannelIndex=ChannelIndex)
  87. userDevice.save()
  88. except Exception as e:
  89. return response.json(10, repr(e))
  90. else:
  91. dvqs = Device_Info.objects.filter(id=pk).values('id', 'userID', 'NickName', 'UID',
  92. 'View_Account',
  93. 'View_Password', 'ChannelIndex', 'Type',
  94. 'isShare',
  95. 'primaryUserID', 'primaryMaster',
  96. 'data_joined', 'version',
  97. 'isVod', 'isExist')
  98. dvql = CommonService.qs_to_list(dvqs)
  99. ubqs = UID_Bucket.objects.filter(uid=UID). \
  100. values('bucket__content', 'status', 'channel', 'endTime', 'uid')
  101. res = dvql[0]
  102. res['vod'] = list(ubqs)
  103. return response.json(0, res)
  104. else:
  105. return response.json(444, {'param': 'UID'})
  106. else:
  107. return response.json(tko.code)
  108. else:
  109. return response.json(444, {'param': 'UID,NickName,View_Account,View_Password,Type,ChannelIndex'})
  110. # 新查询设备字段
  111. def do_query(self, userID, request_dict, response):
  112. token = request_dict.get('token', None)
  113. page = request_dict.get('page', None)
  114. line = request_dict.get('line', None)
  115. NickName = request_dict.get('NickName', None)
  116. if not token or not page or not line:
  117. return response.json(444)
  118. page = int(page)
  119. line = int(line)
  120. uid = request_dict.get('uid', None)
  121. tko = TokenObject(token)
  122. response.lang = tko.lang
  123. if page <= 0:
  124. return response.json(0)
  125. if tko.code == 0:
  126. userID = tko.userID
  127. dvqs = Device_Info.objects.filter(userID_id=userID)
  128. # # 过滤已重置的设备
  129. dvqs = dvqs.filter(~Q(isExist=2))
  130. if NickName:
  131. dvqs = dvqs.filter(NickName__icontains=NickName)
  132. if uid:
  133. dvqs = dvqs.filter(UID=uid)
  134. dvql = dvqs[(page - 1) * line:page * line]. \
  135. values('id', 'userID', 'NickName', 'UID', 'View_Account',
  136. 'View_Password', 'ChannelIndex', 'Type', 'isShare',
  137. 'primaryUserID', 'primaryMaster', 'data_joined', 'vodPrimaryUserID', 'vodPrimaryMaster', 'userID__userEmail',
  138. 'version',
  139. 'isVod', 'isExist', 'NotificationMode', 'isOpenCloud')
  140. dvls = CommonService.qs_to_list(dvql)
  141. uid_list = []
  142. for dvl in dvls:
  143. if dvl['primaryUserID'] and dvl['id'] == dvl['primaryUserID']:
  144. dvl['isPrimaryUser'] = 1
  145. else:
  146. dvl['isPrimaryUser'] = 0
  147. uid_list.append(dvl['UID'])
  148. # if dvl['isShare'] is False:
  149. # uid_list.append(dvl['UID'])
  150. ubqs = UID_Bucket.objects.filter(uid__in=uid_list). \
  151. values('bucket__content', 'status', 'channel', 'endTime', 'uid')
  152. upqs = UID_Preview.objects.filter(uid__in=uid_list).order_by('channel').values('id', 'uid', 'channel')
  153. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  154. bucket = oss2.Bucket(auth, 'oss-cn-hongkong.aliyuncs.com', 'statres')
  155. nowTime = int(time.time())
  156. data = []
  157. # 设备拓展信息表
  158. us_qs = UidSetModel.objects.filter(uid__in=uid_list).values('uid', 'version', 'nickname', 'ucode','detect_interval', 'is_human', 'is_custom_voice')
  159. uv_dict = {}
  160. for us in us_qs:
  161. uv_dict[us['uid']] = {
  162. 'version': us['version'],
  163. 'nickname': us['nickname'],
  164. 'ucode': us['ucode'],
  165. 'detect_interval': us['detect_interval'],
  166. 'is_human': us['is_human'],
  167. 'is_custom_voice': us['is_custom_voice'],
  168. }
  169. for p in dvls:
  170. p['vod'] = []
  171. for dm in ubqs:
  172. if p['UID'] == dm['uid']:
  173. if dm['endTime'] > nowTime:
  174. p['vod'].append(dm)
  175. p['preview'] = []
  176. for up in upqs:
  177. if p['UID'] == up['uid']:
  178. obj = 'uid_preview/{uid}/channel_{channel}.png'.format(uid=up['uid'], channel=up['channel'])
  179. img_sign = bucket.sign_url('GET', obj, 300)
  180. p['preview'].append(img_sign)
  181. p_uid = p['UID']
  182. if p_uid in uv_dict:
  183. # 设备版本号
  184. p['uid_version'] = uv_dict[p_uid]['version']
  185. p['ucode'] = uv_dict[p_uid]['ucode']
  186. p['detect_interval'] = uv_dict[p_uid]['detect_interval']
  187. p['is_human'] = uv_dict[p_uid]['is_human']
  188. p['is_custom_voice'] = uv_dict[p_uid]['is_custom_voice']
  189. # 设备昵称 调用影子信息昵称,先阶段不可
  190. if uv_dict[p_uid]['nickname']:
  191. p['NickName'] = uv_dict[p_uid]['nickname']
  192. else:
  193. # 设备版本号
  194. p['uid_version'] = ''
  195. p['ucode'] = ''
  196. p['detect_interval'] = ''
  197. data.append(p)
  198. return response.json(0, data)
  199. else:
  200. return response.json(tko.code)
  201. # 新查询设备字段
  202. def do_query_reset(self, userID, request_dict, response):
  203. dvqs = Device_Info.objects.filter(userID_id=userID, isExist=2)
  204. dvql = dvqs.values('id', 'userID', 'NickName', 'UID', 'View_Account',
  205. 'View_Password', 'ChannelIndex', 'Type', 'isShare',
  206. 'primaryUserID', 'primaryMaster', 'data_joined', 'vodPrimaryUserID', 'vodPrimaryMaster',
  207. 'userID__userEmail',
  208. 'version', 'isVod', 'isExist', 'NotificationMode', 'isCameraOpenCloud')
  209. dvls = CommonService.qs_to_list(dvql)
  210. uid_list = []
  211. for dvl in dvls:
  212. if dvl['primaryUserID'] and dvl['id'] == dvl['primaryUserID']:
  213. dvl['isPrimaryUser'] = 1
  214. else:
  215. dvl['isPrimaryUser'] = 0
  216. uid_list.append(dvl['UID'])
  217. ubqs = UID_Bucket.objects.filter(uid__in=uid_list). \
  218. values('bucket__content', 'status', 'channel', 'endTime', 'uid')
  219. upqs = UID_Preview.objects.filter(uid__in=uid_list).order_by('channel').values('id', 'uid', 'channel')
  220. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  221. bucket = oss2.Bucket(auth, 'oss-cn-hongkong.aliyuncs.com', 'statres')
  222. nowTime = int(time.time())
  223. data = []
  224. # 设备拓展信息表
  225. us_qs = UidSetModel.objects.filter(uid__in=uid_list).values('id', 'uid', 'version', 'nickname', 'ucode',
  226. 'detect_status', 'detect_group',
  227. 'detect_interval',
  228. 'region_alexa', 'is_alexa', 'deviceModel',
  229. 'TimeZone', 'TimeStatus', 'SpaceUsable',
  230. 'SpaceSum', 'MirrorType', 'RecordType',
  231. 'OutdoorModel', 'WIFIName', 'isDetector',
  232. 'DetectorRank')
  233. uv_dict = {}
  234. for us in us_qs:
  235. uv_dict[us['uid']] = {
  236. 'version': us['version'],
  237. 'nickname': us['nickname'],
  238. 'ucode': us['ucode'],
  239. 'detect_interval': us['detect_interval'],
  240. 'detect_group': us['detect_group'],
  241. 'detect_status': us['detect_status'],
  242. 'region_alexa': us['region_alexa'],
  243. 'is_alexa': us['is_alexa'],
  244. 'deviceModel': us['deviceModel'],
  245. 'TimeZone': us['TimeZone'],
  246. 'TimeStatus': us['TimeStatus'],
  247. 'SpaceUsable': us['SpaceUsable'],
  248. 'SpaceSum': us['SpaceSum'],
  249. 'MirrorType': us['MirrorType'],
  250. 'RecordType': us['RecordType'],
  251. 'OutdoorModel': us['OutdoorModel'],
  252. 'WIFIName': us['WIFIName'],
  253. 'isDetector': us['isDetector'],
  254. 'DetectorRank': us['DetectorRank']
  255. }
  256. # 从uid_channel里面取出通道配置信息
  257. ucs_qs = UidChannelSetModel.objects.filter(uid__id=us['id']).values('channel', 'pir_audio', 'mic_audio',
  258. 'battery_status', 'battery_level',
  259. 'sleep_status', 'sleep_time',
  260. 'light_night_model',
  261. 'light_alarm_type',
  262. 'light_alarm_level',
  263. 'light_alarm_man_en',
  264. 'light_alarm_vol',
  265. 'light_long_light'
  266. )
  267. channels = []
  268. for ucs in ucs_qs:
  269. channels_dict = {
  270. 'channel': ucs['channel'],
  271. 'pir_audio': ucs['pir_audio'],
  272. 'mic_audio': ucs['mic_audio'],
  273. 'battery_status': ucs['battery_status'],
  274. 'battery_level': ucs['battery_level'],
  275. 'sleep_status': ucs['sleep_status'],
  276. 'sleep_time': ucs['sleep_time'],
  277. 'light_night_model': ucs['light_night_model'],
  278. 'light_alarm_type': ucs['light_alarm_type'],
  279. 'light_alarm_level': ucs['light_alarm_level'],
  280. 'light_alarm_man_en': ucs['light_alarm_man_en'],
  281. 'light_alarm_vol': ucs['light_alarm_vol'],
  282. 'light_long_light': ucs['light_long_light']
  283. }
  284. channels.append(channels_dict)
  285. uv_dict[us['uid']]['channels'] = channels
  286. for p in dvls:
  287. p['vod'] = []
  288. for dm in ubqs:
  289. if p['UID'] == dm['uid']:
  290. if dm['endTime'] > nowTime:
  291. p['vod'].append(dm)
  292. p['preview'] = []
  293. for up in upqs:
  294. if p['UID'] == up['uid']:
  295. obj = 'uid_preview/{uid}/channel_{channel}.png'.format(uid=up['uid'], channel=up['channel'])
  296. img_sign = bucket.sign_url('GET', obj, 300)
  297. p['preview'].append(img_sign)
  298. p_uid = p['UID']
  299. if p_uid in uv_dict:
  300. # 设备版本号
  301. p['uid_version'] = uv_dict[p_uid]['version']
  302. p['ucode'] = uv_dict[p_uid]['ucode']
  303. p['detect_interval'] = uv_dict[p_uid]['detect_interval']
  304. p['detect_status'] = uv_dict[p_uid]['detect_status']
  305. p['detect_group'] = uv_dict[p_uid]['detect_group']
  306. p['region_alexa'] = uv_dict[p_uid]['region_alexa']
  307. p['is_alexa'] = uv_dict[p_uid]['is_alexa']
  308. p['deviceModel'] = uv_dict[p_uid]['deviceModel']
  309. p['TimeZone'] = uv_dict[p_uid]['TimeZone']
  310. p['TimeStatus'] = uv_dict[p_uid]['TimeStatus']
  311. p['SpaceUsable'] = uv_dict[p_uid]['SpaceUsable']
  312. p['SpaceSum'] = uv_dict[p_uid]['SpaceSum']
  313. p['MirrorType'] = uv_dict[p_uid]['MirrorType']
  314. p['RecordType'] = uv_dict[p_uid]['RecordType']
  315. p['OutdoorModel'] = uv_dict[p_uid]['OutdoorModel']
  316. p['WIFIName'] = uv_dict[p_uid]['WIFIName']
  317. p['isDetector'] = uv_dict[p_uid]['isDetector']
  318. p['DetectorRank'] = uv_dict[p_uid]['DetectorRank']
  319. p['channels'] = uv_dict[p_uid]['channels']
  320. # 设备昵称 调用影子信息昵称,先阶段不可
  321. if uv_dict[p_uid]['nickname']:
  322. p['NickName'] = uv_dict[p_uid]['nickname']
  323. else:
  324. # 设备版本号
  325. p['uid_version'] = ''
  326. p['ucode'] = ''
  327. data.append(p)
  328. result = data
  329. return response.json(0, result)