@nexgen-os/mqtt 0.5.1 → 0.6.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/README.md +25 -15
- package/dist/index.d.ts +15 -16
- package/dist/index.js +40 -47
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -6,25 +6,28 @@ what the meter says every second or every few seconds: the JSON message of
|
|
|
6
6
|
the open P1M5 firmware, or the DSMR telegram as the meter wrote it. This
|
|
7
7
|
package places each message on the meter (or the device) its dongle is
|
|
8
8
|
known by, dates it by the meter's clock in the time zone of that object's
|
|
9
|
-
participant, keeps one reading a minute, and
|
|
10
|
-
batches
|
|
9
|
+
participant, keeps one reading a minute, and hands the kept readings in
|
|
10
|
+
batches to its caller, which writes them: the core's `measuredObjects(db)`
|
|
11
|
+
writes through `store()`, the one write path. MIT, and it runs without the
|
|
12
|
+
core ([0064](../../../docs/decisions/0064-an-mit-package-reaches-no-agpl-package.md)).
|
|
11
13
|
|
|
12
14
|
The broker is a protocol, never a library this package ships
|
|
13
15
|
([0014](../../../docs/decisions/0014-integrations-dashboards-warehouses-streams.md)):
|
|
14
16
|
any client that subscribes and emits `message` events fits, MQTT.js as it
|
|
15
17
|
is. The readings themselves are read by [`@nexgen-os/dsmr`](../../formats/dsmr/README.md)
|
|
16
|
-
and turned into series by `meterSeriesOfDsmr()` of
|
|
17
|
-
same mapping a file of telegrams goes through.
|
|
18
|
+
and turned into series by `meterSeriesOfDsmr()` of
|
|
19
|
+
[`@nexgen-os/connector`](../../connector/README.md), the same mapping a file of telegrams goes through.
|
|
18
20
|
|
|
19
21
|
## Use
|
|
20
22
|
|
|
21
23
|
```ts
|
|
22
24
|
import mqtt from 'mqtt';
|
|
23
|
-
import {
|
|
25
|
+
import { measuredObjects, registerSource } from '@nexgen-os/core';
|
|
26
|
+
import { listen, P1_PORT_SOURCE, p1Stream } from '@nexgen-os/mqtt';
|
|
24
27
|
|
|
25
|
-
await
|
|
28
|
+
await registerSource(db, P1_PORT_SOURCE); // the `p1_port` source, once
|
|
26
29
|
const client = mqtt.connect(process.env.BROKER_URL!, { username, password }); // the secret from the vault
|
|
27
|
-
const stream = p1Stream(db, { scheme: 'p1_dongle', organisationId });
|
|
30
|
+
const stream = p1Stream(measuredObjects(db), { scheme: 'p1_dongle', organisationId });
|
|
28
31
|
const listening = listen(client, stream, {
|
|
29
32
|
topics: ['p1/+/data/readings'],
|
|
30
33
|
onReceived: (r) => { if (r.status === 'refused') console.warn(r.identifier, r.problems); },
|
|
@@ -34,7 +37,9 @@ await listening.stop(); // writes what is left
|
|
|
34
37
|
```
|
|
35
38
|
|
|
36
39
|
The installation records each dongle once, as an identifier of the meter it
|
|
37
|
-
reads, in a scheme of its own (the scheme is data, the package names none)
|
|
40
|
+
reads, in a scheme of its own (the scheme is data, the package names none),
|
|
41
|
+
and the gas, water or heat meter of its bus by its equipment identifier in
|
|
42
|
+
the same scheme when it wants its volume:
|
|
38
43
|
|
|
39
44
|
```sql
|
|
40
45
|
insert into identifier_schemes (code, name, pattern) values ('p1_dongle', 'A P1 dongle', '^[A-Z0-9]+$');
|
|
@@ -47,8 +52,9 @@ select add_identifier('meter', '<meter id>', 'p1_dongle', 'P1000001');
|
|
|
47
52
|
message, otherwise the meter's equipment identifier (`0-0:96.1.1`) of a
|
|
48
53
|
telegram; `identifierOf` replaces it (a segment of the topic, say).
|
|
49
54
|
2. **Its object.** The identifier, valid now, in the stream's `scheme` (and
|
|
50
|
-
`organisationId` when given),
|
|
51
|
-
|
|
55
|
+
`organisationId` when given), asked of the caller's `byIdentifier()`;
|
|
56
|
+
`measuredObjects(db)` reads it under the connection's rights: a meter or
|
|
57
|
+
a device. Found or not, a dongle is remembered for
|
|
52
58
|
`rememberSeconds` (300) before it is looked up again.
|
|
53
59
|
3. **Its time.** The reading is dated by the meter's clock in the time zone
|
|
54
60
|
of the object's participant, never by the time it was received.
|
|
@@ -57,10 +63,16 @@ select add_identifier('meter', '<meter id>', 'p1_dongle', 'P1000001');
|
|
|
57
63
|
quarter hours `resample()` gives lose nothing, and the power and voltage
|
|
58
64
|
are kept once a minute.
|
|
59
65
|
5. **Written.** `flush()` (every `flushEverySeconds`, 60, under `listen()`)
|
|
60
|
-
|
|
66
|
+
hands each object's batch to the caller's `write()`, which
|
|
67
|
+
`measuredObjects(db)` writes as its series of kind `measured` under the
|
|
61
68
|
source `p1_port`: the energy registers as cumulative `offtake_energy` /
|
|
62
69
|
`injection_energy` series by tariff, the power and the voltage as
|
|
63
70
|
instants by phase.
|
|
71
|
+
6. **Its bus.** A gas, water or heat meter on the bus of the port (channel
|
|
72
|
+
1 to 4) is found by its own equipment identifier (`bus` of the reading),
|
|
73
|
+
in the same `scheme`: its volume is written on that meter, at the stamp
|
|
74
|
+
it sent, as a cumulative `volume` series. A flush names the `channel` of
|
|
75
|
+
each batch, 0 for the electricity meter.
|
|
64
76
|
|
|
65
77
|
| What came | What it gives |
|
|
66
78
|
|---|---|
|
|
@@ -70,15 +82,13 @@ select add_identifier('meter', '<meter id>', 'p1_dongle', 'P1000001');
|
|
|
70
82
|
| a participant without a time zone | refused, `time_zone_required` |
|
|
71
83
|
| a message naming no dongle, or not a message | refused, `identifier_missing`, `telegram_unreadable` |
|
|
72
84
|
| a reading without the meter's time | refused, `time_missing` |
|
|
85
|
+
| a meter of the bus whose identifier no object carries | said, `bus_meter_unknown`, among the problems of the message; the electricity meter is written all the same |
|
|
73
86
|
| a telegram whose checksum does not match | left out at the flush, in `skipped` |
|
|
74
|
-
| a batch the
|
|
87
|
+
| a batch the caller refuses (another value kind on the series) | the flush's `error`, its readings dropped |
|
|
75
88
|
|
|
76
89
|
A refusal never stops the stream: the next message is read.
|
|
77
90
|
|
|
78
91
|
## Not yet
|
|
79
92
|
|
|
80
|
-
- The meters of the bus (gas, water, heat on channels 1 to 4): their volume
|
|
81
|
-
is in the telegram, and `meterSeriesOfDsmr(readings, { channel })` gives
|
|
82
|
-
it; the stream writes the electricity meter only.
|
|
83
93
|
- The one message shape of the stream door, the envelope of the
|
|
84
94
|
connectors' series, received from any broker.
|
package/dist/index.d.ts
CHANGED
|
@@ -9,9 +9,13 @@
|
|
|
9
9
|
* object it belongs to by the dongle's identifier in the core's
|
|
10
10
|
* `identifiers` (a scheme the installation names), reads it with
|
|
11
11
|
* `@nexgen-os/dsmr` in the time zone of that object's participant, keeps one
|
|
12
|
-
* reading per object every `keepEverySeconds`, and
|
|
13
|
-
* batches (`flush()`)
|
|
14
|
-
*
|
|
12
|
+
* reading per object every `keepEverySeconds`, and hands what it kept in
|
|
13
|
+
* batches (`flush()`), as series by `meterSeriesOfDsmr()`, to be written.
|
|
14
|
+
* Finding the object and writing its series are its caller's
|
|
15
|
+
* (`MeasuredObjects` of `@nexgen-os/connector`): the core gives them with
|
|
16
|
+
* `measuredObjects(db)`, which reads under the connection's rights and
|
|
17
|
+
* writes through `store()`, the one write path, so that this package runs
|
|
18
|
+
* without the core (decision 0064). `listen()` ties a stream to any MQTT client
|
|
15
19
|
* that subscribes and emits `message` events (MQTT.js, or another): the
|
|
16
20
|
* broker is a protocol, never a library this package ships
|
|
17
21
|
* ([0014](../../../docs/decisions/0014-integrations-dashboards-warehouses-streams.md)).
|
|
@@ -21,12 +25,10 @@
|
|
|
21
25
|
* `object_not_measured`, `time_zone_required`. A stream is never stopped by
|
|
22
26
|
* one bad message.
|
|
23
27
|
*/
|
|
24
|
-
import { type
|
|
28
|
+
import { type MeasuredObjects, type MeasuredTarget, type SourceRow } from '@nexgen-os/connector';
|
|
25
29
|
import { type DsmrOptions, type DsmrProblem, type DsmrReading } from '@nexgen-os/dsmr';
|
|
26
|
-
/** The source of what a P1 port says, as this package ships it
|
|
30
|
+
/** The source of what a P1 port says, as this package ships it: write it once with `registerSource()`. */
|
|
27
31
|
export declare const P1_PORT_SOURCE: SourceRow;
|
|
28
|
-
/** Writes the `sources` row of the P1 port, once. */
|
|
29
|
-
export declare function registerP1PortSource(db: Db): Promise<void>;
|
|
30
32
|
/**
|
|
31
33
|
* Reads one message of a dongle: a P1M5 JSON message (text starting with
|
|
32
34
|
* `{`, or a parsed object), or a DSMR telegram (text starting with `/`).
|
|
@@ -56,12 +58,7 @@ export interface P1StreamOptions {
|
|
|
56
58
|
now?: () => number;
|
|
57
59
|
}
|
|
58
60
|
/** The object a dongle's readings land on. */
|
|
59
|
-
export
|
|
60
|
-
organisationId: string;
|
|
61
|
-
objectKind: 'meter' | 'device';
|
|
62
|
-
objectId: string;
|
|
63
|
-
timeZone: string;
|
|
64
|
-
}
|
|
61
|
+
export type P1Target = MeasuredTarget;
|
|
65
62
|
/** What became of one message. */
|
|
66
63
|
export type P1Received = {
|
|
67
64
|
status: 'queued';
|
|
@@ -83,6 +80,8 @@ export type P1Received = {
|
|
|
83
80
|
export interface P1Flushed {
|
|
84
81
|
objectKind: 'meter' | 'device';
|
|
85
82
|
objectId: string;
|
|
83
|
+
/** The channel of the bus written: 0 the electricity meter, 1 to 4 a gas, water or heat meter of its bus. */
|
|
84
|
+
channel: number;
|
|
86
85
|
readings: number;
|
|
87
86
|
/** Readings left out whole (undated, checksum), by their index in the batch. */
|
|
88
87
|
skipped: {
|
|
@@ -95,7 +94,7 @@ export interface P1Flushed {
|
|
|
95
94
|
seriesId: string;
|
|
96
95
|
inserted: number;
|
|
97
96
|
}[];
|
|
98
|
-
/** The refusal of the
|
|
97
|
+
/** The refusal of the caller when the batch could not be written; its readings are dropped. */
|
|
99
98
|
error?: string;
|
|
100
99
|
}
|
|
101
100
|
/** A stream of P1 messages: receive them as they come, write them in batches. */
|
|
@@ -105,8 +104,8 @@ export interface P1Stream {
|
|
|
105
104
|
/** Readings waiting for the next flush. */
|
|
106
105
|
readonly pending: number;
|
|
107
106
|
}
|
|
108
|
-
/** A stream that writes the readings of dongles on the objects their identifiers name
|
|
109
|
-
export declare function p1Stream(
|
|
107
|
+
/** A stream that writes the readings of dongles on the objects their identifiers name, found and written by `objects`. */
|
|
108
|
+
export declare function p1Stream(objects: MeasuredObjects, options: P1StreamOptions): P1Stream;
|
|
110
109
|
/** What `listen()` needs of an MQTT client: MQTT.js fits as it is. */
|
|
111
110
|
export interface MqttClient {
|
|
112
111
|
subscribe(topic: string | string[], ...rest: unknown[]): unknown;
|
package/dist/index.js
CHANGED
|
@@ -9,9 +9,13 @@
|
|
|
9
9
|
* object it belongs to by the dongle's identifier in the core's
|
|
10
10
|
* `identifiers` (a scheme the installation names), reads it with
|
|
11
11
|
* `@nexgen-os/dsmr` in the time zone of that object's participant, keeps one
|
|
12
|
-
* reading per object every `keepEverySeconds`, and
|
|
13
|
-
* batches (`flush()`)
|
|
14
|
-
*
|
|
12
|
+
* reading per object every `keepEverySeconds`, and hands what it kept in
|
|
13
|
+
* batches (`flush()`), as series by `meterSeriesOfDsmr()`, to be written.
|
|
14
|
+
* Finding the object and writing its series are its caller's
|
|
15
|
+
* (`MeasuredObjects` of `@nexgen-os/connector`): the core gives them with
|
|
16
|
+
* `measuredObjects(db)`, which reads under the connection's rights and
|
|
17
|
+
* writes through `store()`, the one write path, so that this package runs
|
|
18
|
+
* without the core (decision 0064). `listen()` ties a stream to any MQTT client
|
|
15
19
|
* that subscribes and emits `message` events (MQTT.js, or another): the
|
|
16
20
|
* broker is a protocol, never a library this package ships
|
|
17
21
|
* ([0014](../../../docs/decisions/0014-integrations-dashboards-warehouses-streams.md)).
|
|
@@ -21,9 +25,9 @@
|
|
|
21
25
|
* `object_not_measured`, `time_zone_required`. A stream is never stopped by
|
|
22
26
|
* one bad message.
|
|
23
27
|
*/
|
|
24
|
-
import { meterSeriesOfDsmr
|
|
28
|
+
import { meterSeriesOfDsmr } from '@nexgen-os/connector';
|
|
25
29
|
import { readP1m5, readTelegram } from '@nexgen-os/dsmr';
|
|
26
|
-
/** The source of what a P1 port says, as this package ships it
|
|
30
|
+
/** The source of what a P1 port says, as this package ships it: write it once with `registerSource()`. */
|
|
27
31
|
export const P1_PORT_SOURCE = {
|
|
28
32
|
code: 'p1_port',
|
|
29
33
|
name: 'The P1 port of a smart meter',
|
|
@@ -34,10 +38,6 @@ export const P1_PORT_SOURCE = {
|
|
|
34
38
|
redistributable: false,
|
|
35
39
|
published_by: '@nexgen-os/mqtt',
|
|
36
40
|
};
|
|
37
|
-
/** Writes the `sources` row of the P1 port, once. */
|
|
38
|
-
export async function registerP1PortSource(db) {
|
|
39
|
-
await registerSource(db, P1_PORT_SOURCE);
|
|
40
|
-
}
|
|
41
41
|
/**
|
|
42
42
|
* Reads one message of a dongle: a P1M5 JSON message (text starting with
|
|
43
43
|
* `{`, or a parsed object), or a DSMR telegram (text starting with `/`).
|
|
@@ -74,8 +74,8 @@ export function topicMatches(filter, topic) {
|
|
|
74
74
|
return f.length === t.length;
|
|
75
75
|
}
|
|
76
76
|
const problem = (code, detail) => ({ code, detail });
|
|
77
|
-
/** A stream that writes the readings of dongles on the objects their identifiers name
|
|
78
|
-
export function p1Stream(
|
|
77
|
+
/** A stream that writes the readings of dongles on the objects their identifiers name, found and written by `objects`. */
|
|
78
|
+
export function p1Stream(objects, options) {
|
|
79
79
|
const sourceCode = options.sourceCode ?? P1_PORT_SOURCE.code;
|
|
80
80
|
const keepMs = (options.keepEverySeconds ?? 60) * 1000;
|
|
81
81
|
const rememberMs = (options.rememberSeconds ?? 300) * 1000;
|
|
@@ -85,34 +85,21 @@ export function p1Stream(db, options) {
|
|
|
85
85
|
const lastKept = new Map();
|
|
86
86
|
let batches = new Map();
|
|
87
87
|
let pending = 0;
|
|
88
|
+
function queue(target, channel, reading) {
|
|
89
|
+
const key = `${target.objectKind} ${target.objectId} ${channel}`;
|
|
90
|
+
let batch = batches.get(key);
|
|
91
|
+
if (!batch) {
|
|
92
|
+
batch = { target, channel, readings: [] };
|
|
93
|
+
batches.set(key, batch);
|
|
94
|
+
}
|
|
95
|
+
batch.readings.push(reading);
|
|
96
|
+
}
|
|
88
97
|
async function find(identifier) {
|
|
89
98
|
const remembered = known.get(identifier);
|
|
90
99
|
if (remembered && remembered.until > now())
|
|
91
100
|
return remembered;
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
left join participants p on p.id = participant_of(i.object_kind, i.object_id)
|
|
95
|
-
where i.scheme = $1 and i.value = $2
|
|
96
|
-
and (i.valid_from is null or i.valid_from <= now())
|
|
97
|
-
and (i.valid_to is null or i.valid_to > now())
|
|
98
|
-
and ($3::uuid is null or i.organisation_id = $3::uuid)`, [options.scheme, identifier, options.organisationId ?? null]);
|
|
99
|
-
let found = { until: now() + rememberMs, target: null, problem: null };
|
|
100
|
-
const row = rows[0];
|
|
101
|
-
if (!row) {
|
|
102
|
-
found.problem = problem('identifier_unknown', `no meter or device is known by ${options.scheme} ${identifier}`);
|
|
103
|
-
}
|
|
104
|
-
else if (rows.length > 1) {
|
|
105
|
-
found.problem = problem('identifier_ambiguous', `${options.scheme} ${identifier} names an object in ${rows.length} organisations; give organisationId`);
|
|
106
|
-
}
|
|
107
|
-
else if (row.object_kind !== 'meter' && row.object_kind !== 'device') {
|
|
108
|
-
found.problem = problem('object_not_measured', `${options.scheme} ${identifier} names a ${row.object_kind}, not a meter or a device`);
|
|
109
|
-
}
|
|
110
|
-
else if (!row.time_zone) {
|
|
111
|
-
found.problem = problem('time_zone_required', `the participant of ${row.object_kind} ${row.object_id} has no time zone: the meter's clock cannot be dated`);
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
found = { ...found, target: { organisationId: row.organisation_id, objectKind: row.object_kind, objectId: row.object_id, timeZone: row.time_zone } };
|
|
115
|
-
}
|
|
101
|
+
const answer = await objects.byIdentifier(options.scheme, identifier, options.organisationId ? { organisationId: options.organisationId } : {});
|
|
102
|
+
const found = { until: now() + rememberMs, target: answer.target ?? null, problem: answer.problem ?? null };
|
|
116
103
|
known.set(identifier, found);
|
|
117
104
|
return found;
|
|
118
105
|
}
|
|
@@ -146,28 +133,34 @@ export function p1Stream(db, options) {
|
|
|
146
133
|
}
|
|
147
134
|
if (last === undefined || at > last)
|
|
148
135
|
lastKept.set(key, at);
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
136
|
+
queue(target, 0, reading);
|
|
137
|
+
// The meters of its bus land on the objects their own equipment
|
|
138
|
+
// identifiers name, in the same scheme; one the installation did not
|
|
139
|
+
// record is said, and the electricity meter is written all the same.
|
|
140
|
+
const problems = [...reading.problems];
|
|
141
|
+
for (const bus of reading.bus) {
|
|
142
|
+
if (!bus.equipment_id)
|
|
143
|
+
continue;
|
|
144
|
+
const found = await find(bus.equipment_id);
|
|
145
|
+
if (found.target)
|
|
146
|
+
queue(found.target, bus.channel, reading);
|
|
147
|
+
else
|
|
148
|
+
problems.push(problem('bus_meter_unknown', `channel ${bus.channel}: ${found.problem.detail}`));
|
|
153
149
|
}
|
|
154
|
-
batch.readings.push(reading);
|
|
155
150
|
pending += 1;
|
|
156
|
-
return { status: 'queued', identifier, target, at: reading.at, problems
|
|
151
|
+
return { status: 'queued', identifier, target, at: reading.at, problems };
|
|
157
152
|
},
|
|
158
153
|
async flush() {
|
|
159
154
|
const taken = batches;
|
|
160
155
|
batches = new Map();
|
|
161
156
|
pending = 0;
|
|
162
157
|
const out = [];
|
|
163
|
-
for (const { target, readings } of taken.values()) {
|
|
164
|
-
const { series, skipped } = meterSeriesOfDsmr(readings);
|
|
165
|
-
const done = { objectKind: target.objectKind, objectId: target.objectId, readings: readings.length, skipped, series: [] };
|
|
158
|
+
for (const { target, channel, readings } of taken.values()) {
|
|
159
|
+
const { series, skipped } = meterSeriesOfDsmr(readings, { channel });
|
|
160
|
+
const done = { objectKind: target.objectKind, objectId: target.objectId, channel, readings: readings.length, skipped, series: [] };
|
|
166
161
|
try {
|
|
167
162
|
for (const s of series) {
|
|
168
|
-
const stored = await
|
|
169
|
-
organisationId: target.organisationId, objectKind: target.objectKind, objectId: target.objectId, sourceCode, kind: 'measured',
|
|
170
|
-
}, s);
|
|
163
|
+
const stored = await objects.write(target, sourceCode, s);
|
|
171
164
|
done.series.push({ quantity_code: s.quantity_code, name: s.name ?? null, seriesId: stored.seriesId, inserted: stored.inserted });
|
|
172
165
|
}
|
|
173
166
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexgen-os/mqtt",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "The readings of P1 dongles received from an MQTT broker as the series of the meters they read: a P1M5 message or a DSMR telegram, the meter found by the dongle's identifier, dated in its participant's time zone, thinned and
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "The readings of P1 dongles received from an MQTT broker as the series of the meters they read: a P1M5 message or a DSMR telegram, the meter found by the dongle's identifier, dated in its participant's time zone, thinned and handed in batches to the caller that writes them (the core's measuredObjects()). Any MQTT client fits; no broker library is shipped.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"build": "tsc -p tsconfig.build.json"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@nexgen-os/
|
|
36
|
-
"@nexgen-os/dsmr": "^0.
|
|
35
|
+
"@nexgen-os/connector": "^0.6.0",
|
|
36
|
+
"@nexgen-os/dsmr": "^0.6.0"
|
|
37
37
|
}
|
|
38
38
|
}
|