-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplet.js
226 lines (198 loc) · 6.61 KB
/
applet.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const Applet = imports.ui.applet;
const Mainloop = imports.mainloop; // Needed for timer update loop
const Soup = imports.gi.Soup;
const Settings = imports.ui.settings;
const _httpSession = new Soup.SessionAsync();
Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());
function NightscoutApplet(metadata, orientation, panelHeight, instance_id) {
this._init(metadata, orientation, panelHeight, instance_id);
}
const logging = false;
function log(message) {
if (logging) global.log(`[nightscout@ranneft]: ${message}`);
}
function makeHttpRequest(method, uri, cb) {
return new Promise((resolve, reject) => {
const request = Soup.Message.new(method, uri);
request.request_headers.append('accept', 'application/json');
_httpSession.queue_message(request, (_httpSession, message) => {
if (message.status_code === 200) {
cb(resolve, message);
} else {
log(`Failed to acquire request (${message.status_code})`);
reject(`Failed to acquire request (${message.status_code})`);
}
});
});
}
roundUsing = function(func, prec, value){
var temp = value * Math.pow(10, prec)
temp = func(temp);
return temp / Math.pow(10, prec)
}
NightscoutApplet.prototype = {
__proto__: Applet.TextIconApplet.prototype, // Now TextIcon Applet
last: null,
applyAlert: false,
alerting: null,
_init: function (metadata, orientation, panelHeight, instance_id) {
Applet.TextIconApplet.prototype._init.call(this, orientation, panelHeight, instance_id);
this.settings = new Settings.AppletSettings(this, metadata.uuid, instance_id);
this.settings.bindProperty(Settings.BindingDirection.IN, // Setting type
"usemmol", // The setting key
"usemmol", // The property to manage (this.refreshInterval)
this.on_settings_changed, // Callback when value changes
null); // Optional callback data
this.settings.bindProperty(Settings.BindingDirection.IN, // Setting type
"refreshInterval", // The setting key
"refreshInterval", // The property to manage (this.refreshInterval)
this.on_settings_changed, // Callback when value changes
null); // Optional callback data
this.settings.bindProperty(Settings.BindingDirection.IN,
"host",
"host",
this.on_settings_changed,
null);
this.settings.bindProperty(Settings.BindingDirection.IN,
"showMissing",
"showMissing",
this.on_settings_changed,
null);
this.settings.bindProperty(Settings.BindingDirection.IN,
"showMissingInterval",
"showMissingInterval",
this.on_settings_changed,
null);
this.settings.bindProperty(Settings.BindingDirection.IN,
"showiob",
"showiob",
this.on_settings_changed,
null);
try {
// Set initial value
this.set_applet_icon_path(metadata.path + '/icons/nightscout.png')
this.set_applet_label("Loading...");
this.set_applet_tooltip("Nightscout");
this.startUp(true);
} catch (e) {
global.logError(e);
}
},
on_settings_changed: function(event) {
this.startUp(false);
},
on_applet_clicked: function(event) {
this.updateUI();
},
startUp: function(setupLoop) {
if(setupLoop) {
this.updateLoop(true);
} else {
this.updateUI();
}
},
makeBGstring(current, status) {
let bgString = "BG: ";
const bgValue = this.usemmol ? roundUsing(Math.ceil, 1, current.sgv * 0.0555).toFixed(1) : current.sgv;
bgString += bgValue;
switch(current.direction) {
case 'Flat':
bgString += ' →';
break;
case 'SingleDown':
bgString += ' ↓';
break;
case 'DoubleDown':
bgString += ' ↓↓';
break;
case 'TripleDown':
bgString += ' ↓↓↓';
break;
case 'SingleUp':
bgString += ' ↑';
break;
case 'DoubleUp':
bgString += ' ↑↑';
break;
case 'TripleUp':
bgString += ' ↑↑↑';
break;
default:
break;
}
if(this.showiob) {
bgString += " (IoB: " + status.pump.iob.bolusiob + "U)";
}
if(this.showMissing && this.last) {
const lastDate = this.last.date;
const currentDate = Date.now();
const minutesAgo = Math.floor((currentDate - lastDate) / 60 / 1000);
if(minutesAgo > this.showMissingInterval) {
bgString = "!Last " + minutesAgo + " m ago! " + bgString;
}
}
this.set_applet_label(bgString);
},
requestCurrentBg() {
return makeHttpRequest('GET', `${this.host}/api/v1/entries/current`, (resolve, message) => {
let current = {};
log('Requested current state' + message.response_body.data);
const entries = JSON.parse(message.response_body.data);
if(entries.length > 0) {
current = entries[0];
}
resolve(current)
});
},
requestDeviceStatus() {
return makeHttpRequest('GET', `${this.host}/api/v1/devicestatus?count=1`, (resolve, message) => {
let status = {};
log('Requested device status' + message.response_body.data);
const statuses = JSON.parse(message.response_body.data);
if(statuses.length > 0) {
status = statuses[0];
}
resolve(status);
});
},
makeTooltip(status) {
let tooltip = "";
if(this.last) {
const date = new Date();
date.setTime(this.last.date);
tooltip += "Last update: " + date.toUTCString() + "\n";
}
this.set_applet_tooltip(tooltip +
"IOB: " + status.pump.iob.bolusiob + "U\n" +
"Bat: " + status.pump.battery.percent + "%\n" +
"UpBat: " + status.uploaderBattery + "%\n" +
"Reservoir: " + status.pump.reservoir + "U"
);
},
// This updates the numerical display in the applet and in the tooltip
updateUI: function() {
try {
Promise.all([this.requestCurrentBg(), this.requestDeviceStatus()]).then(values => {
if(!this.last) {
this.last = values[0];
}
this.makeBGstring(values[0], values[1]);
this.makeTooltip(values[1]);
if(values[0] && values[0]._id && (this.last._id !== values[0]._id)) {
this.last = values[0];
}
});
} catch (e) {
global.logError(e);
}
},
// This is the loop run at refreshInterval rate to call updateUI() to update the display in the applet and tooltip
updateLoop: function() {
this.updateUI();
Mainloop.timeout_add_seconds(this.refreshInterval * 60, this.updateLoop.bind(this));
},
};
function main(metadata, orientation, panelHeight, instance_id) {
let nightscoutApplet = new NightscoutApplet(metadata, orientation, panelHeight, instance_id);
return nightscoutApplet;
}