Skip to content

Commit

Permalink
Adding workflow actions (#53)
Browse files Browse the repository at this point in the history
* Adding workflow actions
* Update testdata
* Update dependencies
* Fix sonarcloud warnings (adding awaits)
* Bump version

---------

Co-authored-by: Thomas Bowman Mørch <[email protected]>
  • Loading branch information
tbowmo and Thomas Bowman Mørch authored May 15, 2023
1 parent 443ada4 commit a1ca3e1
Show file tree
Hide file tree
Showing 15 changed files with 787 additions and 650 deletions.
22 changes: 0 additions & 22 deletions .github/workflow/build.yml

This file was deleted.

38 changes: 38 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Node.js Package

on:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- run: yarn install --immutable
- run: yarn build
- run: yarn coverage

publish-gpr:
needs: build
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: https://registry.npmjs.org/
- run: yarn install --immutable
- run: yarn build
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.PUBLISH}}
25 changes: 25 additions & 0 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Sonarcloud
on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]
jobs:
sonarcloud:
name: SonarCloud
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Install dependencies
run: yarn
- name: Test and coverage
run: yarn coverage:ci
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ _lib
_nodes

dist/**/*
.test_output
.coverage
.nyc_output

src/**/*.js
Expand Down
9 changes: 1 addition & 8 deletions .nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@
"require": [
"ts-node/register"
],
"reporter": [
"text-summary",
"cobertura",
"json-summary",
"json",
"html"
],
"sourceMap": true,
"instrument": true,
"report-dir": "./.test_output/coverage"
"report-dir": "./.coverage"
}
1 change: 0 additions & 1 deletion .sonarcloud.properties

