PowerWarningController.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # -*- coding: utf-8 -*-
  2. """
  3. @Author : Rocky
  4. @Time : 2022/9/16 10:39
  5. @File :PowerWarningController.py
  6. """
  7. import logging
  8. import time
  9. from django.http import JsonResponse
  10. from django.views.generic.base import View
  11. from Model.models import UidPushModel, SysMsgModel, Device_Info
  12. from Object.RedisObject import RedisObject
  13. from Service.HuaweiPushService.HuaweiPushService import HuaweiPushObject
  14. from Service.PushService import PushObject
  15. from AnsjerPush.config import XM_PUSH_CHANNEL_ID
  16. class PowerWarningView(View):
  17. # 低电量推送接口
  18. def get(self, request, *args, **kwargs):
  19. request.encoding = 'utf-8'
  20. return self.validation(request.GET)
  21. def post(self, request, *args, **kwargs):
  22. request.encoding = 'utf-8'
  23. return self.validation(request.POST)
  24. @staticmethod
  25. def validation(request_dict):
  26. logger = logging.getLogger('info')
  27. uid = request_dict.get('uid', None)
  28. # 限制每6小时推一次
  29. redis_obj = RedisObject()
  30. is_limit = redis_obj.CONN.setnx(uid + 'limit_power_warning', 1)
  31. redis_obj.CONN.expire(uid + 'limit_power_warning', 6 * 60 * 60)
  32. if not is_limit:
  33. return JsonResponse(status=200, data={'code': 0, 'msg': 'push limited!'})
  34. channel = request_dict.get('channel', None)
  35. electricity = request_dict.get('electricity', None)
  36. logger.info('调用低电量推送接口的uid: {},electricity: {}'.format(uid, electricity))
  37. try:
  38. uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid). \
  39. values('push_type', 'appBundleId', 'token_val', 'lang', 'tz', 'userID_id')
  40. if not uid_push_qs.exists():
  41. res_data = {'code': 173, 'msg': 'uid push data not exit!'}
  42. return JsonResponse(status=200, data=res_data)
  43. uid_push_list = [uid_push for uid_push in uid_push_qs]
  44. # 查询设备数据
  45. device_info_qs = Device_Info.objects.filter(UID=uid).first()
  46. nickname = uid if device_info_qs is None else device_info_qs.NickName
  47. now_time = int(time.time())
  48. event_type = 704
  49. sys_msg_list = []
  50. user_id_list = []
  51. for uid_push in uid_push_list:
  52. push_type = uid_push['push_type']
  53. app_bundle_id = uid_push['appBundleId']
  54. token_val = uid_push['token_val']
  55. lang = uid_push['lang']
  56. tz = uid_push['tz']
  57. if tz is None or tz == '':
  58. tz = 0
  59. # 推送标题和推送内容
  60. msg_title = PushObject.get_msg_title(nickname=nickname)
  61. msg_text = PushObject.get_low_power_msg_text(channel=channel, n_time=now_time, lang=lang, tz=tz,
  62. electricity=electricity)
  63. kwargs = {
  64. 'nickname': nickname,
  65. 'app_bundle_id': app_bundle_id,
  66. 'token_val': token_val,
  67. 'n_time': now_time,
  68. 'event_type': event_type,
  69. 'msg_title': msg_title,
  70. 'msg_text': msg_text,
  71. 'uid': uid,
  72. 'channel': channel,
  73. }
  74. try:
  75. # 推送消息
  76. if push_type == 0: # ios apns
  77. PushObject.ios_apns_push(**kwargs)
  78. elif push_type == 1: # android gcm
  79. PushObject.android_fcm_push(**kwargs)
  80. elif push_type == 2: # android jpush
  81. kwargs.pop('uid')
  82. PushObject.android_jpush(**kwargs)
  83. elif push_type == 3:
  84. huawei_push_object = HuaweiPushObject()
  85. huawei_push_object.send_push_notify_message(**kwargs)
  86. elif push_type == 4: # android 小米推送
  87. channel_id = XM_PUSH_CHANNEL_ID['device_reminder']
  88. PushObject.android_xmpush(channel_id=channel_id, **kwargs)
  89. elif push_type == 5: # android vivo推送
  90. PushObject.android_vivopush(**kwargs)
  91. elif push_type == 6: # android oppo推送
  92. channel_id = 'VALUE_ADDED'
  93. PushObject.android_oppopush(channel_id=channel_id, **kwargs)
  94. elif push_type == 7: # android 魅族推送
  95. PushObject.android_meizupush(**kwargs)
  96. except Exception as e:
  97. logger.info('低电量推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  98. continue
  99. # 保存系统消息
  100. user_id = uid_push['userID_id']
  101. if user_id not in user_id_list:
  102. sys_msg_text = PushObject.get_low_power_msg_text(channel=channel, n_time=now_time, lang=lang, tz=tz,
  103. electricity=electricity, is_sys=1)
  104. sys_msg_list.append(SysMsgModel(
  105. userID_id=user_id,
  106. msg=sys_msg_text,
  107. addTime=now_time,
  108. updTime=now_time,
  109. uid=uid,
  110. eventType=event_type,
  111. ))
  112. user_id_list.append(user_id)
  113. SysMsgModel.objects.bulk_create(sys_msg_list)
  114. return JsonResponse(status=200, data={'code': 0})
  115. except Exception as e:
  116. logger.info('低电量推送接口异常: {}'.format(e))
  117. return JsonResponse(status=500, data={'msg': 'power warning error'})