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 packages and a bit of cleanup #57

Open
wants to merge 1 commit into
base: master
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
37 changes: 18 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author": {
"name": "Thomas Bowman Mørch"
},
"version": "4.2.0",
"version": "4.3.0",
"engines": {
"node": ">=16.0.0"
},
Expand Down Expand Up @@ -65,28 +65,27 @@
"date-fns-tz": "^2.0.0"
},
"devDependencies": {
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.1",
"@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",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-import-newlines": "^1.3.1",
"chai": "^4.3.7",
"eslint": "^8.40.0",
"@types/chai": "^4.3.6",
"@types/mocha": "^10.0.2",
"@types/node": "^20.8.0",
"@types/node-red": "1.3.2",
"@types/node-red-node-test-helper": "^0.3.1",
"@types/sinon": "^10.0.17",
"@typescript-eslint/eslint-plugin": "^6.7.3",
"@typescript-eslint/parser": "^6.7.3",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-import-newlines": "^1.3.4",
"chai": "^4.3.10",
"eslint": "^8.50.0",
"husky": "^8.0.3",
"lint-staged": "^13.2.2",
"lint-staged": "^14.0.1",
"mocha": "^10.2.0",
"node-red": "^3.0.2",
"node-red-node-test-helper": "^0.3.1",
"node-red": "^3.1.0",
"node-red-node-test-helper": "^0.3.2",
"nyc": "^15.1.0",
"sinon": "^15.0.4",
"sinon": "^16.0.0",
"source-map-support": "^0.5.21",
"ts-node": "^10.9.1",
"tslint": "^6.1.3",
"typescript": "*"
"typescript": "5.2.2"
}
}
14 changes: 7 additions & 7 deletions src/lib/decoder/auto-decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import {
MsgOrigin,
MysensorsCommand,
validateStrongMysensorsMsg,
} from '../mysensors-msg';
import { MysensorsMqtt } from './mysensors-mqtt';
import { MysensorsSerial } from './mysensors-serial';
} from '../mysensors-msg'
import { MysensorsMqtt } from './mysensors-mqtt'
import { MysensorsSerial } from './mysensors-serial'

export async function AutoDecode(
msg: Readonly<IMysensorsMsg>
msg: Readonly<IMysensorsMsg>,
): Promise<IStrongMysensorsMsg<MysensorsCommand> | undefined> {
if (validateStrongMysensorsMsg(msg)) {
return {
...msg,
origin: MsgOrigin.decoded,
};
}
}

if (!msg.topic) {
return new MysensorsSerial().decode(msg as INodeMessage);
return new MysensorsSerial().decode(msg as INodeMessage)
} else {
return new MysensorsMqtt().decode(msg as INodeMessage);
return new MysensorsMqtt().decode(msg as INodeMessage)
}
}
6 changes: 5 additions & 1 deletion src/lib/decoder/decoder-interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { INodeMessage, IStrongMysensorsMsg, MysensorsCommand } from '../mysensors-msg';
import {
INodeMessage,
IStrongMysensorsMsg,
MysensorsCommand,
} from '../mysensors-msg'

