@mschaeffler/node-red-bthome 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bthome.html +5 -0
- package/bthome.js +24 -37
- package/package.json +1 -1
- package/tools.js +38 -0
package/bthome.html
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
defaults: {
|
|
6
6
|
name: {value:""},
|
|
7
7
|
devices:{value:"[]",required:true},
|
|
8
|
+
counterTime:{value:false},
|
|
8
9
|
statusPrefix:{value:""},
|
|
9
10
|
eventPrefix:{value:""},
|
|
10
11
|
contextVar:{value:"bthome",required:true},
|
|
@@ -65,6 +66,10 @@
|
|
|
65
66
|
<label for="node-input-devices"><i class="fa fa-bluetooth-b"></i> Devices</label>
|
|
66
67
|
<input type="text" id="node-input-devices">
|
|
67
68
|
</div>
|
|
69
|
+
<div class="form-row">
|
|
70
|
+
<label for="node-input-counterTime"><i class="fa fa-clock-o"></i> counter is time</label>
|
|
71
|
+
<input type="checkbox" id="node-input-counterTime" style="display:inline-block; width:20px; vertical-align:baseline;">
|
|
72
|
+
</div>
|
|
68
73
|
<div class="form-row">
|
|
69
74
|
<label for="node-input-statusPrefix"><i class="fa fa-star"></i> Status-Prefix</label>
|
|
70
75
|
<input type="text" id="node-input-statusPrefix"></input>
|
package/bthome.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const Crypto = require( 'node:crypto' );
|
|
2
|
+
const Tools = require( './tools.js' );
|
|
2
3
|
const Rawdata = require( "./rawdata.js" );
|
|
3
4
|
const BtEvent = require( "./btevent.js" );
|
|
4
5
|
|
|
@@ -9,6 +10,7 @@ module.exports = function(RED) {
|
|
|
9
10
|
var node = this;
|
|
10
11
|
this.flowcontext = this.context().flow;
|
|
11
12
|
this.devices = JSON.parse( config.devices ?? "{}" );
|
|
13
|
+
this.counterTime = Boolean( config.counterTime );
|
|
12
14
|
this.statusPrefix = config.statusPrefix ? config.statusPrefix+'/' : "";
|
|
13
15
|
this.eventPrefix = config.eventPrefix ? config.eventPrefix +'/' : "";
|
|
14
16
|
this.contextVar = config.contextVar ?? "bthome";
|
|
@@ -36,28 +38,14 @@ module.exports = function(RED) {
|
|
|
36
38
|
for( const mac in node.devices )
|
|
37
39
|
{
|
|
38
40
|
const device = node.devices[mac];
|
|
39
|
-
|
|
41
|
+
try
|
|
40
42
|
{
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
for( let i=0; i<16 ; i++ )
|
|
48
|
-
{
|
|
49
|
-
buf[i] = Number.parseInt( device.key.slice( i*2, i*2 + 2 ), 16 );
|
|
50
|
-
}
|
|
51
|
-
device.key = buf;
|
|
52
|
-
}
|
|
53
|
-
else
|
|
54
|
-
{
|
|
55
|
-
device.key = Buffer.alloc( 16 );
|
|
56
|
-
node.log( mac + " keylength must be 16 bytes" );
|
|
57
|
-
}
|
|
58
|
-
break;
|
|
59
|
-
default:
|
|
60
|
-
device.key = Buffer.from( device.key );
|
|
43
|
+
device.key = Tools.key2bytes( device.key );
|
|
44
|
+
}
|
|
45
|
+
catch( e )
|
|
46
|
+
{
|
|
47
|
+
node.log( mac + e.message );
|
|
48
|
+
device.key = null;
|
|
61
49
|
}
|
|
62
50
|
}
|
|
63
51
|
|
|
@@ -90,26 +78,24 @@ module.exports = function(RED) {
|
|
|
90
78
|
|
|
91
79
|
function decryptMsg()
|
|
92
80
|
{
|
|
93
|
-
|
|
94
|
-
for( const help of msg.payload.addr.split( ":" ) )
|
|
95
|
-
{
|
|
96
|
-
mac.push( Number.parseInt( help, 16 ) );
|
|
97
|
-
}
|
|
98
|
-
const uuid16 = [0xD2,0xFC];
|
|
81
|
+
const mac = Tools.mac2bytes( msg.payload.addr );
|
|
99
82
|
const ciphertext = Buffer.from( rawdata.slice( 0, -8 ) );
|
|
100
83
|
const counter = rawdata.slice( -8, -4 );
|
|
101
|
-
|
|
84
|
+
if( node.counterTime )
|
|
85
|
+
{
|
|
86
|
+
const counterInt = counter[0] | (counter[1]<<8) | (counter[2]<<16) | (counter[3]<<24);
|
|
87
|
+
const delta = msgTime - counterInt*1000;
|
|
88
|
+
if( delta < -5000 || delta > 15000 )
|
|
89
|
+
{
|
|
90
|
+
throw new Error( "bthome "+msg.payload.gateway+" "+name+" "+(new Date(counterInt*1000))+" "+delta );
|
|
91
|
+
}
|
|
92
|
+
}
|
|
102
93
|
const mic = Buffer.from( rawdata.slice( -4 ) );
|
|
103
|
-
const nonce = Buffer.from( mac.concat( uuid16, dib, counter ) );
|
|
94
|
+
const nonce = Buffer.from( mac.concat( Tools.uuid16, dib, counter ) );
|
|
104
95
|
const decipher = Crypto.createDecipheriv( "aes-128-ccm", node.devices[msg.payload.addr].key, nonce, { authTagLength: 4 } );
|
|
105
96
|
decipher.setAuthTag( mic );
|
|
106
97
|
rawdata = Array.from( decipher.update( ciphertext ) );
|
|
107
98
|
decipher.final();
|
|
108
|
-
|
|
109
|
-
if( Date.now() - counterInt*1000 > 7500 )
|
|
110
|
-
{
|
|
111
|
-
node.error("bthome "+name+" "+(new Date(counterInt*1000))+" "+(Date.now()-counterInt*1000));
|
|
112
|
-
}
|
|
113
99
|
}
|
|
114
100
|
|
|
115
101
|
function setData(name,value)
|
|
@@ -186,13 +172,13 @@ module.exports = function(RED) {
|
|
|
186
172
|
if( pid < item.pid && pid > 10 /*&& pid > item.pid - 10*/ )
|
|
187
173
|
{
|
|
188
174
|
// veraltete Nachricht und nicht reboot
|
|
189
|
-
node.warn( `old ble message
|
|
175
|
+
node.warn( `old ble message ${name} from ${msg.payload.gateway} dropped, ${pid} < ${item.pid}` );
|
|
190
176
|
return false;
|
|
191
177
|
}
|
|
192
178
|
if( msg.payload.gateway )
|
|
193
179
|
{
|
|
194
180
|
item.gw[msg.payload.gateway] = {
|
|
195
|
-
time:
|
|
181
|
+
time: msgTime,
|
|
196
182
|
rssi: msg.payload.rssi ?? null
|
|
197
183
|
};
|
|
198
184
|
}
|
|
@@ -201,7 +187,7 @@ module.exports = function(RED) {
|
|
|
201
187
|
|
|
202
188
|
function newMessage()
|
|
203
189
|
{
|
|
204
|
-
item.time =
|
|
190
|
+
item.time = msgTime;
|
|
205
191
|
item.pid = pid;
|
|
206
192
|
item.encrypted = encrypted;
|
|
207
193
|
if( node.contextStore !== "none" )
|
|
@@ -216,6 +202,7 @@ module.exports = function(RED) {
|
|
|
216
202
|
}
|
|
217
203
|
|
|
218
204
|
const name = node.devices[msg.payload.addr]?.topic;
|
|
205
|
+
const msgTime = msg.payload.time ?? Date.now()
|
|
219
206
|
const dib = msg.payload.data[0];
|
|
220
207
|
let rawdata = msg.payload.data.slice( 1 );
|
|
221
208
|
const encrypted = Boolean( dib & 0x1 );
|
package/package.json
CHANGED
package/tools.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Tooling for bthome
|
|
2
|
+
|
|
3
|
+
exports.uuid16 = [0xd2,0xfc];
|
|
4
|
+
|
|
5
|
+
exports.key2bytes = function(str)
|
|
6
|
+
{
|
|
7
|
+
switch( typeof str )
|
|
8
|
+
{
|
|
9
|
+
case "undefined":
|
|
10
|
+
return null;
|
|
11
|
+
case "string":
|
|
12
|
+
if( str.length == 32 )
|
|
13
|
+
{
|
|
14
|
+
const buf = Buffer.alloc( 16 );
|
|
15
|
+
for( let i=0; i<16 ; i++ )
|
|
16
|
+
{
|
|
17
|
+
buf[i] = Number.parseInt( str.slice( i*2, i*2 + 2 ), 16 );
|
|
18
|
+
}
|
|
19
|
+
return buf;
|
|
20
|
+
}
|
|
21
|
+
else
|
|
22
|
+
{
|
|
23
|
+
throw new Error( " keylength must be 16 bytes" );
|
|
24
|
+
}
|
|
25
|
+
default:
|
|
26
|
+
return Buffer.from( str );
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
exports.mac2bytes = function(str)
|
|
31
|
+
{
|
|
32
|
+
let mac = [];
|
|
33
|
+
for( const help of str.split( ":" ) )
|
|
34
|
+
{
|
|
35
|
+
mac.push( Number.parseInt( help, 16 ) );
|
|
36
|
+
}
|
|
37
|
+
return mac;
|
|
38
|
+
}
|