-
Notifications
You must be signed in to change notification settings - Fork 4
/
tcpserver.js
168 lines (141 loc) · 4.75 KB
/
tcpserver.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
console.log("Made by JohNan - https://github.com/JohNan/pyflichub-tcpclient")
const network = require('network');
const net = require('net');
const buttons = require('buttons');
const EOL = "\n";
const VERSION = "0.1.9";
// Configuration - start
const HOST = "0.0.0.0";
const PORT = 8124;
const EVENT_BUTTON = "button";
// Configuration - end
net.createServer(function (socket) {
function write(_payload) {
socket.write(JSON.stringify(_payload) + EOL)
}
function sendButtons() {
const _buttons = buttons.getButtons();
console.log(JSON.stringify(_buttons))
const payload = {
'command': 'buttons',
'data': _buttons
};
write(payload)
}
function sendNetworkInfo() {
const _network = network.getState();
console.log(JSON.stringify(_network))
const payload = {
'command': 'network',
'data': _network
};
write(payload)
}
function sendServerInfo() {
const payload = {
'command': 'server',
'data': {
'version': VERSION
}
};
write(payload)
}
console.log("Connection from " + socket.remoteAddress);
const buttonConnectedHandler = function (button) {
console.log('Button connected:' + button.bdaddr)
const payload = {
'event': 'buttonConnected',
'button': button.bdaddr,
'action': ''
}
write(payload)
};
const buttonReadyHandler = function (button) {
console.log('Button ready:' + button.bdaddr)
const payload = {
'event': 'buttonReady',
'button': button.bdaddr,
'action': ''
}
write(payload)
};
const buttonAddedHandler = function (button) {
console.log('Button added:' + button.bdaddr)
const payload = {
'event': 'buttonAdded',
'button': button.bdaddr,
'action': ''
}
write(payload)
};
const buttonDownHandler = function (button) {
console.log('Button clicked:' + button.bdaddr + ' - down')
const payload = {
'event': EVENT_BUTTON,
'button': button.bdaddr,
'action': 'down'
}
write(payload)
};
const buttonUpHandler = function (button) {
console.log('Button clicked:' + button.bdaddr + ' - up')
const payload = {
'event': EVENT_BUTTON,
'button': button.bdaddr,
'action': 'up'
}
write(payload)
};
const buttonSingleOrDoubleClickOrHoldHandler = function (button) {
const action = button.isSingleClick ? 'single' : button.isDoubleClick ? 'double' : 'hold';
console.log('Button clicked:' + button.bdaddr + ' - ' + action)
const payload = {
'event': EVENT_BUTTON,
'button': button.bdaddr,
'action': action
};
write(payload)
};
buttons.on('buttonSingleOrDoubleClickOrHold', buttonSingleOrDoubleClickOrHoldHandler);
buttons.on('buttonUp', buttonUpHandler);
buttons.on('buttonDown', buttonDownHandler);
buttons.on('buttonConnected', buttonConnectedHandler);
buttons.on('buttonReady', buttonReadyHandler);
buttons.on('buttonAdded', buttonAddedHandler);
socket.setEncoding("utf8");
socket.on('end', function () {
console.log('Client disconnected: ' + socket.remoteAddress);
buttons.removeListener('buttonUp', buttonUpHandler);
buttons.removeListener('buttonDown', buttonDownHandler);
buttons.removeListener('buttonSingleOrDoubleClickOrHold', buttonSingleOrDoubleClickOrHoldHandler);
buttons.removeListener('buttonConnected', buttonConnectedHandler);
buttons.removeListener('buttonReady', buttonReadyHandler);
buttons.removeListener('buttonAdded', buttonAddedHandler);
socket.destroy();
});
socket.on('data', function (data) {
data.trim().split(EOL).forEach(function (msg) {
console.log("Received message: " + msg)
switch (msg) {
case "buttons":
sendButtons();
break;
case "network":
sendNetworkInfo();
break;
case "server":
sendServerInfo();
break;
case "ping":
write("pong")
break;
default:
console.error("Unknown command: " + msg)
}
});
});
}).listen(PORT, HOST, function () {
console.log("Opened server on port: " + PORT);
});
console.log("The server should have started now!")
console.log("Waiting for connections...")