费森4008s 网口通讯 ,原生串口透传网口
chenyc
2026-03-22 106a1256ccec6feef931d57923474180fa1a5ade
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
const axios = require('axios');
const Qs = require('qs');
const fs = require('fs');
const path = require('path');
 
function loadAliyunConfig() {
    const baseDir = (typeof process !== 'undefined' && process.pkg) ? path.dirname(process.execPath) : __dirname;
    const candidates = [
        { path: path.join(baseDir, 'aliyun-config.json'), source: 'exedir' },
        { path: path.join(__dirname, 'aliyun-config.json'), source: 'srcdir' }
    ];
    for (const c of candidates) {
        try {
            const raw = fs.readFileSync(c.path, 'utf8');
            const obj = JSON.parse(raw);
            // 默认值兜底
            if (obj.apiBaseUrl === undefined) obj.apiBaseUrl = 'https://things.icoldchain.cn/';
            if (obj.secretApiPath === undefined) obj.secretApiPath = 'device/info/getAliyunDeviceSecret';
            if (obj.region === undefined) obj.region = 'cn-shanghai';
            if (obj.productKeyOverride === undefined) obj.productKeyOverride = '';
            if (obj.printPayload === undefined) obj.printPayload = true;
            obj.__meta = { source: c.source, path: c.path };
            return obj;
        } catch (_) { /* continue */ }
    }
    const def = {
        apiBaseUrl: 'https://things.icoldchain.cn/',
        secretApiPath: 'device/info/getAliyunDeviceSecret',
        region: 'cn-shanghai',
        productKeyOverride: '',
        printPayload: true
    };
    def.__meta = { source: 'default', path: null };
    return def;
}
const aliyunCfg = loadAliyunConfig();
 
function getAliyunConfig() {
    return aliyunCfg;
}
 
function getAliyunDeviceSecret(url, deviceName) {
    var serve = axios({
        url: url,
        method: 'post',
        baseURL: aliyunCfg.apiBaseUrl || 'https://things.icoldchain.cn/',
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
        },
        data: Qs.stringify({
            isAutoRegister: 1,
            deviceName: deviceName
        })
    })
    return serve;
}
function getDevices(url, userClient, productCode) {
    var serve = axios({
        url: url,
        method: 'post',
        baseURL: aliyunCfg.apiBaseUrl || 'https://things.icoldchain.cn/',
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
        },
        data: Qs.stringify({
            client_code: userClient,
            product_code: productCode
        })
    })
    return serve;
}
 
function loadMqttConfig() {
    const baseDir = (typeof process !== 'undefined' && process.pkg) ? path.dirname(process.execPath) : __dirname;
    const candidates = [
        { path: path.join(baseDir, 'mqtt-config.json'), source: 'exedir' },
        { path: path.join(__dirname, 'mqtt-config.json'), source: 'srcdir' }
    ];
    for (const c of candidates) {
        try {
            const raw = fs.readFileSync(c.path, 'utf8');
            const obj = JSON.parse(raw);
            // 默认值兜底
            if (obj.enabled === undefined) obj.enabled = false;
            if (obj.brokerUrl === undefined) obj.brokerUrl = 'mqtt.ihemodialysis.com';
            if (obj.port === undefined) obj.port = 1883;
            if (obj.username === undefined) obj.username = '';
            if (obj.password === undefined) obj.password = '';
            if (obj.reconnectPeriod === undefined) obj.reconnectPeriod = 5000;
            if (obj.defaultTopicPrefix === undefined) obj.defaultTopicPrefix = 'touxiji';
            obj.__meta = { source: c.source, path: c.path };
            return obj;
        } catch (_) { /* continue */ }
    }
    const def = {
        enabled: false,
        brokerUrl: 'mqtt.ihemodialysis.com',
        port: 1883,
        username: '',
        password: '',
        reconnectPeriod: 5000,
        defaultTopicPrefix: 'touxiji'
    };
    def.__meta = { source: 'default', path: null };
    return def;
}
const mqttCfg = loadMqttConfig();
 
function getMqttConfig() {
    return mqttCfg;
}
 
module.exports = { getAliyunDeviceSecret, getDevices, getAliyunConfig, getMqttConfig }