Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up Smart Start #71

Open
wants to merge 3 commits into
base: 4.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Release Workflow

on:
release:
types: [published]

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [18]
include:
- os: ubuntu-latest
output-path: build/dist/server
output-name: server.psi.ubuntu
- os: windows-latest
output-path: build/dist/server.exe
output-name: server.psi.win
- os: macos-latest
output-path: build/dist/server
output-name: server.psi.macos

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm install

- name: Run script from package.json
run: |
cd PSI
npm run build

- name: Upload build artifact
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ${{ matrix.output-path }}
asset_name: ${{ matrix.output-name }}
asset_content_type: application/octet-stream
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# Node Stuff
**/node_modules/*
**/dist/*
**/build/*

# Yarn config and stuff
**/.yarn/*
Expand All @@ -21,3 +21,4 @@

PSI/server.js
PSI/package-lock.json
package-lock.json
76 changes: 76 additions & 0 deletions PSI/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module.exports = {
extends: ['eslint:recommended'],
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
allowImportExportEverywhere: true
},
env: {
node: true,
browser: true,
es2021: true
},
rules: {
'@typescript-eslint/no-this-alias': 0,
'@typescript-eslint/no-non-null-assertion': 0,
quotes: ['error', 'single', { avoidEscape: true }],
'prefer-const': 2,
'constructor-super': 2,
'for-direction': 2,
'getter-return': 2,
'no-async-promise-executor': 0,
'no-class-assign': 2,
'no-compare-neg-zero': 2,
'no-cond-assign': 2,
'no-const-assign': 2,
'no-constant-condition': 2,
'no-control-regex': 2,
'no-debugger': 2,
'no-delete-var': 2,
'no-dupe-args': 2,
'no-dupe-class-members': 2,
'no-dupe-else-if': 2,
'no-dupe-keys': 2,
'no-duplicate-case': 2,
'no-empty': 2,
'no-empty-character-class': 2,
'no-empty-pattern': 2,
'no-ex-assign': 2,
'no-extra-boolean-cast': 2,
'no-extra-semi': 2,
'no-fallthrough': 2,
'no-func-assign': 2,
'no-global-assign': 2,
'no-import-assign': 2,
'no-inner-declarations': 2,
'no-invalid-regexp': 2,
'no-irregular-whitespace': 2,
'no-misleading-character-class': 2,
'no-mixed-spaces-and-tabs': 0,
'no-new-symbol': 2,
'no-obj-calls': 2,
'no-octal': 2,
'no-prototype-builtins': 2,
'no-redeclare': 2,
'no-regex-spaces': 2,
'no-self-assign': 2,
'no-setter-return': 2,
'no-shadow-restricted-names': 2,
'no-sparse-arrays': 2,
'no-this-before-super': 2,
'no-undef': 2,
'no-unexpected-multiline': 2,
'no-unreachable': 2,
'no-unsafe-finally': 2,
'no-unsafe-negation': 2,
'no-unused-labels': 2,
'no-unused-vars': 1,
'no-useless-catch': 2,
'no-useless-escape': 2,
'no-with': 2,
'require-yield': 2,
'use-isnan': 2,
'valid-typeof': 2,
'no-case-declarations': 2
}
};
11 changes: 11 additions & 0 deletions PSI/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
arrowParens: 'always',
printWidth: 120,
semi: true,
singleQuote: true,
tabWidth: 2,
trailingComma: 'none',
useTabs: true,
endOfLine: 'lf',
bracketSpacing: true
};
104 changes: 104 additions & 0 deletions PSI/esb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const esbuild = require('esbuild');
const { cp, readFile, writeFile } = require('fs/promises');
const { exists } = require('fs-extra');
const { join } = require('path');
const outputDir = 'build';
const outfile = `${outputDir}/server.js`;

const externals = [
'@serialport/bindings-cpp/prebuilds',
'zwave-js/package.json',
'@zwave-js/config/package.json',
'@zwave-js/config/config',
'@zwave-js/config/build'
];

const cleanPkgJson = (json) => {
delete json.devDependencies;
delete json.dependencies;
return json;
};

const patchPkgJson = async (path) => {
const pkgJsonPath = join(outputDir, path, 'package.json');
const pkgJson = require('./' + pkgJsonPath);
cleanPkgJson(pkgJson);
delete pkgJson.scripts;
delete pkgJson.exports;
await writeFile(pkgJsonPath, JSON.stringify(pkgJson, null, 2));
};

const nativeNodeModulesPlugin = {
name: 'native-node-modules',
setup(build) {
build.onResolve({ filter: /\.node$/, namespace: 'file' }, (args) => ({
path: require.resolve(args.path, { paths: [args.resolveDir] }),
namespace: 'node-file'
}));

build.onLoad({ filter: /.*/, namespace: 'node-file' }, (args) => ({
contents: `
import path from ${JSON.stringify(args.path)}
try { module.exports = require(path) }
catch {}
`
}));

build.onResolve({ filter: /\.node$/, namespace: 'node-file' }, (args) => ({
path: args.path,
namespace: 'file'
}));

const opts = build.initialOptions;
opts.loader = opts.loader || {};
opts.loader['.node'] = 'file';
}
};

