北斗短报文通信服务平台

This commit is contained in:
2026-03-27 17:51:58 +08:00
commit 90a8407924
103 changed files with 2714 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
// 计算数组中十六进制数据的总字节数,并在最终结果中加上时间信息的4字节长度
module.exports = (arr) => {
let length = 0;
arr.forEach(element => {
length = length + (element.length / 2);
});
return length + 4;
};

View File

@@ -0,0 +1,8 @@
// 将数组中的所有元素按下标顺序拼接成字符串
module.exports = (arr) => {
let total = '';
arr.forEach(element => {
total = total + element;
});
return total;
};

View File

@@ -0,0 +1,10 @@
// 截取数组元素,组成新数组并返回
module.exports = (arr, start, end) => {
let newArr = [];
arr.forEach((element, index) => {
if (index >= start && index <= end) {
newArr.push(element);
}
});
return newArr;
};

View File

@@ -0,0 +1,10 @@
// 截取数组元素,拼接成字符串并返回
module.exports = (arr, start, end) => {
let newString = '';
arr.forEach((element, index) => {
if (index >= start && index <= end) {
newString = newString + element;
}
});
return newString;
};

View File

@@ -0,0 +1,13 @@
// 返回时间对象
module.exports = () => {
let now = new Date(),
times = {
year: now.getFullYear(),
month: now.getMonth() + 1,
day: now.getDate(),
hour: now.getHours(),
minute: now.getMinutes(),
second: now.getSeconds()
};
return times;
};

View File

@@ -0,0 +1,15 @@
// 十六进制转二进制
module.exports = (hex) => {
let hexChars = '0123456789ABCDEF';
let binaryString = '';
for (let i = 0; i < hex.length; i++) {
let hexIndex = hexChars.indexOf(hex[i].toUpperCase());
let binaryDigit = hexIndex.toString(2);
// 确保每个十六进制数字转换为四位二进制数字
while (binaryDigit.length < 4) {
binaryDigit = '0' + binaryDigit;
}
binaryString += binaryDigit;
}
return binaryString;
};

View File

@@ -0,0 +1,8 @@
module.exports = {
// 数据包最后固定十六进制串作为TCP流的分隔识别符
separateIdentifiers: '7878787888888888',
// 心跳检测信息固定格式
heart: 'e5bf83e8b7b37878787888888888',
// 包序号,从0开始,随程序运行变大
packageNumber: 0,
};

View File

@@ -0,0 +1,72 @@
// 错误日志
const fs = require('fs');
const os = require('os');
const path = require('path');
const dirCheck = (dirPath) => {
// 检查文件夹是否存在
fs.access(dirPath, fs.constants.F_OK, (err) => {
if (err) {
// 文件夹不存在
console.log('文件夹不存在,正在创建...');
fs.mkdir(dirPath, { recursive: true }, (err) => {
if (err) throw err;
console.log('文件夹已创建');
});
} else {
// 文件夹存在
console.log('文件夹已存在');
}
});
}
module.exports = (filePath, msg) => {
// 获取时间
let now = new Date(),
times = {
year: now.getFullYear(),
month: now.getMonth() + 1,
day: now.getDate(),
hour: now.getHours(),
minute: now.getMinutes(),
second: now.getSeconds()
};
// 获取操作系统类型
const type = os.type();
console.log(`操作系统类型: ${type}`);
filePath = path.join(__dirname, filePath);
dirCheck(filePath);
const yearPath = filePath + '/' + times.year;
dirCheck(yearPath);
const monthPath = yearPath + '/' + times.month;
dirCheck(monthPath);
const dayPath = monthPath + '/' + times.day + '.txt';
const logTime = '[' + times.year + '-' + times.month + '-' + times.day + '>>>' + times.hour + ':' + times.minute + ':' + times.second + ']' + '>>>';
let crlf;
// 判断操作系统类型,设置换行符
if (type === 'Windows_NT') {
crlf = '\n';
} else {
crlf = '\r\n';
}
// 读取文件内容
fs.readFile(dayPath, 'utf8', (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
// 文件不存在,直接写入新内容(这里实际上还是使用了覆盖的方式)
fs.writeFile(dayPath, logTime + msg + crlf, (err) => {
if (err) throw err;
console.log('文件已创建并写入新内容');
});
} else {
throw err;
}
} else {
// 文件存在,追加新内容
fs.appendFile(dayPath, logTime + msg + crlf, (err) => {
if (err) throw err;
console.log('新内容已追加到文件');
});
}
});
};

View File

@@ -0,0 +1,8 @@
// 判断十六进制数据的长度,若长度不满足条件,则在左侧补0,直至长度满足条件
module.exports = (length, data) => {
let item = data.toString(16);
for (let i = 0; i < (length * 2) - data.toString(16).length; i++) {
item = '0' + item;
}
return item;
};