-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyvision.js
71 lines (62 loc) · 2.24 KB
/
keyvision.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
/* Copyright (C) 2017 Giancarlo Trevisan - All Rights Reserved
* You may use, distribute and modify this code under the
* terms of the MIT license.
*/
const
express = require("express"),
io = require("socket.io"),
fs = require("fs");
// Parse command line
let args = process.argv.slice(2),
hostname = lanIP(),
port = 8080,
layout = "qwerty-us";
for (let i = 0; i < args.length; ++i) {
if (args[i] === "-h" || args[i] === "--hostname")
hostname = args[++i];
else if (args[i] === "-p" || args[i] === "--port")
port = args[++i];
else if (args[i] === "-l" || args[i] === "--layout")
layout = args[++i];
else {
console.log(`Usage: node [options] server.js [-p|--port port_number] [-h|--hostname hostname][-l|--layout layout_name]`);
return;
}
}
let app = express();
app.use(express.static(__dirname));
let server = app.listen(port, hostname, () => console.log(`Running Programmable Visual Keyboard http://${hostname}:${port}`));
io.listen(server).sockets.on("connection", socket => {
console.log(`Requested "${layout}" on ${(new Date()).toLocaleString()}`);
fs.watch(`${__dirname}/.keyvisionrc`, (eventType, filename) => {
try {
let cmd = JSON.parse(fs.readFileSync(`${__dirname}/${filename}`).toString());
if (cmd.type === "layout" || cmd.layout) {
socket.emit("init", fs.readFileSync(`${__dirname}/keyboards/${cmd.layout}.json`).toString());
layout = cmd.layout;
console.log(`Requested "${layout}" on ${(new Date()).toLocaleString()}`);
} else
socket.emit(cmd.type, cmd.data);
console.log(`Requested "${cmd.type}: ${cmd.data}" on ${(new Date()).toLocaleString()}`);
} catch (ex) {
console.log(ex.message);
}
});
socket.emit("init", fs.readFileSync(`${__dirname}/keyboards/${layout}.json`).toString());
socket.on("message", (data) => {
// Write to USB HOST
fs.writeFileSync('/dev/hidg0', data); //, err => { if (err) console.log(err); });
});
});
function lanIP() {
var ifaces = require("os").networkInterfaces(),
address;
Object.keys(ifaces).forEach(ifname => {
ifaces[ifname].forEach(iface => {
if (typeof address !== "undefined" || iface.internal)
return;
address = iface.address;
});
});
return address;
}