This file was deleted.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# node-red-contrib-mysensors
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=tbowmo_node-red-small-timer&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=tbowmo_node-red-small-timer)
![Sonar cloud test](https://github.com/tbowmo/node-red-small-timer/actions/workflows/sonarcloud.yml/badge.svg)

A node-RED [mysensors](http://www.mysensors.org) protocol decoder / encoder / wrapper package, including basic controller functionality
Contains a node to decode / encode mysensors serial protocol to / from node-red messages, and a node for adding mysensors specific data like sensor type, nodeid etc. which can then be sent to mysensors network
Expand Down
33 changes: 17 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
"author": {
"name": "Thomas Bowman Mørch"
},
"version": "4.0.4",
"version": "4.1.0",
"engines": {
"node": ">=16.0.0"
},
"scripts": {
"postcoverage": "nyc check-coverage --functions 50 --branches 50 --statements 90",
"build": "mkdir -p dist/nodes/ && cp -ar src/nodes/*.html dist/nodes/ && tsc ",
"prepublish": "npm run build",
"mocha": "mocha -r ts-node/register -r source-map-support/register",
"coverage": "nyc --clean --cache false mocha --forbid-only -r ts-node/register -r source-map-support/register 'src/**/*.spec.ts'",
"mocha": "TZ=Z mocha -r ts-node/register -r source-map-support/register",
"coverage": "TZ=Z nyc --clean --cache false --reporter=text-summary --reporter=html mocha --forbid-only -r ts-node/register -r source-map-support/register 'src/**/*.spec.ts'",
"format": "prettier --write src/**/*.ts",
"lint": "tsc --noEmit && eslint src/**/*.ts"
"lint": "tsc --noEmit && eslint src/**/*.ts",
"coverage:ci": "TZ=Z nyc --clean --cache false --reporter=lcov mocha --forbid-only -r ts-node/register -r source-map-support/register 'src/**/*.spec.ts'"
},
"husky": {
"hooks": {
Expand Down Expand Up @@ -60,27 +61,27 @@
"url": "git+https://github.com/tbowmo/node-red-contrib-mysensors.git"
},
"dependencies": {
"date-fns": "^2.29.3",
"date-fns": "^2.30.0",
"date-fns-tz": "^2.0.0"
},
"devDependencies": {
"@types/chai": "^4.3.4",
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.1",
"@types/node": "^18.13.0",
"@types/node-red": "1.2.1",
"@types/node-red-node-test-helper": "^0.2.2",
"@types/sinon": "^10.0.13",
"@typescript-eslint/eslint-plugin": "^5.52.0",
"@typescript-eslint/parser": "^5.52.0",
"@types/node": "^20.1.4",
"@types/node-red": "1.3.1",
"@types/node-red-node-test-helper": "^0.2.3",
"@types/sinon": "^10.0.15",
"@typescript-eslint/eslint-plugin": "^5.59.6",
"@typescript-eslint/parser": "^5.59.6",
"chai": "^4.3.7",
"eslint": "^8.34.0",
"eslint": "^8.40.0",
"husky": "^8.0.3",
"lint-staged": "^13.1.2",
"lint-staged": "^13.2.2",
"mocha": "^10.2.0",
"node-red": "^3.0.2",
"node-red-node-test-helper": "^0.3.0",
"node-red-node-test-helper": "^0.3.1",
"nyc": "^15.1.0",
"sinon": "^15.0.1",
"sinon": "^15.0.4",
"source-map-support": "^0.5.21",
"ts-node": "^10.9.1",
"tslint": "^6.1.3",
Expand Down
13 changes: 13 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sonar.projectKey=tbowmo_node-red-contrib-mysensors
sonar.organization=tbowmogithub

# This is the name and version displayed in the SonarCloud UI.
#sonar.projectName=node-red-small-timer
#sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
#sonar.sources=.

# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
sonar.javascript.lcov.reportPaths=./.coverage/lcov.info
4 changes: 2 additions & 2 deletions src/lib/decoder/mysensors-mqtt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ describe('MQTT decode / encode', () => {
_msgid: '',
ack: 0,
childSensorId: 2,
messageType: 6,
messageType: mysensor_command.C_PRESENTATION,
nodeId: 1,
payload: '100',
subType: 4,
topicRoot: 'mys-out',
};
const out = new MysensorsMqtt().encode(msg);
expect(out).to.include({topic: 'mys-out/1/2/6/0/4', payload: '100'});
expect(out).to.include({topic: 'mys-out/1/2/0/0/4', payload: '100'});
});
});
6 changes: 3 additions & 3 deletions src/lib/decoder/mysensors-serial.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ describe('Serial decode / encode', () => {
});

it('Encode to mysensors serial message', () => {
const msg: IStrongMysensorsMsg<mysensor_command.C_PRESENTATION> = {
const msg: IStrongMysensorsMsg<mysensor_command.C_REQ> = {
_msgid: 'id',
ack: 0,
childSensorId: 2,
messageType: 6,
messageType: mysensor_command.C_REQ,
nodeId: 1,
payload: '100',
subType: 4,
};
const out = decode.encode(msg);
expect(out).to.include({payload: '1;2;6;0;4;100'});
expect(out).to.include({payload: '1;2;2;0;4;100'});
});
});
40 changes: 36 additions & 4 deletions src/lib/mysensors-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { mysensor_command, mysensor_internal } from './mysensors-types';
describe('Controller test', () => {
const sinon = useSinonSandbox();

function setupTest() {
function setupTest(timeZone = 'CET') {
const storage = {
getFreeNodeId: sinon.stub().resolves(777),
getChild: sinon.stub().resolves(''),
Expand All @@ -28,7 +28,7 @@ describe('Controller test', () => {
storage,
true,
true,
'CET',
timeZone,
'M',
'mys-out',
),
Expand Down Expand Up @@ -137,9 +137,41 @@ describe('Controller test', () => {
expect(result).to.include(expected);
});

it('should decoded time request', async () => {
it('should decoded time request CET zone', async () => {
const { controller } = setupTest();

sinon.clock.setSystemTime(new Date('2023-01-01 00:00Z'));

const request: IMysensorsMsg = {
_msgid: '',
ack: 0,
childSensorId: 255,
messageType: mysensor_command.C_INTERNAL,
nodeId: 10,
payload: '',
subType: mysensor_internal.I_TIME,
};

const expected: IStrongMysensorsMsg<mysensor_command.C_INTERNAL> = {
_msgid: '',
payload: '1672534800',
ack: 0,
childSensorId: 255,
messageType: mysensor_command.C_INTERNAL,
nodeId: 10,
origin: 0,
subType: mysensor_internal.I_TIME,
};

const result = await controller.messageHandler(request);
expect(result).to.deep.equal(expected);
});

it('should decoded time request ZULU zone', async () => {
const { controller } = setupTest('Z');

sinon.clock.setSystemTime(new Date('2023-01-01 00:00Z'));

const request: IMysensorsMsg = {
_msgid: '',
ack: 0,
Expand All @@ -152,7 +184,7 @@ describe('Controller test', () => {

const expected: IStrongMysensorsMsg<mysensor_command.C_INTERNAL> = {
_msgid: '',
payload: '0',
payload: '1672531200',
ack: 0,
childSensorId: 255,
messageType: mysensor_command.C_INTERNAL,
Expand Down
9 changes: 5 additions & 4 deletions src/lib/mysensors-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class MysensorsController {
return;
}

this.updateHeard(msg);
await this.updateHeard(msg);

if (
msg.messageType === mysensor_command.C_PRESENTATION
Expand Down Expand Up @@ -85,6 +85,7 @@ export class MysensorsController {
): Promise<IStrongMysensorsMsg<mysensor_command.C_INTERNAL> | undefined> {
const msgCopy = {...msg};
msgCopy.subType = mysensor_internal.I_TIME;

if (this.timeResponse && msg.messageType) {
if (this.timeZone === 'Z') {
msgCopy.payload = Math.trunc(new Date().getTime() / 1000).toString();
Expand Down Expand Up @@ -116,7 +117,7 @@ export class MysensorsController {
const r = /TSF:MSG:READ,(\d+)-(\d+)-(\d+)/;
const m = r.exec(msg.payload as string);
if (m) {
this.database.setParent(Number(m[1]), Number(m[2]));
return this.database.setParent(Number(m[1]), Number(m[2]));
}
}

Expand All @@ -126,9 +127,9 @@ export class MysensorsController {

private async handleSketchVersion(msg: Readonly<IStrongMysensorsMsg<mysensor_command.C_INTERNAL>>): Promise<void> {
if (msg.subType === mysensor_internal.I_SKETCH_VERSION) {
this.database.sketchVersion(msg.nodeId, msg.payload as string);
return this.database.sketchVersion(msg.nodeId, msg.payload as string);
} else if (msg.subType === mysensor_internal.I_SKETCH_NAME) {
this.database.sketchName(msg.nodeId, msg.payload as string);
return this.database.sketchName(msg.nodeId, msg.payload as string);
}
}

Expand Down
Loading

0 comments on commit a1ca3e1

Please sign in to comment.