From a85e047fa3f1a04aca4aeb079f06db69a96c8838 Mon Sep 17 00:00:00 2001
From: chenyc <501753378@qq.com>
Date: 星期三, 10 十二月 2025 23:33:25 +0800
Subject: [PATCH] gx完成
---
index.js | 32 +++++++++++++++++++++++++++-----
1 files changed, 27 insertions(+), 5 deletions(-)
diff --git a/index.js b/index.js
index 2b941f8..9c2bac5 100644
--- a/index.js
+++ b/index.js
@@ -9,9 +9,13 @@
const appPath = process.pkg ? path.dirname(process.execPath) : __dirname;
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)
@@ -26,6 +30,8 @@
const aliyunIot = require('aliyun-iot-device-sdk');
const { getAliyunDeviceSecret } = require('./api');
const toModel = require('./Strholp');
+const dataCache = require('./dataCache');
+const HttpServer = require('./httpServer');
// 初始化 MQTT(独立于阿里云)
initMqtt(mqttConfig);
@@ -63,7 +69,12 @@
}
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 = {
@@ -186,9 +197,15 @@
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);
+
// ✅【核心改动】收到数据立即发 MQTT(不管阿里云)
if (mqttConfig.enabled) {
const topic = `${mqttConfig.defaultTopicPrefix}/${masData.n}`;
@@ -317,7 +334,12 @@
manager.handleDevice(socket);
});
-const PORT = process.env.PORT || 10961;
+const PORT = homeConfig.socketPort || 10961;
server.listen(PORT, () => {
logger.info(`Socket 服务已启动,监听超级端口: ${PORT}`);
-});
\ No newline at end of file
+});
+
+// ========== 启动 HTTP 服务 ==========
+const HTTP_PORT = process.env.HTTP_PORT || httpConfig.port || 8080;
+const httpServer = new HttpServer(HTTP_PORT, httpConfig);
+httpServer.start();
\ No newline at end of file
--
Gitblit v1.8.0