-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.html
378 lines (296 loc) · 13 KB
/
main.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<!DOCTYPE html>
<html lang="es">
<link rel="stylesheet" href="./css/photon.min.css">
<link rel="stylesheet" href="./css/main.css">
<head>
<title>Rasskin Data Logger</title>
</head>
<body>
<div class="window">
<header class="toolbar-header">
<div class="toolbar-actions">
<div class="btn-group">
<button class="btn btn-default btn-large" id="save-data">Grabar datos</button>
<button class="btn btn-default btn-large" id="timed-save-data"><span class="icon icon-clock"></span></button>
</div>
<button class="btn btn-large btn-negative" id="delete-data">Borrar datos</button>
</div>
</header>
<div class="window-content">
<div class="pane-group">
<div class="pane-sm sidebar">
<ul class="list-group" id="trace-config">
</ul>
</div>
<div class="pane">
<div id="live-plot"><div class="padded-more">Pulse Ctrl+N.</div></div>
<div id="live-numerical" class="flex-container">
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
const plotly = require("plotly-latest.min.js");
const timer = require("clock-timer.js");
const storage = require('electron-store');
const {ipcRenderer} = require("electron");
const {dialog} = require('electron').remote;
const csvWriter = require("csv-write-stream");
const fs = require('fs');
const temp = require('temp');
const store = new storage();
let writer;
let traces = [];
let lock = false;
let temp_file_dir; ///////////////////////////// <----------------------------
var numeric_displays = [];
default_colors = [ '#1f77b4', // muted blue
'#ff7f0e', // safety orange
'#2ca02c', // cooked asparagus green
'#d62728', // brick red
'#9467bd', // muted purple
'#8c564b', // chestnut brown
'#e377c2', // raspberry yogurt pink
'#7f7f7f', // middle gray
'#bcbd22', // curry yellow-green
'#17becf'];
window.addEventListener("resize", function(){
plotly.Plots.resize("live-plot");
})
var new_x, new_y;
ipcRenderer.on("arduino:data-stream-number", newDataStreamSetup);
function newDataStreamSetup(e, number_of_streams){
if(!lock){
numeric_displays = [];
number_of_streams = parseInt(number_of_streams)
plotly.purge("live-plot");
ipcRenderer.removeAllListeners("arduino:data");
while(document.querySelector("#live-plot").firstChild){
document.querySelector("#live-plot").removeChild(document.querySelector("#live-plot").firstChild);
}
traces = [];
store.set("numberOfStreams", number_of_streams)
let trace_config = document.querySelector("#trace-config");
let numeric_display = document.querySelector("#live-numerical");
// Clean the DOM
while(trace_config.firstChild){
trace_config.removeChild(trace_config.firstChild);
}
while(numeric_display.firstChild){
numeric_display.removeChild(numeric_display.firstChild);
}
let main_selector, color_selector, name_selector;
let live_number_container, live_number, live_number_label;
let traceNameSelectorStoreName, traceColorSelectorStoreColor;
for(let i = 1; i < number_of_streams; i++){
traceNameSelectorStoreName = store.get("traceNameSelector" + i +".name")
traceColorSelectorStoreColor = store.get("traceColorSelector"+ i +".color")
main_selector = document.createElement("li");
color_selector = document.createElement("input");
name_selector = document.createElement("input");
live_number_container = document.createElement("div");
live_number = document.createElement("h1");
live_number_label = document.createElement("h4");
main_selector.className = "list-group-item form-control";
color_selector.className = "pull-left";
color_selector.setAttribute("type", "color");
name_selector.className = "pull-right";
name_selector.setAttribute("type", "text")
name_selector.setAttribute("placeholder", traceNameSelectorStoreName || ("Serie "+ i));
store.set("traceNameSelector" + i +".num", i);
color_selector.setAttribute("name", "traceColorSelector" + i);
name_selector.setAttribute("name", "traceNameSelector" + i);
if(number_of_streams == 2) color_selector.className += " zero-fix";
store.set("traceColorSelector" + i + ".num", i);
color_selector.value = traceColorSelectorStoreColor || default_colors[(i-1)%10];
live_number.style.color = traceColorSelectorStoreColor || default_colors[(i-1)%10];
live_number_container.className = "text-center padded btn";
live_number.id = "liveNumericDisplay" + i;
live_number_label.id = "traceNameSelector" + i;
live_number.innerHTML = 0;
live_number_label.innerHTML = traceNameSelectorStoreName || ("Serie "+ i);
main_selector.appendChild(color_selector);
main_selector.appendChild(name_selector);
trace_config.appendChild(main_selector);
live_number_container.appendChild(live_number);
live_number_container.appendChild(live_number_label);
numeric_display.appendChild(live_number_container);
numeric_displays[i-1] = live_number;
name_selector.removeEventListener("change", nameSelectorChange);
name_selector.addEventListener("change", nameSelectorChange);
color_selector.removeEventListener("change", colorSelectorChange);
color_selector.addEventListener("change", colorSelectorChange);
traces[i-1] = {x: [], y: [], type: "scatter", name: traceNameSelectorStoreName || "Serie " + i, marker: {color: traceColorSelectorStoreColor || default_colors[(i-1)%10]}};
// main_selector = color_selector = name_selector = live_number_container = live_number = live_number_label = null;
}
plotly.newPlot('live-plot', traces); // Most important piece of code in this doc !important
// Cleanup
// Various listeners
document.querySelector("#save-data").removeEventListener("click", StartGrabar);
document.querySelector("#save-data").addEventListener("click", StartGrabar);
document.querySelector("#timed-save-data").removeEventListener("click", StartTimedGrabar);
document.querySelector("#timed-save-data").addEventListener("click", StartTimedGrabar);
document.querySelector("#delete-data").removeEventListener("click", deleteDataClick);
document.querySelector("#delete-data").addEventListener("click", deleteDataClick);
plotly.Plots.resize("live-plot");
ipcRenderer.on("arduino:data", newDataNoCam);
}
}
function deleteDataClick(e){
plotly.purge("live-plot");
traces = [];
for(let i = 1; i < store.get("numberOfStreams"); i++){
traces[i-1] = {x: [], y: [], type: "scatter", name: store.get("traceNameSelector" + i + ".name") || "Serie " +i, marker: {color: store.get("traceColorSelector"+i+".color") || default_colors[(i-1)%10]}};
}
plotly.newPlot('live-plot', traces);
ipcRenderer.removeAllListeners("arduino:data")
ipcRenderer.on("arduino:data", newDataNoCam);
};
function nameSelectorChange(e){
// Event listener for change of trace name
e.preventDefault()
store.set(this.name+".name", this.value);
document.querySelector("#" + this.name).innerHTML = this.value;
plotly.restyle("live-plot", {name: this.value}, [store.get(this.name+".num")-1]);
}
function colorSelectorChange(e){
// Event listener for change of trace color
e.preventDefault();
let i = store.get(this.name+".num");
store.set(this.name+".color", this.value);
document.querySelector("#liveNumericDisplay" + i).style.color = this.value;
plotly.restyle("live-plot", {marker: {color : this.value || default_colors[(i-1)%10]}}, [i-1]);
}
function newDataNoCam(e, new_data){
//function for data display no save
let stream_numbers = store.get("numberOfStreams");
if(new_data !== null && new_data !== undefined){
try{
if(new_data.length == stream_numbers){
new_x = [];
new_y = [];
for(let i = 1; i<new_data.length; i++){
new_x.push([new_data[0]]);
new_y.push([new_data[i]]);
numeric_displays[i-1].innerHTML = new_data[i];
}
plotly.extendTraces("live-plot", {x: new_x, y: new_y}, Array.from(Array(stream_numbers - 1).keys()), 200);
}
} catch (err){
console.log(err.message);
}
}
}
function newDataYesCam(e, new_data){
//function for data display yes save
let stream_numbers = store.get("numberOfStreams");
if(new_data !== null && new_data !== undefined){
try{
if(new_data.length == stream_numbers){
new_x = [];
new_y = [];
for(let i = 1; i<new_data.length; i++){
new_x.push([new_data[0]]);
new_y.push([new_data[i]]);
numeric_displays[i-1].innerHTML = new_data[i];
}
lock = true;
plotly.extendTraces("live-plot", {x: new_x, y: new_y}, Array.from(Array(stream_numbers - 1).keys()), 200);
// new y reuse
new_y.unshift(new_data[0]);
writer.write(new_y);
}
} catch (err){
console.log(err.message);
}
}
}
function StartGrabar(e){
// Function start on save-data button click
this.removeEventListener("click", StartGrabar);
this.addEventListener("click", StopGrabar);
this.innerHTML = "Dejar de grabar datos";
this.className = "btn btn-warning btn-large";
let CSVHeaders = ["Tiempo (ms)"];
for(let i = 1; i < store.get("numberOfStreams"); i++){
CSVHeaders.push(store.get("traceNameSelector"+i+".name")||("Serie "+i));
}
writer = csvWriter({headers: CSVHeaders});
// Add writer and headers
temp_file_dir = temp.path();
console.log(temp_file_dir)
fs.closeSync(fs.openSync(temp_file_dir, 'w'));
var temp_file = fs.createWriteStream(temp_file_dir);
writer.pipe(temp_file);
// Change ipc functions
ipcRenderer.removeListener("arduino:data", newDataNoCam);
ipcRenderer.on("arduino:data", newDataYesCam);
}
ipcRenderer.on("arduino:countdown-send-data-done", StopTimedGrabar);
function StartTimedGrabar(e){
document.querySelector("#save-data").removeEventListener("click", StartGrabar);
this.removeEventListener("click", StartTimedGrabar);
this.addEventListener("click", EvilUserStopTimedGrabar);
this.className = "btn btn-warning btn-large";
ipcRenderer.send("arduino:countdown-save-data");
// Extremely WET, please fix TODO (function is writen 4 times?)
let CSVHeaders = ["Tiempo (ms)"];
for(let i = 1; i < store.get("numberOfStreams"); i++){
CSVHeaders.push(store.get("traceNameSelector"+i+".name")||("Serie "+i));
}
writer = csvWriter({headers: CSVHeaders});
// Add writer and headers
temp_file_dir = temp.path();
console.log(temp_file_dir)
fs.closeSync(fs.openSync(temp_file_dir, 'w'));
var temp_file = fs.createWriteStream(temp_file_dir);
writer.pipe(temp_file);
// Change ipc functions
ipcRenderer.removeListener("arduino:data", newDataNoCam);
ipcRenderer.on("arduino:data", newDataYesCam);
}
function EvilUserStopTimedGrabar(e){
ipcRenderer.send("arduino:countdown-send-data-done");
this.removeEventListener("click", EvilUserStopTimedGrabar);
}
function StopTimedGrabar(e){
ipcRenderer.removeListener("arduino:data", newDataYesCam);
ipcRenderer.on("arduino:data", newDataNoCam);
writer.end();
writer = null;
let currentdate = new Date();
let defaultname =currentdate.getDate()+"-"+(currentdate.getMonth()+1)+"-"+currentdate.getFullYear()+"@"+currentdate.getHours()+"-"+currentdate.getMinutes()+"-"+currentdate.getSeconds();
let savepath = dialog.showSaveDialog({defaultPath: defaultname, filters: [{name: "CSV", extensions:["csv"]}]});
if(savepath !== undefined){
fs.rename(temp_file_dir, savepath, (err) => {
if(err) dialog.showErrorBox("Error guardando el archivo", err.message + "\n Puedes encontrar tus datos en: " + temp_file_dir +".");
});
}
document.querySelector("#save-data").addEventListener("click", StartGrabar);
document.querySelector("#timed-save-data").addEventListener("click", StartTimedGrabar);
document.querySelector("#timed-save-data").className = "btn btn-default btn-large";
}
function StopGrabar(e){
//Function start on save-data antithesis button click
// Change ipc functions
ipcRenderer.removeListener("arduino:data", newDataYesCam);
ipcRenderer.on("arduino:data", newDataNoCam);
writer.end();
writer = null;
this.removeEventListener("click", StopGrabar);
let currentdate = new Date();
let defaultname =currentdate.getDate()+"-"+(currentdate.getMonth()+1)+"-"+currentdate.getFullYear()+"@"+currentdate.getHours()+"-"+currentdate.getMinutes()+"-"+currentdate.getSeconds();
let savepath = dialog.showSaveDialog({defaultPath: defaultname, filters: [{name: "CSV", extensions:["csv"]}]});
if(savepath !== undefined){
fs.rename(temp_file_dir, savepath, (err) => {
if(err) dialog.showErrorBox("Error guardando el archivo", err.message + "\n Puedes encontrar tus datos en: " + temp_file_dir +".");
});
}
this.addEventListener("click", StartGrabar);
this.innerHTML = "Grabar datos";
this.className = "btn btn-default btn-large";
}
</script>
</body>
</html>