@airiot/modbus 4.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +2 -0
- package/dist/package.json +31 -0
- package/package.json +31 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
(()=>{var e={100:(e,o,n)=>{const t=n(692);const r=n(611);const s=n(896);const c=n(928);const i="modbus";const a=c.join(process.cwd(),"airiot","lib","driver");const l="https://d.airiot.link";function getPackageVersion(){const e=n.ab+"package.json";const o=JSON.parse(s.readFileSync(n.ab+"package.json","utf-8"));return o.version}function getPlatformInfo(){const e=process.platform;const o=process.arch;let n=o;if(o==="x64"||o==="ia32"){n="x86_64"}else if(o==="arm64"||o==="aarch64"){n="aarch64"}else if(o==="loong64"||o==="loongarch64"){n="loong64"}let t=e;if(e==="win32"){t="windows"}return{platform:t,arch:n,originalPlatform:e}}function getDownloadUrl(e,o,n){const t=e==="windows"||e==="darwin";let r;let s;if(e==="windows"){r=`${i}-${e}-${o}`;s="zip"}else if(t){r=`${i}-${e}-${o}-binary`;s="tar.gz"}else if(e==="linux"&&o==="loong64"){r=`${i}-abi1-${e}-${o}`;s="tar.gz"}else{r=`${i}-${e}-${o}`;s="tar.gz"}return`${l}/${i}/v${n}/${r}.${s}`}function isBinaryVersion(e){return e.includes("windows")||e.includes("binary")}async function getAvailablePort(e,o="0.0.0.0"){const t=n(278);return new Promise((n=>{const r=t.createServer();r.listen(e,o,(()=>{const e=r.address().port;r.close((()=>n(e)))}));r.on("error",(()=>{n(getAvailablePort(e+1,o))}))}))}async function downloadFile(e,o,n){const c=o+".tmp";const i=new URL(e);const a=i.protocol==="https:"?t:r;return new Promise(((t,r)=>{const cleanup=()=>{try{if(s.existsSync(c))s.unlinkSync(c)}catch{}};const i=a.get(e,(i=>{if(i.statusCode===301||i.statusCode===302||i.statusCode===307||i.statusCode===308){return downloadFile(i.headers.location,o,n).then(t).catch(r)}if(i.statusCode!==200){cleanup();return r(new Error(`HTTP ${i.statusCode} downloading ${e}`))}const a=parseInt(i.headers["content-length"],10);let l=0;const u=s.createWriteStream(c);i.on("data",(e=>{l+=e.length;if(n)n(l,a)}));i.pipe(u);u.on("finish",(()=>{u.close();try{s.renameSync(c,o);t(o)}catch(e){cleanup();r(e)}}))}));i.on("error",(e=>{cleanup();r(e)}))}))}async function extractFile(e,o){if(e.endsWith(".zip")){const t=n(211);const r=new t(e);r.extractAllTo(o,true)}else if(e.endsWith(".tar.gz")){const t=n(218);await s.promises.mkdir(o,{recursive:true});await t.x({file:e,cwd:o})}else{throw new Error(`Unsupported archive format: ${e}`)}}async function generateConfig(e,o){const n=c.join(e,"etc");const t=c.join(n,"config.yaml");if(s.existsSync(t)){let e=s.readFileSync(t,"utf-8");if(e.includes("http:")){e=e.replace(/(http:[\s\S]*?port:\s*)\d+/,`$1${o}`)}else{const n=`http:\n host: 0.0.0.0\n port: ${o}\n\n`;e=n+e}e=e.replace(/driverGrpc:([\s\S]*?)\n\n/,"driverGrpc:\n\n");e=e.replace(/mq:[\s\S]*?\n\ndriver:/,`mq:\n type: local\n local:\n logPublish: true\n logConsume: true\n showPayload: false\n\ndriver:`);await s.promises.writeFile(t,e,"utf-8");return t}const r=`http:\n host: 0.0.0.0\n port: ${o}\n\ndriverGrpc:\n\nmq:\n type: local\n local:\n logPublish: true\n logConsume: true\n showPayload: false\n\ndriver:\n id: modbus\n name: Modbus-TCP\n timeout: 10\n idle: 30\n\npprof:\n enable: false\n port: 9002\n\nlog:\n level: 4\n`;await s.promises.mkdir(n,{recursive:true});await s.promises.writeFile(t,r,"utf-8");return t}e.exports={DRIVER_NAME:i,CACHE_DIR:a,getPackageVersion:getPackageVersion,getPlatformInfo:getPlatformInfo,getDownloadUrl:getDownloadUrl,isBinaryVersion:isBinaryVersion,getAvailablePort:getAvailablePort,downloadFile:downloadFile,extractFile:extractFile,generateConfig:generateConfig}},211:e=>{"use strict";e.exports=require("adm-zip")},317:e=>{"use strict";e.exports=require("child_process")},896:e=>{"use strict";e.exports=require("fs")},611:e=>{"use strict";e.exports=require("http")},692:e=>{"use strict";e.exports=require("https")},278:e=>{"use strict";e.exports=require("net")},928:e=>{"use strict";e.exports=require("path")},218:e=>{"use strict";e.exports=require("tar")}};var o={};function __nccwpck_require__(n){var t=o[n];if(t!==undefined){return t.exports}var r=o[n]={exports:{}};var s=true;try{e[n](r,r.exports,__nccwpck_require__);s=false}finally{if(s)delete o[n]}return r.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var n={};const{execSync:t}=__nccwpck_require__(317);const r=__nccwpck_require__(896);const s=__nccwpck_require__(928);const{DRIVER_NAME:c,CACHE_DIR:i,getPackageVersion:a,getPlatformInfo:l,getDownloadUrl:u,isBinaryVersion:d,getAvailablePort:f,downloadFile:p,extractFile:g,generateConfig:w}=__nccwpck_require__(100);const h=8080;function parseArgs(){const e=process.argv.slice(2);const o={dataJson:null,update:false};for(let n=0;n<e.length;n++){if(e[n]==="-d"&&n+1<e.length){o.dataJson=e[n+1];n++}else if(e[n]==="--update"||e[n]==="-u"){o.update=true}}return o}function getVersionedPaths(e){const o=s.join(i,c,e);const n=s.join(o,"archive");return{INSTALL_DIR:o,ARCHIVE_PATH:n}}function writeDataJson(e,o){try{const n=JSON.parse(o);const t=JSON.stringify(n,null,2);const c=s.join(e,"data.json");r.writeFileSync(c,t,"utf-8");console.log(`已写入 data.json: ${c}`);return c}catch(e){throw new Error(`无效的 JSON 格式: ${e.message}`)}}function showProgress(e,o){const n=o?Math.round(e/o*100):0;const t=(e/(1024*1024)).toFixed(1);const r=o?(o/(1024*1024)).toFixed(1):"?";process.stderr.write(`\r 下载中: ${t}MB / ${r}MB (${n}%)`)}function commandExists(e){try{const o=process.platform==="win32"?"where":"which";t(`${o} ${e}`,{stdio:"ignore"});return true}catch{return false}}async function runWithDocker(e,o,n){console.log("\n使用 Docker 运行驱动...");const r=findDockerImage(e);if(!r){throw new Error("未找到 Docker 镜像")}console.log(`加载 Docker 镜像: ${r}`);try{t(`docker load -i "${r}"`,{stdio:"inherit",cwd:e})}catch(e){throw new Error(`加载 Docker 镜像失败: ${e.message}`)}const i=a();const l=`airiot/${c}:v${i}`;const u=s.resolve(o);const d=["docker","run","-d","--name",`${c}-driver`,"--restart","unless-stopped","-p",`${n}:8080`,"-v",`${u}:/app/etc/config.yaml`,l].join(" ");console.log(`运行命令: ${d}`);t(d,{stdio:"inherit"});console.log("驱动已在 Docker 中启动")}async function runWithPm2(e){console.log("\n使用 PM2 运行驱动...");if(!commandExists("pm2")){console.error("错误: 未安装 PM2");console.error("请运行: npm install -g pm2");process.exit(1)}const o=findBinary(e);if(!o){throw new Error("未找到二进制文件")}if(process.platform!=="win32"){try{r.chmodSync(o,493)}catch(e){console.warn(`警告: 无法设置执行权限: ${e.message}`)}}const n=s.basename(o);const i=`pm2 start "${n}" --name ${c}-driver`;console.log(`运行命令: ${i}`);console.log(`工作目录: ${e}`);try{t(i,{stdio:"inherit",cwd:e});console.log("驱动已通过 PM2 启动")}catch(e){throw new Error(`PM2 启动失败: ${e.message}`)}}function findDockerImage(e){const o=r.readdirSync(e);return o.find((e=>e.endsWith(".tar.gz")&&!e.includes("binary")))}function findBinary(e){const o=r.readdirSync(e);const n=process.platform;const t=[c,`${c}.exe`,"modbus","modbus.exe"];for(const o of t){const n=s.join(e,o);if(r.existsSync(n)){return n}}for(const t of o){const o=s.join(e,t);try{if(r.statSync(o).isFile()){if(n==="win32"&&t.endsWith(".exe")){return o}else if(n!=="win32"&&!t.includes(".")){return o}}}catch{}}return null}async function main(){const{dataJson:e,update:o}=parseArgs();console.log(`AIRIOT Modbus 驱动安装程序`);console.log("=".repeat(40));const{platform:n,arch:t}=l();console.log(`检测到平台: ${n}-${t}`);const c=a();console.log(`驱动版本: ${c}`);const{INSTALL_DIR:i,ARCHIVE_PATH:m}=getVersionedPaths(c);console.log(`安装目录: ${i}`);const y=u(n,t,c);console.log(`下载地址: ${y}`);const $=y.split("/").pop();const _=s.join(m,$);if(r.existsSync(_)&&!o){console.log("\n使用已缓存的文件")}else if(r.existsSync(_)&&o){console.log("\n检测到 --update 参数,重新下载...");r.unlinkSync(_)}if(!r.existsSync(_)){r.mkdirSync(i,{recursive:true});r.mkdirSync(m,{recursive:true});console.log("\n开始下载...");try{await p(y,_,showProgress);console.error("\n下载完成!")}catch(e){console.error(`\n下载失败: ${e.message}`);process.exit(1)}}console.log("\n解压驱动文件...");const x=s.join(i,"extracted");r.mkdirSync(x,{recursive:true});try{await g(_,x);console.log("解压完成!")}catch(e){console.error(`解压失败: ${e.message}`);process.exit(1)}const k=r.readdirSync(x);let v=x;if(k.length===1){const e=s.join(x,k[0]);if(r.statSync(e).isDirectory()){v=e}}console.log("\n检测可用端口...");const P=await f(h);if(P!==h){console.log(`端口 ${h} 被占用,使用端口 ${P}`)}else{console.log(`使用端口 ${P}`)}console.log("\n生成配置文件...");const S=await w(v,P);console.log(`配置文件: ${S}`);if(e){console.log("\n写入数据文件...");writeDataJson(v,e)}const b=d($);if(b){console.log("\n这是二进制版本,将使用 PM2 运行");await runWithPm2(v)}else{console.log("\n这是 Docker 版本,将使用 Docker 运行");await runWithDocker(v,S,P)}console.log("\n"+"=".repeat(40));console.log("安装完成!");console.log(`驱动正在运行,端口: ${P}`)}main().catch((e=>{console.error("\n致命错误:",e.message);if(process.env.DEBUG){console.error(e.stack)}process.exit(1)}));module.exports=n})();
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@airiot/modbus",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "4.3.7",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"@airiot/modbus": "dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"keywords": ["modbus", "airiot", "driver", "iot"],
|
|
10
|
+
"author": "airiot",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"description": "AIRIOT Modbus 驱动安装程序 - 自动下载、配置和运行 Modbus 驱动",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"adm-zip": "^0.5.16",
|
|
15
|
+
"tar": "^6.2.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@vercel/ncc": "^0.38.1"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "ncc build bin/cli.js -o dist -m --external adm-zip --external tar",
|
|
22
|
+
"prepublishOnly": "npm run build"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"package.json"
|
|
30
|
+
]
|
|
31
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@airiot/modbus",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "4.3.7",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"@airiot/modbus": "dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"keywords": ["modbus", "airiot", "driver", "iot"],
|
|
10
|
+
"author": "airiot",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"description": "AIRIOT Modbus 驱动安装程序 - 自动下载、配置和运行 Modbus 驱动",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"adm-zip": "^0.5.16",
|
|
15
|
+
"tar": "^6.2.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@vercel/ncc": "^0.38.1"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "ncc build bin/cli.js -o dist -m --external adm-zip --external tar",
|
|
22
|
+
"prepublishOnly": "npm run build"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"package.json"
|
|
30
|
+
]
|
|
31
|
+
}
|