-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
230 lines (171 loc) · 5.79 KB
/
renderer.js
File metadata and controls
230 lines (171 loc) · 5.79 KB
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
/*
* Strimer Plus Arduino Desktop
* Version: 2022.04.23
* Author: Murat TAMCI
* Web Site: www.themt.co
* License: Read LICENSE.txt
* Note: In loving memory of my grandfather (Ahmet Ozdil).
*/
const { SerialPort } = require('serialport')
const { ReadlineParser } = require('@serialport/parser-readline')
const jQuery = require('jquery')
function delay(delayInms) {
return new Promise(resolve => {
setTimeout(() => {
resolve(2);
}, delayInms);
});
}
var lastReaded = "";
const connectionStatusDom = jQuery('#connectionStatus');
const connectionButtonDom = jQuery('#connectButton');
const customizeTabDom = jQuery('#customizeTab');
const portListDom = jQuery("#portList");
const messageDom = jQuery('#msg');
messageDom.keydown((e) => {
if(['|'].indexOf(e.key) === -1){
} else {
e.preventDefault();
}
});
async function listPorts() {
connectionStatusDom.html("Pick a com port and connect");
const listingListItem = jQuery('<li class="uk-text-center">Listing...</li>');
portListDom.html(listingListItem);
await delay(1000);
await SerialPort.list().then((ports, err) => {
if (err) {
portListDom.html('<li class="uk-text-center">' + err.message + '</li>');
return
}
if (ports.length === 0) {
portListDom.html('<li class="uk-text-center">Not found</li>');
}
portListDom.empty();
for (var i = 0; i < ports.length; i++) {
var node = jQuery('<li class="uk-text-center" style="cursor: pointer"><div first></div><div second class="uk-text-meta"></div></li>');
var port = ports[i];
function x(node, port) {
node.click(() => setPort(node, port));
node.find('[first]').html(port.path);
node.find('[second]').html(port.friendlyName);
portListDom.append(node);
}
x(node, port);
}
})
}
function setPort(node, port) {
portListDom.find('li').removeClass('uk-text-primary');
node.addClass('uk-text-primary');
selectedPort = port;
}
listPorts()
var selectedPort = null;
var port;
var deviceCheckOK = false;
async function disconnectPort(showMessage = true) {
if (port !== undefined && port.isOpen) {
deviceCheckOK = false;
port.close();
customizeTabDom.addClass('uk-disabled');
if (showMessage) connectionStatusDom.html("Disconnected");
}
}
async function connectPort() {
if (selectedPort !== null) {
connectionStatusDom.html("Connecting...");
if (port !== undefined && port.isOpen) {
deviceCheckOK = false;
port.close();
customizeTabDom.addClass('uk-disabled');
await delay(1000);
}
port = new SerialPort({
path: selectedPort.path,
baudRate: 9600,
autoOpen: false,
//parser: new ReadlineParser('\n'),
});
port.open(function (err) {
if (err) {
connectionStatusDom.html("Connection fail");
document.getElementById("status").innerHTML = err.message;
return;
}
const parser = port.pipe(new ReadlineParser({ delimiter: '\n' }))
connectionStatusDom.html("Checking device...");
var checkDeviceTimeout = setTimeout(function () {
connectionStatusDom.html("Device not found");
disconnectPort(false);
}, 4000);
parser.on('data', data => {
var readline = data.toString();
console.log ("readed: ", readline);
if (deviceCheckOK == false) {
if (readline.indexOf("Strimer Plus Arduino") > -1) {
deviceCheckOK = true;
clearTimeout(checkDeviceTimeout);
var splittedCommands = readline.split("|");
for (var i=0; i<splittedCommands.length; i++) {
var keyValue = splittedCommands[i].split(':');
if (keyValue[0] == 'msg') {
jQuery('#msg').val(keyValue[1]);
} else if (keyValue[0] == 'delay') {
jQuery('#delay').val(keyValue[1]);
} else if (keyValue[0] == 'bri') {
jQuery('#bri').val(keyValue[1]);
} else if (keyValue[0] == 'color') {
var color = '#'+parseInt(keyValue[1]).toString(16);
jQuery('#color').val(color);
} else if (keyValue[0] == 'bgcolor') {
var color = '#'+parseInt(keyValue[1]).toString(16);
jQuery('#bgcolor').val(color);
} else if (keyValue[0] == 'inv') {
if (keyValue[1] == '1') {
jQuery('#inv-1').prop("checked", true);
} else {
jQuery('#inv-0').prop("checked", true);
}
}
}
customizeTabDom.removeClass("uk-disabled");
connectionStatusDom.html("Connected");
UIkit.switcher(jQuery('#tabs').get(0)).show(1);
}
}
lastReaded = readline;
});
});
}
}
var dontFlood = false;
var lastLine = "";
async function serialWrite(line, checkOK = true) {
lastLine = line;
if (dontFlood) return;
dontFlood = true;
setTimeout(async function () {
dontFlood = false;
if (port !== undefined && port.isOpen) {
lastReaded = "";
console.log ("write: ", lastLine);
var result = await port.write(lastLine + '\n');
await delay(250);
if (!result) {
customizeTabDom.addClass('uk-disabled');
connectionStatusDom.html("Please make connect");
UIkit.switcher(jQuery('#tabs').get(0)).show(0);
} else if (checkOK && lastReaded.indexOf("OK") == -1) {
customizeTabDom.addClass('uk-disabled');
connectionStatusDom.html("Device not found");
UIkit.switcher(jQuery('#tabs').get(0)).show(0);
}
}
else {
customizeTabDom.addClass('uk-disabled');
connectionStatusDom.html("Please make connect");
UIkit.switcher(jQuery('#tabs').get(0)).show(0);
}
}, 500)
}