Skip to content

Commit

Permalink
v1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bugsounet committed Mar 8, 2024
2 parents 9721880 + 9a2bea2 commit ccb5f95
Show file tree
Hide file tree
Showing 9 changed files with 374 additions and 1,927 deletions.
64 changes: 0 additions & 64 deletions .eslintrc.js

This file was deleted.

18 changes: 18 additions & 0 deletions .github/workflows/esbuild.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "ESBuild Testing"

on: [pull_request]

jobs:
eslint:
name: Run esbuild
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install Dependencies
run: npm prune

- name: Run ESBuild
run: npm run test:minify
continue-on-error: false
6 changes: 3 additions & 3 deletions .github/workflows/eslint.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "ESLint test"
name: "ESLint Testing"

on: [pull_request]

Expand All @@ -8,11 +8,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install Dependencies
run: npm prune

- name: Run ESLint
run: npx eslint *.js installer/*.js components/*.js --config .eslintrc.js
run: npm run test
continue-on-error: false
3 changes: 1 addition & 2 deletions EXT-Pir.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ Module.register("EXT-Pir", {
requiresVersion: "2.25.0",
defaults: {
debug: false,
gpio: 21,
reverseValue: false
gpio: 21
},

start () {
Expand Down
31 changes: 31 additions & 0 deletions components/MotionSensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from gpiozero import MotionSensor
from signal import pause
import argparse, sys

parser = argparse.ArgumentParser(
description='Read MotionSensor state from GPIO for EXT-Pir',
epilog="Support: https://forum.bugsounet.fr ©bugsounet 2024"
)

def gpio_check(x):
x = int(x)
if x < 1 or x > 21:
raise argparse.ArgumentTypeError("GPIO must be between 1 and 21")
return x

parser.add_argument("-g", "--gpio", help="Define GPIO", type=gpio_check, required=True)

args = parser.parse_args(None if sys.argv[1:] else ['-h'])

GPIO = "GPIO" + str(args.gpio)

def detected():
print('Detected')

try:
pir = MotionSensor(GPIO)
pir.when_motion = detected
pause()
except Exception as e:
print("Error:", e)

54 changes: 33 additions & 21 deletions components/pirLib.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/*******************
* PIR library
* bugsounet ©02/24
* bugsounet ©03/24
*******************/

var log = (...args) => { /* do nothing */ };
const Gpio = require("onoff").Gpio;
const { PythonShell } = require("python-shell");

