CloudPhotoController.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : CloudPhotoController.py
  4. @Time : 2022/10/24 15:48
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. import datetime
  10. import logging
  11. import time
  12. import traceback
  13. from django.views import View
  14. from Ansjer.cn_config.config_formal import PUSH_CLOUD_PHOTO
  15. from Ansjer.config import ACCESS_KEY_ID, SECRET_ACCESS_KEY, REGION_NAME, PUSH_BUCKET, PUSH_INACCURATE_BUCKET
  16. from Object.AWS.AmazonS3Util import AmazonS3Util
  17. from Object.ResponseObject import ResponseObject
  18. from Service.EquipmentInfoService import EquipmentInfoService
  19. LOGGER = logging.getLogger('info')
  20. class CloudPhotoView(View):
  21. def get(self, request, *args, **kwargs):
  22. request.encoding = 'utf-8'
  23. operation = kwargs.get('operation')
  24. return self.validation(request.GET, request, operation)
  25. def post(self, request, *args, **kwargs):
  26. request.encoding = 'utf-8'
  27. operation = kwargs.get('operation')
  28. return self.validation(request.POST, request, operation)
  29. def validation(self, request_dict, request, operation):
  30. response = ResponseObject()
  31. if operation == 'get-photo':
  32. return self.get_photo(response)
  33. else:
  34. return response.json(404)
  35. @classmethod
  36. def get_photo(cls, response):
  37. """
  38. 定时获取云存消息推送图
  39. """
  40. try:
  41. today = datetime.date.today()
  42. eq_qs = EquipmentInfoService.get_equipment_info_model(str(today))
  43. start_time = "{} 00:00:00".format(str(today))
  44. # 先转换为时间数组
  45. timeArray = time.strptime(start_time, "%Y-%m-%d %H:%M:%S")
  46. # 转换为时间戳
  47. timeStamp = int(time.mktime(timeArray))
  48. eq_qs = eq_qs.filter(event_time__gt=timeStamp, is_st=1, device_uid='HA154GVEDH41RY8Y111A').values()
  49. count = eq_qs.count()
  50. page = int(count / 2) if count > 1 else count
  51. if page == 0:
  52. return response.json(0)
  53. eq_qs = eq_qs[(page - 1) * 1:page * 1]
  54. eq_vo = eq_qs[0]
  55. s3 = AmazonS3Util(
  56. aws_access_key_id=ACCESS_KEY_ID,
  57. secret_access_key=SECRET_ACCESS_KEY,
  58. region_name=REGION_NAME
  59. )
  60. file_path = '{uid}/{channel}/{event_time}.jpeg'.format(uid=eq_vo['device_uid'],
  61. channel=eq_vo['channel'],
  62. event_time=eq_vo['event_time'])
  63. s3.copy_obj(PUSH_BUCKET, PUSH_CLOUD_PHOTO, file_path)
  64. return response.json(0)
  65. except Exception as e:
  66. print(e)
  67. ex = traceback.format_exc()
  68. LOGGER.info('--->抽取推送图片异常:{}'.format(ex))
  69. return response.json(177, repr(e))