@nexgen-os/homewizard 0.0.0-stage → 0.5.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/LICENSE +21 -0
- package/README.md +76 -2
- package/dist/index.d.ts +85 -0
- package/dist/index.js +210 -0
- package/package.json +37 -4
- package/source.json +15 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nexgen OS contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,77 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@nexgen-os/homewizard`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The [HomeWizard](https://www.homewizard.com) P1 meter as a connector of Nexgen
|
|
4
|
+
OS, on its **local API v1**
|
|
5
|
+
([documentation](https://api-documentation.homewizard.com/docs/v1/measurement)).
|
|
6
|
+
The dongle sits on the P1 port of a smart meter and answers on the
|
|
7
|
+
participant's own network, without a key, once the participant has enabled
|
|
8
|
+
the local API in the HomeWizard Energy app. MIT.
|
|
9
|
+
|
|
10
|
+
No mandate is involved: the participant owns the dongle, and the program that
|
|
11
|
+
reads it runs where the dongle is. The operator's hub is the other path to the
|
|
12
|
+
same meter (a `MeterDataProvider` under mandate, `switchgrid`, `enedis-sge`):
|
|
13
|
+
the hub gives the validated quarter hours the next day, the dongle gives the
|
|
14
|
+
meter as it is now.
|
|
15
|
+
|
|
16
|
+
## Reading the meter
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { storeSeries } from '@nexgen-os/core';
|
|
20
|
+
import { homewizard, homewizardSource } from '@nexgen-os/homewizard';
|
|
21
|
+
|
|
22
|
+
const dongle = homewizard({ host: '192.168.1.20', timeZone }); // host from the connectors row, zone from time_zone_of()
|
|
23
|
+
await dongle.identify(); // refuses another product than HWE-P1, or another API than v1
|
|
24
|
+
const reading = await dongle.read();
|
|
25
|
+
for (const s of reading.series) await storeSeries(db, { organisationId, objectKind: 'meter', objectId: meterId, sourceCode: 'homewizard', kind: 'measured' }, s);
|
|
26
|
+
for (const b of reading.bus) await storeSeries(db, { organisationId, objectKind: 'meter', objectId: gasMeterId, sourceCode: 'homewizard', kind: 'measured' }, b.series);
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`registerSource(db, homewizardSource)` writes the `homewizard` source once
|
|
30
|
+
(`kind = 'device'`, `auth = 'none'`, not redistributable: the data is the
|
|
31
|
+
participant's).
|
|
32
|
+
|
|
33
|
+
## What a reading gives
|
|
34
|
+
|
|
35
|
+
| Field of `/api/v1/data` | Quantity | Value kind | Unit | Name |
|
|
36
|
+
|---|---|---|---|---|
|
|
37
|
+
| `total_power_import_kwh`, `…_t1_kwh` to `…_t4_kwh` | `offtake_energy` | cumulative | kWh | none for the total, `tariff <n>` |
|
|
38
|
+
| `total_power_export_kwh`, `…_t1_kwh` to `…_t4_kwh` | `injection_energy` | cumulative | kWh | none for the total, `tariff <n>` |
|
|
39
|
+
| `active_power_w`, `active_power_l1_w` to `l3` | `offtake_power` and `injection_power` | instant | W | none for the connection, the phase |
|
|
40
|
+
| `active_voltage_l1_v` to `l3` | `voltage` | instant | V | the phase |
|
|
41
|
+
| `external[]` of unit `m3` (gas, water), or `total_gas_m3` of an older firmware | `volume`, in `bus` | cumulative | m³ | none: one meter each |
|
|
42
|
+
|
|
43
|
+
- The dongle gives one signed power per phase, positive when the place takes
|
|
44
|
+
from the grid: it is split into the two directions, the other one at 0.
|
|
45
|
+
- The electricity values carry no stamp: they are dated when they are read,
|
|
46
|
+
to the second, by the clock the caller gives (`now`).
|
|
47
|
+
- A meter of the bus stamps its value in local time (`YYMMDDhhmmss`), without
|
|
48
|
+
the summer or winter flag of the telegram. It is dated in the participant's
|
|
49
|
+
time zone (`timeZone`); without one it is skipped as `time_zone_required`,
|
|
50
|
+
and in the hour repeated when the clocks go back as `stamp_ambiguous`.
|
|
51
|
+
- Each meter of the bus comes back with its type and its identifier (the hex
|
|
52
|
+
the dongle gives, decoded when it is printable text), to be stored on its
|
|
53
|
+
own meter, never on the electricity one.
|
|
54
|
+
- Skipped, with the field and the reason in `skipped`: a value that is not a
|
|
55
|
+
number (`value_invalid`), a unit the core has no quantity for
|
|
56
|
+
(`unit_unsupported`, the GJ of a heat meter), a stamp that is not a date
|
|
57
|
+
(`stamp_invalid`).
|
|
58
|
+
- Left out on purpose: the running quarter-hour demand and the month's peak
|
|
59
|
+
(the core computes demand from the series), the current (no `current` in
|
|
60
|
+
the core vocabulary yet), the counts of sags, swells and failures, the
|
|
61
|
+
Wi-Fi signal.
|
|
62
|
+
|
|
63
|
+
`resample()` turns the cumulative registers into energy per step.
|
|
64
|
+
|
|
65
|
+
## Refusals
|
|
66
|
+
|
|
67
|
+
`connector_config_required` (no address), `source_unauthorised` (403: the
|
|
68
|
+
local API is not enabled in the app), `source_unavailable` (the dongle does
|
|
69
|
+
not answer, or answers an error), `device_unsupported` (another product,
|
|
70
|
+
another API version).
|
|
71
|
+
|
|
72
|
+
## Not yet
|
|
73
|
+
|
|
74
|
+
The API v2 (HTTPS with a token the participant approves on the device), the
|
|
75
|
+
kWh meters and sockets (they measure a device, not a connection: they belong
|
|
76
|
+
with the device contract of the core), the raw telegram (`/api/v1/telegram`,
|
|
77
|
+
read by the DSMR format library).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { type CollectedSeries, type SourceRow } from '@nexgen-os/core';
|
|
2
|
+
/** The subset of `fetch` the connector uses, so a test or a proxy can stand in. */
|
|
3
|
+
export type HttpFetch = (url: string, init?: {
|
|
4
|
+
method?: string;
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
}) => Promise<{
|
|
7
|
+
ok: boolean;
|
|
8
|
+
status: number;
|
|
9
|
+
json(): Promise<unknown>;
|
|
10
|
+
text(): Promise<string>;
|
|
11
|
+
}>;
|
|
12
|
+
export interface HomeWizardOptions {
|
|
13
|
+
/** The dongle on the local network: `192.168.1.20`, `p1meter-0a1b2c.local` or a whole `http://` base. From `connectors.config.host`. */
|
|
14
|
+
host: string;
|
|
15
|
+
/** The IANA time zone of the participant, by which the meters of the bus stamp their values. Without it, their values are skipped (`time_zone_required`). */
|
|
16
|
+
timeZone?: string | null;
|
|
17
|
+
fetch?: HttpFetch;
|
|
18
|
+
/** When a reading is taken, in UTC milliseconds; `Date.now` by default. */
|
|
19
|
+
now?: () => number;
|
|
20
|
+
}
|
|
21
|
+
/** What the dongle says it is (`GET /api`). */
|
|
22
|
+
export interface HomeWizardDevice {
|
|
23
|
+
/** `HWE-P1` for the P1 meter. */
|
|
24
|
+
product_type: string;
|
|
25
|
+
product_name: string;
|
|
26
|
+
/** The dongle's own serial, which `identifiers` may keep on its device. */
|
|
27
|
+
serial: string;
|
|
28
|
+
firmware_version: string;
|
|
29
|
+
api_version: string;
|
|
30
|
+
}
|
|
31
|
+
/** A meter on the bus of the P1 port: its identifier as the meter sends it, and its register. */
|
|
32
|
+
export interface BusMeter {
|
|
33
|
+
/** `gas_meter`, `water_meter`… as the dongle names it. */
|
|
34
|
+
type: string;
|
|
35
|
+
/** The meter's equipment identifier, decoded from the hex the dongle gives when it is printable text. */
|
|
36
|
+
identifier: string | null;
|
|
37
|
+
series: CollectedSeries;
|
|
38
|
+
}
|
|
39
|
+
/** One reading of the dongle, in the core's terms. */
|
|
40
|
+
export interface HomeWizardReading {
|
|
41
|
+
/** When the electricity values were read, ISO 8601 UTC, to the second. */
|
|
42
|
+
at: string;
|
|
43
|
+
/** The smart meter itself: its equipment identifier, its DSMR version, the tariff that runs. */
|
|
44
|
+
meter: {
|
|
45
|
+
identifier: string | null;
|
|
46
|
+
model: string | null;
|
|
47
|
+
smr_version: number | null;
|
|
48
|
+
active_tariff: number | null;
|
|
49
|
+
};
|
|
50
|
+
/** The series of the electricity meter, to be stored on the meter of the connection. */
|
|
51
|
+
series: CollectedSeries[];
|
|
52
|
+
/** The meters of the bus, each to be stored on its own meter. */
|
|
53
|
+
bus: BusMeter[];
|
|
54
|
+
/** What could not be read, with the name of the reason. */
|
|
55
|
+
skipped: {
|
|
56
|
+
field: string;
|
|
57
|
+
reason: string;
|
|
58
|
+
}[];
|
|
59
|
+
}
|
|
60
|
+
export interface HomeWizardConnector {
|
|
61
|
+
readonly source_code: 'homewizard';
|
|
62
|
+
/** What the dongle is; refuses `device_unsupported` for another product than the P1 meter, or another API than v1. */
|
|
63
|
+
identify(): Promise<HomeWizardDevice>;
|
|
64
|
+
/** One reading of the meter, dated now. */
|
|
65
|
+
read(): Promise<HomeWizardReading>;
|
|
66
|
+
}
|
|
67
|
+
/** The product the connector reads: the P1 meter. HomeWizard's kWh meters and sockets measure a device, not a connection. */
|
|
68
|
+
export declare const P1_METER = "HWE-P1";
|
|
69
|
+
/** A connector on the local API v1 of a HomeWizard P1 meter. `source_code` is `homewizard`. */
|
|
70
|
+
export declare function homewizard(options: HomeWizardOptions): HomeWizardConnector;
|
|
71
|
+
/**
|
|
72
|
+
* An answer of `GET /api/v1/data` in the core's terms, dated `at` (UTC
|
|
73
|
+
* milliseconds). A field the meter does not give is absent, and so is its
|
|
74
|
+
* series; a value that is not a number is skipped by name.
|
|
75
|
+
*/
|
|
76
|
+
export declare function readingOfHomeWizard(data: unknown, options: {
|
|
77
|
+
at: number;
|
|
78
|
+
timeZone?: string | null;
|
|
79
|
+
}): HomeWizardReading;
|
|
80
|
+
/** `YYMMDDhhmmss`, as a number or a string, read as a wall-clock time in UTC milliseconds; null when it is not one. */
|
|
81
|
+
export declare function wallClockOf(stamp: unknown): number | null;
|
|
82
|
+
/** The dongle gives a meter's identifier as the hex of its text; the text when it is printable, the value as given otherwise. */
|
|
83
|
+
export declare function decodeIdentifier(value: unknown): string | null;
|
|
84
|
+
/** The `sources` row this package ships (`source.json`): write it with `registerSource()`. */
|
|
85
|
+
export declare const homewizardSource: SourceRow;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// HomeWizard P1 meter — https://www.homewizard.com, a dongle on the P1 port
|
|
2
|
+
// of a smart meter, read on its local API v1
|
|
3
|
+
// (https://api-documentation.homewizard.com/docs/v1/measurement), as a
|
|
4
|
+
// connector. Source `homewizard`, shipped with the package.
|
|
5
|
+
//
|
|
6
|
+
// The dongle answers on the participant's own network, without a key, once
|
|
7
|
+
// the participant has enabled the local API in the HomeWizard Energy app:
|
|
8
|
+
// `GET /api` says what it is, `GET /api/v1/data` gives the meter as it read
|
|
9
|
+
// it last. No mandate is involved: the participant owns the dongle and runs
|
|
10
|
+
// the program that reads it; the operator's hub is another path
|
|
11
|
+
// (`MeterDataProvider`, under mandate).
|
|
12
|
+
//
|
|
13
|
+
// What comes back is in the core's terms, ready for `storeSeries()`:
|
|
14
|
+
//
|
|
15
|
+
// - the offtake and injection registers (`offtake_energy`,
|
|
16
|
+
// `injection_energy`, cumulative, kWh), the total unnamed and each tariff
|
|
17
|
+
// named `tariff <n>`;
|
|
18
|
+
// - the power of the connection and of each phase, split by direction
|
|
19
|
+
// (`offtake_power`, `injection_power`, instant, W): the dongle gives one
|
|
20
|
+
// signed figure, positive when the place takes from the grid;
|
|
21
|
+
// - the voltage of each phase (`voltage`, instant, V);
|
|
22
|
+
// - the meters on the bus (gas, water), each with its own identifier and
|
|
23
|
+
// its own stamp, to be stored on its own meter.
|
|
24
|
+
//
|
|
25
|
+
// The electricity values carry no stamp of their own: they are dated when
|
|
26
|
+
// they are read, by the clock the caller gives. A meter of the bus stamps
|
|
27
|
+
// its value in local time without an offset: it is dated with the time zone
|
|
28
|
+
// of the participant (`time_zone_of()`), never one assumed here.
|
|
29
|
+
//
|
|
30
|
+
// Left out, on purpose: the running quarter-hour demand and the month's
|
|
31
|
+
// peak (the core computes demand from the series), the current (the core
|
|
32
|
+
// vocabulary has no `current` yet), the counts of sags, swells and power
|
|
33
|
+
// failures, the Wi-Fi signal.
|
|
34
|
+
//
|
|
35
|
+
// Nothing here reads the environment or the database: the address of the
|
|
36
|
+
// dongle comes from the organisation's `connectors` row.
|
|
37
|
+
import { readFileSync } from 'node:fs';
|
|
38
|
+
import { GestureError } from '@nexgen-os/core';
|
|
39
|
+
import { instantsOf } from '@nexgen-os/enedis-r6x';
|
|
40
|
+
/** The product the connector reads: the P1 meter. HomeWizard's kWh meters and sockets measure a device, not a connection. */
|
|
41
|
+
export const P1_METER = 'HWE-P1';
|
|
42
|
+
/** A connector on the local API v1 of a HomeWizard P1 meter. `source_code` is `homewizard`. */
|
|
43
|
+
export function homewizard(options) {
|
|
44
|
+
if (!options.host)
|
|
45
|
+
throw new GestureError('connector_config_required: homewizard needs the address of the dongle (host in its connectors row)');
|
|
46
|
+
const base = (/^https?:\/\//.test(options.host) ? options.host : `http://${options.host}`).replace(/\/+$/, '');
|
|
47
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
48
|
+
const now = options.now ?? Date.now;
|
|
49
|
+
const call = async (path) => {
|
|
50
|
+
let response;
|
|
51
|
+
try {
|
|
52
|
+
response = await doFetch(`${base}${path}`, { method: 'GET', headers: { Accept: 'application/json' } });
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
throw new GestureError(`source_unavailable: homewizard at ${base} does not answer: ${error.message}`);
|
|
56
|
+
}
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
const detail = (await response.text().catch(() => '')).slice(0, 300);
|
|
59
|
+
// The dongle answers 403 while its local API is not enabled in the app.
|
|
60
|
+
if (response.status === 403)
|
|
61
|
+
throw new GestureError(`source_unauthorised: homewizard ${path} answers 403; enable the local API in the HomeWizard Energy app: ${detail}`);
|
|
62
|
+
throw new GestureError(`source_unavailable: homewizard ${response.status} on ${path}: ${detail}`);
|
|
63
|
+
}
|
|
64
|
+
return response.json();
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
source_code: 'homewizard',
|
|
68
|
+
async identify() {
|
|
69
|
+
const r = (await call('/api'));
|
|
70
|
+
const device = {
|
|
71
|
+
product_type: String(r.product_type ?? ''),
|
|
72
|
+
product_name: String(r.product_name ?? ''),
|
|
73
|
+
serial: String(r.serial ?? ''),
|
|
74
|
+
firmware_version: String(r.firmware_version ?? ''),
|
|
75
|
+
api_version: String(r.api_version ?? ''),
|
|
76
|
+
};
|
|
77
|
+
if (device.product_type !== P1_METER)
|
|
78
|
+
throw new GestureError(`device_unsupported: homewizard reads the P1 meter (${P1_METER}), not ${device.product_type || 'an unnamed product'}`);
|
|
79
|
+
if (device.api_version !== 'v1')
|
|
80
|
+
throw new GestureError(`device_unsupported: homewizard reads the local API v1, the dongle gives ${device.api_version || 'none'}`);
|
|
81
|
+
return device;
|
|
82
|
+
},
|
|
83
|
+
async read() {
|
|
84
|
+
const data = await call('/api/v1/data');
|
|
85
|
+
return readingOfHomeWizard(data, { at: now(), timeZone: options.timeZone ?? null });
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* An answer of `GET /api/v1/data` in the core's terms, dated `at` (UTC
|
|
91
|
+
* milliseconds). A field the meter does not give is absent, and so is its
|
|
92
|
+
* series; a value that is not a number is skipped by name.
|
|
93
|
+
*/
|
|
94
|
+
export function readingOfHomeWizard(data, options) {
|
|
95
|
+
const r = (data ?? {});
|
|
96
|
+
const at = new Date(Math.floor(options.at / 1000) * 1000).toISOString();
|
|
97
|
+
const skipped = [];
|
|
98
|
+
const series = [];
|
|
99
|
+
const num = (field) => {
|
|
100
|
+
const v = r[field];
|
|
101
|
+
if (v === undefined || v === null)
|
|
102
|
+
return null;
|
|
103
|
+
if (typeof v !== 'number' || !Number.isFinite(v)) {
|
|
104
|
+
skipped.push({ field, reason: 'value_invalid' });
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
return v;
|
|
108
|
+
};
|
|
109
|
+
const push = (quantity_code, value_kind, unit, name, value) => {
|
|
110
|
+
series.push({ quantity_code, value_kind, step: null, unit, name, values: [{ at, value }] });
|
|
111
|
+
};
|
|
112
|
+
// Registers: the total, then each tariff the meter gives.
|
|
113
|
+
for (const [direction, quantity] of [['import', 'offtake_energy'], ['export', 'injection_energy']]) {
|
|
114
|
+
const total = num(`total_power_${direction}_kwh`);
|
|
115
|
+
if (total !== null)
|
|
116
|
+
push(quantity, 'cumulative', 'KiloW-HR', null, total);
|
|
117
|
+
for (let t = 1; t <= 4; t++) {
|
|
118
|
+
const v = num(`total_power_${direction}_t${t}_kwh`);
|
|
119
|
+
if (v !== null)
|
|
120
|
+
push(quantity, 'cumulative', 'KiloW-HR', `tariff ${t}`, v);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Power: one signed figure per phase, positive when taking from the grid.
|
|
124
|
+
for (const [field, name] of [['active_power_w', null], ['active_power_l1_w', 'L1'], ['active_power_l2_w', 'L2'], ['active_power_l3_w', 'L3']]) {
|
|
125
|
+
const v = num(field);
|
|
126
|
+
if (v === null)
|
|
127
|
+
continue;
|
|
128
|
+
push('offtake_power', 'instant', 'W', name, Math.max(v, 0));
|
|
129
|
+
push('injection_power', 'instant', 'W', name, Math.max(-v, 0));
|
|
130
|
+
}
|
|
131
|
+
for (const phase of [1, 2, 3]) {
|
|
132
|
+
const v = num(`active_voltage_l${phase}_v`);
|
|
133
|
+
if (v !== null)
|
|
134
|
+
push('voltage', 'instant', 'V', `L${phase}`, v);
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
at,
|
|
138
|
+
meter: {
|
|
139
|
+
identifier: decodeIdentifier(r.unique_id),
|
|
140
|
+
model: typeof r.meter_model === 'string' ? r.meter_model : null,
|
|
141
|
+
smr_version: typeof r.smr_version === 'number' ? r.smr_version : null,
|
|
142
|
+
active_tariff: typeof r.active_tariff === 'number' ? r.active_tariff : null,
|
|
143
|
+
},
|
|
144
|
+
series,
|
|
145
|
+
bus: busMeters(r, options.timeZone ?? null, skipped),
|
|
146
|
+
skipped,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/** The volume units of the bus the core's `volume` takes; another unit (`GJ` of a heat meter) waits for its quantity. */
|
|
150
|
+
const VOLUME_UNITS = { m3: 'M3' };
|
|
151
|
+
function busMeters(r, timeZone, skipped) {
|
|
152
|
+
// `external` lists every meter of the bus; older firmware gives the gas
|
|
153
|
+
// meter alone in `total_gas_m3`, `gas_timestamp` and `gas_unique_id`.
|
|
154
|
+
const external = Array.isArray(r.external)
|
|
155
|
+
? r.external
|
|
156
|
+
: r.total_gas_m3 !== undefined && r.total_gas_m3 !== null
|
|
157
|
+
? [{ type: 'gas_meter', unique_id: r.gas_unique_id, timestamp: r.gas_timestamp, value: r.total_gas_m3, unit: 'm3' }]
|
|
158
|
+
: [];
|
|
159
|
+
const out = [];
|
|
160
|
+
external.forEach((e, i) => {
|
|
161
|
+
const type = String(e.type ?? 'unknown');
|
|
162
|
+
const field = Array.isArray(r.external) ? `external[${i}]` : 'total_gas_m3';
|
|
163
|
+
const unit = VOLUME_UNITS[String(e.unit ?? '').toLowerCase()];
|
|
164
|
+
if (!unit)
|
|
165
|
+
return skipped.push({ field, reason: 'unit_unsupported' });
|
|
166
|
+
if (typeof e.value !== 'number' || !Number.isFinite(e.value))
|
|
167
|
+
return skipped.push({ field, reason: 'value_invalid' });
|
|
168
|
+
if (!timeZone)
|
|
169
|
+
return skipped.push({ field, reason: 'time_zone_required' });
|
|
170
|
+
const wall = wallClockOf(e.timestamp);
|
|
171
|
+
if (wall === null)
|
|
172
|
+
return skipped.push({ field, reason: 'stamp_invalid' });
|
|
173
|
+
const instants = instantsOf(timeZone, wall);
|
|
174
|
+
// The dongle drops the summer or winter flag of the telegram: the hour
|
|
175
|
+
// repeated in autumn names two instants, and neither is chosen.
|
|
176
|
+
if (instants.length !== 1)
|
|
177
|
+
return skipped.push({ field, reason: instants.length ? 'stamp_ambiguous' : 'stamp_invalid' });
|
|
178
|
+
out.push({
|
|
179
|
+
type,
|
|
180
|
+
identifier: decodeIdentifier(e.unique_id),
|
|
181
|
+
series: { quantity_code: 'volume', value_kind: 'cumulative', step: null, unit, name: null, values: [{ at: new Date(instants[0]).toISOString(), value: e.value }] },
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
/** `YYMMDDhhmmss`, as a number or a string, read as a wall-clock time in UTC milliseconds; null when it is not one. */
|
|
187
|
+
export function wallClockOf(stamp) {
|
|
188
|
+
const s = typeof stamp === 'number' ? String(Math.trunc(stamp)).padStart(12, '0') : typeof stamp === 'string' ? stamp : '';
|
|
189
|
+
const m = /^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/.exec(s);
|
|
190
|
+
if (!m)
|
|
191
|
+
return null;
|
|
192
|
+
const [, yy, mo, dd, hh, mi, ss] = m.map(Number);
|
|
193
|
+
const ms = Date.UTC(2000 + yy, mo - 1, dd, hh, mi, ss);
|
|
194
|
+
const d = new Date(ms);
|
|
195
|
+
// Refuses a date that does not exist (a 31st of June rolls over).
|
|
196
|
+
if (d.getUTCMonth() !== mo - 1 || d.getUTCDate() !== dd || hh > 23 || mi > 59 || ss > 59)
|
|
197
|
+
return null;
|
|
198
|
+
return ms;
|
|
199
|
+
}
|
|
200
|
+
/** The dongle gives a meter's identifier as the hex of its text; the text when it is printable, the value as given otherwise. */
|
|
201
|
+
export function decodeIdentifier(value) {
|
|
202
|
+
if (typeof value !== 'string' || value === '')
|
|
203
|
+
return null;
|
|
204
|
+
if (!/^(?:[0-9a-fA-F]{2})+$/.test(value))
|
|
205
|
+
return value;
|
|
206
|
+
const text = value.match(/../g).map((h) => String.fromCharCode(parseInt(h, 16))).join('');
|
|
207
|
+
return /^[\x20-\x7e]+$/.test(text) ? text.trim() : value;
|
|
208
|
+
}
|
|
209
|
+
/** The `sources` row this package ships (`source.json`): write it with `registerSource()`. */
|
|
210
|
+
export const homewizardSource = JSON.parse(readFileSync(new URL('../source.json', import.meta.url), 'utf8'));
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexgen-os/homewizard",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "The HomeWizard P1 meter's local API (v1) as a Nexgen OS connector: the offtake and injection registers, the power and the voltage of the connection, and the meters of its bus, read on the participant's own network.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/nexgen-os/nexgen-os.git",
|
|
9
|
+
"directory": "packages/connectors/homewizard"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=20"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"source.json",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc -p tsconfig.build.json"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@nexgen-os/core": "^0.5.0",
|
|
37
|
+
"@nexgen-os/enedis-r6x": "^0.5.0"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/source.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"code": "homewizard",
|
|
3
|
+
"name": "HomeWizard P1 meter, local API",
|
|
4
|
+
"description": "A dongle on the P1 port of a smart meter that answers on the participant's own network (API v1, enabled by the participant in the HomeWizard Energy app): the meter's offtake and injection registers by tariff, its power and voltage per phase, and the meters on its bus (gas, water). Read by @nexgen-os/homewizard; nothing leaves the place unless the installation stores it.",
|
|
5
|
+
"kind": "device",
|
|
6
|
+
"base_url": null,
|
|
7
|
+
"docs_url": "https://api-documentation.homewizard.com/docs/v1/measurement",
|
|
8
|
+
"auth": "none",
|
|
9
|
+
"countries": [],
|
|
10
|
+
"license": "The data is the participant's, read on their own network",
|
|
11
|
+
"attribution": "HomeWizard",
|
|
12
|
+
"retention_days": null,
|
|
13
|
+
"redistributable": false,
|
|
14
|
+
"published_by": "@nexgen-os/homewizard"
|
|
15
|
+
}
|