This repository has been archived by the owner on Feb 7, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog_tcpip_device.html
543 lines (448 loc) · 16.2 KB
/
dialog_tcpip_device.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<!DOCTYPE html>
<html lang="en">
<head>
<title>VSCP tcp/ip remote host</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="./node_modules/@primer/octicons/build/build.css">
<link rel="stylesheet" href="./node_modules/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="./node_modules/animate.css/animate.min.css">
<style>
.active {
background-color: rgb(154, 243, 154) !important;
}
.connerr {
background-color: rgb(250, 8, 8) !important;
}
body.busy-cursor {
cursor: progress;
}
</style>
</head>
<body class="bg-light">
<div class="container">
<div class="col-sm-12 text-center">
<h2>Remote VSCP Server</h2>
</div>
<form action="/action_page.php" class="needs-validation" novalidate>
<div class="form-group">
<label class="font-weight-bold" for="description">Name:</label>
<input id="connectionName" type="text" class="form-control form-control-sm"
placeholder="Enter descriptive name for connection" name="Name" required>
<div id="errorName" class="invalid-feedback">You must set a unique name for the connection.</div>
</div>
<div class="form-group">
<label class="font-weight-bold" for="host">Remote host/device (host:port):</label>
<input id="remoteHost" type="text" class="form-control form-control-sm" placeholder="Enter host:port"
name="host" required>
<div id="errorHost" class="invalid-feedback">There must be a remote host/device specified (and port) on the form
host:port.
Default
port is 9598 if not entered).</div>
</div>
<div class="form-row">
<div class="col">
<div class="form-group">
<label class="font-weight-bold" for="username">Username:</label>
<input id="username" type="text" class="form-control form-control-sm" placeholder="Enter username"
name="username">
</div>
</div>
<div class="col">
<div class="form-group">
<label class="font-weight-bold" for="pwd">Password:</label>
<input id="password" type="password" class="form-control form-control-sm" placeholder="Enter password"
name="password">
</div>
</div>
</div>
<div class="form-row">
<div class="col-6">
<div class="form-group">
<label class="font-weight-bold" for="contimeout">Connection timeout (ms):</label>
<input id="connTimeout" type="text" class="form-control form-control-sm"
placeholder="Enter connection timeout in ms" name="connTimeout">
</div>
</div>
<div class="form-group">
<div class="col">
<label class="font-weight-bold" for="wcyd">Default wcyd:</label>
<input id="wcyd" type="text" class="form-control form-control-sm" placeholder="Enter default wcyd code"
name="wcyd">
</div>
</div>
</div>
<div class="form-row">
<div class="col-6">
<div class="form-group">
<label class="font-weight-bold" for="resptimeout">Response timeout (ms):</label>
<input id="respTimeout" type="text" class="form-control form-control-sm"
placeholder="Enter respons timeout in ms" name="respTimeout">
</div>
</div>
</div>
<div class="form-group">
<label class="font-weight-bold" for="pwd">GUID for connection (optional):</label>
<input id="guid" type="text" class="form-control form-control-sm" placeholder="Enter optional GUID" name="guid">
</div>
<div class="form-group">
<label class="font-weight-bold" for="interface">Interface (optional):</label>
<input id="interface" type="text" class="form-control form-control-sm" placeholder="Enter interface GUID"
name="interface">
</div>
<div class="form-group form-check form-inline">
<label class="font-weight-bold" for="pwd">Options:</label>
<div class="col">
<label class="font-weight-bold" class="form-check-label">
<input id="bSSL" class="form-check-input form-control-sm" type="checkbox" name="bssl"> SSL </label>
</div>
<div class="col">
<label class="font-weight-bold" class="form-check-label">
<input id="bLevel2" class="form-check-input" type="checkbox" name="blevel2"> Full level II
</label>
</div>
</div>
<div>
<hr />
</div>
<div class="col-sm-12 text-center">
<button id="btnSetFilter" type="button" class="btn btn-warning">Set/edit filter</button>
<button id="btnTestConnection" type="button" class="btn btn-warning">Test connection</button>
<button id="btnGetInterfaces" type="button" class="btn btn-warning">Get interfaces</button></div>
<div>
<hr />
</div>
<div class="row">
<div class="col-sm-12 text-center">
<button id="btnCancel" type="submit" class="btn-sm">Cancel</button>
<button id="btnSubmit" type="submit" class="btn-sm btn-default"><span class="octicon octicon-check"
aria-hidden="true"></span>OK</button>
<i class="opticon octicon-mark-github"></i>
</div>
</div>
</form>
</div>
<!-- Bootstrap Optional JavaScript -->
<!-- jQuery first, then popper.js, then Bootstrap JS -->
<script>let $ = jQuery = require('./node_modules/jquery/dist/jquery.min.js');</script>
<script>require('./node_modules/@popperjs/core/dist/cjs/popper.js');</script>
<script>require('./node_modules/bootstrap/dist/js/bootstrap.min.js');</script>
</body>
<script>
const { remote, ipcRenderer } = require('electron');
const { app, dialog } = remote;
const win = remote.getCurrentWindow();
const vscp = require('node-vscp'); // TODO: use module
const vscp_tcp_Client = require('./src/vscptcp'); // TODO: use module
const vscp_class = require('node-vscp-class');
const vscp_type = require('node-vscp-type');
let bAdd = true; // Add record (false for edit)
let testProgress = ''; // Holds progress for the connection test
let filter = {
"name": "",
"filterPriority": 0,
"filterClass": 0,
"filterType": 0,
"filterIndex": 0,
"filterZone": 0,
"filterSubzone": 0,
"filterGuid": "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00",
"maskPriority": 0,
"maskClass": 0,
"maskType": 0,
"maskIndex": 0,
"maskZone": 0,
"maskSubzone": 0,
"maskGuid": "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00"
}
///////////////////////////////////////////////////////////////////////////
// validate
//
// Validate form before submission
//
let validate = function () {
let rv = true;
// Test name
if (0 == $("#connectionName").val().trim().length) {
$("#errorName").html("You must set a unique name for the connection.");
$("#errorName").addClass("d-block");
rv = false;
}
else {
$("#errorName").removeClass("d-block");
}
// Is name already used
if (bAdd) {
ipcRenderer.invoke('get-named-connection',
$("#connectionName").val().trim()).then( conn => {
if (null != conn) {
console.log('Name is already used: [' + $("#connectionName").val().trim() + ']');
$("#errorName").html("Name must be unique and this name is already used.");
$("#errorName").addClass("d-block");
rv = false;
}
});
}
// Test host
if (0 == $("#remoteHost").val().trim().length) {
$("#errorHost").html("You must enter a remote host/device address (host:port).");
$("#errorHost").addClass("d-block");
rv = false;
}
else {
$("#errorHost").removeClass("d-block");
}
// Username - everything accepted
// password - everything accepted
// guid - everything accepted
// interface - everything accepted
return rv;
};
///////////////////////////////////////////////////////////////////////////
// testConnection
//
const testConnection = async () => {
testProgress = '';
// Defaults
let host = 'localhost';
let port = 9598;
let user = 'admin';
let password = 'secret';
if (0 == $("#remoteHost").val().trim().length) {
let options = {
type: 'error',
buttons: ['OK'],
title: 'Missing parameter',
message: 'Need a remote host on the form host:port to be able to test.'
}
dialog.showMessageBox(remote.getCurrentWindow(), options);
validate();
return;
}
$("#errorHost").removeClass("d-block");
let remoteHost = $("#remoteHost").val();
if (-1 == remoteHost.search(':')) {
// No port given - use default
host = remoteHost;
}
else {
let sep = remoteHost.split(':');
host = sep[0];
if (sep.length > 1) port = parseInt(sep[1], 10);
}
user = $("#username").val().trim();
password = $("#password").val().trim();
let response = {};
let vscp_tcp_client = new vscp_tcp_Client();
response = await vscp_tcp_client.connect(
{
host: host,
port: port,
timeout: vscp.readValue($("#connTimeout").val()),
onSuccess: null
});
console.log(response);
if ('success' === response.result) {
testProgress = "connection OK\n"
}
response = await vscp_tcp_client.sendCommand(
{
command: "noop"
});
console.log(response);
if ('success' === response.result) {
testProgress += "noop OK\n"
}
if (user.length) {
response = await vscp_tcp_client.user({ username: user });
console.log(response);
if ('success' === response.result) {
testProgress += "user OK\n"
}
}
if (password.length) {
response = await vscp_tcp_client.password({ password: password });
console.log(response);
if ('success' === response.result) {
testProgress += "pass OK\n"
}
}
response = await vscp_tcp_client.disconnect();
console.log(response);
if ('success' === response.result) {
testProgress += "disconnect OK\n"
}
$("#btnTestConnection").removeClass('connerr').addClass('active ');
}
///////////////////////////////////////////////////////////////////////////
// Document ready
//
$(document).ready(function ($) {
var options = ipcRenderer.sendSync("dialog-open", "");
var params = JSON.parse(options);
console.log(params);
if ('undefined' !== typeof params.connection) {
bAdd = false; // edit
// Fill in initial data
$("#connectionName").val(params.connection.name);
$("#connectionName").prop("readonly", true);
$("#remoteHost").val(params.connection.host);
$("#username").val(params.connection.username);
$("#password").val(params.connection.password);
if ('undefined' == typeof params.connection.connTimeout) {
params.connection.connTimeout = 10000;
}
$("#connTimeout").val(params.connection.connTimeout);
console.log(params.connection.wcyd);
if ('undefined' == typeof params.connection.wcyd) {
params.connection.wcyd = [];
for (let i = 0; i < 8; i++) {
params.connection.wcyd[i] = 0;
}
}
$("#wcyd").val(params.connection.wcyd.join(','));
if ('undefined' == typeof params.connection.respTimeout) {
params.connection.respTimeout = 3000;
}
$("#respTimeout").val(params.connection.respTimeout);
$("#guid").val(params.connection.guid);
$("#interface").val(params.connection.interface);
filter = params.connection.filter;
$('#bLevel2').prop('checked', params.connection.bLevel2);
$('#bSSL').prop('checked', params.connection.bSSL);
}
else {
$("#connTimeout").val('10000');
$("#wcyd").val('0,0,0,0,0,0,0,0');
$("#respTimeout").val('2000');
}
///////////////////////////////////////////////////////////////////////////
// Remove active class
//
$("button").click(function () {
$("button").removeClass("active");
//$(this).addClass("active");
});
///////////////////////////////////////////////////////////////////////////
// Cancel
//
$("#btnCancel").on('click', function (e) {
ipcRenderer.send("dialog-close", '');
remote.getCurrentWindow().close();
});
///////////////////////////////////////////////////////////////////////////
// Save new driver info
//
$("#btnSubmit").on('click', function (e) {
e.preventDefault();
if (validate()) {
let result = {};
result.type = 'tcpip';
result.name = $("#connectionName").val().trim();
result.host = $("#remoteHost").val().trim();
result.username = $("#username").val().trim();
result.password = $("#password").val().trim();
result.connTimeout = vscp.readValue($("#connTimeout").val());
result.respTimeout = vscp.readValue($("#respTimeout").val());
result.wcyd = [0,0,0,0,0,0,0,0];
let wcyd = [];
wcyd = $("#wcyd").val().split(',');
let idx = 0;
wcyd.forEach((item) => {
result.wcyd[idx++] = vscp.readValue(item);
});
// Make sure we have eight entries
while (result.wcyd < 8) {
result.wcyd.push(0);
}
// Make sure we don't have more than eight entries
while (result.wcyd > 8) {
result.wcyd.pop();
}
result.guid = $("#guid").val().trim();
result.interface = $("#interface").val().trim();
if ($("#bLevel2").is(":checked")) {
result.bLevel2 = true;
}
else {
result.bLevel2 = false;
}
if ($("#bSSL").is(":checked")) {
result.bSSL = true;
}
else {
result.bSSL = false;
}
result.filter = filter;
ipcRenderer.send("dialog-close", result);
remote.getCurrentWindow().close();
}
});
///////////////////////////////////////////////////////////////////////////
// Set filter
//
$("#btnSetFilter").on('click', function (e) {
response = ipcRenderer.sendSync("open-modal-dialog",
{
title: "Set/Edit filter",
width: 800, height: 650,
win: remote.getCurrentWindow(),
url: '../dialog_set_filter.html',
filter: filter
});
if (null !== response) {
filter = response;
}
});
///////////////////////////////////////////////////////////////////////////
// Test connection
//
$("#btnTestConnection").on('click', function (e) {
document.body.classList.add('busy-cursor');
testConnection()
.then(() => {
document.body.classList.remove('busy-cursor');
})
.catch(err => {
let options = {
type: 'error',
buttons: ['OK'],
title: 'Host failure',
message: 'Failed to connect to remote host/device',
detail: 'Error: ' + err.message + ' \n' + testProgress
}
dialog.showMessageBox(remote.getCurrentWindow(), options);
console.log("Test failed");
console.log(err);
$(this).removeClass('active').addClass('connerr ');
$("#errorHost").addClass("d-block");
$("#errorHost").html(err.message);
console.log('d-block');
document.body.classList.remove('busy-cursor');
});
//document.body.classList.remove('busy-cursor');
});
///////////////////////////////////////////////////////////////////////////
// Get/set interface
//
$("#btnGetInterfaces").on('click', function (e) {
let result = ipcRenderer.sendSync("open-modal-dialog",
{
title: "Get/Set interface",
width: 900, height: 500,
win: remote.getCurrentWindow(),
url: '../dialog_get_interfaces.html',
interface: $("#interface").val(),
host: $("#remoteHost").val(),
username: $("#username").val(),
password: $("#password").val()
});
console.log(result)
if (result.length) {
password: $("#interface").val(result)
}
});
});
</script>
</html>