-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
289 lines (262 loc) · 11 KB
/
renderer.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
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
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
const { SerialPort, ReadlineParser } = require('serialport')
// const port = new SerialPort({
// path: "COM7",
// baudRate: 115200,
// })
var portOpened = '';
var port = null;
SerialPort.list().then((ports) => {
for(var i = 0; i < ports.length; i++) {
const newDiv = document.createElement("button")
newDiv.setAttribute("class","dropdown-item")
newDiv.setAttribute("type","button")
const newContent = document.createTextNode(`${ports[i]["path"]} , ${ports[i]["manufacturer"]}`);
newDiv.appendChild(newContent);
// add the newly created element and its content into the DOM
const parentDiv = document.getElementById("ports-list")
const currentDiv = document.getElementById("list");
parentDiv.insertBefore(newDiv, currentDiv);
}
})
document.getElementById('dropdownMenu2').addEventListener('click',() => {
const portList = document.getElementsByClassName('dropdown-item')
var portResults = '';
for(var i = 0; i < portList.length; i++) {
portResults += portList[i].innerHTML
portList[i].setAttribute('onclick', `portSelected(${i})`)
}
})
function portSelected(portIndex){
console.log(portIndex)
const portList = document.getElementsByClassName('dropdown-item')[portIndex].innerHTML
console.log(portList)
var comPort = portList.slice(0, (portList.indexOf(',')))
console.log(comPort)
document.getElementById('com-port').innerHTML = comPort
}
async function serialService(){
const portSelected = document.getElementById('com-port').innerHTML
if(portSelected === '') {
return //TODO err msg
}
if(!portOpened){
port = await new SerialPort({
path: portSelected,
baudRate: 115200,
})
portOpened = true;
const { path } = port
console.log(`Port ${path} oppened!`)
port.write('R')
const parser = new ReadlineParser()
port.pipe(parser)
var dataRecieved = "";
parser.on('data', dataRecieved =>{
console.log(dataRecieved)
var startIndex = dataRecieved.indexOf('<');
var endIndex = dataRecieved.indexOf('>', startIndex);
var dadosSerialTratados = dataRecieved.substring(startIndex + 2, endIndex - startIndex - 1);
var leituraArray = dadosSerialTratados.split(',');
console.log(leituraArray)
/*leituraArray anatomy
leituraArray[0] -> V: xxxxx = Voltage being read
leituraArray[1] -> T: xxxxx = Time elapsed
leituraArray[2] -> C: xxxxx = Total charge cycles
leituraArray[3] -> S: xxxxx = Charge status
leituraArray[4] -> CFG1: xxxxx = minimum/start voltage
leituraArray[5] -> CFG2: xxxxx = max/end voltage
leituraArray[6] -> CFG3: xxxxx = max/limit charge time
leituraArray[7] -> CFG4: xxxxx = system current mode
*/
document.getElementById('value1').innerText = leituraArray[0].replace('V: ','') + " V"
document.getElementById('value2').innerText = leituraArray[1].replace('T: ','')
document.getElementById('value3').innerText = leituraArray[2].replace('C: ','')
document.getElementById('value4').innerText = leituraArray[3].replace('S: ','')
// Progress bar call
progressUpdate(
leituraArray[0].replace('V: ',''),
leituraArray[4].replace('CFG1: ',''),
leituraArray[5].replace('CFG2: ','')
)
// Config display call
cfgDisplay(
leituraArray[4].replace('CFG1: ',''),
leituraArray[5].replace('CFG2: ',''),
leituraArray[6].replace('CFG3: ',''),
leituraArray[7].replace('CFG4: ','')
)
})
function cfgDisplay(minVoltageCfg, maxVoltageCfg, maxTimeCfg, systemMode){
document.getElementById('value5').innerText = minVoltageCfg + " V"
document.getElementById('value6').innerText = maxVoltageCfg + " V"
document.getElementById('value7').innerText = maxTimeCfg + " Horas"
// System mode descripton
// System mode 0 = Default do sistema
// System mode 1 = Modo Automatico
// System mode 2 = Modo Tracionario
// System mode 3 = Modo Bloqueador
switch(systemMode){
case "0":
systemMode = "Contador pré carga - iniciando..."
break;
case "1":
systemMode = "Automático"
break;
case "2":
systemMode = "Tracionário"
break;
case "3":
systemMode = "Bloqueador"
break;
default:
systemMode = "erro"
break;
}
document.getElementById('value8').innerText = systemMode
}
function progressUpdate(voltageValue, minVoltageCfg, maxVoltageCfg){
console.log(voltageValue)
let voltageParsed = parseFloat(voltageValue)
let chargePercent = 0;
let chargeRange = maxVoltageCfg - minVoltageCfg
voltageParsed -= minVoltageCfg
chargePercent = voltageParsed / chargeRange
console.log(chargePercent)
chargePercent *= 100
chargePercent = Math.round(chargePercent)
if(chargePercent <= 0) {
chargePercent = 0;
} else if(chargePercent <= 25) {
document.getElementsByClassName('progress-bar')[0].style.backgroundColor = "#fac71e"
document.getElementsByClassName('progress-bar')[0].style.color = "white"
} else if(chargePercent <= 50){
document.getElementsByClassName('progress-bar')[0].style.backgroundColor = "#f3fa1e"
document.getElementsByClassName('progress-bar')[0].style.color = "white"
} else if(chargePercent <= 75){
document.getElementsByClassName('progress-bar')[0].style.backgroundColor = "#a9fa1e"
document.getElementsByClassName('progress-bar')[0].style.color = "white"
} else if(chargePercent <= 100){
document.getElementsByClassName('progress-bar')[0].style.backgroundColor = "#43fa1e"
document.getElementsByClassName('progress-bar')[0].style.color = "white"
} else if(chargePercent >= 100) {
chargePercent = 100
document.getElementsByClassName('progress-bar')[0].style.backgroundColor = "#1efae4"
document.getElementsByClassName('progress-bar')[0].style.color = "white"
}
chargePercent += "%"
console.log(chargePercent)
document.getElementsByClassName('progress-bar')[0].innerHTML = chargePercent
document.getElementsByClassName('progress-bar')[0].style.width = chargePercent
}
}
}
document.getElementById('com-open').addEventListener('click', serialService)
// const { path } = port
// console.log(`Port ${path} oppened!`)
// console.log(port.isOpen)
// if(port.isOpen){
// port.write('R')
// const parser = new ReadlineParser()
// port.pipe(parser)
// var dataRecieved = "";
// parser.on('data', dataRecieved =>{
// console.log(dataRecieved)
// var startIndex = dataRecieved.indexOf('<');
// var endIndex = dataRecieved.indexOf('>', startIndex);
// var dadosSerialTratados = dataRecieved.substring(startIndex + 2, endIndex - startIndex - 1);
// var leituraArray = dadosSerialTratados.split(',');
// console.log(leituraArray)
// /*leituraArray anatomy
// leituraArray[0] -> V: xxxxx = Voltage being read
// leituraArray[1] -> T: xxxxx = Time elapsed
// leituraArray[2] -> C: xxxxx = Total charge cycles
// leituraArray[3] -> S: xxxxx = Charge status
// leituraArray[4] -> CFG1: xxxxx = minimum/start voltage
// leituraArray[5] -> CFG2: xxxxx = max/end voltage
// leituraArray[6] -> CFG3: xxxxx = max/limit charge time
// leituraArray[7] -> CFG4: xxxxx = system current mode
// */
// document.getElementById('value1').innerText = leituraArray[0].replace('V: ','') + " V"
// document.getElementById('value2').innerText = leituraArray[1].replace('T: ','')
// document.getElementById('value3').innerText = leituraArray[2].replace('C: ','')
// document.getElementById('value4').innerText = leituraArray[3].replace('S: ','')
// // Progress bar call
// progressUpdate(
// leituraArray[0].replace('V: ',''),
// leituraArray[4].replace('CFG1: ',''),
// leituraArray[5].replace('CFG2: ','')
// )
// // Config display call
// cfgDisplay(
// leituraArray[4].replace('CFG1: ',''),
// leituraArray[5].replace('CFG2: ',''),
// leituraArray[6].replace('CFG3: ',''),
// leituraArray[7].replace('CFG4: ','')
// )
// })
// function cfgDisplay(minVoltageCfg, maxVoltageCfg, maxTimeCfg, systemMode){
// document.getElementById('value5').innerText = minVoltageCfg + " V"
// document.getElementById('value6').innerText = maxVoltageCfg + " V"
// document.getElementById('value7').innerText = maxTimeCfg + " Horas"
// // System mode descripton
// // System mode 0 = Default do sistema
// // System mode 1 = Modo Automatico
// // System mode 2 = Modo Tracionario
// // System mode 3 = Modo Bloqueador
// switch(systemMode){
// case "0":
// systemMode = "Contador pré carga - iniciando..."
// break;
// case "1":
// systemMode = "Automático"
// break;
// case "2":
// systemMode = "Tracionário"
// break;
// case "3":
// systemMode = "Bloqueador"
// break;
// default:
// systemMode = "erro"
// break;
// }
// document.getElementById('value8').innerText = systemMode
// }
// function progressUpdate(voltageValue, minVoltageCfg, maxVoltageCfg){
// console.log(voltageValue)
// let voltageParsed = parseFloat(voltageValue)
// let chargePercent = 0;
// let chargeRange = maxVoltageCfg - minVoltageCfg
// voltageParsed -= minVoltageCfg
// chargePercent = voltageParsed / chargeRange
// console.log(chargePercent)
// chargePercent *= 100
// chargePercent = Math.round(chargePercent)
// if(chargePercent <= 0) {
// chargePercent = 0;
// } else if(chargePercent <= 25) {
// document.getElementsByClassName('progress-bar')[0].style.backgroundColor = "#fac71e"
// document.getElementsByClassName('progress-bar')[0].style.color = "white"
// } else if(chargePercent <= 50){
// document.getElementsByClassName('progress-bar')[0].style.backgroundColor = "#f3fa1e"
// document.getElementsByClassName('progress-bar')[0].style.color = "white"
// } else if(chargePercent <= 75){
// document.getElementsByClassName('progress-bar')[0].style.backgroundColor = "#a9fa1e"
// document.getElementsByClassName('progress-bar')[0].style.color = "white"
// } else if(chargePercent <= 100){
// document.getElementsByClassName('progress-bar')[0].style.backgroundColor = "#43fa1e"
// document.getElementsByClassName('progress-bar')[0].style.color = "white"
// } else if(chargePercent >= 100) {
// chargePercent = 100
// document.getElementsByClassName('progress-bar')[0].style.backgroundColor = "#1efae4"
// document.getElementsByClassName('progress-bar')[0].style.color = "white"
// }
// chargePercent += "%"
// console.log(chargePercent)
// document.getElementsByClassName('progress-bar')[0].innerHTML = chargePercent
// document.getElementsByClassName('progress-bar')[0].style.width = chargePercent
// }
// }