export interface IDecoder {
decode(msg: Readonly<INodeMessage>): Promise<IStrongMysensorsMsg<MysensorsCommand>| undefined>
Expand Down
48 changes: 24 additions & 24 deletions src/lib/decoder/mysensors-decoder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {MysensorsDecoder} from './mysensors-decoder';
import { IStrongMysensorsMsg, MysensorsCommand } from '../mysensors-msg';
import { expect } from 'chai';
import { useSinonSandbox } from '../../../test/sinon';
import {MysensorsDecoder} from './mysensors-decoder'
import { IStrongMysensorsMsg, MysensorsCommand } from '../mysensors-msg'
import { expect } from 'chai'
import { useSinonSandbox } from '../../../test/sinon'

describe('lib/decoder/mysensors-decoder', () => {
const sinon = useSinonSandbox();
const sinon = useSinonSandbox()

class dummy extends MysensorsDecoder {
public testEnrich(msg: IStrongMysensorsMsg<MysensorsCommand>) {
return this.enrich(msg);
return this.enrich(msg)
}
}

it('should descriptions for C_SET message', async () => {
const decoder = new dummy(false);
const decoder = new dummy(false)

const result = await decoder.testEnrich({
ack: 0,
Expand All @@ -22,7 +22,7 @@ describe('lib/decoder/mysensors-decoder', () => {
messageType: 1,
nodeId: 1,
subType: 1,
});
})

expect(result).to.deep.equal({
_msgid: '',
Expand All @@ -33,11 +33,11 @@ describe('lib/decoder/mysensors-decoder', () => {
nodeId: 1,
subType: 1,
subTypeStr: 'V_HUM',
});
});
})
})

it('should descriptions for C_REQ message', async () => {
const decoder = new dummy(false);
const decoder = new dummy(false)

const result = await decoder.testEnrich({
ack: 0,
Expand All @@ -46,7 +46,7 @@ describe('lib/decoder/mysensors-decoder', () => {
messageType: 2,
nodeId: 1,
subType: 5,
});
})

expect(result).to.deep.equal({
_msgid: '',
Expand All @@ -57,11 +57,11 @@ describe('lib/decoder/mysensors-decoder', () => {
nodeId: 1,
subType: 5,
subTypeStr: 'V_FORECAST',
});
});
})
})

it('should return descriptions for STREAM message', async() => {
const decoder = new dummy(false);
const decoder = new dummy(false)

const result = await decoder.testEnrich({
ack: 0,
Expand All @@ -70,7 +70,7 @@ describe('lib/decoder/mysensors-decoder', () => {
messageType: 4,
nodeId: 1,
subType: 1,
});
})

expect(result).to.deep.equal({
_msgid: '',
Expand All @@ -81,16 +81,16 @@ describe('lib/decoder/mysensors-decoder', () => {
nodeId: 1,
subType: 1,
subTypeStr: 'ST_FIRMWARE_CONFIG_RESPONSE',
});
})

});
})

it('should enrich with database lookup', async() => {
const database = {
getChild: sinon.stub().resolves({sType: 1}),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;
const decoder = new dummy(true, database);
} as any
const decoder = new dummy(true, database)

const result = await decoder.testEnrich({
ack: 0,
Expand All @@ -99,7 +99,7 @@ describe('lib/decoder/mysensors-decoder', () => {
messageType: 1,
nodeId: 1,
subType: 1,
});
})

expect(result).to.deep.equal({
_msgid: '',
Expand All @@ -111,6 +111,6 @@ describe('lib/decoder/mysensors-decoder', () => {
subType: 1,
subTypeStr: 'V_HUM',
sensorTypeStr: 'S_MOTION',
});
});
});
})
})
})
50 changes: 25 additions & 25 deletions src/lib/decoder/mysensors-decoder.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
import { IStorage } from '../storage-interface';
import { IStrongMysensorsMsg, MysensorsCommand } from '../mysensors-msg';
import { IStorage } from '../storage-interface'
import { IStrongMysensorsMsg, MysensorsCommand } from '../mysensors-msg'
import {
mysensor_command,
mysensor_data,
mysensor_internal,
mysensor_sensor,
mysensor_stream,
} from '../mysensors-types';
import { NullCheck } from '../nullcheck';
} from '../mysensors-types'
import { NullCheck } from '../nullcheck'

