forked from pimnijdam/eventScripting
/
notifications.py
129 lines (110 loc) · 4.07 KB
/
notifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
Crappy notifications, just put in here for some experimenting with my iPhone
"""
#import gevent.monkey
#from gevent.pool import Pool
#gevent.monkey.patch_socket()
from multiprocessing import Process, Queue
import httplib
import json
from apns import APNs, Payload
#from applepushnotification import NotificationService, NotificationMessage
import os
import logging
import time
verbose = False
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s')
logger = logging.getLogger(__name__)
WARN_THRESHOLD = 5
class NotificationPusher:
useSense = False
def __init__(self, nrConsumers=1, useSense = False, certfile = None, keyfile=None, sandbox = True):
self.certfile = certfile
self.keyfile = keyfile
self.sandbox = sandbox
#self.pool = Pool(10)
self.queue = Queue(1000)
self.processes = []
if useSense:
runner = NotificationPusher.runSense
else:
runner = NotificationPusher.runSelf
self.apns = APNs(use_sandbox=self.sandbox, cert_file=self.certfile, key_file=self.keyfile)
process = Process(name="apns feedback daemon", target=NotificationPusher.runFeedbackCheck, args=(self,))
process.daemon = True
process.start()
self.processes.append(process)
for i in range(nrConsumers):
process = Process(name="apns sender daemon", target=runner, args=(self,))
process.daemon = True
process.start()
self.processes.append(process)
def runSense (self):
while True:
try:
(deviceId, userMessage, payload) = self.queue.get()
notification = {"device_type": "ios",
"device_id": deviceId,
"message":{
"badge":1,
"sound":"default",
"alert":userMessage
}
}
NotificationPusher.request('POST', '/jump/push', json.dumps(notification));
except:
logger.exception("Exception trying to send push notification via Sense")
def runSelf (self):
while True:
try:
(deviceId, userMessage, payload) = self.queue.get()
payload = Payload(alert=userMessage, sound="default", badge=1)
self.apns.gateway_server.send_notification(deviceId, payload)
except:
#Prepare for reconnect
self.apns._gateway_server = None
retrySucceed = False
try:
self.apns.gateway_server.send_notification(deviceId, payload)
retrySucceed = True
except:
self.apns._gateway_server = None
#log exception
logger.exception("{}: Exception trying to send push notification. RetrySucceed={}".format(self.certfile, retrySucceed))
def runFeedbackCheck (self):
while True:
try:
for (timestamp, devicetoken) in self.apns.feedback_server.items():
logger.info("{}:got feedback from apple push notification service: ({},{})".format(self.certfile, timestamp, devicetoken))
time.sleep(5 * 60)
except:
#prepare for reconnect
self.apns._feedback_server = None
#log exception
logger.exception("{}: Exception trying to get apple push notification feedback".format(self.certfile))
@staticmethod
def request(method,action,body):
#url = "localhost:5000"
url = "dev.sense-os.nl"
headers = {"Content-type": "application/json"}
conn = httplib.HTTPConnection(url)
conn.request(method, action, body, headers)
response = conn.getresponse()
responseBody = response.read()
if verbose:
print "#"*80
print '{}{}'.format(url,action)
print response.status, response.reason, responseBody
print "#"*80
return responseBody
def sendNotification(self, deviceId, userMessage, payload=None):
if self.queue.full():
logger.critical("{}: notification queue is full. {} items.".format(self.certfile, self.queue.qsize()))
elif self.queue.qsize() >= WARN_THRESHOLD:
logger.warning("{}: {} items in notification queue".format(self.certfile, self.queue.qsize()));
self.queue.put((deviceId, userMessage, payload))
#some monitoring
#notificationPusher= NotificationPusher(1, certfile='certificates/jumpstart_dev_cer.pem', keyfile='certificates/jumpstart_dev_key.pem', sandbox=True)
notificationPusher= NotificationPusher(1, useSense=True)
def sendNotification(deviceId, userMessage=None, payload=None):
notificationPusher.sendNotification(deviceId, userMessage, payload)