| | |
| | | const mqttConfigPath = path.join(appPath, 'mqtt.json'); |
| | | const aliyunConfigPath = path.join(appPath, 'aliyun.json'); |
| | | const httpConfigPath = path.join(appPath, 'httpConfig.json'); |
| | | const homeConfigPath = path.join(appPath, 'homeConfig.json'); |
| | | |
| | | const mqttConfig = JSON.parse(fs.readFileSync(mqttConfigPath, 'utf8')); |
| | | const aliyunConfig=JSON.parse(fs.readFileSync(aliyunConfigPath, 'utf8')) |
| | | const aliyunConfig = JSON.parse(fs.readFileSync(aliyunConfigPath, 'utf8')); |
| | | const httpConfig = JSON.parse(fs.readFileSync(httpConfigPath, 'utf8')); |
| | | const homeConfig = JSON.parse(fs.readFileSync(homeConfigPath, 'utf8')); |
| | | |
| | | |
| | | console.log(aliyunConfig) |
| | |
| | | } |
| | | |
| | | handleDevice(socket) { |
| | | const deviceId = socket.remoteAddress + ':' + socket.remotePort; |
| | | // 处理 IPv6 映射 IPv4 地址 (::ffff:127.0.0.1 -> 127.0.0.1) |
| | | let remoteAddress = socket.remoteAddress; |
| | | if (remoteAddress.startsWith('::ffff:')) { |
| | | remoteAddress = remoteAddress.slice(7); // 移除 ::ffff: 前缀 |
| | | } |
| | | const deviceId = remoteAddress + ':' + socket.remotePort; |
| | | logger.info(`建立新连接: ${deviceId}`); |
| | | |
| | | const deviceInfo = { |
| | |
| | | const message = buffer.substring(startIdx, endIdx).trim(); |
| | | buffer = buffer.substring(endIdx + 2); // 移除已处理部分(含 \r\n) |
| | | |
| | | logger.info(`${deviceId} 接收到完整消息: ${message}`); |
| | | logger.info(`${deviceId} 接收到完整消息: q1261A00.00B00.00C00.00D0DSDASDSADSADSADSew5w5${message}nq4ds65g6m6767m`); |
| | | this.handleData(deviceId, message); |
| | | } |
| | | }); |
| | |
| | | if (deviceInfo.status === 'pending' || deviceInfo.status === 'invalid') { |
| | | // 握手阶段:在 'K' 和 'K0000' 之间切换(你的原始逻辑) |
| | | deviceInfo.lastSignal = deviceInfo.lastSignal === 'K' ? 'K0000' : 'K'; |
| | | logger.info(`重试发送 '${deviceInfo.lastSignal}' 给设备 ${deviceId}`); |
| | | logger.info(`重试发送 'abc${deviceInfo.lastSignal==='K'?'1':'0000'}abc' 给设备 ${deviceId}`); |
| | | this.sendKeepAliveToDevice(deviceId); |
| | | } |
| | | }, RETRY_INTERVAL_MS); |
| | |
| | | if (!deviceInfo || !deviceInfo.socket.writable) return; |
| | | |
| | | try { |
| | | deviceInfo.socket.write(`${deviceInfo.lastSignal}\r\n`); |
| | | logger.info(`发送信号 '${deviceInfo.lastSignal}' 给设备 ${deviceId}`); |
| | | deviceInfo.socket.write(`abc${deviceInfo.lastSignal}abc\r\n`); |
| | | logger.info(`发送信号 'abc${deviceInfo.lastSignal==='K'?'1':'0000'}abc' 给设备 ${deviceId}`); |
| | | } catch (err) { |
| | | logger.error(`发送信号失败 ${deviceId}:`, err.message); |
| | | this.removeDevice(deviceId); |
| | |
| | | deviceInfo.lastAck = Date.now(); |
| | | |
| | | try { |
| | | const masData = toModel(message); |
| | | const ipAddress = deviceId |
| | | const masData = toModel(message, ipAddress); |
| | | deviceInfo.iotDeviceNo = masData.n; |
| | | deviceInfo.masData = masData; |
| | | |
| | | |
| | | // ✅【新增】缓存数据到内存(按设备序号) |
| | | dataCache.setDeviceData(masData.n, masData); |
| | |
| | | manager.handleDevice(socket); |
| | | }); |
| | | |
| | | const PORT = process.env.PORT || 10961; |
| | | const PORT = homeConfig.socketPort || 10961; |
| | | server.listen(PORT, () => { |
| | | logger.info(`Socket 服务已启动,监听超级端口: ${PORT}`); |
| | | }); |