export abstract class MysensorsDecoder {
protected enrichWithDb: boolean;
protected enrichWithDb: boolean

constructor(enrich?: boolean, private database?: IStorage) {
this.enrichWithDb = enrich && !!database || false;
this.enrichWithDb = enrich && !!database || false
}

protected async enrich(msg: IStrongMysensorsMsg<MysensorsCommand>): Promise<IStrongMysensorsMsg<MysensorsCommand>> {
const newMsg: IStrongMysensorsMsg<MysensorsCommand> = {
...msg
};
newMsg.messageTypeStr = mysensor_command[msg.messageType];
...msg,
}
newMsg.messageTypeStr = mysensor_command[msg.messageType]
switch (msg.messageType)
{
case mysensor_command.C_INTERNAL:
newMsg.subTypeStr = mysensor_internal[msg.subType];
break;
case mysensor_command.C_PRESENTATION:
newMsg.subTypeStr = mysensor_sensor[msg.subType];
break;
case mysensor_command.C_REQ:
case mysensor_command.C_SET:
newMsg.subTypeStr = mysensor_data[msg.subType];
break;
case mysensor_command.C_STREAM:
newMsg.subTypeStr = mysensor_stream[msg.subType];
break;
case mysensor_command.C_INTERNAL:
newMsg.subTypeStr = mysensor_internal[msg.subType]
break
case mysensor_command.C_PRESENTATION:
newMsg.subTypeStr = mysensor_sensor[msg.subType]
break
case mysensor_command.C_REQ:
case mysensor_command.C_SET:
newMsg.subTypeStr = mysensor_data[msg.subType]
break
case mysensor_command.C_STREAM:
newMsg.subTypeStr = mysensor_stream[msg.subType]
break
}
if (this.enrichWithDb && this.database)
{
const res = await this.database.getChild(msg.nodeId, msg.childSensorId);
const res = await this.database.getChild(msg.nodeId, msg.childSensorId)
if (NullCheck.isDefinedOrNonNull(res)) {
newMsg.sensorTypeStr = mysensor_sensor[res.sType];
newMsg.sensorTypeStr = mysensor_sensor[res.sType]
}
}

return newMsg;
return newMsg
}
}
42 changes: 23 additions & 19 deletions src/lib/decoder/mysensors-mqtt.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { expect } from 'chai';
import 'mocha';
import { IMysensorsMsg, INodeMessage, IStrongMysensorsMsg } from '../mysensors-msg';
import { mysensor_command } from '../mysensors-types';
import { MysensorsMqtt } from './mysensors-mqtt';
import { expect } from 'chai'
import 'mocha'
import {
IMysensorsMsg,
INodeMessage,
IStrongMysensorsMsg,
} from '../mysensors-msg'
import { mysensor_command } from '../mysensors-types'
import { MysensorsMqtt } from './mysensors-mqtt'

describe('MQTT decode / encode', () => {
it('Should create correct decoded output when mqtt topic is received', async () => {
const msg: INodeMessage = {
_msgid: '',
payload: '6',
topic: 'mys-in/1/2/3/0/5',
};
}
const expected: IMysensorsMsg = {
_msgid: '',
ack: 0,
Expand All @@ -21,19 +25,19 @@ describe('MQTT decode / encode', () => {
subType: 5,
topicRoot: 'mys-in',

};
const out = await new MysensorsMqtt().decode(msg);
expect(out).to.include(expected);
});
}
const out = await new MysensorsMqtt().decode(msg)
expect(out).to.include(expected)
})

it('if not mysensors formatted input return undefined', async () => {
const msg: INodeMessage = {
_msgid: '',
payload: '200',
};
const out = await new MysensorsMqtt().decode(msg);
expect(out).to.equal(undefined);
});
}
const out = await new MysensorsMqtt().decode(msg)
expect(out).to.equal(undefined)
})

it('Encode to mysensors mqtt message', () => {
const msg: IStrongMysensorsMsg<mysensor_command.C_PRESENTATION> = {
Expand All @@ -45,8 +49,8 @@ describe('MQTT decode / encode', () => {
payload: '100',
subType: 4,
topicRoot: 'mys-out',
};
const out = new MysensorsMqtt().encode(msg);
expect(out).to.include({topic: 'mys-out/1/2/0/0/4', payload: '100'});
});
});
}
const out = new MysensorsMqtt().encode(msg)
expect(out).to.include({topic: 'mys-out/1/2/0/0/4', payload: '100'})
})
})
Loading