-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
145 lines (123 loc) · 3.1 KB
/
extension.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
const St = imports.gi.St;
const Main = imports.ui.main;
const Soup = imports.gi.Soup;
const Lang = imports.lang;
const Mainloop = imports.mainloop;
const Clutter = imports.gi.Clutter;
const PanelMenu = imports.ui.panelMenu;
const BG_URL = "https://xxxx.herokuapp.com/api/v1/entries/current.json";
const MIN_WAIT_TIME = 10;
let _httpSession;
const BGIndicator = new Lang.Class({
Name : 'BGIndicator',
Extends : PanelMenu.Button,
_init : function() {
this.parent(0.0, "Glucose Indicator", false);
this.buttonText = new St.Label({
text : _("Loading..."),
y_align : Clutter.ActorAlign.CENTER
});
this.actor.add_actor(this.buttonText);
this.bg = null;
this._refresh();
},
_refresh : function() {
this._removeTimeout();
if(this.bg == null || this._readingAge() > 300) {
// First reading or older than 5 minutes, reload
this._loadData();
} else {
// Update age and refresh on next full minute
this._refreshUI();
this._setRefreshTimeout(Math.ceil(this._readingAge() % 60));
}
return true;
},
_setRefreshTimeout: function(seconds) {
this._timeout = Mainloop.timeout_add_seconds(seconds, Lang.bind(this,
this._refresh));
},
_loadData : function() {
let params = {};
if (_httpSession !== undefined)
_httpSession.abort();
_httpSession = new Soup.Session();
let message = Soup.form_request_new_from_hash('GET', BG_URL, params);
_httpSession.queue_message(message, Lang.bind(this, function(
_httpSession, message) {
if (message.status_code == 200) {
let json = JSON.parse(message.response_body.data);
this._parseData(json);
} else {
global.log("Nightscout returned status " + message.status_code);
this.bg = null;
}
this._refreshUI();
this._setRefreshTimeout(10);
}));
},
_parseData : function(data) {
// Convert to mmol/l and round
let entry = data[0];
let value = Math.round(entry.sgv / 18 * 10) / 10;
let date = new Date(entry.dateString);
this.bg = {
value: value,
date: date
};
},
_refreshUI : function() {
if(this.bg == null) {
this.buttonText.set_text("--");
} else {
this.buttonText.set_text(this._valueString() + " | " + this._ageString());
}
},
_readingAge : function() {
if(this.bg == null) {
return -1;
}
return (new Date() - this.bg.date) / 1000;
},
_valueString : function() {
if(this.bg == null) {
return "---";
}
let value = this.bg.value;
return value.toFixed(1);
},
_ageString : function() {
let minutes = Math.floor(this._readingAge() / 60);
if(minutes == 0) {
return "now";
} else {
return minutes + " min";
}
},
_removeTimeout : function() {
if (this._timeout) {
Mainloop.source_remove(this._timeout);
this._timeout = null;
}
},
stop : function() {
if (_httpSession !== undefined)
_httpSession.abort();
_httpSession = undefined;
if (this._timeout)
Mainloop.source_remove(this._timeout);
this._timeout = undefined;
this.menu.removeAll();
}
});
let bgMenu;
function init() {
}
function enable() {
bgMenu = new BGIndicator;
Main.panel.addToStatusArea('bg-indicator', bgMenu);
}
function disable() {
bgMenu.stop();
bgMenu.destroy();
}