-
Notifications
You must be signed in to change notification settings - Fork 71.8k
/
loop.js
145 lines (121 loc) · 4.91 KB
/
loop.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//'use strict';
const apn = require('apn');
function init (env, ctx) {
function loop () {
return loop;
}
loop.sendNotification = function sendNotification (data, remoteAddress, completion) {
// console.info("JAP");
// console.info(data);
if (env.extendedSettings.loop.apnsKey === undefined || env.extendedSettings.loop.apnsKey.length == 0) {
completion("Loop notification failed: LOOP_APNS_KEY not set.");
return;
}
if (env.extendedSettings.loop.apnsKeyId === undefined || env.extendedSettings.loop.apnsKeyId.length == 0) {
completion("Loop notification failed: LOOP_APNS_KEY_ID not set.");
return;
}
if (env.extendedSettings.loop.developerTeamId === undefined || env.extendedSettings.loop.developerTeamId.length != 10) {
completion("Loop notification failed: LOOP_DEVELOPER_TEAM_ID not set.");
return;
}
if (ctx.ddata.profiles === undefined || ctx.ddata.profiles.length < 1 || ctx.ddata.profiles[0].loopSettings === undefined) {
completion("Loop notification failed: Could not find loopSettings in profile.");
return;
}
let loopSettings = ctx.ddata.profiles[0].loopSettings;
if (loopSettings.deviceToken === undefined) {
completion("Loop notification failed: Could not find deviceToken in loopSettings.");
return;
}
if (loopSettings.bundleIdentifier === undefined) {
completion("Loop notification failed: Could not find bundleIdentifier in loopSettings.");
return;
}
var options = {
token: {
key: env.extendedSettings.loop.apnsKey
, keyId: env.extendedSettings.loop.apnsKeyId
, teamId: env.extendedSettings.loop.developerTeamId
},
production: env.extendedSettings.loop.pushServerEnvironment === "production"
};
var provider = new apn.Provider(options);
var payload = {
'remote-address': remoteAddress,
'notes': data.notes,
'entered-by': data.enteredBy
};
var alert;
if (data.eventType === 'Temporary Override Cancel') {
payload["cancel-temporary-override"] = "true";
alert = "Cancel Temporary Override";
} else if (data.eventType === 'Temporary Override') {
payload["override-name"] = data.reason;
if (data.duration !== undefined && parseInt(data.duration) > 0) {
payload["override-duration-minutes"] = parseInt(data.duration);
}
alert = data.reasonDisplay + " Temporary Override";
} else if (data.eventType === 'Remote Carbs Entry') {
payload["carbs-entry"] = parseFloat(data.remoteCarbs);
if(payload["carbs-entry"] > 0.0 ) {
payload["absorption-time"] = 3.0;
if (data.remoteAbsorption !== undefined && parseFloat(data.remoteAbsorption) > 0.0) {
payload["absorption-time"] = parseFloat(data.remoteAbsorption);
}
if (data.otp !== undefined && data.otp.length > 0) {
payload["otp"] = ""+data.otp
}
if (data.created_at !== undefined) {
payload['start-time'] = data.created_at;
}
alert = "Remote Carbs Entry: "+payload["carbs-entry"]+" grams\n";
alert += "Absorption Time: "+payload["absorption-time"]+" hours";
} else {
completion("Loop remote carbs failed. Incorrect carbs entry: ", data.remoteCarbs);
return;
}
} else if (data.eventType === 'Remote Bolus Entry') {
payload["bolus-entry"] = parseFloat(data.remoteBolus);
if(payload["bolus-entry"] > 0.0 ) {
alert = "Remote Bolus Entry: "+payload["bolus-entry"]+" U\n";
if (data.otp !== undefined && data.otp.length > 0) {
payload["otp"] = ""+data.otp
}
} else {
completion("Loop remote bolus failed. Incorrect bolus entry: ", data.remoteBolus);
return;
}
} else {
completion("Loop notification failed: Unhandled event type:", data.eventType);
return;
}
if (data.notes !== undefined && data.notes.length > 0) {
alert += " - " + data.notes
}
if (data.enteredBy !== undefined && data.enteredBy.length > 0) {
alert += " - " + data.enteredBy
}
// Track time notification was sent
let now = new Date()
payload['sent-at'] = now.toISOString();
// Expire after 5 minutes.
let expiration = new Date(now.getTime() + 5 * 60 * 1000)
payload['expiration'] = expiration.toISOString();
let notification = new apn.Notification();
notification.alert = alert;
notification.topic = loopSettings.bundleIdentifier;
notification.contentAvailable = 1;
notification.payload = payload;
provider.send(notification, [loopSettings.deviceToken]).then( (response) => {
if (response.sent && response.sent.length > 0) {
completion();
} else {
console.log("APNs delivery failed:", response.failed)
completion("APNs delivery failed: " + response.failed[0].response.reason);
}
});
};
return loop();
}
module.exports = init;