-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainWindow.html
219 lines (214 loc) · 10.6 KB
/
mainWindow.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Electroscout</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
</head>
<body>
<nav>
<div class="nav-wrapper">
<a class="brand-logo center">Nightscout Data</a>
</div>
</nav>
<div style="height: 20px"></div>
<div id="container" style="height: 300px; min-width: 310px"></div>
<div style="height: 2px; background-color: #ee6e73"></div>
<table class="striped">
<thead>
<tr>
<th>Date</th>
<th>Value</th>
<th>Direction</th>
</tr>
</thead>
<tbody id="json">
</tbody>
</table>
<div id="modal" class="modal">
<div class="modal-content">
<h4 id="modalTitle"></h4>
<p id="modalBody"></p>
</div>
<div class="modal-footer">
<a href="#!" class="modal-action modal-close waves-effect waves-green btn-flat">OK</a>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<script>
const highcharts = require('highcharts');
const https = require('https');
const http = require('http');
const crypto = require('crypto');
const {ipcRenderer} = require('electron');
const Store = require('electron-store');
const utils = require('./utils');
const store = new Store();
const settings = store.get('settings');
const jsonElement = $('#json');
const titleElement = $('head title');
const offset = moment().utcOffset();
$(document).ready(() => {
refreshData();
setInterval(refreshData, 60000);
});
/*
// Test data for the tray icons
const testData = [
{direction:'Flat', value:1},
{direction:'Flat', value:4},
{direction:'Flat', value:7},
{direction:'Flat', value:11},
{direction:'Flat', value:19},
{direction:'FortyFiveUp', value:1},
{direction:'FortyFiveUp', value:4},
{direction:'FortyFiveUp', value:7},
{direction:'FortyFiveUp', value:11},
{direction:'FortyFiveUp', value:19},
{direction:'SingleUp', value:1},
{direction:'SingleUp', value:4},
{direction:'SingleUp', value:7},
{direction:'SingleUp', value:11},
{direction:'SingleUp', value:19},
{direction:'DoubleUp', value:1},
{direction:'DoubleUp', value:4},
{direction:'DoubleUp', value:7},
{direction:'DoubleUp', value:11},
{direction:'DoubleUp', value:19},
{direction:'FortyFiveDown', value:1},
{direction:'FortyFiveDown', value:4},
{direction:'FortyFiveDown', value:7},
{direction:'FortyFiveDown', value:11},
{direction:'FortyFiveDown', value:19},
{direction:'SingleDown', value:1},
{direction:'SingleDown', value:4},
{direction:'SingleDown', value:7},
{direction:'SingleDown', value:11},
{direction:'SingleDown', value:19},
{direction:'DoubleDown', value:1},
{direction:'DoubleDown', value:4},
{direction:'DoubleDown', value:7},
{direction:'DoubleDown', value:11},
{direction:'DoubleDown', value:19},
{direction:'NONE', value:1},
{direction:'NONE', value:4},
{direction:'NONE', value:7},
{direction:'NONE', value:11},
{direction:'NONE', value:19},
{direction:'NOT COMPUTABLE', value:1},
{direction:'NOT COMPUTABLE', value:4},
{direction:'NOT COMPUTABLE', value:7},
{direction:'NOT COMPUTABLE', value:11},
{direction:'NOT COMPUTABLE', value:19},
{direction:'RATE OUT OF RANGE', value:1},
{direction:'RATE OUT OF RANGE', value:4},
{direction:'RATE OUT OF RANGE', value:7},
{direction:'RATE OUT OF RANGE', value:11},
{direction:'RATE OUT OF RANGE', value:19},
];
let index = 0;
setInterval(() => {
ipcRenderer.send('glucose:update', {direction: testData[index].direction, text: testData[index].value + ' ' + testData[index].direction, value: testData[index].value, targets: {veryHigh:15, high:10, low:5, veryLow:3}});
titleElement.html(testData[index].value + ' ' + testData[index].direction);
index++;
if (index == testData.length) index = 0;
}, 2000);
*/
ipcRenderer.on('main:refresh', refreshData);
function refreshData() {
if (settings) {
const hashedNightscoutSecret = crypto.createHash('sha1').update(settings.nightscoutSecret).digest('hex');
let options = {
host: settings.nightscoutUrl,
port: settings.nightscoutPort,
method: 'GET',
headers: {
'API-SECRET': hashedNightscoutSecret
}
};
const connection = settings.nightscoutPort == 80 ? http : https;
options.path = '/api/v1/profile.json';
connection.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (settingsData) => {
let serverSettings = JSON.parse(settingsData)[0];
const targets = {
veryHigh: serverSettings.store.Default.target_high[0].value*1.5,
high: serverSettings.store.Default.target_high[0].value*1,
veryLow: serverSettings.store.Default.target_low[0].value*0.5,
low: serverSettings.store.Default.target_low[0].value*1
}
options.path = '/api/v1/entries.json';
connection.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (entriesData) => {
const result = JSON.parse(entriesData);
const sgvs = _.filter(result, (x) => { return x.type == 'sgv' });
jsonElement.empty();
const minutesAgo = moment.duration(moment().diff(moment(sgvs[0].dateString))).minutes();
const currentInfo = utils.displaySgv(serverSettings, sgvs[0].sgv) + ' ' + utils.displayDirection(sgvs[0].direction) + ' ' + utils.displayDelta(serverSettings, sgvs[0].sgv, sgvs[1].sgv) + ' ' + utils.displayUnits(serverSettings) + ' [' + minutesAgo + 'm]';
titleElement.html(currentInfo);
ipcRenderer.send('glucose:update', {direction: sgvs[0].direction, text: currentInfo, value: utils.displaySgv(serverSettings, sgvs[0].sgv), targets: targets});
let graphData = [];
_.each(sgvs, (element) => {
const momentDate = moment(element.dateString);
const momentDateLocal = momentDate.clone().add(offset, 'minutes');
const date = momentDate.fromNow();
const sgv = utils.displaySgv(serverSettings, element.sgv);
const direction = utils.displayDirection(element.direction);
jsonElement.append('<tr><td>' + date + '</td><td>' + sgv + '</td><td>' + direction + '</td></tr>');
graphData.push([momentDateLocal.valueOf(), sgv]);
});
graphData.sort((a, b) => a[0] < b[0] ? -1 : 1);
highcharts.chart('container', {
chart: { type: 'spline' },
title: { text: '' },
xAxis: {
type: 'datetime',
title: {
text: 'Time (HH:MM)'
}
},
yAxis: {
title: {
text: 'Glucose (' + utils.displayUnits(serverSettings) + ')'
},
min: 0
},
tooltip: {
headerFormat: '<b>{series.name}</b><br>',
pointFormat: '{point.x:%H:%M}: {point.y:.2f} ' + utils.displayUnits(serverSettings)
},
plotOptions: {
spline: {
marker: {
enabled: true
}
}
},
colors: ['#ee6e73'],
series: [{
showInLegend: false,
name: "Blood Glucose level",
data: graphData
}],
credits: { enabled: false }
});
});
}).end();
});
}).on('error', (error) => {
$('#modalTitle').text('Error');
$('#modalBody').text('Cannot connect to Nightscout, check your settings.');
$('#modal').modal();
$('#modal').modal('open');
}).end();
}
}
</script>
</body>
</html>