const run = async () => {
const config = {
entryPoints: ['./server_source.js'],
plugins: [nativeNodeModulesPlugin],
bundle: true,
platform: 'node',
target: 'node18',
outfile,
external: externals
};
await esbuild.build(config);

const patchedServer = (await readFile(outfile, 'utf-8'))
.replace(/__dirname, "\.\.\/"/g, '__dirname, "./node_modules/@serialport/bindings-cpp"')
.replace('__dirname, "../package.json"', '__dirname, "./node_modules/@zwave-js/config/package.json"')
.replace('__dirname, "../config"', '__dirname, "./node_modules/@zwave-js/config/config"');

await writeFile(outfile, patchedServer);

for (const ext of externals) {
const path = ext.startsWith('./') ? ext : `node_modules/${ext}`;
if (await exists(path)) {
await cp(path, `${outputDir}/${path}`, { recursive: true });
}
}

const pkgJson = require('./package.json');
cleanPkgJson(pkgJson);
pkgJson.scripts = {
start: 'node server.js'
};

pkgJson.bin = 'server.js';
pkgJson.pkg = {
assets: ['node_modules/**']
};

await writeFile(`${outputDir}/package.json`, JSON.stringify(pkgJson, null, 2));

await patchPkgJson('node_modules/@zwave-js/config');
await patchPkgJson('node_modules/zwave-js');
};

run().catch((err) => {
console.error(err);
process.exit(1);
});
45 changes: 23 additions & 22 deletions PSI/package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
{
"version": "4.0.0",
"name": "server",
"bin": "./server.js",
"dependencies": {
"@zwave-js/server": "1.35.0",
"zwave-js": "12.5.6"
},
"devDependencies": {
"@yao-pkg/pkg": "^5.11.5",
"esbuild": "^0.20.2"
},
"scripts": {
"build": "npm run do_esbuild && npm run do_pkgbuild",
"do_esbuild": "esbuild server_source.js --bundle --outfile=server.js --platform=node --target=node18 --external:@serialport/* --external:@zwave-js/config --external:zwave-js/package.json",
"do_pkgbuild": "pkg . --compress gzip -t host --targets node18"
},
"pkg": {
"assets": "./node_modules/@serialport/bindings-cpp/prebuilds/**/*",
"outputPath": "dist"
}
}
{
"version": "4.0.0",
"name": "server",
"bin": "./server.js",
"dependencies": {
"@zwave-js/server": "1.35.0",
"zwave-js": "12.5.6"
},
"devDependencies": {
"@yao-pkg/pkg": "^5.11.5",
"esbuild": "^0.21.5",
"eslint": "^8.57.0",
"prettier": "^3.3.2"
},
"scripts": {
"build": "npm run do_esbuild && npm run do_pkgbuild",
"do_esbuild": "node esb.js",
"do_pkgbuild": "pkg ./build/package.json --compress gzip -t host --targets node18 --output ./build/dist/server",
"test": "export WS_PORT=7070 SERIAL_PORT=/dev/tty.usbmodem214301 CONFIG=$(<./test_config.json) && ./build/dist/server",
"lint": "eslint --ext .js .",
"lint:fix": "eslint --fix --ext .js . && prettier -w ."
}
}
67 changes: 31 additions & 36 deletions PSI/server_source.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Driver } = require("zwave-js");
const { ZwavejsServer } = require("@zwave-js/server");
const { Driver } = require('zwave-js');
const { ZwavejsServer } = require('@zwave-js/server');

const serialPort = process.env.SERIAL_PORT;
const wsPort = parseInt(process.env.WS_PORT);
Expand All @@ -8,47 +8,42 @@ let ServerStarted = false;
let DriverStarted = false;

if (driverOptions.securityKeys) {
for (const key of Object.keys(driverOptions.securityKeys)) {
driverOptions.securityKeys[key] = Buffer.from(
driverOptions.securityKeys[key],
"hex"
);
}
for (const key of Object.keys(driverOptions.securityKeys)) {
driverOptions.securityKeys[key] = Buffer.from(driverOptions.securityKeys[key], 'hex');
}
}

if (driverOptions.securityKeysLongRange) {
for (const key of Object.keys(driverOptions.securityKeysLongRange)) {
driverOptions.securityKeysLongRange[key] = Buffer.from(
driverOptions.securityKeysLongRange[key],
"hex"
);
}
for (const key of Object.keys(driverOptions.securityKeysLongRange)) {
driverOptions.securityKeysLongRange[key] = Buffer.from(driverOptions.securityKeysLongRange[key], 'hex');
}
}

const driver = new Driver(serialPort, driverOptions);
const server = new ZwavejsServer(driver, { port: wsPort, host: "localhost" });
server.on("listening",() =>{
ServerStarted = true;
})
driver.on("error", (e) => {});
const server = new ZwavejsServer(driver, { port: wsPort, host: 'localhost' });
server.on('listening', () => {
ServerStarted = true;
});
driver.on('error', (e) => {});

driver.on("driver ready", () => {
server.start();
driver.on('driver ready', () => {
server.start();
});

driver.start()
.then(() =>{
DriverStarted = true;
process.stdin.on("data",HandleInput)
})
.catch((e) => {
process.stderr.write("1\n");
})
driver
.start()
.then(() => {
DriverStarted = true;
process.stdin.on('data', HandleInput);
})
.catch((e) => {
process.stderr.write('1\n');
});

const HandleInput = async (Data) =>{
if(Data.toString().trim() === "KILL"){
if(ServerStarted) await server.destroy();
if(DriverStarted) await driver.destroy();
process.exit(0);
}
}
const HandleInput = async (Data) => {
if (Data.toString().trim() === 'KILL') {
if (ServerStarted) await server.destroy();
if (DriverStarted) await driver.destroy();
process.exit(0);
}
};
5 changes: 5 additions & 0 deletions PSI/test_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"storage": {
"cacheDir": "/Users/marcusdavies/Documents/GitHub/TEST"
}
}
Loading