PushService.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # -*- coding: utf-8 -*-
  2. """
  3. @Time : 2022/5/19 11:43
  4. @Auth : Locky
  5. @File :PushService.py
  6. @IDE :PyCharm
  7. """
  8. import logging
  9. import os
  10. import apns2
  11. import jpush
  12. from pyfcm import FCMNotification
  13. from AnsjerPush.config import APP_BUNDLE_DICT, APNS_MODE, BASE_DIR, APNS_CONFIG, FCM_CONFIG, JPUSH_CONFIG
  14. from Service.CommonService import CommonService
  15. class PushObject:
  16. # 推送对象
  17. @staticmethod
  18. def get_msg_title(app_bundle_id, nickname):
  19. """
  20. 获取推送消息标题
  21. @param app_bundle_id: app包id
  22. @param nickname: 设备名
  23. @return: msg_title
  24. """
  25. if app_bundle_id in APP_BUNDLE_DICT.keys():
  26. msg_title = APP_BUNDLE_DICT[app_bundle_id] + '(' + nickname + ')'
  27. else:
  28. msg_title = nickname
  29. return msg_title
  30. @staticmethod
  31. def get_gateway_msg_text(n_time, tz, lang, alarm):
  32. """
  33. 获取网关推送消息内容
  34. @param n_time: 当前时间
  35. @param tz: 时区
  36. @param lang: 语言
  37. @param alarm: 警报
  38. @return: msg_text
  39. """
  40. n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
  41. if lang == 'cn':
  42. msg_text = '{} 日期:{}'.format(alarm, n_date)
  43. else:
  44. msg_text = '{} date:{}'.format(alarm, n_date)
  45. return msg_text
  46. @staticmethod
  47. def get_ai_msg_text(channel, n_time, lang, tz, label):
  48. """
  49. 获取AI推送内容
  50. @param channel: 通道
  51. @param n_time: 当前时间
  52. @param lang: 语言
  53. @param tz: 时区
  54. @param label: 识别到的标签
  55. @return: ai_msg_text
  56. """
  57. n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
  58. if lang == 'cn':
  59. msg = '摄像头AI识别到了{}'.format(label)
  60. ai_msg_text = '{msg} 通道:{channel} 日期:{date}'.format(msg=msg, channel=channel, date=n_date)
  61. else:
  62. msg = 'Camera AI recognizes {}'.format(label)
  63. ai_msg_text = '{msg} channel:{channel} date:{date}'.format(msg=msg, channel=channel, date=n_date)
  64. return ai_msg_text
  65. @staticmethod
  66. def get_low_power_msg_text(channel, n_time, lang, tz, electricity, is_sys=0):
  67. """
  68. 获取低电量推送内容
  69. @param channel: 通道
  70. @param n_time: 当前时间
  71. @param lang: 语言
  72. @param tz: 时区
  73. @param electricity: 电量
  74. @param is_sys: 是否为系统消息
  75. @return: low_power_msg_text
  76. """
  77. n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
  78. if lang == 'cn':
  79. alarm = '剩余电量:' + electricity
  80. if is_sys:
  81. low_power_msg_text = '{} 通道:{}'.format(alarm, channel)
  82. else:
  83. low_power_msg_text = '{} 通道:{} 日期:{}'.format(alarm, channel, n_date)
  84. else:
  85. alarm = 'Battery remaining:' + electricity
  86. if is_sys:
  87. low_power_msg_text = '{} channel:{}'.format(alarm, channel)
  88. else:
  89. low_power_msg_text = '{} channel:{} date:{}'.format(alarm, channel, n_date)
  90. return low_power_msg_text
  91. @staticmethod
  92. def ios_apns_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
  93. uid='', channel='1', launch_image=None):
  94. """
  95. ios apns 推送
  96. @param nickname: 设备昵称
  97. @param app_bundle_id: app包id
  98. @param token_val: 推送token
  99. @param n_time: 当前时间
  100. @param event_type: 事件类型
  101. @param msg_title: 推送标题
  102. @param msg_text: 推送内容
  103. @param uid: uid
  104. @param channel: 通道
  105. @param launch_image: 推送图片链接
  106. @return: None
  107. """
  108. logger = logging.getLogger('info')
  109. try:
  110. pem_path = os.path.join(BASE_DIR, APNS_CONFIG[app_bundle_id]['pem_path'])
  111. logger.info('apns推送app_bundle_id:{}, pem_path:{}'.format(app_bundle_id, pem_path))
  112. cli = apns2.APNSClient(mode=APNS_MODE, client_cert=pem_path)
  113. alert = apns2.PayloadAlert(title=msg_title, body=msg_text, launch_image=launch_image)
  114. push_data = {'alert': 'Motion', 'msg': '', 'sound': '', 'zpush': '1', 'uid': uid, 'channel': channel,
  115. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  116. 'image_url': launch_image
  117. }
  118. payload = apns2.Payload(alert=alert, custom=push_data, sound='default', category='myCategory',
  119. mutable_content=True)
  120. n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
  121. res = cli.push(n=n, device_token=token_val, topic=app_bundle_id)
  122. assert res.status_code == 200
  123. except Exception as e:
  124. logger.info('--->IOS推送异常{}'.format(repr(e)))
  125. return repr(e)
  126. @staticmethod
  127. def android_fcm_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
  128. uid='', channel='1', image=''):
  129. """
  130. android fcm 推送
  131. @param nickname: 设备昵称
  132. @param app_bundle_id: app包id
  133. @param token_val: 推送token
  134. @param n_time: 当前时间
  135. @param event_type: 事件类型
  136. @param msg_title: 推送标题
  137. @param msg_text: 推送内容
  138. @param uid: uid
  139. @param channel: 通道
  140. @param image: 推送图片链接
  141. @return: None
  142. """
  143. logger = logging.getLogger('info')
  144. try:
  145. serverKey = FCM_CONFIG[app_bundle_id]
  146. push_service = FCMNotification(api_key=serverKey)
  147. push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'image': image,
  148. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  149. 'uid': uid, 'channel': channel
  150. }
  151. result = push_service.notify_single_device(registration_id=token_val, message_title=msg_title,
  152. message_body=msg_text, data_message=push_data,
  153. extra_kwargs={'default_sound': True,
  154. 'default_vibrate_timings': True,
  155. 'default_light_settings': True,
  156. }
  157. )
  158. logger.info('fcm推送结果:{}'.format(result))
  159. except Exception as e:
  160. return repr(e)
  161. @staticmethod
  162. def android_jpush(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text):
  163. """
  164. android 极光 推送
  165. @param nickname: 设备昵称
  166. @param app_bundle_id: app包id
  167. @param token_val: 推送token
  168. @param n_time: 当前时间
  169. @param event_type: 事件类型
  170. @param msg_title: 推送标题
  171. @param msg_text: 推送内容
  172. @return: None
  173. """
  174. try:
  175. app_key = JPUSH_CONFIG[app_bundle_id]['Key']
  176. master_secret = JPUSH_CONFIG[app_bundle_id]['Secret']
  177. # 换成各自的app_key和master_secret
  178. _jpush = jpush.JPush(app_key, master_secret)
  179. push = _jpush.create_push()
  180. push.audience = jpush.registration_id(token_val)
  181. push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
  182. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname
  183. }
  184. android = jpush.android(title=msg_title, big_text=msg_text, alert=msg_text, extras=push_data,
  185. priority=1, style=1, alert_type=7
  186. )
  187. push.notification = jpush.notification(android=android)
  188. push.platform = jpush.all_
  189. res = push.send()
  190. assert res.status_code == 200
  191. except Exception as e:
  192. return repr(e)