DetectControllerV2.py 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. import datetime
  2. import json
  3. import time
  4. import boto3
  5. import botocore
  6. import oss2
  7. from botocore import client
  8. from django.http import JsonResponse
  9. from django.views.generic.base import View
  10. from Ansjer.config import DETECT_PUSH_DOMAIN, DETECT_PUSH_DOMAINS, DETECT_PUSH_DOMAIN_JIUAN, DETECT_PUSH_DOMAINS_JIUAN, \
  11. OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, LOGGER, ALGORITHM_COMBO_TYPES
  12. from Ansjer.config import PUSH_BUCKET
  13. from Model.models import Device_Info, Equipment_Info, UidSetModel, UidPushModel, CompanyModel, SysMsgModel, \
  14. AiService, VodBucketModel
  15. from Object.ETkObject import ETkObject
  16. from Object.OCIObjectStorage import OCIObjectStorage
  17. from Object.RedisObject import RedisObject
  18. from Object.ResponseObject import ResponseObject
  19. from Object.TokenObject import TokenObject
  20. from Object.utils import LocalDateTimeUtil
  21. from Service.CommonService import CommonService
  22. from Service.EquipmentInfoService import EquipmentInfoService
  23. from Service.VodHlsService import SplitVodHlsObject
  24. class DetectControllerViewV2(View):
  25. def get(self, request, *args, **kwargs):
  26. request.encoding = 'utf-8'
  27. operation = kwargs.get('operation')
  28. api_version = kwargs.get('apiVersion')
  29. # self.ip = CommonService.get_ip_address(request)
  30. return self.validation(request, request.GET, operation, api_version)
  31. def post(self, request, *args, **kwargs):
  32. request.encoding = 'utf-8'
  33. operation = kwargs.get('operation')
  34. api_version = kwargs.get('apiVersion')
  35. # self.ip = CommonService.get_ip_address(request)
  36. return self.validation(request, request.POST, operation, api_version)
  37. def validation(self, request, request_dict, operation, api_version):
  38. response = ResponseObject()
  39. if operation is None:
  40. return response.json(444, 'error path')
  41. token = request_dict.get('token', None)
  42. lang = request_dict.get('lang', None)
  43. if lang:
  44. response = ResponseObject(lang)
  45. tko = TokenObject(token)
  46. if tko.code == 0:
  47. userID = tko.userID
  48. # 修改推送设置
  49. if operation == 'changeStatus':
  50. return self.do_change_status(userID, request_dict, response)
  51. # 查询推送信息
  52. elif operation == 'queryInfo':
  53. return self.do_query(request_dict, response, userID)
  54. # 点击推送信息跳转到列表信息
  55. elif operation == 'transferInfo':
  56. return self.do_transfer(request_dict, response, userID)
  57. # 更新推送延迟
  58. elif operation == 'updateInterval':
  59. return self.do_update_interval(userID, request_dict, response)
  60. # 消息提醒配置
  61. elif operation == 'messageNotificationSet':
  62. return self.message_notification_set(api_version, request_dict, response)
  63. else:
  64. return response.json(414)
  65. else:
  66. return response.json(tko.code)
  67. @classmethod
  68. def message_notification_set(cls, api_version, request_dict, response):
  69. """
  70. 消息提醒设置
  71. @param api_version: 版本号
  72. @param request_dict: 参数json格式
  73. @param response: 响应数据
  74. """
  75. try:
  76. msg_data = request_dict.get('msgData', None)
  77. uid = request_dict.get('uid', None)
  78. LOGGER.info('*****DetectControllerViewV2.message_notification_set*****api_version:{},uid:{}'
  79. .format(api_version, uid))
  80. if not all([msg_data, uid]):
  81. return response.json(444)
  82. data = json.loads(msg_data)
  83. uid_set_qs = UidSetModel.objects.filter(uid=uid)
  84. if not uid_set_qs.exists():
  85. return response.json(173)
  86. uid_set_qs.update(msg_notify=data, updTime=int(time.time()))
  87. return response.json(0)
  88. except Exception as e:
  89. LOGGER.info('*****DetectControllerViewV2.message_notification_set:errLine:{}, errMsg:{}'
  90. .format(e.__traceback__.tb_lineno, repr(e)))
  91. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  92. def do_change_status(self, userID, request_dict, response):
  93. token_val = request_dict.get('token_val', None)
  94. jg_token_val = request_dict.get('jg_token_val', '')
  95. appBundleId = request_dict.get('appBundleId', None)
  96. app_type = request_dict.get('app_type', None)
  97. push_type = request_dict.get('push_type', None)
  98. status = request_dict.get('status', None)
  99. m_code = request_dict.get('m_code', None)
  100. uid = request_dict.get('uid', None)
  101. lang = request_dict.get('lang', 'en')
  102. tz = request_dict.get('tz', '0')
  103. company_secrete = request_dict.get('company_secrete', None)
  104. region = request_dict.get('region', None) # app必须传:1:国外,2:国内
  105. electricity_status = request_dict.get('electricity_status', None)
  106. domain_name = request_dict.get('domain_name', None)
  107. if not region:
  108. return response.json(444, 'region')
  109. region = int(region)
  110. # 消息提醒功能新增
  111. # 如果传空上来,就默认为0
  112. if tz == '':
  113. tz = 0
  114. else:
  115. tz = tz.replace("GMT", "")
  116. detect_group = request_dict.get('detect_group', None)
  117. interval = request_dict.get('interval', None)
  118. if not status and not electricity_status:
  119. return response.json(444, 'status and electricity_status')
  120. if not company_secrete:
  121. return response.json(444, 'company_secrete')
  122. company = CompanyModel.objects.filter(secret=company_secrete)
  123. if not company.exists():
  124. return response.json(444, 'company_secrete')
  125. # 关闭推送
  126. if not all([appBundleId, app_type, token_val, uid, m_code]):
  127. return response.json(444, 'appBundleId,app_type,token_val,uid,m_code')
  128. try:
  129. # 判断用户是否拥有设备
  130. device_info_qs = Device_Info.objects.filter(userID_id=userID, UID=uid)
  131. if not device_info_qs.exists():
  132. device_info_qs = Device_Info.objects.filter(userID_id=userID, serial_number=uid)
  133. if not device_info_qs.exists():
  134. return response.json(14)
  135. # 更新或创建uid_set数据
  136. nowTime = int(time.time())
  137. uid_set_data = {
  138. 'device_type': device_info_qs[0].Type
  139. }
  140. # 设置开关状态,0:关闭,1:开启
  141. if status:
  142. status = int(status)
  143. uid_set_data['detect_status'] = status
  144. device_info_qs.update(NotificationMode=status)
  145. # 检测类型
  146. if detect_group:
  147. uid_set_data['detect_group'] = detect_group
  148. uid_set_qs = UidSetModel.objects.filter(uid=uid)
  149. # 设置消息推送间隔
  150. if interval:
  151. interval = int(interval)
  152. if uid_set_qs.exists() and status == 1 and uid_set_qs.first().detect_status == 0:
  153. interval = 60
  154. uid_set_data['detect_interval'] = interval
  155. # 开通了ai服务的设备,通过mqtt通知设备修改消息推送间隔
  156. ai_service_qs = AiService.objects.filter(uid=uid, use_status=1, endTime__gte=nowTime)
  157. if ai_service_qs.exists():
  158. topic_name = 'ansjer/generic/{}'.format(uid)
  159. msg = {
  160. 'commandType': 'AIState',
  161. 'payload': {
  162. 'IntervalTime': interval
  163. }
  164. }
  165. CommonService.req_publish_mqtt_msg(uid, topic_name, msg)
  166. if uid_set_qs.exists():
  167. msg_data = uid_set_qs.first().msg_notify
  168. if status == 0 and msg_data:
  169. msg_data['appPush'] = -1
  170. uid_set_data['msg_notify'] = msg_data
  171. elif status == 1 and uid_set_qs.first().detect_status == 0:
  172. uid_set_data['detect_interval'] = 60
  173. msg_data = {'appPush': 1,
  174. 'pushTime': {'allDay': 1, 'repeat': 127, 'endTime': 0, 'timeZone': '+08.00',
  175. 'startTime': 0},
  176. 'eventTypes': {'device': ALGORITHM_COMBO_TYPES, 'aiCloud': 1}
  177. }
  178. uid_set_data['msg_notify'] = msg_data
  179. uid_set_id = uid_set_qs[0].id
  180. uid_set_data['updTime'] = nowTime
  181. uid_set_qs.update(**uid_set_data)
  182. else:
  183. uid_set_data['uid'] = uid
  184. uid_set_data['addTime'] = nowTime
  185. uid_set_data['updTime'] = nowTime
  186. uid_set_qs = UidSetModel.objects.create(**uid_set_data)
  187. uid_set_id = uid_set_qs.id
  188. # 初始化UidPushModel推送表
  189. if electricity_status:
  190. if m_code != 0 and m_code != '0':
  191. uid_push_qs = UidPushModel.objects.filter(userID_id=userID, m_code=m_code, uid_set__uid=uid)
  192. if uid_push_qs.exists():
  193. uid_push_update_dict = {
  194. 'appBundleId': appBundleId,
  195. 'app_type': app_type,
  196. 'push_type': push_type,
  197. 'token_val': token_val,
  198. 'jg_token_val': jg_token_val,
  199. 'updTime': nowTime,
  200. 'lang': lang,
  201. 'tz': tz
  202. }
  203. uid_push_qs.update(**uid_push_update_dict)
  204. else:
  205. uid_push_create_dict = {
  206. 'uid_set_id': uid_set_id,
  207. 'userID_id': userID,
  208. 'appBundleId': appBundleId,
  209. 'app_type': app_type,
  210. 'push_type': push_type,
  211. 'token_val': token_val,
  212. 'jg_token_val': jg_token_val,
  213. 'm_code': m_code,
  214. 'addTime': nowTime,
  215. 'updTime': nowTime,
  216. 'lang': lang,
  217. 'tz': tz
  218. }
  219. # 绑定设备推送
  220. UidPushModel.objects.create(**uid_push_create_dict)
  221. return response.json(0)
  222. if status == 0:
  223. # 状态为0的时候删除redis缓存数据
  224. self.do_delete_redis(uid)
  225. return response.json(0)
  226. elif status == 1:
  227. if m_code != 0 and m_code != '0':
  228. uid_push_qs = UidPushModel.objects.filter(userID_id=userID, m_code=m_code, uid_set__uid=uid)
  229. if uid_push_qs.exists():
  230. uid_push_update_dict = {
  231. 'appBundleId': appBundleId,
  232. 'app_type': app_type,
  233. 'push_type': push_type,
  234. 'token_val': token_val,
  235. 'jg_token_val': jg_token_val,
  236. 'updTime': nowTime,
  237. 'lang': lang,
  238. 'tz': tz
  239. }
  240. uid_push_qs.update(**uid_push_update_dict)
  241. else:
  242. uid_push_create_dict = {
  243. 'uid_set_id': uid_set_id,
  244. 'userID_id': userID,
  245. 'appBundleId': appBundleId,
  246. 'app_type': app_type,
  247. 'push_type': push_type,
  248. 'token_val': token_val,
  249. 'jg_token_val': jg_token_val,
  250. 'm_code': m_code,
  251. 'addTime': nowTime,
  252. 'updTime': nowTime,
  253. 'lang': lang,
  254. 'tz': tz
  255. }
  256. # 绑定设备推送
  257. UidPushModel.objects.create(**uid_push_create_dict)
  258. if interval:
  259. self.do_delete_redis(uid, interval)
  260. else:
  261. self.do_delete_redis(uid)
  262. etkObj = ETkObject(etk='')
  263. etk = etkObj.encrypt(uid)
  264. if company_secrete == 'MTEyMTNB':
  265. d_type = device_info_qs[0].Type
  266. url = DETECT_PUSH_DOMAIN
  267. if d_type in [103, 26]:
  268. url = DETECT_PUSH_DOMAINS
  269. urls = DETECT_PUSH_DOMAINS
  270. else:
  271. url = DETECT_PUSH_DOMAIN_JIUAN
  272. urls = DETECT_PUSH_DOMAINS_JIUAN
  273. if domain_name in ['api.zositeche.com', 'api.loocam3.com', 'common.neutral3.com']:
  274. url = 'http://push.zositeche.com/'
  275. urls = 'https://push.zositeche.com/'
  276. detectUrl = "{DETECT_PUSH_DOMAIN}notifyV2/push?etk={etk}&company_secrete={company_secrete}&region={region}". \
  277. format(etk=etk, company_secrete=company_secrete, DETECT_PUSH_DOMAIN=url, region=region)
  278. detectUrls = "{DETECT_PUSH_DOMAIN_V2}notifyV2/push?etk={etk}&company_secrete={company_secrete}&region={region}". \
  279. format(etk=etk, company_secrete=company_secrete, DETECT_PUSH_DOMAIN_V2=urls, region=region)
  280. return response.json(0, {'detectUrl': detectUrl, 'detectUrls': detectUrls})
  281. else:
  282. return response.json(173)
  283. except Exception as e:
  284. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  285. def do_delete_redis(self, uid, detect_interval=0):
  286. keyPattern = '{uid}*'.format(uid=uid)
  287. redisObj = RedisObject()
  288. keys = redisObj.get_keys(keyPattern)
  289. if keys:
  290. for key in keys:
  291. key = key.decode()
  292. if detect_interval == 0:
  293. redisObj.del_data(key=key)
  294. elif key.find('plt') != -1:
  295. continue
  296. elif key.find('flag') != -1:
  297. redisObj.set_data(key=key, val=1, expire=detect_interval)
  298. else:
  299. redisObj.del_data(key=key)
  300. def do_query(self, request_dict, response, userID):
  301. page = int(request_dict.get('page', None))
  302. line = int(request_dict.get('line', None))
  303. start_time = request_dict.get('startTime', None)
  304. end_time = request_dict.get('endTime', None)
  305. event_type = request_dict.get('eventType', None)
  306. region = int(request_dict.get('region', None))
  307. uids = request_dict.get('uids', None)
  308. try:
  309. uid_list = []
  310. if uids:
  311. uid_list = uids.split(',')
  312. if not start_time and not end_time:
  313. # 默认查询近七天内数据
  314. end_time = int(time.time())
  315. start_time = LocalDateTimeUtil.get_before_days_timestamp(end_time, 7)
  316. equipment_info_qs, count = EquipmentInfoService. \
  317. union_equipment_info(userID, uid_list, event_type, start_time, end_time, page, line)
  318. # 查询设备类型,昵称
  319. if uid_list:
  320. dvqs = Device_Info.objects.filter(UID__in=uid_list, userID_id=userID).values('UID', 'Type', 'NickName')
  321. uid_type_dict = {}
  322. for dv in dvqs:
  323. uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}
  324. else:
  325. dvqs = Device_Info.objects.filter(userID_id=userID).values('UID', 'Type', 'NickName')
  326. uid_type_dict = {}
  327. for dv in dvqs:
  328. uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}
  329. # 没有推送数据返回空列表
  330. if count == 0:
  331. return response.json(0, {'datas': [], 'count': 0})
  332. res = []
  333. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  334. oss_img_bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
  335. aws_s3 = boto3.client(
  336. 's3',
  337. aws_access_key_id=AWS_ACCESS_KEY_ID[1],
  338. aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
  339. config=botocore.client.Config(signature_version='s3v4'),
  340. region_name='us-east-1'
  341. )
  342. aws_s3_cn = boto3.client(
  343. 's3',
  344. aws_access_key_id=AWS_ACCESS_KEY_ID[0],
  345. aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],
  346. config=botocore.client.Config(signature_version='s3v4'),
  347. region_name='cn-northwest-1'
  348. )
  349. oci_eur = OCIObjectStorage('eur')
  350. oci_us = OCIObjectStorage('us')
  351. redis_obj = RedisObject(3)
  352. # ai消息标识所有组合标签
  353. ai_all_event_type = EquipmentInfoService.get_all_comb_event_type()
  354. for equipment_info in equipment_info_qs:
  355. uid = equipment_info['devUid']
  356. event_time = equipment_info['eventTime']
  357. channel = equipment_info['Channel']
  358. storage_location = equipment_info['storage_location']
  359. border_coords = equipment_info['borderCoords']
  360. event_type = equipment_info['eventType']
  361. event_tag = equipment_info['eventTag']
  362. if equipment_info['is_st'] == 1:
  363. thumbspng = '{}/{}/{}.jpeg'.format(uid, channel, event_time)
  364. if storage_location == 1: # 阿里云oss
  365. img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
  366. elif storage_location in [3, 4]:
  367. oci = oci_eur if storage_location == 4 else oci_us
  368. img_url = DetectControllerViewV2.oci_object_url(oci, thumbspng)
  369. else:
  370. params = {'Key': thumbspng}
  371. if region == 1: # AWS国外
  372. params['Bucket'] = 'foreignpush'
  373. img_url = aws_s3.generate_presigned_url(
  374. 'get_object', Params=params, ExpiresIn=300)
  375. else: # AWS国内
  376. params['Bucket'] = 'push'
  377. img_url = aws_s3_cn.generate_presigned_url(
  378. 'get_object', Params=params, ExpiresIn=300)
  379. equipment_info['img'] = img_url
  380. equipment_info['img_list'] = [img_url]
  381. elif equipment_info['is_st'] == 2:
  382. # 列表装载回放时间戳标记
  383. split_vod_hls_obj = SplitVodHlsObject()
  384. vodqs = split_vod_hls_obj.get_vod_hls_data(
  385. uid=uid, channel=channel, start_time=int(event_time)).values('bucket_id')
  386. if not vodqs.exists():
  387. return response.json(173)
  388. vod_bucket_qs = VodBucketModel.objects.filter(id=vodqs[0]['bucket_id']).values('bucket', 'endpoint')
  389. if not vod_bucket_qs.exists():
  390. return response.json(173)
  391. bucket_name = vod_bucket_qs[0]['bucket']
  392. endpoint = vod_bucket_qs[0]['endpoint']
  393. bucket = oss2.Bucket(auth, endpoint, bucket_name)
  394. ts = '{}/vod{}/{}/ts0.ts'.format(uid, channel, event_time)
  395. if storage_location == 1: # 阿里云oss
  396. thumb0 = bucket.sign_url('GET', ts, 3600,
  397. params={'x-oss-process': 'video/snapshot,t_0000,w_700'})
  398. thumb1 = bucket.sign_url('GET', ts, 3600,
  399. params={'x-oss-process': 'video/snapshot,t_1000,w_700'})
  400. thumb2 = bucket.sign_url('GET', ts, 3600,
  401. params={'x-oss-process': 'video/snapshot,t_2000,w_700'})
  402. equipment_info['img_list'] = [thumb0, thumb1, thumb2]
  403. else:
  404. params = {'Key': ts}
  405. if region == 1: # AWS国外
  406. params['Bucket'] = 'foreignpush'
  407. img_url = aws_s3.generate_presigned_url(
  408. 'get_object', Params=params, ExpiresIn=300)
  409. else: # AWS国内
  410. params['Bucket'] = 'push'
  411. img_url = aws_s3_cn.generate_presigned_url(
  412. 'get_object', Params=params, ExpiresIn=300)
  413. equipment_info['img_list'] = [img_url]
  414. elif equipment_info['is_st'] == 3 or equipment_info['is_st'] == 4:
  415. # 列表装载回放时间戳标记
  416. equipment_info['img_list'] = []
  417. msg_key = f'{uid}:{channel}:{event_time}'
  418. # OCI三张图 获取缓存临时URL
  419. img_data = DetectControllerViewV2.get_msg_redis_url(redis_obj, msg_key,
  420. equipment_info['is_st'], storage_location)
  421. if img_data:
  422. equipment_info['img_list'] = img_data
  423. else:
  424. for i in range(equipment_info['is_st']):
  425. thumbspng = '{}/{}/{}_{}.jpeg'.format(uid, channel, event_time, i)
  426. if storage_location == 1: # 阿里云oss
  427. img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
  428. elif storage_location in [3, 4]: # 国外OCI云
  429. oci = oci_eur if storage_location == 4 else oci_us
  430. img_url = DetectControllerViewV2.oci_object_url(oci, thumbspng)
  431. else:
  432. params = {'Key': thumbspng}
  433. if region == 1: # 国外AWS
  434. params['Bucket'] = 'foreignpush'
  435. img_url = aws_s3.generate_presigned_url(
  436. 'get_object', Params=params, ExpiresIn=300)
  437. else: # 国内AWS
  438. params['Bucket'] = 'push'
  439. img_url = aws_s3_cn.generate_presigned_url(
  440. 'get_object', Params=params, ExpiresIn=300)
  441. equipment_info['img_list'].append(img_url)
  442. if not img_data and equipment_info['is_st'] == 3 and storage_location in [3, 4]:
  443. DetectControllerViewV2.set_msg_redis_url(redis_obj, msg_key, equipment_info['img_list'])
  444. if uid in uid_type_dict.keys():
  445. equipment_info['uid_type'] = uid_type_dict[uid]['type']
  446. equipment_info['devNickName'] = uid_type_dict[uid]['NickName']
  447. else:
  448. equipment_info['uid_type'] = ''
  449. equipment_info['borderCoords'] = '' if border_coords == '' else eval(border_coords) # ai消息坐标信息
  450. equipment_info['ai_event_type_list'] = []
  451. # 如果是ai消息类型,则分解eventType, 如:123 -> [1,2,3]
  452. if border_coords and event_type in ai_all_event_type:
  453. equipment_info['ai_event_type_list'] = list(map(int, str(event_type)))
  454. if EquipmentInfoService.is_combo_tag(event_type, event_tag):
  455. equipment_info['ai_event_type_list'] += EquipmentInfoService.get_combo_types(event_type, event_tag)
  456. res.append(equipment_info)
  457. return response.json(0, {'datas': res, 'count': count})
  458. except Exception as e:
  459. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  460. @staticmethod
  461. def get_msg_redis_url(redis_client, msg_key, is_st, storage_location):
  462. if is_st == 3 and storage_location in [3, 4]:
  463. img_data = redis_client.get_data(msg_key)
  464. if img_data:
  465. return json.loads(img_data)
  466. return None
  467. @staticmethod
  468. def set_msg_redis_url(redis_client, msg_key, img_list):
  469. redis_client.set_data(msg_key, img_list, 580)
  470. @staticmethod
  471. def oci_object_url(oci, obj_name):
  472. """
  473. 获取OCI对象存储URL 有效期5分钟
  474. @param oci:
  475. @param obj_name: 对象名称
  476. @return: url
  477. """
  478. time_expires = datetime.datetime.utcnow() + datetime.timedelta(minutes=10)
  479. result = oci.get_preauthenticated_request_url(PUSH_BUCKET, 'ociPush', obj_name, time_expires)
  480. return result.full_path if result else ''
  481. def do_transfer(self, request_dict, response, userID):
  482. event_time = request_dict.get('eventTime', None)
  483. event_type = request_dict.get('eventType', None)
  484. region = int(request_dict.get('region', None))
  485. uid = request_dict.get('uid', None)
  486. if not all([event_time, region, uid]):
  487. return response.json(444)
  488. try:
  489. kwargs = {'device_user_id': userID, 'device_uid': uid, 'event_time': event_time, 'event_type': event_type}
  490. equipment_info_qs, count = EquipmentInfoService.get_equipment_info(**kwargs)
  491. # 查询设备类型,昵称
  492. dvqs = Device_Info.objects.filter(UID=uid, userID_id=userID).values('UID', 'Type', 'NickName')
  493. uid_type_dict = {}
  494. for dv in dvqs:
  495. uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}
  496. # 没有推送数据返回空列表
  497. if count == 0:
  498. return response.json(0, {'datas': [], 'count': 0})
  499. res = []
  500. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  501. oss_img_bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
  502. aws_s3 = boto3.client(
  503. 's3',
  504. aws_access_key_id=AWS_ACCESS_KEY_ID[1],
  505. aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
  506. config=botocore.client.Config(signature_version='s3v4'),
  507. region_name='us-east-1'
  508. )
  509. aws_s3_cn = boto3.client(
  510. 's3',
  511. aws_access_key_id=AWS_ACCESS_KEY_ID[0],
  512. aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],
  513. config=botocore.client.Config(signature_version='s3v4'),
  514. region_name='cn-northwest-1'
  515. )
  516. oci_eur = OCIObjectStorage('eur')
  517. oci_us = OCIObjectStorage('us')
  518. # ai消息标识所有组合标签
  519. ai_all_event_type = EquipmentInfoService.get_all_comb_event_type()
  520. for equipment_info in equipment_info_qs:
  521. uid = equipment_info['devUid']
  522. event_time = equipment_info['eventTime']
  523. channel = equipment_info['Channel']
  524. storage_location = equipment_info['storage_location']
  525. border_coords = equipment_info['borderCoords']
  526. event_type = equipment_info['eventType']
  527. event_tag = equipment_info['eventTag']
  528. if equipment_info['is_st'] == 1:
  529. thumbspng = '{}/{}/{}.jpeg'.format(uid, channel, event_time)
  530. if storage_location == 1: # 阿里云oss
  531. img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
  532. elif storage_location in [3, 4]:
  533. oci = oci_eur if storage_location == 4 else oci_us
  534. img_url = DetectControllerViewV2.oci_object_url(oci, thumbspng)
  535. else:
  536. params = {'Key': thumbspng}
  537. if region == 1: # AWS国外
  538. params['Bucket'] = 'foreignpush'
  539. img_url = aws_s3.generate_presigned_url(
  540. 'get_object', Params=params, ExpiresIn=300)
  541. else: # AWS国内
  542. params['Bucket'] = 'push'
  543. img_url = aws_s3_cn.generate_presigned_url(
  544. 'get_object', Params=params, ExpiresIn=300)
  545. equipment_info['img'] = img_url
  546. equipment_info['img_list'] = [img_url]
  547. elif equipment_info['is_st'] == 2:
  548. # 列表装载回放时间戳标记
  549. split_vod_hls_obj = SplitVodHlsObject()
  550. vodqs = split_vod_hls_obj.get_vod_hls_data(
  551. uid=uid, channel=channel, start_time=int(event_time)).values('bucket_id')
  552. if not vodqs.exists():
  553. return response.json(173)
  554. vod_bucket_qs = VodBucketModel.objects.filter(id=vodqs[0]['bucket_id']).values('bucket', 'endpoint')
  555. if not vod_bucket_qs.exists():
  556. return response.json(173)
  557. bucket_name = vod_bucket_qs[0]['bucket']
  558. endpoint = vod_bucket_qs[0]['endpoint']
  559. bucket = oss2.Bucket(auth, endpoint, bucket_name)
  560. ts = '{}/vod{}/{}/ts0.ts'.format(uid, channel, event_time)
  561. if storage_location == 1: # 阿里云oss
  562. thumb0 = bucket.sign_url('GET', ts, 3600,
  563. params={'x-oss-process': 'video/snapshot,t_0000,w_700'})
  564. thumb1 = bucket.sign_url('GET', ts, 3600,
  565. params={'x-oss-process': 'video/snapshot,t_1000,w_700'})
  566. thumb2 = bucket.sign_url('GET', ts, 3600,
  567. params={'x-oss-process': 'video/snapshot,t_2000,w_700'})
  568. equipment_info['img_list'] = [thumb0, thumb1, thumb2]
  569. else:
  570. params = {'Key': ts}
  571. if region == 1: # AWS国外
  572. params['Bucket'] = 'foreignpush'
  573. img_url = aws_s3.generate_presigned_url(
  574. 'get_object', Params=params, ExpiresIn=300)
  575. else: # AWS国内
  576. params['Bucket'] = 'push'
  577. img_url = aws_s3_cn.generate_presigned_url(
  578. 'get_object', Params=params, ExpiresIn=300)
  579. equipment_info['img_list'] = [img_url]
  580. elif equipment_info['is_st'] == 3 or equipment_info['is_st'] == 4:
  581. # 列表装载回放时间戳标记
  582. equipment_info['img_list'] = []
  583. for i in range(equipment_info['is_st']):
  584. thumbspng = '{}/{}/{}_{}.jpeg'.format(uid, channel, event_time, i)
  585. if storage_location == 1: # 阿里云oss
  586. img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
  587. elif storage_location in [3, 4]:
  588. oci = oci_eur if storage_location == 4 else oci_us
  589. img_url = DetectControllerViewV2.oci_object_url(oci, thumbspng)
  590. else:
  591. params = {'Key': thumbspng}
  592. if region == 1: # 国外AWS
  593. params['Bucket'] = 'foreignpush'
  594. img_url = aws_s3.generate_presigned_url(
  595. 'get_object', Params=params, ExpiresIn=300)
  596. else: # 国内AWS
  597. params['Bucket'] = 'push'
  598. img_url = aws_s3_cn.generate_presigned_url(
  599. 'get_object', Params=params, ExpiresIn=300)
  600. equipment_info['img_list'].append(img_url)
  601. if uid in uid_type_dict.keys():
  602. equipment_info['uid_type'] = uid_type_dict[uid]['type']
  603. equipment_info['devNickName'] = uid_type_dict[uid]['NickName']
  604. else:
  605. equipment_info['uid_type'] = ''
  606. equipment_info['borderCoords'] = '' if border_coords == '' else eval(border_coords) # ai消息坐标信息
  607. equipment_info['ai_event_type_list'] = []
  608. # 如果是ai消息类型,则分解eventType, 如:123 -> [1,2,3]
  609. if border_coords and event_type in ai_all_event_type:
  610. equipment_info['ai_event_type_list'] = list(map(int, str(event_type)))
  611. if EquipmentInfoService.is_combo_tag(event_type, event_tag):
  612. equipment_info['ai_event_type_list'] += EquipmentInfoService.get_combo_types(event_type, event_tag)
  613. res.append(equipment_info)
  614. return response.json(0, {'datas': res, 'count': count})
  615. except Exception as e:
  616. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  617. def do_update_interval(self, userID, request_dict, response):
  618. uid = request_dict.get('uid', None)
  619. interval = request_dict.get('interval', None)
  620. dvqs = Device_Info.objects.filter(userID_id=userID, UID=uid)
  621. if dvqs.exists():
  622. uid_set_qs = UidSetModel.objects. \
  623. filter(uid=uid, uidpushmodel__userID_id=userID)
  624. if uid_set_qs.exists():
  625. uid_set_qs.update(detect_interval=int(interval))
  626. else:
  627. return response.json(173)
  628. else:
  629. return response.json(0)
  630. # 这个接口没有调用过,不敢动
  631. # http://test.dvema.com/detect/add?uidToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJQMldOR0pSRDJFSEE1RVU5MTExQSJ9.xOCI5lerk8JOs5OcAzunrKCfCrtuPIZ3AnkMmnd-bPY&n_time=1526845794&channel=1&event_type=51&is_st=0
  632. # 移动侦测接口
  633. class PushNotificationView(View):
  634. def get(self, request, *args, **kwargs):
  635. request.encoding = 'utf-8'
  636. # operation = kwargs.get('operation')
  637. return self.validation(request.GET)
  638. def post(self, request, *args, **kwargs):
  639. request.encoding = 'utf-8'
  640. # operation = kwargs.get('operation')
  641. return self.validation(request.POST)
  642. def validation(self, request_dict):
  643. etk = request_dict.get('etk', None)
  644. channel = request_dict.get('channel', '1')
  645. n_time = request_dict.get('n_time', None)
  646. event_type = request_dict.get('event_type', None)
  647. is_st = request_dict.get('is_st', None)
  648. region = request_dict.get('region', '2')
  649. region = int(region)
  650. eto = ETkObject(etk)
  651. uid = eto.uid
  652. if len(uid) == 20:
  653. redisObj = RedisObject()
  654. # pkey = '{uid}_{channel}_ptl'.format(uid=uid, channel=channel)
  655. pkey = '{uid}_ptl'.format(uid=uid)
  656. ykey = '{uid}_redis_qs'.format(uid=uid)
  657. if redisObj.get_data(key=pkey):
  658. res_data = {'code': 0, 'msg': 'success,!33333333333'}
  659. return JsonResponse(status=200, data=res_data)
  660. else:
  661. redisObj.set_data(key=pkey, val=1, expire=60)
  662. ##############
  663. redis_data = redisObj.get_data(key=ykey)
  664. if redis_data:
  665. redis_list = eval(redis_data)
  666. else:
  667. # 设置推送时间为60秒一次
  668. redisObj.set_data(key=pkey, val=1, expire=60)
  669. print("从数据库查到数据")
  670. # 从数据库查询出来
  671. uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
  672. values('token_val', 'app_type', 'appBundleId',
  673. 'push_type', 'userID_id', 'lang', 'm_code',
  674. 'tz', 'uid_set__nickname')
  675. # 新建一个list接收数据
  676. redis_list = []
  677. # 把数据库数据追加进redis_list
  678. for qs in uid_push_qs:
  679. redis_list.append(qs)
  680. # 修改redis数据,并设置过期时间为10分钟
  681. if redis_list:
  682. redisObj.set_data(key=ykey, val=str(redis_list), expire=600)
  683. # auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  684. # bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
  685. aws_s3_guonei = boto3.client(
  686. 's3',
  687. aws_access_key_id=AWS_ACCESS_KEY_ID[0],
  688. aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],
  689. config=botocore.client.Config(signature_version='s3v4'),
  690. region_name='cn-northwest-1'
  691. )
  692. aws_s3_guowai = boto3.client(
  693. 's3',
  694. aws_access_key_id=AWS_ACCESS_KEY_ID[1],
  695. aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
  696. config=botocore.client.Config(signature_version='s3v4'),
  697. region_name='us-east-1'
  698. )
  699. self.do_bulk_create_info(redis_list, n_time, channel, event_type, is_st, uid)
  700. if is_st == '0' or is_st == '2':
  701. return JsonResponse(status=200, data={'code': 0, 'msg': 'success44444444444444444'})
  702. elif is_st == '1':
  703. # Endpoint以杭州为例,其它Region请按实际情况填写。
  704. # obj = '{uid}/{channel}/{filename}.jpeg'.format(uid=uid, channel=channel, filename=n_time)
  705. # 设置此签名URL在60秒内有效。
  706. # url = bucket.sign_url('PUT', obj, 7200)
  707. thumbspng = '{uid}/{channel}/{filename}.jpeg'.format(uid=uid, channel=channel, filename=n_time)
  708. if region == 2: # 2:国内
  709. response_url = aws_s3_guonei.generate_presigned_url(
  710. ClientMethod='put_object',
  711. Params={
  712. 'Bucket': 'push',
  713. 'Key': thumbspng
  714. },
  715. ExpiresIn=3600
  716. )
  717. else: # 1:国外
  718. response_url = aws_s3_guowai.generate_presigned_url(
  719. ClientMethod='put_object',
  720. Params={
  721. 'Bucket': 'foreignpush',
  722. 'Key': thumbspng
  723. },
  724. ExpiresIn=3600
  725. )
  726. # res_data = {'code': 0, 'img_push': url, 'msg': 'success'}
  727. # response_url = response_url[:4] + response_url[5:]
  728. res_data = {'code': 0, 'img_push': response_url, 'msg': 'success'}
  729. return JsonResponse(status=200, data=res_data)
  730. elif is_st == '3':
  731. # 人形检测带动图
  732. img_url_list = []
  733. for i in range(int(is_st)):
  734. # obj = '{uid}/{channel}/{filename}_{st}.jpeg'. \
  735. # format(uid=uid, channel=channel, filename=n_time, st=i)
  736. # 设置此签名URL在60秒内有效。
  737. # url = bucket.sign_url('PUT', obj, 7200)
  738. thumbspng = '{uid}/{channel}/{filename}_{st}.jpeg'. \
  739. format(uid=uid, channel=channel, filename=n_time, st=i)
  740. if region == 2: # 2:国内
  741. response_url = aws_s3_guonei.generate_presigned_url(
  742. ClientMethod='put_object',
  743. Params={
  744. 'Bucket': 'push',
  745. 'Key': thumbspng
  746. },
  747. ExpiresIn=3600
  748. )
  749. else: # 1:国外
  750. response_url = aws_s3_guowai.generate_presigned_url(
  751. ClientMethod='put_object',
  752. Params={
  753. 'Bucket': 'foreignpush',
  754. 'Key': thumbspng
  755. },
  756. ExpiresIn=3600
  757. )
  758. # response_url = response_url[:4] + response_url[5:]
  759. img_url_list.append(response_url)
  760. # img_url_list.append(url)
  761. res_data = {'code': 0, 'img_url_list': img_url_list, 'msg': 'success'}
  762. return JsonResponse(status=200, data=res_data)
  763. else:
  764. return JsonResponse(status=200, data={'code': 404, 'msg': 'data is not exist'})
  765. else:
  766. return JsonResponse(status=200, data={'code': 404, 'msg': 'wrong etk'})
  767. def do_bulk_create_info(self, uaqs, n_time, channel, event_type, is_st, uid):
  768. now_time = int(time.time())
  769. # 设备昵称
  770. userID_ids = []
  771. sys_msg_list = []
  772. is_sys_msg = self.is_sys_msg(int(event_type))
  773. is_st = int(is_st)
  774. eq_list = []
  775. nickname = uaqs[0]['uid_set__nickname']
  776. if not nickname:
  777. nickname = uid
  778. for ua in uaqs:
  779. lang = ua['lang']
  780. tz = ua['tz']
  781. userID_id = ua["userID_id"]
  782. if userID_id not in userID_ids:
  783. eq_list.append(Equipment_Info(
  784. userID_id=userID_id,
  785. eventTime=n_time,
  786. eventType=event_type,
  787. devUid=uid,
  788. devNickName=nickname,
  789. Channel=channel,
  790. alarm='Motion \tChannel:{channel}'.format(channel=channel),
  791. is_st=is_st,
  792. receiveTime=n_time,
  793. addTime=now_time,
  794. storage_location=2
  795. ))
  796. if is_sys_msg:
  797. sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
  798. event_type=event_type, is_sys=1)
  799. sys_msg_list.append(SysMsgModel(
  800. userID_id=userID_id,
  801. msg=sys_msg_text,
  802. addTime=now_time,
  803. updTime=now_time,
  804. uid=uid,
  805. eventType=event_type))
  806. if eq_list:
  807. print('eq_list')
  808. Equipment_Info.objects.bulk_create(eq_list)
  809. if is_sys_msg:
  810. print('sys_msg')
  811. SysMsgModel.objects.bulk_create(sys_msg_list)
  812. return True
  813. def is_sys_msg(self, event_type):
  814. event_type_list = [702, 703, 704]
  815. if event_type in event_type_list:
  816. return True
  817. return False
  818. def get_msg_text(self, channel, n_time, lang, tz, event_type, is_sys=0):
  819. n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz)
  820. etype = int(event_type)
  821. if lang == 'cn':
  822. if etype == 704:
  823. msg_type = '电量过低'
  824. elif etype == 702:
  825. msg_type = '摄像头休眠'
  826. elif etype == 703:
  827. msg_type = '摄像头唤醒'
  828. else:
  829. msg_type = ''
  830. if is_sys:
  831. send_text = '{msg_type} 通道:{channel}'.format(msg_type=msg_type, channel=channel)
  832. else:
  833. send_text = '{msg_type} 通道:{channel} 日期:{date}'.format(msg_type=msg_type, channel=channel, date=n_date)
  834. else:
  835. if etype == 704:
  836. msg_type = 'Low battery'
  837. elif etype == 702:
  838. msg_type = 'Camera sleep'
  839. elif etype == 703:
  840. msg_type = 'Camera wake'
  841. else:
  842. msg_type = ''
  843. if is_sys:
  844. send_text = '{msg_type} channel:{channel}'. \
  845. format(msg_type=msg_type, channel=channel)
  846. else:
  847. send_text = '{msg_type} channel:{channel} date:{date}'. \
  848. format(msg_type=msg_type, channel=channel, date=n_date)
  849. return send_text