|
@@ -14,15 +14,19 @@ 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 Ansjer.config import ACCESS_KEY_ID, SECRET_ACCESS_KEY, REGION_NAME, PUSH_BUCKET
|
|
|
+from Model.models import UidSetModel, DeviceCloudPhotoInfo, DevicePicturePushInfo
|
|
|
from Object.AWS.AmazonS3Util import AmazonS3Util
|
|
|
+from Object.RedisObject import RedisObject
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Service.EquipmentInfoService import EquipmentInfoService
|
|
|
|
|
|
LOGGER = logging.getLogger('info')
|
|
|
+UID_KEY = 'ANSJER:UID:SET:INFO'
|
|
|
|
|
|
|
|
|
class CloudPhotoView(View):
|
|
|
+
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
request.encoding = 'utf-8'
|
|
|
operation = kwargs.get('operation')
|
|
@@ -37,6 +41,8 @@ class CloudPhotoView(View):
|
|
|
response = ResponseObject()
|
|
|
if operation == 'get-photo':
|
|
|
return self.get_photo(response)
|
|
|
+ elif operation == 'cache-uid-set':
|
|
|
+ return self.cache_photo_uid_set(response)
|
|
|
else:
|
|
|
return response.json(404)
|
|
|
|
|
@@ -46,6 +52,10 @@ class CloudPhotoView(View):
|
|
|
定时获取云存消息推送图
|
|
|
"""
|
|
|
try:
|
|
|
+ redis = RedisObject()
|
|
|
+ uid_set_list = redis.get_data(UID_KEY)
|
|
|
+ if not uid_set_list:
|
|
|
+ return response.json(0)
|
|
|
today = datetime.date.today()
|
|
|
eq_qs = EquipmentInfoService.get_equipment_info_model(str(today))
|
|
|
start_time = "{} 00:00:00".format(str(today))
|
|
@@ -53,26 +63,56 @@ class CloudPhotoView(View):
|
|
|
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]
|
|
|
+ now_time = int(time.time())
|
|
|
+ for item in uid_set_list:
|
|
|
+ try:
|
|
|
+ eq_qs = eq_qs.filter(event_time__gt=timeStamp, is_st=1, device_uid=item) \
|
|
|
+ .values('device_uid', 'channel', 'event_time')
|
|
|
+ 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)
|
|
|
+ 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)
|
|
|
+ push_data = {'type': 1, 'uid': eq_vo['device_uid'], 'channel': eq_vo['channel'],
|
|
|
+ 'event_time': eq_vo['event_time'], 'updated_time': now_time, 'created_time': now_time}
|
|
|
+ DevicePicturePushInfo.objects.create(**push_data)
|
|
|
+ except Exception as e:
|
|
|
+ LOGGER.info('异常详情,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+ continue
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
- ex = traceback.format_exc()
|
|
|
- LOGGER.info('--->抽取推送图片异常:{}'.format(ex))
|
|
|
+ LOGGER.info('--->抽取推送图片异常:{}'.format(traceback.format_exc()))
|
|
|
+ return response.json(177, repr(e))
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def cache_photo_uid_set(cls, response):
|
|
|
+ """
|
|
|
+ 缓存uid_set
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ photo_qs = DeviceCloudPhotoInfo.objects.filter(status=1).values('uid')
|
|
|
+ if not photo_qs.exists():
|
|
|
+ return response.json(0)
|
|
|
+ redis = RedisObject()
|
|
|
+ for item in photo_qs:
|
|
|
+ uid_set_qs = UidSetModel.objects.filter(uid=item['uid'], detect_status=1)
|
|
|
+ if not uid_set_qs:
|
|
|
+ continue
|
|
|
+ redis.rpush(UID_KEY, item['uid'])
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ LOGGER.info('--->获取uid_set信息异常:{}'.format(traceback.format_exc()))
|
|
|
return response.json(177, repr(e))
|