forked from dragino/dragino-end-node-decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_all_v3.js
More file actions
175 lines (142 loc) · 6.21 KB
/
convert_all_v3.js
File metadata and controls
175 lines (142 loc) · 6.21 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
const fs = require('fs');
const path = require('path');
const BASE_DIR = '/root/.openclaw/workspace/dragino-end-node-decoder';
// 已转换设备(跳过)
const SKIP_DEVICES = ['PB01', 'LHT52', 'LDS03A', 'CS01-LB', 'LHT65N', 'LTC2', 'LDS02', 'TrackerD', 'TS01-LB', 'UV254-LB', 'N720-SX'];
// 辅助函数模板
const HELPER_FUNCTIONS = `function getzf(c_num){
if(parseInt(c_num) < 10)
c_num = '0' + c_num;
return c_num;
}
function getMyDate(str){
var c_Date;
if(str > 9999999999)
c_Date = new Date(parseInt(str));
else
c_Date = new Date(parseInt(str) * 1000);
var c_Year = c_Date.getFullYear(),
c_Month = c_Date.getMonth()+1,
c_Day = c_Date.getDate(),
c_Hour = c_Date.getHours(),
c_Min = c_Date.getMinutes(),
c_Sen = c_Date.getSeconds();
var c_Time = c_Year +'-'+ getzf(c_Month) +'-'+ getzf(c_Day) +' '+ getzf(c_Hour) +':'+ getzf(c_Min) +':'+getzf(c_Sen);
return c_Time;
}
`;
// 获取设备目录
function getDeviceDirs() {
const dirs = fs.readdirSync(BASE_DIR).filter(item => {
const itemPath = path.join(BASE_DIR, item);
return fs.statSync(itemPath).isDirectory() && !SKIP_DEVICES.includes(item) && !item.startsWith('.');
});
return dirs;
}
// 查找解码器文件
function findDecoderFile(deviceDir) {
const files = fs.readdirSync(deviceDir);
// 排除已经转换的文件
const decoderFiles = files.filter(f => f.endsWith('.txt') && !f.includes('_decoder.txt'));
if (decoderFiles.length > 0) {
return path.join(deviceDir, decoderFiles[0]);
}
return null;
}
// 读取原始文件
function readOriginalFile(filePath) {
return fs.readFileSync(filePath, 'utf8');
}
// 转换解码器为统一格式
function convertToUnifiedFormat(originalContent, deviceName) {
let converted = originalContent;
// 检查是否已经是统一格式
if (converted.includes('function decodeUplink(input)')) {
return converted;
}
// 1. 移除旧的注释头(如果有)
converted = converted.replace(/^\/\/ Decode decodes.*?\n/gm, '');
converted = converted.replace(/^\/\/ - fPort.*?\n/gm, '');
converted = converted.replace(/^\/\/ - bytes.*?\n/gm, '');
converted = converted.replace(/^\/\/ - variables.*?\n/gm, '');
converted = converted.replace(/^\/\/ The function must return.*?\n/gm, '');
// 2. 替换函数签名 - 同时替换函数名和开括号
// 处理 function Decode(fPort, bytes, variables) {
converted = converted.replace(
/function\s+Decode\s*\(\s*fPort\s*,\s*bytes\s*,\s*variables\s*\)\s*\{/g,
'function decodeUplink(input) {\n var fPort = input.fPort;\n var bytes = input.bytes;\n var variables = input.variables;'
);
// 处理 function Decoder(bytes, port) {
converted = converted.replace(
/function\s+Decoder\s*\(\s*bytes\s*,\s*port\s*\)\s*\{/g,
'function decodeUplink(input) {\n var port = input.fPort;\n var bytes = input.bytes;'
);
// 处理 function Decoder(bytes, fPort) {
converted = converted.replace(
/function\s+Decoder\s*\(\s*bytes\s*,\s*fPort\s*\)\s*\{/g,
'function decodeUplink(input) {\n var fPort = input.fPort;\n var bytes = input.bytes;'
);
// 3. 处理 return 语句:将直接返回对象改为返回 { data: {...} }
// 将 return result; 改为 return { data: result };
converted = converted.replace(/return\s+result\s*;/g, 'return { data: result };');
// 4. 添加辅助函数(如果不存在)
if (!converted.includes('function getzf') && !converted.includes('function getMyDate')) {
converted = HELPER_FUNCTIONS + converted;
}
return converted;
}
// 保存转换后的文件
function saveConvertedFile(deviceDir, deviceName, content) {
const outputPath = path.join(deviceDir, `${deviceName}_decoder.txt`);
fs.writeFileSync(outputPath, content);
return outputPath;
}
// 主函数
function main() {
const deviceDirs = getDeviceDirs();
const convertedDevices = [];
const skippedDevices = [];
const errorDevices = [];
const alreadyUnifiedDevices = [];
console.log(`找到 ${deviceDirs.length} 个需要处理的设备目录`);
console.log(`跳过 ${SKIP_DEVICES.length} 个已转换设备:${SKIP_DEVICES.join(', ')}`);
console.log('---');
for (const deviceDir of deviceDirs) {
const devicePath = path.join(BASE_DIR, deviceDir);
const decoderFile = findDecoderFile(devicePath);
if (!decoderFile) {
console.log(`⚠️ ${deviceDir}: 未找到解码器文件`);
skippedDevices.push(deviceDir);
continue;
}
try {
const originalContent = readOriginalFile(decoderFile);
// 检查是否已经是统一格式
if (originalContent.includes('function decodeUplink(input)')) {
console.log(`⏭️ ${deviceDir}: 已是统一格式,复制为 _decoder.txt`);
saveConvertedFile(devicePath, deviceDir, originalContent);
alreadyUnifiedDevices.push(deviceDir);
convertedDevices.push(deviceDir);
continue;
}
let convertedContent = convertToUnifiedFormat(originalContent, deviceDir);
const outputPath = saveConvertedFile(devicePath, deviceDir, convertedContent);
console.log(`✅ ${deviceDir}: 已转换 -> ${path.basename(outputPath)}`);
convertedDevices.push(deviceDir);
} catch (error) {
console.log(`❌ ${deviceDir}: 转换失败 - ${error.message}`);
errorDevices.push(deviceDir);
}
}
console.log('---');
console.log(`转换完成:`);
console.log(` ✅ 成功:${convertedDevices.length} (其中 ${alreadyUnifiedDevices.length} 个已是统一格式)`);
console.log(` ⚠️ 跳过:${skippedDevices.length}`);
console.log(` ❌ 失败:${errorDevices.length}`);
if (convertedDevices.length > 0) {
console.log('\n已转换设备列表:');
convertedDevices.forEach(d => console.log(` - ${d}`));
}
return convertedDevices;
}
main();