-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
291 lines (250 loc) · 7.52 KB
/
server.js
File metadata and controls
291 lines (250 loc) · 7.52 KB
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
/**
* PiAV Controller - Main Express Server
* Replaces Apache/PHP backend with Node.js
*/
const express = require('express');
const cors = require('cors');
const path = require('path');
const SerialDevice = require('./serialDevice');
const app = express();
const PORT = process.env.PORT || 80;
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Serve static files
app.use(express.static(path.join(__dirname, 'Style')));
// Initialize serial devices
// These device paths and configurations can be modified based on your setup
const devices = {
projector: null,
switcher: null
};
// Initialize devices on startup
async function initializeDevices() {
try {
// Try to initialize projector on /dev/ttyUSB1 (for power commands)
devices.projector = new SerialDevice('/dev/ttyUSB1', 9600);
// Try to initialize switcher on /dev/ttyUSB0 (for input switching, volume, mic)
devices.switcher = new SerialDevice('/dev/ttyUSB0', 9600);
console.log('Serial devices initialized (connections will be made on demand)');
} catch (err) {
console.error('Error initializing devices:', err.message);
}
}
/**
* Helper function to send a command to a device
*/
async function sendCommand(device, command, useResponse = true) {
try {
if (!device) {
throw new Error('Device not initialized');
}
// Open port if not already open
if (!device.isOpen) {
await device.open();
}
let result;
if (useResponse) {
result = await device.writeCommand(command);
} else {
await device.writeCommandNoResponse(command);
result = 'OK';
}
return result;
} catch (err) {
console.error(`Error sending command: ${err.message}`);
throw err;
}
}
// ============================================
// POWER CONTROL ENDPOINTS
// ============================================
/**
* Power On
*/
app.get('/api/power/on', async (req, res) => {
try {
await sendCommand(devices.projector, 'PWR ON', false);
res.json({ status: 'success', message: 'Projector powered on' });
} catch (err) {
res.status(500).json({ status: 'error', message: err.message });
}
});
/**
* Power Off
*/
app.get('/api/power/off', async (req, res) => {
try {
await sendCommand(devices.projector, 'PWR OFF', false);
res.json({ status: 'success', message: 'Projector powered off' });
} catch (err) {
res.status(500).json({ status: 'error', message: err.message });
}
});
// ============================================
// INPUT CONTROL ENDPOINTS
// ============================================
const inputCommands = {
1: '1!',
2: '2!',
3: '3!',
4: '4!',
5: '5!',
6: '6!',
7: '7!',
8: '8!'
};
app.get('/api/input/:number', async (req, res) => {
try {
const inputNum = parseInt(req.params.number);
const command = inputCommands[inputNum];
if (!command) {
return res.status(400).json({ status: 'error', message: 'Invalid input number' });
}
await sendCommand(devices.switcher, command, false);
res.json({ status: 'success', message: `Input ${inputNum} selected` });
} catch (err) {
res.status(500).json({ status: 'error', message: err.message });
}
});
// ============================================
// VOLUME CONTROL ENDPOINTS
// ============================================
const volumeCommands = {
0: '\x1BD1*-1000GRPM', // Mute
5: '\x1BD1*-800GRPM',
10: '\x1BD1*-600GRPM',
15: '\x1BD1*-400GRPM',
20: '\x1BD1*-200GRPM',
25: '\x1BD1*0GRPM',
30: '\x1BD1*200GRPM',
35: '\x1BD1*400GRPM',
40: '\x1BD1*600GRPM',
45: '\x1BD1*800GRPM',
50: '\x1BD1*1000GRPM',
55: '\x1BD1*1200GRPM',
60: '\x1BD1*1400GRPM',
65: '\x1BD1*1600GRPM',
70: '\x1BD1*1800GRPM',
75: '\x1BD1*2000GRPM',
80: '\x1BD1*2200GRPM',
85: '\x1BD1*2400GRPM',
90: '\x1BD1*2600GRPM',
95: '\x1BD1*2800GRPM',
100: '\x1BD1*3000GRPM'
};
app.get('/api/volume/:level', async (req, res) => {
try {
const level = parseInt(req.params.level);
const command = volumeCommands[level];
if (!command) {
return res.status(400).json({ status: 'error', message: 'Invalid volume level' });
}
await sendCommand(devices.switcher, command, false);
res.json({ status: 'success', message: `Volume set to ${level}` });
} catch (err) {
res.status(500).json({ status: 'error', message: err.message });
}
});
app.get('/api/volume/mute', async (req, res) => {
try {
await sendCommand(devices.switcher, volumeCommands[0], false);
res.json({ status: 'success', message: 'Volume muted' });
} catch (err) {
res.status(500).json({ status: 'error', message: err.message });
}
});
// Alias endpoints for backward compatibility
app.get('/api/volume/unmute', async (req, res) => {
try {
// Unmute by setting to a default volume level (e.g., 50)
await sendCommand(devices.switcher, volumeCommands[50], false);
res.json({ status: 'success', message: 'Volume unmuted' });
} catch (err) {
res.status(500).json({ status: 'error', message: err.message });
}
});
// ============================================
// MICROPHONE CONTROL ENDPOINTS
// ============================================
const micCommands = {
0: 'MIC_0',
5: 'MIC_5',
10: 'MIC_10',
15: 'MIC_15',
20: 'MIC_20',
25: 'MIC_25',
30: 'MIC_30',
35: 'MIC_35',
40: 'MIC_40',
45: 'MIC_45',
50: 'MIC_50',
55: 'MIC_55',
60: 'MIC_60',
mute: 'MIC_MUTE',
unmute: 'MIC_UNMUTE'
};
app.get('/api/mic/:level', async (req, res) => {
try {
const level = req.params.level;
const command = micCommands[level];
if (!command) {
return res.status(400).json({ status: 'error', message: 'Invalid mic level' });
}
await sendCommand(devices.switcher, command, false);
res.json({ status: 'success', message: `Mic set to ${level}` });
} catch (err) {
res.status(500).json({ status: 'error', message: err.message });
}
});
// ============================================
// HEALTH CHECK ENDPOINT
// ============================================
app.get('/api/health', (req, res) => {
res.json({ status: 'ok', message: 'Server is running' });
});
// ============================================
// SERVE HTML FILES
// ============================================
// Serve the projector control pages
app.get('/projector.html', (req, res) => {
res.sendFile(path.join(__dirname, 'Style', 'projector.html'));
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'Style', 'projector.html'));
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ status: 'error', message: 'Internal server error' });
});
// Initialize and start server
initializeDevices().then(() => {
app.listen(PORT, () => {
console.log(`PiAV Controller server running on port ${PORT}`);
console.log(`Access the controller at http://localhost/projector.html`);
});
}).catch(err => {
console.error('Failed to initialize devices:', err);
// Still start the server even if devices fail to initialize
app.listen(PORT, () => {
console.log(`PiAV Controller server running on port ${PORT} (devices may not be available)`);
});
});
// Graceful shutdown
process.on('SIGINT', async () => {
console.log('\nShutting down gracefully...');
try {
if (devices.projector && devices.projector.isOpen) {
await devices.projector.close();
}
if (devices.switcher && devices.switcher.isOpen) {
await devices.switcher.close();
}
} catch (err) {
console.error('Error closing serial ports:', err);
}
process.exit(0);
});
module.exports = app;