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

Bump to v2.0.0 #40

Merged
merged 17 commits into from
Jul 24, 2023
Merged
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
8 changes: 2 additions & 6 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
module.exports = {
env: {
commonjs: true,
es6: true,
es2021: true,
node: true,
mocha: true,
},
extends: [
'airbnb-base',
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parserOptions: {
ecmaVersion: 2018,
ecmaVersion: 'latest',
},
rules: {
},
Expand Down
39 changes: 15 additions & 24 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,34 @@ name: buildx

on:
push:
branches:
branches:
- development
tags:

jobs:
buildx:
runs-on: ubuntu-latest
steps:
-
name: Docker login

steps:
- name: Docker login
run: echo '${{ secrets.DOCKERHUB_PASS }}' | docker login --username userbuilder --password-stdin
-
name: Checkout
- name: Checkout
uses: actions/checkout@v1
-
name: run
run: pwd
-
name: run
- name: run
run: pwd
- name: run
run: npm i
-
name: run
- name: run
run: npm test
-
name: run
run: ls -la
-
name: Set up Docker Buildx
- name: run
run: ls -la
- name: Set up Docker Buildx
id: buildx
uses: crazy-max/ghaction-docker-buildx@v1
uses: crazy-max/ghaction-docker-buildx@v3.3.1
with:
version: latest
-
name: Available platforms
- name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}
-
env:
- env:
TAG: staging
name: Run Buildx
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
-
name: Set up Docker Buildx
id: buildx
uses: crazy-max/ghaction-docker-buildx@v1
uses: crazy-max/ghaction-docker-buildx@v3.3.1
with:
version: latest
-
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/slack-notify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ jobs:
name: Slack Notification
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Slack Notification
uses: rtCamp/action-slack-notify@v2.0.0
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
- uses: actions/checkout@v2
- name: Slack Notification
uses: rtCamp/action-slack-notify@v2.2.0
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- support to currently stable Node.js version (v18.x)

### Changed
- bumped to `modbus-herdsman-converters` 2.0.7
- bumped dependencies `modbus-serial`, `lodash`, `git-last-commit`, `mkdir-recursive` `winston`
- bumped dev dependencies `mocha`, `nodemon`,`eslint`, `sinon`

### Removed
- compatibility with deprecated Node.js versions (v10.x,v12.x)

## [1.1.6] - 2023-01-12
### Changed
- modbus integration period to 30 seconds
- Modbus integration period to 30 seconds

## [1.1.4] - 2022-05-12
### Changed
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:12.16.2
FROM node:18.17.0

WORKDIR /app
COPY package.json .
Expand Down
1 change: 0 additions & 1 deletion lib/events/onMQTTMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module.exports = function onMQTTMessage(payload) {
switch (topic) {
case `${baseTopic}/configure/set`: {
const parsedMessage = JSON.parse(message);
console.log(parsedMessage);
const {
id,
model,
Expand Down
5 changes: 3 additions & 2 deletions lib/methods/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const handler = Handler.getInstance();
module.exports = async function start() {
const info = await utils.getModbus2mqttVersion();
logger.info(`Starting modbus2mqtt version ${info.version} (commit #${info.commitHash})`);

logger.debug('Debugging enabled');
// Start modbus
try {
await this.mqtt.connect();
Expand All @@ -20,7 +20,8 @@ module.exports = async function start() {
baudRate: modbus.baud_rate,
};
await this.modbus.connectRTUBuffered(modbus.port, options);

// set timeout, if slave did not reply back
this.modbus.setTimeout(1000);
const deviceIds = Object.keys(devices);
// eslint-disable-next-line no-restricted-syntax
for (const deviceId of deviceIds) {
Expand Down
28 changes: 13 additions & 15 deletions lib/modbus.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
const { setTimeout } = require('timers/promises');
const events = require('events');
const _ = require('lodash');
const modbusHerdsmanConverters = require('@instathings/modbus-herdsman-converters');

const logger = require('./util/logger');
const { default: ModbusRTU } = require('modbus-serial');
const delay = ms => new Promise(r => setTimeout(r, ms));

class Modbus extends events.EventEmitter {
supportedFCs = [3, 4];

constructor(mqtt, modbus, device) {
super();
this.mqtt = mqtt;
/** @type {ModbusRTU} */
this.modbus = modbus;
this.id = device.id;
this.model = device.model;
this.modbusId = device.modbus_id;
this.descriptor = modbusHerdsmanConverters.findByModbusModel(this.model);
if (this.descriptor === undefined) {
throw (Error(`Device ${device.id} has unknown model: ${this.model}`));
}
this.interval = process.env.INTERVAL || 30000;
}

// eslint-disable-next-line class-methods-use-this
sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

async start() {
try {
await this.poll();
Expand All @@ -34,7 +30,7 @@ class Modbus extends events.EventEmitter {
} finally {
if (!this.stop) {
process.nextTick(async () => {
await this.sleep(this.interval);
await setTimeout(this.interval);
this.start();
});
}
Expand All @@ -49,22 +45,24 @@ class Modbus extends events.EventEmitter {
if (!functionCodes) throw Error('function code must be provided for polling');
await this.modbus.setID(this.modbusId);
const result = {};
// eslint-disable-next-line no-restricted-syntax
for (const fc of functionCodes) {
const descriptorRegistersProperty = fc === 4 ? 'input' : 'keep';
const input = _.get(this.descriptor, `fromModbus.${descriptorRegistersProperty}`);
const keys = Object.keys(input);
console.log(`reading with FC${fc}`);
if (!this.supportedFCs.includes(fc)) {
console.error(`FC${fc} not supported. skipping to next FC...`);
// eslint-disable-next-line no-continue
continue;
}
let promise;
if (fc === 3) {
promise = this.modbus.readHoldingRegisters.bind(this.modbus)
promise = this.modbus.readHoldingRegisters.bind(this.modbus);
} else if (fc === 4) {
promise = this.modbus.readInputRegisters.bind(this.modbus)
promise = this.modbus.readInputRegisters.bind(this.modbus);
} else {
console.error(`FC${fc} not supported. skipping to next FC...`);
// eslint-disable-next-line no-continue
continue;
}
// eslint-disable-next-line
Expand All @@ -77,18 +75,18 @@ class Modbus extends events.EventEmitter {
// eslint-disable-next-line no-await-in-loop
value = await promise(address, registersCountToRead);
// eslint-disable-next-line no-await-in-loop
await delay(1000);
await setTimeout(1000);
const { post } = addressDescriptor;
const interpreted = registersCountToRead === 1 ? _.get(value, 'data[0]') : value.data;
const raw = _.get(value, 'buffer');
value = (post) ? post(interpreted, raw) : interpreted;
_.set(result, key, value);
} catch (err) {
logger.error(err);
logger.error(`While handling Modbus: ${err.toString()}`);
}
}
}

const topic = `${this.id}`;
const payload = JSON.stringify(result);
this.mqtt.publish(topic, payload);
Expand Down
2 changes: 1 addition & 1 deletion lib/mqtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class MQTT extends events.EventEmitter {
options = { qos: 0, retain: false, ...options };

if (!this.client || this.client.reconnecting) {
logger.error(`Not connected to MQTT server!`);
logger.error('Not connected to MQTT server!');
logger.error(`Cannot send message: topic: '${topic}', payload: '${payload}`);
return;
}
Expand Down
Loading
Loading