MessageMangementController.py 15 KB

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