123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- # -*- encoding: utf-8 -*-
- """
- @File : CloudPhotoController.py
- @Time : 2022/10/24 15:48
- @Author : stephen
- @Email : zhangdongming@asj6.wecom.work
- @Software: PyCharm
- """
- import datetime
- import logging
- import time
- import traceback
- from django.views import View
- from Ansjer.cn_config.config_formal import PUSH_CLOUD_PHOTO
- from Ansjer.config import ACCESS_KEY_ID, SECRET_ACCESS_KEY, REGION_NAME, PUSH_BUCKET, PUSH_INACCURATE_BUCKET
- from Object.AWS.AmazonS3Util import AmazonS3Util
- from Object.ResponseObject import ResponseObject
- from Service.EquipmentInfoService import EquipmentInfoService
- LOGGER = logging.getLogger('info')
- class CloudPhotoView(View):
- def get(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- operation = kwargs.get('operation')
- return self.validation(request.GET, request, operation)
- def post(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- operation = kwargs.get('operation')
- return self.validation(request.POST, request, operation)
- def validation(self, request_dict, request, operation):
- response = ResponseObject()
- if operation == 'get-photo':
- return self.get_photo(response)
- else:
- return response.json(404)
- @classmethod
- def get_photo(cls, response):
- """
- 定时获取云存消息推送图
- """
- try:
- today = datetime.date.today()
- eq_qs = EquipmentInfoService.get_equipment_info_model(str(today))
- start_time = "{} 00:00:00".format(str(today))
- # 先转换为时间数组
- timeArray = time.strptime(start_time, "%Y-%m-%d %H:%M:%S")
- # 转换为时间戳
- timeStamp = int(time.mktime(timeArray))
- eq_qs = eq_qs.filter(event_time__gt=timeStamp, is_st=1, device_uid='HA154GVEDH41RY8Y111A').values()
- count = eq_qs.count()
- page = int(count / 2) if count > 1 else count
- if page == 0:
- return response.json(0)
- eq_qs = eq_qs[(page - 1) * 1:page * 1]
- eq_vo = eq_qs[0]
- s3 = AmazonS3Util(
- aws_access_key_id=ACCESS_KEY_ID,
- secret_access_key=SECRET_ACCESS_KEY,
- region_name=REGION_NAME
- )
- file_path = '{uid}/{channel}/{event_time}.jpeg'.format(uid=eq_vo['device_uid'],
- channel=eq_vo['channel'],
- event_time=eq_vo['event_time'])
- s3.copy_obj(PUSH_BUCKET, PUSH_CLOUD_PHOTO, file_path)
- return response.json(0)
- except Exception as e:
- print(e)
- ex = traceback.format_exc()
- LOGGER.info('--->抽取推送图片异常:{}'.format(ex))
- return response.json(177, repr(e))
|