-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsclient.js
107 lines (89 loc) · 2.86 KB
/
wsclient.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
const util = require("util");
var path = require("path");
var fs = require("fs");
const { WebSocket } = require("ws");
//const UNIPI_IP = "192.168.225.143";
var LISTEN_IP = "127.0.0.1";
if (process.argv.indexOf("-l") > -1) {
let index = process.argv.indexOf("-l");
LISTEN_IP = process.argv[index + 1];
console.log("[START] -l set IP to: " + LISTEN_IP);
}
const ws = new WebSocket("ws://" + LISTEN_IP + ":8007");
let debug = false;
let wsOpen = false;
function IsJsonString(str) {
var result;
try {
result = JSON.parse(str);
} catch (e) {
return false;
}
return result;
}
ws.on("message", async function message(data) {
process.stdout.write("Message: ");
var stringJson = await IsJsonString(data);
console.log(util.inspect(stringJson, false, null, true /* enable colors */));
//console.log(util.inspect(data, { showHidden: false, depth: null, colors: true }));
//ws.send(JSON.stringify({ command: "lcstart" }));
});
ws.on("open", function open() {
//ws.send(JSON.stringify({ command: "lcoff" }));
wsOpen = true;
//process.exit(0);
});
function wsInitAwait() {
return new Promise(async (resolve, reject) => {
const wsInitTimer = setInterval(async () => {
if (wsOpen) {
console.log("[SYSTEM] socket is running");
clearTimeout(wsInitTimer);
resolve(true);
}
}, 50);
});
}
var init = async () => {
console.log("[START] ...connecting to " + LISTEN_IP + ":8007");
await wsInitAwait();
if (process.argv.indexOf("-d") > -1) {
console.log("[START] -d startup with debug");
debug = true;
}
if (debug) {
process.argv.forEach(function (val, index, array) {
console.log(index + ": " + val);
});
}
if (process.argv.indexOf("-c") > -1) {
let index = process.argv.indexOf("-c");
let commandInput = process.argv[index + 1];
console.log("[START] -c command set: " + commandInput);
ws.send(JSON.stringify({ command: commandInput }));
}
if (process.argv.indexOf("-f") > -1) {
let index = process.argv.indexOf("-f");
let fadeValue = process.argv[index + 1];
console.log("[START] -f fade the glass with time: " + fadeValue);
ws.send(JSON.stringify({ command: "lcstart", value: fadeValue }));
}
if (process.argv.indexOf("-s") > -1) {
let index = process.argv.indexOf("-f");
let dimValue = process.argv[index + 1];
console.log("[START] -s set the dim to: " + dimValue);
ws.send(JSON.stringify({ command: "lcset", value: dimValue }));
}
if (process.argv.indexOf("-i") > -1) {
let index = process.argv.indexOf("-i");
let ledNo = process.argv[index + 1];
let ledValue = process.argv[index + 2];
console.log("[START] -l set led: " + ledNo + " to: " + ledValue);
ws.send(JSON.stringify({ command: "setled", number: ledNo, value: ledNo }));
}
};
init();
process.on("SIGINT", (_) => {
console.log("SIGINT");
process.exit(0);
});