| Server IP : 3.96.16.70 / Your IP : 216.73.216.15 Web Server : Apache System : Linux ip-172-31-26-103.ca-central-1.compute.internal 6.1.163-186.299.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Feb 24 16:35:42 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.18 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/html/nsm.wondermakr.com/nintendo-slot-machine/node/ |
Upload File : |
const config = require('../config/config_node_app');
const http = require('http');
const { Server } = require('socket.io');
const { SerialPort } = require('serialport');
const server = http.createServer();
const io = new Server(server, {
cors: {
origin: config.corsURL
}
});
// Track connected ports
let armLightPort = null;
let vendPort = null;
// Track reconnection timers
let armLightReconnectTimer = null;
let vendReconnectTimer = null;
// Open serial port
function openPort(portInfo, label) {
const port = new SerialPort({ path: portInfo.path, baudRate: 9600 });
port.on('open', () => console.log(`${label} port opened on ${portInfo.path}`));
port.on('data', data => {
const msg = data.toString().trim();
console.log(`${label} →`, msg);
if (msg.includes('LEVER PULLED')) {
// This is an arm pull, so we notify the front end.
// The armLight controller also sends data after a light command is sent, which we want to ignore.
console.log ('Button pressed');
console.log (`${label}:data`, msg);
io.emit(`${label}:data`, msg);
}
});
port.on('error', err => {
console.error(`${label} port error:`, err.message);
scheduleReconnect(label);
});
port.on('close', () => {
console.log(`${label} port closed`);
scheduleReconnect(label);
});
return port;
}
// Try to find and open a specific device by vendor ID
async function findAndOpenDevice(vendorId, label) {
try {
const ports = await SerialPort.list();
const match = ports.find(
p => (p.vendorId || '').toLowerCase().replace(/^0x/, '') === vendorId
);
if (match) {
console.log(`Found ${label} controller on ${match.path}`);
return openPort(match, label);
} else {
console.log(`No ${label} controller detected.`);
return null;
}
} catch (err) {
console.error(`Error listing ports for ${label}:`, err);
return null;
}
}
// USB Reconnection
function scheduleReconnect(label) {
let timerRef;
let vendorId;
if (label === 'ArmLight') {
if (armLightReconnectTimer) return; // already trying
timerRef = 'armLightReconnectTimer';
vendorId = '2341';
} else if (label === 'Vend') {
if (vendReconnectTimer) return; // already trying
timerRef = 'vendReconnectTimer';
vendorId = '1a86';
} else return;
console.log(`Starting reconnection attempts for ${label}...`);
const tryReconnect = async () => {
const newPort = await findAndOpenDevice(vendorId, label);
if (newPort) {
console.log(`${label} reconnected successfully!`);
if (label === 'ArmLight') {
armLightPort = newPort;
clearInterval(armLightReconnectTimer);
armLightReconnectTimer = null;
} else {
vendPort = newPort;
clearInterval(vendReconnectTimer);
vendReconnectTimer = null;
}
}
};
// Try every 3 seconds until success
const interval = setInterval(tryReconnect, 3000);
if (label === 'ArmLight') armLightReconnectTimer = interval;
else vendReconnectTimer = interval;
}
// Socket.IO setup
io.on('connection', socket => {
console.log('Client connected:', socket.id);
socket.on('disconnect', () => {
console.log('Client disconnected:', socket.id);
});
socket.on('sendCommand', data => {
const { target, command } = data;
console.log(`Received command for ${target}: ${command}`);
let port = null;
if (target === 'vend') port = vendPort;
else if (target === 'armlight') port = armLightPort;
if (port && port.writable) {
port.write(command, err => {
if (err) console.error(`Error writing to ${target}:`, err.message);
else console.log(`Sent "${command}" to ${target}`);
});
} else {
console.warn(`Port for ${target} not available.`);
}
});
});
// Startup
server.listen(config.serverPORT, async () => {
console.log(`Socket.IO server running on port ${config.serverPORT}`);
armLightPort = await findAndOpenDevice(config.armLight_vendorID, 'ArmLight');
vendPort = await findAndOpenDevice(config.vend_vendorID, 'Vend');
});