-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
148 lines (101 loc) · 4.05 KB
/
server.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
// var child = require('child_process');
import child from "child_process";
import WebSocket from "ws"
import { StreamCamera, Codec, SensorMode, Rotation } from "pi-camera-connect";
import * as fs from "fs";
const runApp = async () => {
const streamCamera = new StreamCamera({
codec: Codec.H264,
fps: 3,
sensorMode: SensorMode.Mode2,
rotation: Rotation.Rotate180
});
const videoStream = streamCamera.createStream();
//const writeStream = fs.createWriteStream("video-stream.h264");
// Pipe the video stream to our video file
//videoStream.pipe(writeStream);
await streamCamera.startCapture();
// We can also listen to data events as they arrive
videoStream.on("data", data => {
// console.log("New data", data);
for (const ws of connections) {
// console.log('send data ' + data.length)
//ws.send(data, { binary: true }, (error) => { if (error) console.error(error); });
ws.send(data, { binary: true });
}
});
videoStream.on("end", data => console.log("Video stream has ended"));
// Wait for 5 seconds
// await new Promise(resolve => setTimeout(() => resolve(), 5000));
//await streamCamera.stopCapture();
};
runApp();
const PORT = 8123;
const DEVICE = "PANASONIC"
const connections = []
async function startServer() {
const wss = new WebSocket.Server({ port: PORT });
console.log('Server ready on port ' + PORT);
wss.on('connection', (ws) => {
console.log('Socket connected. sending data...');
ws.on('message', (message) => {
console.log(`message ${message}`);
switch(message) {
case "Home": sendIRCommand("PANASONIC2","KEY_HOME"); break;
case "Escape": sendIRCommand(DEVICE,"KEY_ENTER"); break;
case "Backspace": sendIRCommand(DEVICE,"KEY_ENTER");break;
case "End": sendIRCommand(DEVICE,"KEY_EXIT");break;
case "`":sendIRCommand(DEVICE,"KEY_POWER");break;
case "PageUp": sendIRCommand(DEVICE,"KEY_CHANNELUP");break;
case "PageDown": sendIRCommand(DEVICE,"KEY_CHANNELDOWN");break;
case "ArrowUp": sendIRCommand(DEVICE,"KEY_UP");break;
case "ArrowDown": sendIRCommand(DEVICE,"KEY_DOWN");break;
case "ArrowLeft": sendIRCommand(DEVICE,"KEY_LEFT");break;
case "ArrowRight": sendIRCommand(DEVICE,"KEY_RIGHT");break;
case "Enter": sendIRCommand(DEVICE,"KEY_OK");break;
case "r": sendIRCommand(DEVICE,"KEY_R");break;
case "g": sendIRCommand(DEVICE,"KEY_G");break;
case "b": sendIRCommand(DEVICE,"KEY_B");break;
case "y": sendIRCommand(DEVICE,"KEY_Y");break;
case "[": sendIRCommand(DEVICE,"KEY_REWIND");break;
case "]": sendIRCommand(DEVICE,"KEY_FASTFORWARD");break;
case "p": sendIRCommand(DEVICE,"KEY_PAUSE");break;
case "s": sendIRCommand(DEVICE,"KEY_STOP");break;
}
});
ws.on('open', (error) => {
console.log('WebSocket open');
connections.push(ws);
});
ws.on('error', (error) => {
console.log('WebSocket error');
});
ws.on('close', (msg) => {
console.log('WebSocket close');
// Remove connection from array.
const index = connections.indexOf(ws);
if (index > -1) {
console.log(`Removed connection at index ${index}`);
connections.splice(index, 1);
}
});
connections.push(ws);
});
}
function sendIRCommand(device, key){
// irsend --device=/var/run/lirc/lircd-tx SEND_ONCE PANASONIC KEY_POWER
console.info(`SEND ${key} TO ${device}`);
var args = [
'--device=/var/run/lirc/lircd-tx',
'SEND_ONCE',
device,
key
]
// the avconv stream that inherits stderr
var irsend_process = child.spawn('irsend', args, {
stdio: ['ignore', 'pipe', 'inherit']
});
return irsend_process.stdout;
}
//sendIRCommand("PANASONIC","KEY_POWER");
startServer();