MessageMangementController.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import time
  2. import boto3
  3. import botocore
  4. import oss2
  5. from botocore import client
  6. from django.core.paginator import Paginator
  7. from django.db.models import Q
  8. from django.views.generic.base import View
  9. from obs import ObsClient
  10. from Ansjer.config import CONFIG_EUR, CONFIG_US, HUAWEICLOUD_PUSH_BUKET, HUAWEICLOUD_OBS_SERVER
  11. from Ansjer.config import SERVER_TYPE
  12. from Controller.DetectControllerV2 import DetectControllerViewV2
  13. from Model import models
  14. from Model.models import Device_Info, VodBucketModel, DeviceTypeModel
  15. from Object.OCIObjectStorage import OCIObjectStorage
  16. from Object.RedisObject import RedisObject
  17. from Object.ResponseObject import ResponseObject
  18. from Object.TokenObject import TokenObject
  19. from Object.utils import LocalDateTimeUtil
  20. from Service.EquipmentInfoService import EquipmentInfoService
  21. from Service.VodHlsService import SplitVodHlsObject
  22. from django.conf import settings
  23. AWS_ACCESS_KEY_ID = settings.AWS_ACCESS_KEY_ID
  24. AWS_SECRET_ACCESS_KEY = settings.AWS_SECRET_ACCESS_KEY
  25. OSS_STS_ACCESS_KEY = settings.OSS_STS_ACCESS_KEY
  26. OSS_STS_ACCESS_SECRET = settings.OSS_STS_ACCESS_SECRET
  27. HUAWEICLOUD_AK = settings.HUAWEICLOUD_AK
  28. HUAWEICLOUD_SK = settings.HUAWEICLOUD_SK
  29. class MassageView(View):
  30. def get(self, request, *args, **kwargs):
  31. request.encoding = 'utf-8'
  32. operation = kwargs.get('operation')
  33. return self.validation(request, request.GET, operation)
  34. def post(self, request, *args, **kwargs):
  35. request.encoding = 'utf-8'
  36. operation = kwargs.get('operation')
  37. return self.validation(request, request.POST, operation)
  38. def validation(self, request, request_dict, operation):
  39. language = request_dict.get('language', 'en')
  40. response = ResponseObject(language, 'pc')
  41. if operation == 'XXX':
  42. return 0
  43. else:
  44. tko = TokenObject(request.META.get('HTTP_AUTHORIZATION'), returntpye='pc')
  45. if tko.code != 0:
  46. return response.json(tko.code)
  47. if operation == 'queryInfoList':
  48. return self.query_info_list(request_dict, response)
  49. else:
  50. return response.json(414)
  51. def query_info_list(self, request_dict, response):
  52. """
  53. 查询推送数据
  54. @param request_dict: 请求参数
  55. @request_dict uids: 设备id
  56. @request_dict eventType: 事件类型
  57. @request_dict eventType: userID
  58. @request_dict startTime: 开始时间戳
  59. @request_dict endTime: 结束时间戳
  60. @request_dict pageNo: 开始时间戳
  61. @request_dict pageSize: 结束时间戳
  62. @param response:
  63. @return:
  64. """
  65. try:
  66. uids = request_dict.get('uids', None)
  67. event_type = request_dict.get('eventType', None)
  68. user_id = request_dict.get('userID', None)
  69. # 时间
  70. start_time = request_dict.get('startTime', None)
  71. end_time = request_dict.get('endTime', None)
  72. # 分页
  73. page = int(request_dict.get('pageNo', None))
  74. size = int(request_dict.get('pageSize', None))
  75. # 区域
  76. if SERVER_TYPE == 'Ansjer.cn_config.formal_settings' or SERVER_TYPE == 'Ansjer.cn_config.test_settings':
  77. region = 2
  78. else:
  79. region = 1
  80. query = Q()
  81. if not start_time and not end_time:
  82. # 默认查询近七天内数据
  83. end_time = int(time.time())
  84. start_time = LocalDateTimeUtil.get_before_days_timestamp(end_time, 7)
  85. query &= Q(event_time__range=(start_time, end_time))
  86. elif start_time and end_time:
  87. query &= Q(event_time__range=(start_time, end_time))
  88. else:
  89. response.json(10, "需要给出一个时间段")
  90. if user_id is None and uids is None:
  91. return response.json(0, {"list": [], "total": 0})
  92. # 过滤条件
  93. if user_id is not None and user_id != "":
  94. query &= Q(device_user_id=user_id)
  95. if uids is not None and uids != "":
  96. uid_list = uids.split(',')
  97. query &= Q(device_uid__in=uid_list)
  98. if event_type is not None:
  99. event_type_list = EquipmentInfoService.get_comb_event_type(event_type)
  100. event_type_list = list(set(event_type_list))
  101. tags = EquipmentInfoService.get_event_tag(event_type)
  102. if event_type_list:
  103. query &= Q(event_type__in=event_type_list)
  104. tags = ''
  105. query &= Q(event_tag__regex=tags)
  106. elif tags:
  107. query &= Q(event_tag__regex=tags)
  108. # 联表查询
  109. querysets = []
  110. for i in range(1, 41):
  111. table_name = f'EquipmentInfo{i}'
  112. model_class = getattr(models, table_name)
  113. annotated_queryset = model_class.objects.filter(query)
  114. querysets.append(annotated_queryset)
  115. equipment_info_combined_qs = querysets[0].union(*querysets[1:], all=True)
  116. # 创建分页对象
  117. equipment_info_combined_qs = equipment_info_combined_qs.order_by("-event_time")
  118. paginator = Paginator(equipment_info_combined_qs, size)
  119. # 获取请求页的数据
  120. packages_page = paginator.page(page)
  121. # 连接存储桶
  122. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  123. oss_img_bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
  124. # 华为云
  125. obs_client = ObsClient(
  126. access_key_id=HUAWEICLOUD_AK, secret_access_key=HUAWEICLOUD_SK, server=HUAWEICLOUD_OBS_SERVER)
  127. aws_s3 = boto3.client(
  128. 's3',
  129. aws_access_key_id=AWS_ACCESS_KEY_ID[1],
  130. aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
  131. config=botocore.client.Config(signature_version='s3v4'),
  132. region_name='us-east-1'
  133. )
  134. aws_s3_cn = boto3.client(
  135. 's3',
  136. aws_access_key_id=AWS_ACCESS_KEY_ID[0],
  137. aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],
  138. config=botocore.client.Config(signature_version='s3v4'),
  139. region_name='cn-northwest-1'
  140. )
  141. # 国内生产环境默认不实例OCI对象
  142. oci_eur = OCIObjectStorage(CONFIG_EUR)
  143. oci_us = OCIObjectStorage(CONFIG_US)
  144. redis_obj = RedisObject(3)
  145. # ai消息标识所有组合标签
  146. ai_all_event_type = EquipmentInfoService.get_all_comb_event_type()
  147. # 遍历调整返回数据
  148. equipment_info_list = []
  149. for equipment_info in packages_page:
  150. uid = equipment_info.device_uid
  151. channel = equipment_info.channel
  152. event_time = equipment_info.event_time
  153. storage_location = equipment_info.storage_location
  154. border_coords = equipment_info.border_coords
  155. event_tag = equipment_info.event_tag
  156. img_url = ""
  157. if equipment_info.is_st == 1:
  158. thumbspng = '{}/{}/{}.jpeg'.format(uid, channel, event_time)
  159. if storage_location == 1: # 阿里云oss
  160. img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
  161. elif storage_location == 5: # 华为云
  162. create_res = obs_client.createSignedUrl(
  163. method='GET', bucketName=HUAWEICLOUD_PUSH_BUKET, objectKey=thumbspng, expires=300)
  164. img_url = create_res.signedUrl
  165. elif storage_location in [3, 4]:
  166. prefix_name = f'{uid}/'
  167. oci = oci_eur if storage_location == 4 else oci_us
  168. img_url = DetectControllerViewV2.oci_object_url(oci, redis_obj, uid, prefix_name)
  169. if img_url:
  170. img_url = img_url + thumbspng
  171. else:
  172. params = {'Key': thumbspng}
  173. if region == 1: # AWS国外
  174. params['Bucket'] = 'foreignpush'
  175. img_url = aws_s3.generate_presigned_url(
  176. 'get_object', Params=params, ExpiresIn=300)
  177. else: # AWS国内
  178. params['Bucket'] = 'push'
  179. img_url = aws_s3_cn.generate_presigned_url(
  180. 'get_object', Params=params, ExpiresIn=300)
  181. img_url = img_url
  182. img_list = [img_url]
  183. elif equipment_info.is_st == 2:
  184. # 列表装载回放时间戳标记
  185. split_vod_hls_obj = SplitVodHlsObject()
  186. vodqs = split_vod_hls_obj.get_vod_hls_data(
  187. uid=uid, channel=channel, start_time=int(event_time)).values('bucket_id')
  188. if not vodqs.exists():
  189. return response.json(173)
  190. vod_bucket_qs = VodBucketModel.objects.filter(id=vodqs[0]['bucket_id']).values('bucket', 'endpoint')
  191. if not vod_bucket_qs.exists():
  192. return response.json(173)
  193. bucket_name = vod_bucket_qs[0]['bucket']
  194. endpoint = vod_bucket_qs[0]['endpoint']
  195. bucket = oss2.Bucket(auth, endpoint, bucket_name)
  196. ts = '{}/vod{}/{}/ts0.ts'.format(uid, channel, event_time)
  197. if storage_location == 1: # 阿里云oss
  198. thumb0 = bucket.sign_url('GET', ts, 3600,
  199. params={'x-oss-process': 'video/snapshot,t_0000,w_700'})
  200. thumb1 = bucket.sign_url('GET', ts, 3600,
  201. params={'x-oss-process': 'video/snapshot,t_1000,w_700'})
  202. thumb2 = bucket.sign_url('GET', ts, 3600,
  203. params={'x-oss-process': 'video/snapshot,t_2000,w_700'})
  204. img_url = ""
  205. img_list = [thumb0, thumb1, thumb2]
  206. else:
  207. params = {'Key': ts}
  208. if region == 1: # AWS国外
  209. params['Bucket'] = 'foreignpush'
  210. img_url = aws_s3.generate_presigned_url(
  211. 'get_object', Params=params, ExpiresIn=300)
  212. else: # AWS国内
  213. params['Bucket'] = 'push'
  214. img_url = aws_s3_cn.generate_presigned_url(
  215. 'get_object', Params=params, ExpiresIn=300)
  216. img_list = [img_url]
  217. elif equipment_info.is_st == 3 or equipment_info.is_st == 4:
  218. # 列表装载回放时间戳标记
  219. img_list = []
  220. for i in range(equipment_info.is_st - 1, -1, -1):
  221. thumbspng = '{}/{}/{}_{}.jpeg'.format(uid, channel, event_time, i)
  222. if storage_location == 1: # 阿里云oss
  223. img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
  224. elif storage_location == 5: # 华为云
  225. create_res = obs_client.createSignedUrl(
  226. method='GET', bucketName=HUAWEICLOUD_PUSH_BUKET, objectKey=thumbspng, expires=300)
  227. img_url = create_res.signedUrl
  228. elif storage_location in [3, 4]: # 国外OCI云
  229. prefix_name = f'{uid}/'
  230. oci = oci_eur if storage_location == 4 else oci_us
  231. img_url = DetectControllerViewV2.oci_object_url(oci, redis_obj, uid, prefix_name)
  232. if img_url:
  233. img_url = img_url + thumbspng
  234. else:
  235. params = {'Key': thumbspng}
  236. if region == 1: # 国外AWS
  237. params['Bucket'] = 'foreignpush'
  238. img_url = aws_s3.generate_presigned_url(
  239. 'get_object', Params=params, ExpiresIn=300)
  240. else: # 国内AWS
  241. params['Bucket'] = 'push'
  242. img_url = aws_s3_cn.generate_presigned_url(
  243. 'get_object', Params=params, ExpiresIn=300)
  244. img_list.append(img_url)
  245. else:
  246. img_list = []
  247. uid_type = Device_Info.objects.filter(UID=uid).values('Type').first()
  248. if uid_type is None:
  249. return response.json(10077)
  250. device_type = DeviceTypeModel.objects.filter(type=uid_type['Type']).values('name').first()
  251. if device_type is None:
  252. device_type = {"name": uid_type['Type']}
  253. border_coords = '' if border_coords == '' else eval(border_coords)
  254. ai_event_type_list = []
  255. # 如果是ai消息类型,则分解eventType, 如:123 -> [1,2,3]
  256. if border_coords and event_type in ai_all_event_type:
  257. ai_event_type_list = list(map(int, str(event_type)))
  258. if EquipmentInfoService.is_combo_tag(event_type, event_tag):
  259. ai_event_type_list += EquipmentInfoService.get_combo_types(event_type, event_tag)
  260. equipment_info_data = {
  261. "uid": uid,
  262. "status": equipment_info.status,
  263. "answerStatus": equipment_info.answer_status,
  264. "alarm": equipment_info.alarm,
  265. "isSt": equipment_info.is_st,
  266. "storageLocation": storage_location,
  267. "devNickName": equipment_info.device_nick_name,
  268. "channel": channel,
  269. "eventType": equipment_info.event_type,
  270. "eventTime": int(event_time),
  271. "addTime": equipment_info.add_time,
  272. "borderCoords": border_coords,
  273. "eventTag": event_tag,
  274. "img": img_url,
  275. "imgList": img_list,
  276. "uidType": device_type["name"],
  277. "aiEventTypeList": str(ai_event_type_list),
  278. }
  279. equipment_info_list.append(equipment_info_data)
  280. data = {
  281. "list": equipment_info_list,
  282. "total": paginator.count
  283. }
  284. return response.json(0, data)
  285. except Exception as e:
  286. print(e)
  287. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))