AiController.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: AnsjerFormal
  7. @software: PyCharm
  8. @DATE: 2018/12/5 9:30
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: cloudstorage.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. import base64
  15. import json
  16. import os
  17. import time
  18. import glob
  19. import urllib
  20. from urllib.parse import quote, parse_qs, unquote
  21. import apns2
  22. import boto3
  23. import jpush
  24. import oss2
  25. import paypalrestsdk
  26. import threading
  27. import calendar
  28. import datetime
  29. import logging
  30. import sys
  31. import requests
  32. from aliyunsdkcore import client
  33. from aliyunsdksts.request.v20150401 import AssumeRoleRequest
  34. from boto3.session import Session
  35. from django.http import JsonResponse, HttpResponseRedirect, HttpResponse
  36. from django.db import transaction
  37. from django.views.generic.base import View
  38. import jwt
  39. from Object.ETkObject import ETkObject
  40. from pyfcm import FCMNotification
  41. from Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, OSS_ROLE_ARN, SERVER_DOMAIN, PAYPAL_CRD, \
  42. SERVER_DOMAIN_SSL, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ARN, APNS_MODE, APNS_CONFIG, BASE_DIR, \
  43. JPUSH_CONFIG, FCM_CONFIG, OAUTH_ACCESS_TOKEN_SECRET
  44. from Controller.CheckUserData import DataValid
  45. from Model.models import Device_Info, Order_Model, Store_Meal, VodHlsModel, OssCrdModel, UID_Bucket, StsCrdModel, \
  46. ExperienceContextModel, Pay_Type, CDKcontextModel, Device_User, SysMassModel, SysMsgModel, UidPushModel, \
  47. Unused_Uid_Meal, UIDMainUser, UserModel, PromotionRuleModel, VideoPlaybackTimeModel, CloudLogModel, CouponModel, \
  48. AiStoreMeal, AiService, UidSetModel, Ai_Push_Info, iotdeviceInfoModel, AiProcessTime
  49. from Object.AWS.S3Email import S3Email
  50. from Object.AliPayObject import AliPayObject
  51. from Object.AliSmsObject import AliSmsObject
  52. from Object.RedisObject import RedisObject
  53. from Object.ResponseObject import ResponseObject
  54. from Object.TokenObject import TokenObject
  55. from Object.UidTokenObject import UidTokenObject
  56. from Service.CommonService import CommonService
  57. from Object.m3u8generate import PlaylistGenerator
  58. from Object.WechatPayObject import WechatPayObject
  59. from django.db.models import Q, F, Count
  60. from Controller.PaymentCycle import Paypal
  61. from decimal import Decimal
  62. from Ansjer.config import SERVER_TYPE
  63. from Service.ModelService import ModelService
  64. from Object import MergePic
  65. import boto3
  66. import botocore
  67. from botocore import client
  68. # AI服务
  69. class AiView(View):
  70. def get(self, request, *args, **kwargs):
  71. request.encoding = 'utf-8'
  72. operation = kwargs.get('operation')
  73. return self.validation(request.GET, request, operation)
  74. def post(self, request, *args, **kwargs):
  75. request.encoding = 'utf-8'
  76. operation = kwargs.get('operation')
  77. return self.validation(request.POST, request, operation)
  78. def validation(self, request_dict, request, operation):
  79. response = ResponseObject()
  80. if operation is None:
  81. return response.json(444, 'error path')
  82. elif operation == 'identification': # ai识别
  83. return self.do_ai_identification(request.POST, response)
  84. else:
  85. token = request_dict.get('token', None)
  86. # 设备主键uid
  87. tko = TokenObject(token)
  88. response.lang = tko.lang
  89. if tko.code != 0:
  90. return response.json(tko.code)
  91. userID = tko.userID
  92. if operation == 'createpayorder': # 创建支付订单
  93. ip = CommonService.get_ip_address(request)
  94. return self.do_create_pay_order(request_dict, userID, ip, response)
  95. elif operation == 'changeaistatus': # 修改AI开关状态
  96. return self.do_change_ai_status(userID, request_dict, response)
  97. elif operation == 'getAiStatus': # 获取AI开关状态
  98. return self.getAiStatus(userID, request_dict, response)
  99. elif operation == 'commoditylist': # 修改云存状态,传送两个url,即getsignsts接口和storeplaylist接口
  100. return self.do_commodity_list(userID, request_dict, response)
  101. elif operation == 'identification': # ai识别
  102. return self.do_ai_identification(request_dict, response)
  103. elif operation == 'queryInfo': # 查询消息列表
  104. return self.queryInfo(userID, request_dict, response)
  105. elif operation == 'readInfo': # 消息已读
  106. return self.readInfo(userID, request_dict, response)
  107. elif operation == 'deleteInfo': # 删除消息
  108. return self.deleteInfo(userID, request_dict, response)
  109. elif operation == 'queryorderlist': # 查询订单
  110. return self.do_querylist(userID, request_dict, response)
  111. elif operation == 'updateJpushTime': # 查询订单
  112. return self.update_jpush_time(userID, request_dict, response)
  113. elif operation == 'updateJpushTime': # 查询订单
  114. return self.update_jpush_time(userID, request_dict, response)
  115. else:
  116. return response.json(414)
  117. def do_change_ai_status(self, userID, request_dict, response):
  118. token_val = request_dict.get('token_val', None)
  119. appBundleId = request_dict.get('appBundleId', None)
  120. app_type = request_dict.get('app_type', None)
  121. push_type = request_dict.get('push_type', None)
  122. status = request_dict.get('status', None)
  123. m_code = request_dict.get('m_code', None)
  124. uid = request_dict.get('uid', None)
  125. # 设备语言
  126. lang = request_dict.get('lang', 'en')
  127. tz = request_dict.get('tz', '0')
  128. # 消息提醒功能新增
  129. # 如果传空上来,就默认为0
  130. if tz == '':
  131. tz = 0
  132. else:
  133. tz = tz.replace("GMT", "")
  134. detect_group = request_dict.get('detect_group', None)
  135. interval = request_dict.get('interval', None)
  136. if not status:
  137. return response.json(444, 'status')
  138. # 关闭推送
  139. if not all([appBundleId, app_type, token_val, uid, m_code]):
  140. return response.json(444, 'appBundleId,app_type,token_val,uid,m_code')
  141. # 判断推送类型对应key是否存在
  142. print('push_type:', push_type)
  143. if push_type == '0':
  144. if appBundleId not in APNS_CONFIG.keys():
  145. return response.json(904)
  146. elif push_type == '1':
  147. if appBundleId not in FCM_CONFIG.keys():
  148. return response.json(904)
  149. elif push_type == '2':
  150. if appBundleId not in JPUSH_CONFIG.keys():
  151. return response.json(904)
  152. else:
  153. return response.json(173)
  154. hasAiService = AiService.objects.filter(uid=uid,use_status=1)
  155. if not hasAiService.exists():
  156. return response.json(10053)
  157. nowTime = int(time.time())
  158. endTime = hasAiService.values('endTime')[0]['endTime']
  159. if nowTime >endTime:
  160. return response.json(10054)
  161. dvqs = Device_Info.objects.filter(userID_id=userID, UID=uid)
  162. status = int(status)
  163. nowTime = int(time.time())
  164. if dvqs.exists():
  165. # 修改状态
  166. # dvqs.update(NotificationMode=status)
  167. uid_set_qs = UidSetModel.objects.filter(uid=uid)
  168. # uid配置信息是否存在
  169. if uid_set_qs.exists():
  170. uid_set_id = uid_set_qs[0].id
  171. qs_data = {
  172. 'updTime': nowTime,
  173. }
  174. if interval:
  175. qs_data['detect_interval'] = int(interval)
  176. if detect_group:
  177. qs_data['detect_group'] = detect_group
  178. uid_set_qs.update(**qs_data)
  179. else:
  180. qs_data = {
  181. 'uid': uid,
  182. 'addTime': nowTime,
  183. 'updTime': nowTime,
  184. }
  185. if interval:
  186. qs_data['detect_interval'] = int(interval)
  187. if detect_group:
  188. qs_data['detect_group'] = detect_group
  189. # 添加设备配置
  190. uid_set_qs = UidSetModel.objects.create(**qs_data)
  191. uid_set_id = uid_set_qs.id
  192. topic_name = 'AiServer/{}'.format(uid)
  193. if status == 0:
  194. hasAiService.update(**qs_data)
  195. # UidPushModel.objects.filter(uid_set__uid=uid).delete()
  196. # 状态为0的时候删除redis缓存数据
  197. # self.do_delete_redis(uid)
  198. # mqtt通知设备关闭AI识别功能
  199. msg = {'AiStatus': 'inactive'},
  200. req_success = self.requestPublishMqtt(uid, topic_name, msg)
  201. if not req_success:
  202. return response.json(10044)
  203. return response.json(0)
  204. elif status == 1:
  205. hasAiService.update(**qs_data)
  206. uid_push_qs = UidPushModel.objects.filter(userID_id=userID, m_code=m_code, uid_set__uid=uid)
  207. if uid_push_qs.exists():
  208. uid_push_update_dict = {
  209. 'appBundleId': appBundleId,
  210. 'app_type': app_type,
  211. 'push_type': push_type,
  212. 'token_val': token_val,
  213. 'updTime': nowTime,
  214. 'lang': lang,
  215. 'tz': tz
  216. }
  217. uid_push_qs.update(**uid_push_update_dict)
  218. else:
  219. # uid_set_id = uid_set_qs[0].id
  220. uid_push_create_dict = {
  221. 'uid_set_id': uid_set_id,
  222. 'userID_id': userID,
  223. 'appBundleId': appBundleId,
  224. 'app_type': app_type,
  225. 'push_type': push_type,
  226. 'token_val': token_val,
  227. 'm_code': m_code,
  228. 'addTime': nowTime,
  229. 'updTime': nowTime,
  230. 'lang': lang,
  231. 'tz': tz
  232. }
  233. # 绑定设备推送
  234. UidPushModel.objects.create(**uid_push_create_dict)
  235. # if interval:
  236. # self.do_delete_redis(uid, int(interval))
  237. # else:
  238. # self.do_delete_redis(uid)
  239. # utko = UidTokenObject()
  240. # # right
  241. # utko.generate(data={'uid': uid})
  242. etkObj = ETkObject(etk='')
  243. etk = etkObj.encrypt(uid)
  244. #只返回一个接口就行
  245. # detectUrl = "{DETECT_PUSH_DOMAIN}AiService/push?etk={etk}&endTime={endTime}". \
  246. # format(etk=etk, DETECT_PUSH_DOMAIN=SERVER_DOMAIN_SSL, endTime=endTime)
  247. aiIdentificationUrl = "{DETECT_PUSH_DOMAIN}AiService/identification".format(DETECT_PUSH_DOMAIN=SERVER_DOMAIN_SSL)
  248. # mqtt通知设备开启AI识别功能
  249. msg = {
  250. 'AiStatus': 'active',
  251. 'etk': etk,
  252. 'endTime': endTime,
  253. 'aiIdentificationUrl': aiIdentificationUrl,
  254. },
  255. req_success = self.requestPublishMqtt(uid, topic_name, msg)
  256. if not req_success:
  257. return response.json(10044)
  258. return response.json(0, {'aiIdentificationUrl': aiIdentificationUrl, 'endTime': endTime, 'etk': etk})
  259. else:
  260. return response.json(14)
  261. def getAiStatus(self, userID, request_dict, response):
  262. uid = request_dict.get('uid', None)
  263. if not uid:
  264. return response.json(444)
  265. try:
  266. ai_server_qs = AiService.objects.filter(uid=uid).values('detect_status', 'detect_group')
  267. if not ai_server_qs.exists():
  268. return response,json(173)
  269. res = {
  270. 'detect_status': ai_server_qs[0]['detect_status'],
  271. 'detect_group': ai_server_qs[0]['detect_group'],
  272. }
  273. return response.json(0, {'data': res})
  274. except Exception as e:
  275. return response.json(500, repr(e))
  276. def requestPublishMqtt(self, thing_name, topic_name, msg):
  277. # 通用发布MQTT主题通知
  278. if not all([msg, thing_name, topic_name]):
  279. return False
  280. try:
  281. # 获取数据组织将要请求的url
  282. iot = iotdeviceInfoModel.objects.filter(
  283. thing_name__icontains=thing_name).values(
  284. 'endpoint', 'token_iot_number')
  285. if not iot.exists():
  286. return False
  287. endpoint = iot[0]['endpoint']
  288. Token = iot[0]['token_iot_number']
  289. # api doc: https://docs.aws.amazon.com/zh_cn/iot/latest/developerguide/http.html
  290. # url: https://IoT_data_endpoint/topics/url_encoded_topic_name?qos=1
  291. # post请求url发布MQTT消息
  292. url = 'https://{}/topics/{}'.format(endpoint, topic_name)
  293. authorizer_name = 'Ansjer_Iot_Auth'
  294. signature = CommonService.rsa_sign(Token) # Token签名
  295. headers = {
  296. 'x-amz-customauthorizer-name': authorizer_name,
  297. 'Token': Token,
  298. 'x-amz-customauthorizer-signature': signature}
  299. r = requests.post(url=url, headers=headers, json=msg, timeout=2)
  300. if r.status_code == 200:
  301. res = r.json()
  302. if res['message'] == 'OK':
  303. return True
  304. return False
  305. else:
  306. return False
  307. except Exception as e:
  308. return False
  309. def do_commodity_list(self, userID, request_dict, response): # 查询套餐列表
  310. uid = request_dict.get('uid', None)
  311. lang = request_dict.get('lang', 'en')
  312. nowTime = int(time.time())
  313. # DVR/NVR设备暂不返回云存套餐列表
  314. device_info_qs = Device_Info.objects.filter(Q(UID=uid), Q(Type__lte=4) | Q(Type=10001))
  315. if device_info_qs.exists():
  316. return response.json(0)
  317. qs = AiStoreMeal.objects
  318. qs = qs.filter(is_show=1) #过滤隐藏套餐
  319. qs = qs.annotate(ai_meal_id=F('id'))
  320. qs = qs.values("ai_meal_id", "title", "content", "price", "effective_day", "currency",
  321. "virtual_price", "symbol", "pay_type")
  322. if qs.exists():
  323. res = list(qs)
  324. for key, val in enumerate(res):
  325. pay_types = Pay_Type.objects.filter(aistoremeal=res[key]['ai_meal_id']).values("id", "payment")
  326. res[key]['pay_type'] = list(pay_types)
  327. result = {
  328. 'meals': res,
  329. }
  330. return response.json(0, result)
  331. else:
  332. return response.json(0)
  333. def do_querylist(self, userID, request_dict, response):
  334. page = request_dict.get('page', None)
  335. line = request_dict.get('line', None)
  336. uid = request_dict.get('uid', None)
  337. lang = request_dict.get('lang', 'en')
  338. if not page or not line:
  339. return response.json(444, 'page,line')
  340. page = int(page)
  341. line = int(line)
  342. omqs = Order_Model.objects.filter(userID_id=userID, status=1, order_type=1)
  343. # 筛选指定设备id的订单
  344. if uid:
  345. omqs.filter(UID=uid)
  346. if not omqs.exists():
  347. return response.json(173)
  348. # return response.json(10, '订单不存在')
  349. count = omqs.count()
  350. omqs = omqs.annotate(rank__title=F('ai_rank__title'), rank__content=F('ai_rank__content'),
  351. rank__day=F('ai_rank__effective_day'), rank__price=F('ai_rank__price'),
  352. rank__expire=F('ai_rank__effective_day'), rank__id=F('ai_rank_id'), rank__currency=F('ai_rank__currency'))
  353. order_ql = omqs[(page - 1) * line:page * line].values("orderID", "UID", "channel", "desc", "price", "currency",
  354. "addTime",
  355. "updTime", "paypal", "rank__day", "payType",
  356. "rank__price", "status",
  357. "rank__content", "rank__title", "rank__currency",
  358. "rank__expire", "ai_rank_id")
  359. order_list = list(order_ql)
  360. data = []
  361. nowTime = int(time.time())
  362. # 这里需要进行优化
  363. uid_list = []
  364. for od in order_list:
  365. uid_list.append(od['UID'])
  366. didqs = Device_Info.objects.filter(userID_id=userID, UID__in=uid_list).values('id', 'UID', 'Type')
  367. for d in order_list:
  368. if d['status'] == 0:
  369. if d['addTime'] + 3600 < nowTime:
  370. d['status'] = 3
  371. for did in didqs:
  372. if d['UID'] == did['UID']:
  373. d['did'] = did['id']
  374. d['Type'] = did['Type']
  375. data.append(d)
  376. # d['rank__lang__content'] = '月' if lang == 'cn' else 'month'
  377. return response.json(0, {'data': data, 'count': count})
  378. def do_create_pay_order(self, request_dict, userID, ip, response):
  379. uid = request_dict.get('uid', None)
  380. channel = request_dict.get('channel', None)
  381. pay_type = int(request_dict.get('pay_type', 1))
  382. ai_meal_id = request_dict.get('ai_meal_id', None)
  383. lang = request_dict.get('lang', 'en')
  384. if not uid or not channel or not pay_type or not ai_meal_id:
  385. return response.json(444)
  386. # dv_qs = Device_Info.objects.filter(userID_id=userID, UID=uid, isShare=False, isExist=1).values(
  387. # 'vodPrimaryUserID',
  388. # 'vodPrimaryMaster')
  389. # if not dv_qs.exists():
  390. # return response.json(12)
  391. # dvq = Device_Info.objects.filter(UID=uid)
  392. # dvq = dvq.filter(~Q(vodPrimaryUserID='')).values('vodPrimaryUserID')
  393. # if dvq.exists():
  394. # if dvq[0]['vodPrimaryUserID'] != userID:
  395. # return response.json(10033)
  396. nowTime = int(time.time())
  397. smqs = AiStoreMeal.objects.filter(id=ai_meal_id, pay_type=pay_type, is_show=1). \
  398. values('currency', 'price', 'content', 'effective_day', 'title')
  399. if not smqs.exists():
  400. return response.json(173)
  401. currency = smqs[0]['currency']
  402. price = smqs[0]['price']
  403. content = smqs[0]['content']
  404. day = smqs[0]['effective_day']
  405. orderID = CommonService.createOrderID()
  406. price = float(price)
  407. price = round(price, 2)
  408. if pay_type == 1:
  409. #正常扣款
  410. cal_url = "{SERVER_DOMAIN_SSL}web/paid2/fail.html".format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL)
  411. if lang != 'cn':
  412. cal_url = "{SERVER_DOMAIN_SSL}web/paid2/en_fail.html".format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL)
  413. call_sub_url = "{SERVER_DOMAIN_SSL}cloudstorage/dopaypalcallback?orderID={orderID}&lang={lang}". \
  414. format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL, orderID=orderID, lang=lang)
  415. # call_sub_url = "http://binbin.uicp.vip/cloudstorage/dopaypalcallback?orderID={orderID}".format(
  416. # SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL, orderID=orderID)
  417. Order_Model.objects.create(orderID=orderID, UID=uid, channel=channel, userID_id=userID,
  418. desc=content, payType=pay_type, payTime=nowTime,
  419. price=price, currency=currency, addTime=nowTime, updTime=nowTime,
  420. pay_url='', commodity_code='', commodity_type=1,
  421. ai_rank_id=ai_meal_id, rank_id=1, order_type=1, status=1)
  422. has = AiService.objects.filter(uid=uid, channel=channel, use_status=1)
  423. if has.exists():
  424. use_status = 0
  425. endTime = 0
  426. else:
  427. use_status = 1
  428. endTime = nowTime+(day*86400)
  429. # return response.json(0)
  430. AiService.objects.create(orders_id=orderID, uid=uid, channel=channel, detect_status=1,
  431. endTime=endTime, addTime=nowTime, updTime=nowTime,
  432. use_status=use_status, detect_group='1')
  433. return response.json(0, {"orderID": orderID})
  434. return response.json(10, 'generate_order_false')
  435. def update_jpush_time(self,userID, request_dict, response):
  436. logger = logging.getLogger('info')
  437. msg_id = request_dict.get('msg_id', None)
  438. apns_push_time = request_dict.get('apns_push_time', None)
  439. logger.info("----init--apns----push_time={apns_push_time}".format(apns_push_time=apns_push_time))
  440. apns_push_time = int(apns_push_time)
  441. logger.info("---msg--id={msg_id}".format(msg_id=msg_id))
  442. logger.info("---apns----push_time={apns_push_time}".format(apns_push_time=apns_push_time))
  443. try:
  444. aiRes = AiProcessTime.objects.filter(msg_id=msg_id).update(appPushTime=apns_push_time)
  445. except Exception as e:
  446. logger.info(repr(e))
  447. return response.json(902,repr(e))
  448. return response.json(0,aiRes)
  449. def do_ai_identification(self, request_dict,response):
  450. msg_id = CommonService.createOrderID()
  451. AiProcessTime.objects.create(detectTime=0, receiveTime=0, aiTime=0,
  452. aiEndTime=0, pushTime=0,
  453. pushEndTime=0, serverCountTime=0, msg_id=msg_id)
  454. etk = request_dict.get('etk', None)
  455. n_time = request_dict.get('n_time', None)
  456. receiveTime = int(time.time())
  457. logger = logging.getLogger('info')
  458. logger.info('-----------into----ai--api')
  459. logger.info("etk={etk}".format(etk=etk))
  460. if not etk:
  461. return response.json(444)
  462. try:
  463. # 解密uid及判断长度
  464. eto = ETkObject(etk)
  465. uid = eto.uid
  466. logger.info("uid={uid}".format(uid=uid))
  467. if len(uid) != 20 and len(uid) != 14:
  468. return response.json(444)
  469. ##通过uid查出endTime是否过期,并且ai开关是否打开
  470. AiServiceQuery = AiService.objects.filter(uid=uid, detect_status=1, use_status=1, endTime__gt=receiveTime).\
  471. values('detect_group')
  472. if not AiServiceQuery.exists():
  473. logger.info('none-----aiService')
  474. return response.json(173)
  475. detect_group = AiServiceQuery[0]['detect_group']
  476. #{}??
  477. #
  478. file_post_one = request_dict.get('fileOne', None)
  479. file_post_two = request_dict.get('fileTwo', None)
  480. file_post_three = request_dict.get('fileThree', None)
  481. file_post_one = file_post_one.replace(' ', '+')
  482. file_post_two = file_post_two.replace(' ', '+')
  483. file_post_three = file_post_three.replace(' ', '+')
  484. file_post_one = base64.b64decode(file_post_one)
  485. file_post_two = base64.b64decode(file_post_two)
  486. file_post_three = base64.b64decode(file_post_three)
  487. file_list = [file_post_one, file_post_two, file_post_three]
  488. del file_post_one, file_post_two, file_post_three
  489. dir_path = os.path.join(BASE_DIR, 'static/ai/' + uid + '/' + str(receiveTime))
  490. if not os.path.exists(dir_path):
  491. os.makedirs(dir_path)
  492. file_path_list = []
  493. i = 1
  494. for index in file_list:
  495. file_path = dir_path + '/' + str(i) + '.jpg'
  496. file_path_list.append(file_path)
  497. with open(file_path, 'wb') as f:
  498. f.write(index)
  499. f.close()
  500. i += 1
  501. image_size = 500 # 每张小图片的大小
  502. image_colnum = 1 # 合并成一张图后,一行有几个小图
  503. MergePic.merge_images(dir_path, image_size, image_colnum)
  504. photo = open(dir_path + '.jpg', 'rb') #打开合成图
  505. cover = dir_path + '/' + str(i-1) + '.jpg'
  506. desc = dir_path + '.jpg'
  507. logger.info('----------------cover')
  508. logger.info(cover)
  509. logger.info(desc)
  510. # photo = open(r'E:\test---------------\test\snipaste20220121_215952.jpg', 'rb')
  511. #识别合成图片
  512. maxLabels = 50
  513. minConfidence = 70
  514. ai_start_time = int(time.time())
  515. client = boto3.client(
  516. 'rekognition',
  517. aws_access_key_id='AKIA2E67UIMD6JD6TN3J',
  518. aws_secret_access_key='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',
  519. region_name='us-east-1')
  520. # doc:
  521. rekognition_res = client.detect_labels(
  522. Image={'Bytes': photo.read()},
  523. MaxLabels=maxLabels,
  524. MinConfidence=minConfidence)
  525. if rekognition_res['ResponseMetadata']['HTTPStatusCode'] != 200:
  526. return response.json(173)
  527. ai_end_time = int(time.time())
  528. labels =rekognition_res['Labels']
  529. label_name = []
  530. logger.info('--------识别到的标签-------')
  531. logger.info(labels)
  532. for label in labels:
  533. label_name.append(label['Name'])
  534. for Parents in label['Parents']:
  535. label_name.append(Parents['Name'])
  536. labels = self.checkLabels(detect_group, label_name) #检查标签是否符合用户选择的识别类型
  537. if len(labels['label_list']) == 0:
  538. logger.info('没有识别到任何标签-----------------')
  539. return response.json(10055)
  540. event_type = ','.join(labels['label_type'])
  541. label_list = ','.join(labels['label_list'])
  542. logger.info(event_type)
  543. logger.info(label_list)
  544. #存储消息以及推送
  545. channel = request_dict.get('channel', '1')
  546. is_st = 1 #单图
  547. # 查询推送数据
  548. uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid). \
  549. values('token_val', 'app_type', 'appBundleId', 'm_code', 'push_type', 'userID_id',
  550. 'userID__NickName',
  551. 'lang', 'm_code', 'tz', 'uid_set__nickname', 'uid_set__detect_interval',
  552. 'uid_set__detect_group',
  553. 'uid_set__channel')
  554. if not uid_push_qs.exists():
  555. return response.json(173)
  556. uid_push_list = []
  557. for qs in uid_push_qs:
  558. uid_push_list.append(qs)
  559. nickname = uid_push_list[0]['uid_set__nickname']
  560. if not nickname:
  561. nickname = uid
  562. eq_list = []
  563. userID_ids = []
  564. apns_start_time = 0
  565. apns_end_time = 0
  566. for up in uid_push_list:
  567. push_type = up['push_type']
  568. appBundleId = up['appBundleId']
  569. token_val = up['token_val']
  570. lang = up['lang']
  571. tz = up['tz']
  572. if tz is None or tz == '':
  573. tz = 0
  574. # 推送标题
  575. msg_title = self.get_msg_title(appBundleId=appBundleId, nickname=nickname)
  576. # 推送内容
  577. msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz, label_list=label_list)
  578. kwargs = {
  579. 'uid': uid,
  580. 'channel': channel,
  581. 'event_type': event_type,
  582. 'n_time': n_time,
  583. 'appBundleId': appBundleId,
  584. 'token_val': token_val,
  585. 'msg_title': msg_title,
  586. 'msg_text': msg_text,
  587. 'msg_id' : msg_id,
  588. }
  589. # 推送消息
  590. # if push_type == 0: # ios apns
  591. # logger.info('into-------apns')
  592. # apns_start_time = int(time.time())
  593. # res = self.do_apns(**kwargs)
  594. # apns_end_time = int(time.time())
  595. # logger.info(res)
  596. # # elif push_type == 1: # android gcm
  597. # # self.do_fcm(**kwargs)
  598. # elif push_type == 2: # android jpush
  599. # logger.info('into-------jpush')
  600. # jpush_start_time = int(time.time())
  601. # res = self.do_jpush(**kwargs)
  602. # jpush_end_time = int(time.time())
  603. # logger.info(res)
  604. if push_type == 1: # android gcm
  605. logger.info('into-------gcm')
  606. apns_start_time = int(time.time())
  607. res = self.do_fcm(**kwargs)
  608. apns_end_time = int(time.time())
  609. # 以下是存库
  610. userID_id = up["userID_id"]
  611. if userID_id not in userID_ids:
  612. now_time = int(time.time())
  613. eq_list.append(Ai_Push_Info(
  614. userID_id=userID_id,
  615. eventTime=n_time,
  616. eventType=event_type,
  617. devUid=uid,
  618. devNickName=nickname,
  619. Channel=channel,
  620. alarm='检查到{labels} \tChannel:{channel}'.format(labels=','.join(labels['label_list']), channel=channel),
  621. is_st=is_st,
  622. receiveTime=receiveTime,
  623. addTime=now_time,
  624. storage_location=2
  625. ))
  626. userID_ids.append(userID_id)
  627. Ai_Push_Info.objects.bulk_create(eq_list)
  628. #上传缩略图到s3
  629. cover_start_time = int(time.time())
  630. upload_cover_path = "{uid}/{channel}/cover{n_time}.jpg".format(uid=uid, channel=channel, n_time=n_time) #封面图
  631. upload_desc_path = "{uid}/{channel}/desc{n_time}.jpg".format(uid=uid, channel=channel, n_time=n_time) #详情内容图
  632. c_res = self.upload_s3(cover, upload_cover_path)
  633. d_res = self.upload_s3(desc, upload_desc_path)
  634. if c_res and d_res:
  635. logger.info('upload-----S3----success')
  636. endTime = int(time.time())
  637. count_time = endTime - receiveTime
  638. AiProcessTime.objects.filter(msg_id=msg_id).update(detectTime=n_time, receiveTime=receiveTime, aiTime=ai_start_time,
  639. aiEndTime=ai_end_time, pushTime=apns_start_time,
  640. pushEndTime=apns_end_time, serverCountTime=count_time,picUploadTime=cover_start_time,
  641. picUploadEndTime = endTime, endTime=endTime)
  642. #删除临时文件
  643. # delf = os.path.join(BASE_DIR, 'static/ai')
  644. # logger.info(f"删除目录:{delf}下得文件")
  645. self.delfile(path=delf)
  646. return JsonResponse(status=200, data='success', safe=False)
  647. return JsonResponse(status=500, data='fail', safe=False)
  648. except Exception as e:
  649. print(e)
  650. return response.json(500, repr(e))
  651. def delfile(self, path):
  652. # read all the files under the folder
  653. fileNames = glob.glob(path + r'\*')
  654. for fileName in fileNames:
  655. try:
  656. # delete file
  657. os.remove(fileName)
  658. except:
  659. try:
  660. # delete empty folders
  661. os.rmdir(fileName)
  662. except:
  663. # Not empty, delete files under folders
  664. delfile(fileName)
  665. # now, folders are empty, delete it
  666. os.rmdir(fileName)
  667. ## 检查是否有符合条件的标签
  668. def checkLabels(self, user_detect_group, labels):
  669. labels_type = {
  670. '1': ['Person', 'Human'], #人
  671. '2': ['Dog', 'Pet', 'Canine', 'Animal'], #动物
  672. '3': ['Car', '', 'Vehicle', 'Transportation', 'Automobile'] #车
  673. }
  674. user_detect_list = user_detect_group.split(',')
  675. user_labels_type = {}
  676. for user_detect in user_detect_list:
  677. if user_detect in labels_type.keys():
  678. user_labels_type[user_detect] = labels_type[user_detect]
  679. label_list = []
  680. for k, labels_type in user_labels_type.items():
  681. for label in labels_type:
  682. if label in labels:
  683. label_list.append(label)
  684. user_labels_list = list(user_labels_type.keys())
  685. user_labels_list.sort()
  686. return {'label_type': user_labels_list, 'label_list': label_list}
  687. def upload_s3(self, file_path, upload_path):
  688. try:
  689. aws_key = "AKIA2MMWBR4DSFG67DTG" #【你的 aws_access_key】
  690. aws_secret = "aI9gxcAKPmiGgPy9axrtFKzjYGbvpuytEX4xWweL" # 【你的 aws_secret_key】
  691. session = Session(aws_access_key_id=aws_key,
  692. aws_secret_access_key=aws_secret,
  693. region_name="cn-northwest-1")
  694. s3 = session.resource("s3")
  695. # client = session.client("s3")
  696. bucket = "aipush" # 【你 bucket 的名字】 # 首先需要保.证 s3 上已经存在该存储桶,否则报错
  697. upload_data = open(file_path, "rb")
  698. # upload_key = "test"
  699. s3.Bucket(bucket).put_object(Key=upload_path, Body=upload_data)
  700. return True
  701. except Exception as e:
  702. print(repr(e))
  703. return False
  704. def get_msg_title(self, appBundleId, nickname):
  705. package_title_config = {
  706. 'com.ansjer.customizedd_a': 'DVS',
  707. 'com.ansjer.zccloud_a': 'ZosiSmart',
  708. 'com.ansjer.zccloud_ab': '周视',
  709. 'com.ansjer.adcloud_a': 'ADCloud',
  710. 'com.ansjer.adcloud_ab': 'ADCloud',
  711. 'com.ansjer.accloud_a': 'ACCloud',
  712. 'com.ansjer.loocamccloud_a': 'Loocam',
  713. 'com.ansjer.loocamdcloud_a': 'Anlapus',
  714. 'com.ansjer.customizedb_a': 'COCOONHD',
  715. 'com.ansjer.customizeda_a': 'Guardian365',
  716. 'com.ansjer.customizedc_a': 'PatrolSecure',
  717. }
  718. if appBundleId in package_title_config.keys():
  719. return package_title_config[appBundleId] + '(' + nickname + ')'
  720. else:
  721. return nickname
  722. def get_msg_text(self, channel, n_time, lang, tz, label_list):
  723. n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz,lang=lang)
  724. if lang == 'cn':
  725. msg = '摄像头AI识别到了{}'.format(label_list)
  726. send_text = '{msg} 通道:{channel} 日期:{date}'.format(msg=msg, channel=channel, date=n_date)
  727. else:
  728. msg = 'Camera AI recognizes{}'.format(label_list)
  729. send_text = '{msg} channel:{channel} date:{date}'.format(msg=msg, channel=channel, date=n_date)
  730. return send_text
  731. def do_jpush(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text, msg_id=0):
  732. app_key = JPUSH_CONFIG[appBundleId]['Key']
  733. master_secret = JPUSH_CONFIG[appBundleId]['Secret']
  734. # 此处换成各自的app_key和master_secre
  735. _jpush = jpush.JPush(app_key, master_secret)
  736. push = _jpush.create_push()
  737. push.audience = jpush.registration_id(token_val)
  738. push_data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
  739. "received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1", "channel": channel}
  740. android = jpush.android(alert=msg_text, priority=1, style=1, alert_type=7,
  741. big_text=msg_text, title=msg_title,
  742. extras=push_data)
  743. push.notification = jpush.notification(android=android)
  744. push.platform = jpush.all_
  745. res = push.send()
  746. print(res)
  747. return res.status_code
  748. def do_fcm(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text, msg_id=0):
  749. try:
  750. serverKey = FCM_CONFIG[appBundleId]
  751. push_service = FCMNotification(api_key=serverKey)
  752. data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
  753. "received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1", "channel": channel,
  754. "msg_id": msg_id}
  755. result = push_service.notify_single_device(registration_id=token_val, message_title=msg_title,
  756. message_body=msg_text, data_message=data,
  757. extra_kwargs={
  758. 'default_vibrate_timings': True,
  759. 'default_sound': True,
  760. 'default_light_settings': True
  761. })
  762. print('fcm push ing')
  763. print(result)
  764. return result
  765. except Exception as e:
  766. return 'serverKey abnormal'
  767. def do_apns(self, uid, channel, appBundleId, token_val, event_type, n_time, msg_title, msg_text, msg_id=0):
  768. logger = logging.getLogger('info')
  769. logger.info("进来do_apns函数了")
  770. logger.info(token_val)
  771. logger.info(APNS_MODE)
  772. logger.info(os.path.join(BASE_DIR, APNS_CONFIG[appBundleId]['pem_path']))
  773. try:
  774. cli = apns2.APNSClient(mode=APNS_MODE, client_cert=os.path.join(BASE_DIR, APNS_CONFIG[appBundleId]['pem_path']))
  775. push_data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
  776. "received_at": n_time, "sound": "", "uid": uid, "zpush": "1", "channel": channel, "msg_id": msg_id}
  777. alert = apns2.PayloadAlert(body=msg_text, title=msg_title)
  778. payload = apns2.Payload(alert=alert, custom=push_data, sound="default")
  779. n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
  780. res = cli.push(n=n, device_token=token_val, topic=appBundleId)
  781. if res.status_code == 200:
  782. return res.status_code
  783. else:
  784. logger.info('apns push fail')
  785. logger.info(res.reason)
  786. return res.status_code
  787. except (ValueError, ArithmeticError):
  788. return 'The program has a numeric format exception, one of the arithmetic exceptions'
  789. except Exception as e:
  790. print(repr(e))
  791. logger.info(repr(e))
  792. return repr(e)
  793. def queryInfo(self, userID, request_dict, response):
  794. page = int(request_dict.get('page', None))
  795. line = int(request_dict.get('line', None))
  796. if not page or not line:
  797. return response.json(444, 'page,line')
  798. startTime = request_dict.get('startTime', None)
  799. endTime = request_dict.get('endTime', None)
  800. eventType = request_dict.get('eventType', None)
  801. now_time = int(time.time())
  802. seven_days_ago = now_time - 7 * 24 * 3600 # 查询7天内的数据
  803. qs = Ai_Push_Info.objects.filter(userID_id=userID, eventTime__gt=seven_days_ago).order_by('-eventTime')
  804. if startTime and endTime:
  805. qs = qs.filter(eventTime__range=(startTime, endTime))
  806. # if eventType:
  807. # qs = qs.filter(eventType__contains=eventType)
  808. uids = request_dict.get('uids', None)
  809. if uids:
  810. uid_list = uids.split(',')
  811. qs = qs.filter(devUid__in=uid_list)
  812. dvqs = Device_Info.objects.filter(UID__in=uid_list, userID_id=userID).values('UID', 'Type', 'NickName')
  813. uid_type_dict = {}
  814. for dv in dvqs:
  815. uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}
  816. else:
  817. dvqs = Device_Info.objects.filter(userID_id=userID).values('UID', 'Type', 'NickName')
  818. uid_type_dict = {}
  819. for dv in dvqs:
  820. uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}
  821. if not qs.exists():
  822. return response.json(0, {'datas': [], 'count': 0})
  823. count = qs.count()
  824. qs = qs.values('id', 'devUid', 'devNickName', 'Channel', 'eventType', 'status', 'alarm', 'eventTime',
  825. 'receiveTime', 'is_st', 'addTime', 'storage_location')
  826. qs = qs[(page - 1) * line:page * line]
  827. res = []
  828. aws_s3_client = boto3.client(
  829. 's3',
  830. aws_access_key_id=AWS_ACCESS_KEY_ID[0],
  831. aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],
  832. config=botocore.client.Config(signature_version='s3v4'),
  833. region_name='cn-northwest-1'
  834. )
  835. for p in qs:
  836. p['eventType'] = int(p['eventType'][0])
  837. devUid = p['devUid']
  838. eventTime = p['eventTime']
  839. channel = p['Channel']
  840. storage_location = p['storage_location']
  841. if p['is_st'] == 1:
  842. s3_img_cover = '{uid}/{channel}/cover{time}.jpg'.format(uid=devUid, channel=channel, time=eventTime)
  843. s3_img_desc = '{uid}/{channel}/desc{time}.jpg'.format(uid=devUid, channel=channel, time=eventTime)
  844. response_url_cover = aws_s3_client.generate_presigned_url('get_object',
  845. ExpiresIn=300,
  846. Params={
  847. 'Bucket': 'aipush', 'Key': s3_img_cover
  848. },
  849. )
  850. response_url_desc = aws_s3_client.generate_presigned_url('get_object',
  851. ExpiresIn=300,
  852. Params={
  853. 'Bucket': 'aipush', 'Key': s3_img_desc
  854. },
  855. )
  856. p['img'] = response_url_cover
  857. p['img_list'] = [response_url_desc]
  858. elif p['is_st'] == 2:
  859. pass
  860. if devUid in uid_type_dict.keys():
  861. p['uid_type'] = uid_type_dict[devUid]['type']
  862. p['devNickName'] = uid_type_dict[devUid]['NickName']
  863. else:
  864. p['uid_type'] = ''
  865. res.append(p)
  866. return response.json(0, {'datas': res, 'count': count})
  867. def readInfo(self, userID, request_dict, response):
  868. is_update_all = request_dict.get('is_update_all', 0)
  869. try:
  870. if int(is_update_all) == 1: # 全部已读
  871. is_update = Ai_Push_Info.objects.filter(userID_id=userID).update(status=1)
  872. return response.json(0, {'update_count': is_update})
  873. else:
  874. id_list = request_dict.get('id_list', None)
  875. if not id_list:
  876. request_dict.getlist('id_list[]', None) # 获取IOS数组传参
  877. logger = logging.getLogger('info')
  878. logger.info('已读ai消息id_list:{}'.format(id_list))
  879. if not id_list:
  880. return response.json(444)
  881. id_list = eval(id_list) # 字符串转列表
  882. param_flag = CommonService.get_param_flag(data=id_list)
  883. if not param_flag:
  884. return response.json(444)
  885. count = 0
  886. for id in id_list:
  887. ai_push_qs = Ai_Push_Info.objects.filter(id=int(id))
  888. if ai_push_qs.exists():
  889. own_dev = ModelService.check_own_device(userID, ai_push_qs[0].devUid)
  890. if own_dev:
  891. count += 1
  892. ai_push_qs.update(status=1)
  893. return response.json(0, {'update_success': count})
  894. except Exception as e:
  895. print(e)
  896. return response.json(500, repr(e))
  897. def deleteInfo(self, userID, request_dict, response):
  898. id_list = request_dict.get('id_list', None)
  899. if not id_list:
  900. request_dict.getlist('id_list[]', None) # 获取IOS数组传参
  901. logger = logging.getLogger('info')
  902. logger.info('删除ai消息id_list:{}'.format(id_list))
  903. if not id_list:
  904. return response.json(444)
  905. try:
  906. id_list = eval(id_list) # 字符串转列表
  907. param_flag = CommonService.get_param_flag(data=id_list)
  908. if not param_flag:
  909. return response.json(444)
  910. for id in id_list:
  911. ai_push_qs = Ai_Push_Info.objects.filter(id=id)
  912. if ai_push_qs.exists():
  913. own_dev = ModelService.check_own_device(userID, ai_push_qs[0].devUid)
  914. if own_dev:
  915. ai_push_qs.delete()
  916. return response.json(0)
  917. except Exception as e:
  918. print(e)
  919. return response.json(500, repr(e))
  920. # 如果ai套餐过期,更新未使用的关联套餐
  921. def updateUnusedAi(request):
  922. response = ResponseObject()
  923. now_time = int(time.time())
  924. expired_ai_services = AiService.objects.filter(endTime__lte=now_time,use_status=1).values("id","uid")[0:200]
  925. for expired_ai_service in expired_ai_services:
  926. try:
  927. with transaction.atomic():
  928. AiService.objects.filter(id=expired_ai_service['id']).update(use_status=2)
  929. hasUnUse = AiService.objects.filter(uid=expired_ai_service['uid'],use_status=0).order_by('addTime')[0:1]
  930. if hasUnUse.exists():
  931. hasUnUse = hasUnUse.values('id','orders__ai_rank__effective_day')
  932. endTime = now_time + (hasUnUse[0]['orders__ai_rank__effective_day'] * 86400)
  933. AiService.objects.filter(id=hasUnUse[0]['id']).update(use_status=1, endTime=endTime)
  934. except Exception as e:
  935. # return HttpResponse("line={line},error={error}".format(line=e.__traceback__.tb_lineno, error=repr(e)))
  936. continue
  937. # UID_Bucket.objects.filter(id__in=list(id_list)).update(use_status=2) # 更新过期云存关联套餐状态
  938. return response.json(0)