MessageMangementController.py 15 KB

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