class PIR {
constructor (config, Tools) {
this.config = config;
this.callback = (...args) => Tools.sendSocketNotification(...args);
this.default = {
debug: this.config.debug,
gpio: 21,
reverseValue: false
gpio: 21
};
this.config = Object.assign({}, this.default, this.config);
if (this.config.debug) log = (...args) => { console.log("[PIR] [CORE]", ...args); };
Expand All @@ -25,31 +24,44 @@ class PIR {
start () {
if (this.running) return;
log("Start");
try {
this.pir = new Gpio(this.config.gpio, "in", "both");
this.callback("PIR_STARTED");
} catch (err) {
console.error(`[PIR] [CORE] ${err}`);
this.running = false;
return this.callback("PIR_ERROR", err.message);
}
let options = {
mode: "text",
scriptPath: __dirname,
pythonOptions: ["-u"],
args: [ "-g", this.config.gpio ]
};

this.pir = new PythonShell("MotionSensor.py", options);
this.callback("PIR_STARTED");

this.running = true;
this.pir.watch((err, value) => {
if (err) {
console.error(`[PIR] [CORE] ${err}`);
return this.callback("PIR_ERROR", err.message);
}
log(`Sensor read value: ${value}`);
if ((value === 1 && !this.config.reverseValue) || (value === 0 && this.config.reverseValue)) {
this.pir.on("message", (message) => {
// detect pir
if (message === "Detected") {
log("Detected presence");
this.callback("PIR_DETECTED");
log(`Detected presence (value: ${value})`);
} else {
console.error("[PIR] [CORE]", message);
this.callback("PIR_ERROR", message);
this.running = false;
}
});
this.pir.on("stderr", (stderr) => {
// handle stderr (a line of text from stderr)
if (this.config.debug) console.error("[PIR] [CORE]", stderr);
this.running = false;
});

this.pir.end((err,code,signal) => {
if (err) console.error("[PIR] [CORE] [PYTHON]",err);
console.warn(`[PIR] [CORE] [PYTHON] The exit code was: ${code}`);
console.warn(`[PIR] [CORE] [PYTHON] The exit signal was: ${signal}`);
});
}

stop () {
if (!this.running) return;
this.pir.unwatch();
this.pir.kill();
this.pir = null;
this.running = false;
this.callback("PIR_STOP");
Expand Down
89 changes: 89 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const globals = require("globals");
const { configs: eslintConfigs } = require("@eslint/js");
const eslintPluginImport = require("eslint-plugin-import");
const eslintPluginStylistic = require("@stylistic/eslint-plugin");

const config = [
{
files: ["**/*.js"],
languageOptions: {
globals: {
...globals.browser,
...globals.node,
config: true,
Log: true,
MM: true,
Module: true,
moment: true,
document: true,
windows: true,
configMerge: true
}
},
plugins: {
...eslintPluginStylistic.configs["all-flat"].plugins,
import: eslintPluginImport
},
rules: {
eqeqeq: "error",
"import/order": "error",
"import/extensions": [
"error",
"ignorePackages",
{
json: "always" // ignore json require (display EXT version and rev date)
}
],
"import/newline-after-import": "error",
"no-param-reassign": "error",
"no-prototype-builtins": "off",
"no-throw-literal": "error",
"no-unused-vars": "off",
"no-useless-return": "error",
"object-shorthand": ["error", "methods"],
"prefer-template": "error",
"@stylistic/array-element-newline": ["error", "consistent"],
"@stylistic/arrow-parens": ["error", "always"],
"@stylistic/brace-style": "off",
"@stylistic/comma-dangle": ["error", "never"],
"@stylistic/dot-location": ["error", "property"],
"@stylistic/function-call-argument-newline": ["error", "consistent"],
"@stylistic/function-paren-newline": ["error", "consistent"],
"@stylistic/implicit-arrow-linebreak": ["error", "beside"],
"@stylistic/max-statements-per-line": ["error", { max: 2 }],
"@stylistic/multiline-ternary": ["error", "always-multiline"],
"@stylistic/newline-per-chained-call": ["error", { ignoreChainWithDepth: 4 }],
"@stylistic/no-extra-parens": "off",
"@stylistic/no-tabs": "off",
"@stylistic/object-curly-spacing": ["error", "always"],
"@stylistic/object-property-newline": ["error", { allowAllPropertiesOnSameLine: true }],
"@stylistic/operator-linebreak": ["error", "before"],
"@stylistic/padded-blocks": "off",
"@stylistic/quote-props": ["error", "as-needed"],
"@stylistic/quotes": ["error", "double"],
"@stylistic/indent": ["error", 2], // indent 2 spaces
"@stylistic/semi": ["error", "always"],
"@stylistic/space-before-function-paren": ["error", "always"],
"@stylistic/spaced-comment": "off"
}
}
];

/*
* Set debug to true for testing purposes.
* Since some plugins have not yet been optimized for the flat config,
* we will be able to optimize this file in the future. It can be helpful
* to write the ESLint config to a file and compare it after changes.
*/
const debug = false;

if (debug === true) {
const FileSystem = require("fs");
FileSystem.writeFile("eslint-config-DEBUG.json", JSON.stringify(config, null, 2), (error) => {
if (error) {
throw error;
}
});
}

module.exports = config;

0 comments on commit ccb5f95

Please sign in to comment.