PowerWarningController.py 5.2 KB

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