@nexgen-os/core 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 +1 -1
- package/dist/address.d.ts +3 -62
- package/dist/connectors/activations.d.ts +63 -0
- package/dist/connectors/activations.js +73 -0
- package/dist/connectors/devices.d.ts +7 -0
- package/dist/connectors/devices.js +2 -1
- package/dist/connectors/interfaces.d.ts +2 -104
- package/dist/connectors/interfaces.js +4 -11
- package/dist/connectors/measured.d.ts +12 -0
- package/dist/connectors/measured.js +44 -0
- package/dist/connectors/meter-data.d.ts +2 -39
- package/dist/connectors/meter-data.js +3 -13
- package/dist/files/dsmr.d.ts +1 -39
- package/dist/files/dsmr.js +5 -62
- package/dist/files/enedis-r6x.d.ts +1 -32
- package/dist/files/enedis-r6x.js +5 -86
- package/dist/files/index.d.ts +1 -0
- package/dist/files/index.js +1 -0
- package/dist/files/wesmart-db.d.ts +229 -0
- package/dist/files/wesmart-db.js +412 -0
- package/dist/gestures.d.ts +19 -0
- package/dist/gestures.js +12 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/refusal.d.ts +2 -11
- package/dist/refusal.js +5 -29
- package/package.json +12 -11
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ it speaks to.
|
|
|
16
16
|
| `src/gestures.ts` | the gestures the command line and the MCP server share, one code path for both: the reads of the six objects, series, parameters, indicators, estimates, settlements and actions; the writes through the schema's function for each gesture; the keys; and `compute()`, which runs a module's computation handed in as `Computations` |
|
|
17
17
|
| `src/geocoders.ts` | the geocoder chain of a surface, from the connectors the person named (`NEXGEN_GEOCODERS`), keyed by the countries and regions their `sources` rows serve |
|
|
18
18
|
| `src/version.ts` | `compareVersions()` and `nexgenSchemaFloor()`, which refuses a database older than a surface (`schema_too_old`) |
|
|
19
|
-
| `src/files/` | the importers and exporters of the format libraries the core's objects need: `exportGeoJson` (the participants, their devices and, when the `building` module is installed, their ground, buildings and surfaces; the document it writes is described by [`schema/geojson-export.1.json`](schema/geojson-export.1.json)), `importGeoJsonParticipants` (the participants of a collection, at their place), `importWeatherFile`, `exportWeatherYear`, `exportSeriesParquet`, `importEvccCatalogue` (the products evcc's templates drive, into the catalogue `products`, with the templates a device of each model can be driven through), `importProject` (a project described as data, from whatever another system holds, found again on every replay), `importProjectSheet` (the projects of a sheet a spreadsheet saved, through `importProject`), `importPvsolStudy` (a PV\*SOL study: its participant and inverters through `importProject`, its PV system and arrays through `describePvSystem`); each writes the plain shapes of a library through the gestures of the schema (`install_device`, `set_attribute`, `add_identifier`, `locate_participant`, `series_ensure`, `store`) and reads them back. The importers of buildings — CityJSON, IFC, a Home Assistant home — are the `building` module's ([`modules/building/src`](../../modules/building/src/index.ts)) |
|
|
19
|
+
| `src/files/` | the importers and exporters of the format libraries the core's objects need: `exportGeoJson` (the participants, their devices and, when the `building` module is installed, their ground, buildings and surfaces; the document it writes is described by [`schema/geojson-export.1.json`](schema/geojson-export.1.json)), `importGeoJsonParticipants` (the participants of a collection, at their place), `importWeatherFile`, `exportWeatherYear`, `exportSeriesParquet`, `importEvccCatalogue` (the products evcc's templates drive, into the catalogue `products`, with the templates a device of each model can be driven through), `importProject` (a project described as data, from whatever another system holds, found again on every replay), `importProjectSheet` (the projects of a sheet a spreadsheet saved, through `importProject`), `importPvsolStudy` (a PV\*SOL study: its participant and inverters through `importProject`, its PV system and arrays through `describePvSystem`), `planWesmartImport` with `importWesmartHistory` (the history of a WeSmart Database table to table: organisations, projects, participants with their place, the meter of `smart_meter_id`, the devices of each participant, found again on a second import by the `imported_row` identifier); each writes the plain shapes of a library through the gestures of the schema (`install_device`, `set_attribute`, `add_identifier`, `locate_participant`, `series_ensure`, `store`) and reads them back. The importers of buildings — CityJSON, IFC, a Home Assistant home — are the `building` module's ([`modules/building/src`](../../modules/building/src/index.ts)) |
|
|
20
20
|
|
|
21
21
|
The geocoders are connectors (`@nexgen-os/nominatim`, `photon`, `ban`,
|
|
22
22
|
`geopunt`, `urbis`, `icar`) and import this package and nothing else of
|
package/dist/address.d.ts
CHANGED
|
@@ -1,64 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
/** Street and number, as the country writes them. */
|
|
4
|
-
address_line1?: string | null;
|
|
5
|
-
/** Building, floor, unit, box: the second line, when there is one. */
|
|
6
|
-
address_line2?: string | null;
|
|
7
|
-
postal_code?: string | null;
|
|
8
|
-
city?: string | null;
|
|
9
|
-
/** ISO 3166-2 subdivision when the geocoder knows it. */
|
|
10
|
-
region?: string | null;
|
|
11
|
-
/** The municipality in the country's own code (INSEE, NIS, CBS) when the geocoder gives it. */
|
|
12
|
-
municipality_code?: string | null;
|
|
13
|
-
/** ISO 3166-1 alpha-2, upper case. */
|
|
14
|
-
country: string;
|
|
15
|
-
}
|
|
16
|
-
/** How close a point is to the real place. The values of `participants.location_precision`. */
|
|
17
|
-
export type Precision = 'rooftop' | 'parcel' | 'street' | 'postal_code' | 'city' | 'region';
|
|
18
|
-
/** One place a geocoder answered with. */
|
|
19
|
-
export interface Place {
|
|
20
|
-
address: Address;
|
|
21
|
-
lon: number;
|
|
22
|
-
lat: number;
|
|
23
|
-
precision: Precision;
|
|
24
|
-
/** The `sources.code` of the geocoder, so the participant says who placed it. */
|
|
25
|
-
source_code: string;
|
|
26
|
-
/** What the geocoder calls it, for a list of candidates. */
|
|
27
|
-
label: string;
|
|
28
|
-
/** The identifier the geocoder knows the place by, in an `identifier_schemes` scheme. */
|
|
29
|
-
identifier?: {
|
|
30
|
-
scheme: string;
|
|
31
|
-
value: string;
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
export interface SearchOptions {
|
|
35
|
-
/** Restrict to one country, ISO 3166-1 alpha-2 (any case). */
|
|
36
|
-
country?: string;
|
|
37
|
-
/** Prefer places near this point. */
|
|
38
|
-
near?: {
|
|
39
|
-
lon: number;
|
|
40
|
-
lat: number;
|
|
41
|
-
};
|
|
42
|
-
/** At most this many places; the adapter's default otherwise. */
|
|
43
|
-
limit?: number;
|
|
44
|
-
/** Language of the labels, BCP 47 (`fr`, `pt-BR`). */
|
|
45
|
-
language?: string;
|
|
46
|
-
}
|
|
47
|
-
export interface Geocoder {
|
|
48
|
-
readonly source_code: string;
|
|
49
|
-
/** Places matching a typed address or name, best first. Empty when none. */
|
|
50
|
-
search(text: string, options?: SearchOptions): Promise<Place[]>;
|
|
51
|
-
/** The address at a point, or null when the geocoder has none there. */
|
|
52
|
-
reverse(lon: number, lat: number, options?: Pick<SearchOptions, 'language'>): Promise<Place | null>;
|
|
53
|
-
}
|
|
54
|
-
/** The subset of `fetch` the adapters use, so a test or a proxy can stand in. */
|
|
55
|
-
export type Fetch = (url: string, init?: {
|
|
56
|
-
headers?: Record<string, string>;
|
|
57
|
-
}) => Promise<{
|
|
58
|
-
ok: boolean;
|
|
59
|
-
status: number;
|
|
60
|
-
json(): Promise<unknown>;
|
|
61
|
-
}>;
|
|
1
|
+
import type { Address, Geocoder, Place } from '@nexgen-os/connector';
|
|
2
|
+
export type { Address, Fetch, Geocoder, Place, Precision, SearchOptions } from '@nexgen-os/connector';
|
|
62
3
|
/**
|
|
63
4
|
* The place columns of `participants` a place fills: the address, the
|
|
64
5
|
* municipality in its country's own code, the point as GeoJSON, the
|
|
@@ -76,7 +17,7 @@ export declare function placeFields(place: Place): {
|
|
|
76
17
|
readonly type: "Point";
|
|
77
18
|
readonly coordinates: readonly [number, number, 0];
|
|
78
19
|
};
|
|
79
|
-
location_precision: Precision;
|
|
20
|
+
location_precision: import("@nexgen-os/connector").Precision;
|
|
80
21
|
location_source_code: string;
|
|
81
22
|
};
|
|
82
23
|
/**
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Db } from '../db.js';
|
|
2
|
+
import { type DeviceOfSource } from './devices.js';
|
|
3
|
+
/** An activation as the platform that asked it gives it. */
|
|
4
|
+
export interface ReceivedActivation {
|
|
5
|
+
/** The device, by its identifier in the provider's scheme. */
|
|
6
|
+
identifier: string;
|
|
7
|
+
/** `up`: more injection or less offtake; `down`: the reverse, as the grid sees the device. */
|
|
8
|
+
direction: 'up' | 'down';
|
|
9
|
+
/** Its start and end, ISO 8601 with an offset. */
|
|
10
|
+
start_at: string;
|
|
11
|
+
end_at: string;
|
|
12
|
+
/** The power asked, kW, a change from the operating point of the device. */
|
|
13
|
+
requested_kw: number;
|
|
14
|
+
/** The platform's reference of the activation: an activation is kept once per device and reference. */
|
|
15
|
+
external_ref: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The activations a platform asked of the devices it is given, between
|
|
19
|
+
* `from` and `to` (ISO 8601 instants, `to` excluded).
|
|
20
|
+
* `collectActivations()` calls it and keeps what it returns.
|
|
21
|
+
*/
|
|
22
|
+
export interface ActivationProvider {
|
|
23
|
+
readonly source_code: string;
|
|
24
|
+
/** The identifier scheme the platform names its devices by (`identifier_schemes.code`). */
|
|
25
|
+
readonly scheme: string;
|
|
26
|
+
/** The market role of who asks through this platform: `aggregator`, `grid_operator`, `energy_manager`… */
|
|
27
|
+
readonly role: string;
|
|
28
|
+
activations(devices: DeviceOfSource[], from: string, to: string): Promise<ReceivedActivation[]>;
|
|
29
|
+
}
|
|
30
|
+
export interface ActivationRequest {
|
|
31
|
+
/** The organisation's `connectors` row of the provider's source. */
|
|
32
|
+
connectorId: string;
|
|
33
|
+
/** The activations asked for, ISO 8601 instants, `to` excluded. */
|
|
34
|
+
from: string;
|
|
35
|
+
to: string;
|
|
36
|
+
}
|
|
37
|
+
export interface CollectedActivation {
|
|
38
|
+
external_ref: string;
|
|
39
|
+
identifier: string;
|
|
40
|
+
/** The device, when the identifier names one. */
|
|
41
|
+
device_id: string | null;
|
|
42
|
+
/** The activation kept, or the one kept before for the same device and reference. */
|
|
43
|
+
activation_id: string | null;
|
|
44
|
+
/** `kept`: a new activation and its proposed command; `already_received`: kept before; `refused`: nothing written. */
|
|
45
|
+
status: 'kept' | 'already_received' | 'refused';
|
|
46
|
+
/** Why it was refused, by name: `identifier_unknown`, `external_ref_required`, or what `flexibility.activate()` refuses. */
|
|
47
|
+
refusal: string | null;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Asks the platform of a connector for the activations of the flexible
|
|
51
|
+
* devices of its organisation that carry an identifier in the provider's
|
|
52
|
+
* scheme, and keeps each as an activation of the `flexibility` module with
|
|
53
|
+
* the provider's role and the platform's reference; its command is proposed
|
|
54
|
+
* (`source = 'detector'`) and waits for a person. An activation received
|
|
55
|
+
* again — same device, same reference — is not kept twice. One that names
|
|
56
|
+
* an unknown identifier, has no reference or is refused by
|
|
57
|
+
* `flexibility.activate()` writes nothing and says why, the others are kept.
|
|
58
|
+
* Refuses `module_not_installed` without the `flexibility` module,
|
|
59
|
+
* `connector_not_found`, `connector_source_mismatch`.
|
|
60
|
+
*/
|
|
61
|
+
export declare function collectActivations(db: Db, provider: ActivationProvider, request: ActivationRequest): Promise<{
|
|
62
|
+
received: CollectedActivation[];
|
|
63
|
+
}>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// An aggregator connector: the activations an aggregator, a grid operator or
|
|
2
|
+
// an energy manager asked of the flexible devices it knows, kept as
|
|
3
|
+
// activations of the `flexibility` module (decision 0058).
|
|
4
|
+
//
|
|
5
|
+
// The platform knows a device by an identifier in its scheme, the
|
|
6
|
+
// `identifiers` row of the device, as a device connector does (decision
|
|
7
|
+
// 0061). A device is declared to the platform by that identifier and by
|
|
8
|
+
// its row in `flexibility.devices`, which says what it can be asked; nothing
|
|
9
|
+
// else is declared. `collectActivations()` is the one path from what the
|
|
10
|
+
// platform asked to the database: each activation goes through
|
|
11
|
+
// `flexibility.activate()`, checked against what the device declares and
|
|
12
|
+
// carried out as a proposed command of the `actions` module, which a person
|
|
13
|
+
// validates. What the device delivered is never sent back from here: it is
|
|
14
|
+
// read from the series of its meter.
|
|
15
|
+
import { GestureError } from '../refusal.js';
|
|
16
|
+
import { connectorOf } from './devices.js';
|
|
17
|
+
/**
|
|
18
|
+
* Asks the platform of a connector for the activations of the flexible
|
|
19
|
+
* devices of its organisation that carry an identifier in the provider's
|
|
20
|
+
* scheme, and keeps each as an activation of the `flexibility` module with
|
|
21
|
+
* the provider's role and the platform's reference; its command is proposed
|
|
22
|
+
* (`source = 'detector'`) and waits for a person. An activation received
|
|
23
|
+
* again — same device, same reference — is not kept twice. One that names
|
|
24
|
+
* an unknown identifier, has no reference or is refused by
|
|
25
|
+
* `flexibility.activate()` writes nothing and says why, the others are kept.
|
|
26
|
+
* Refuses `module_not_installed` without the `flexibility` module,
|
|
27
|
+
* `connector_not_found`, `connector_source_mismatch`.
|
|
28
|
+
*/
|
|
29
|
+
export async function collectActivations(db, provider, request) {
|
|
30
|
+
const installed = (await db.query(`select to_regclass('flexibility.activations') is not null ok`)).rows[0].ok;
|
|
31
|
+
if (!installed)
|
|
32
|
+
throw new GestureError('module_not_installed: an activation is kept by the flexibility module, which is not installed');
|
|
33
|
+
const connector = await connectorOf(db, request.connectorId, provider.source_code);
|
|
34
|
+
const devices = (await db.query(`select distinct on (d.id) d.id, d.device_type, i.value identifier
|
|
35
|
+
from flexibility.devices f
|
|
36
|
+
join devices d on d.id = f.id
|
|
37
|
+
join identifiers i on i.object_kind = 'device' and i.object_id = d.id and i.scheme = $2
|
|
38
|
+
where d.organisation_id = $1
|
|
39
|
+
and (i.valid_from is null or i.valid_from <= now()) and (i.valid_to is null or i.valid_to > now())
|
|
40
|
+
order by d.id, i.valid_from desc nulls last`, [connector.organisation_id, provider.scheme])).rows;
|
|
41
|
+
if (devices.length === 0)
|
|
42
|
+
return { received: [] };
|
|
43
|
+
const byIdentifier = new Map(devices.map((d) => [d.identifier, d.id]));
|
|
44
|
+
const asked = await provider.activations(devices.map((d) => ({ identifier: d.identifier, scheme: provider.scheme, device_type: d.device_type })), request.from, request.to);
|
|
45
|
+
const received = [];
|
|
46
|
+
for (const activation of asked) {
|
|
47
|
+
const deviceId = byIdentifier.get(activation.identifier) ?? null;
|
|
48
|
+
const base = { external_ref: activation.external_ref, identifier: activation.identifier, device_id: deviceId };
|
|
49
|
+
if (!deviceId) {
|
|
50
|
+
received.push({ ...base, activation_id: null, status: 'refused', refusal: 'identifier_unknown' });
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (!activation.external_ref?.trim()) {
|
|
54
|
+
received.push({ ...base, activation_id: null, status: 'refused', refusal: 'external_ref_required' });
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const before = (await db.query(`select id from flexibility.activations where device_id = $1 and external_ref = $2 order by created_at limit 1`, [deviceId, activation.external_ref])).rows[0];
|
|
58
|
+
if (before) {
|
|
59
|
+
received.push({ ...base, activation_id: before.id, status: 'already_received', refusal: null });
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
const { id } = (await db.query(`select flexibility.activate($1, $2, $3::timestamptz, $4::timestamptz, $5, null, $6, $7, 'detector') id`, [deviceId, activation.direction, activation.start_at, activation.end_at, activation.requested_kw,
|
|
64
|
+
provider.role, activation.external_ref])).rows[0];
|
|
65
|
+
received.push({ ...base, activation_id: id, status: 'kept', refusal: null });
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
69
|
+
received.push({ ...base, activation_id: null, status: 'refused', refusal: message.split(':')[0].trim() });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return { received };
|
|
73
|
+
}
|
|
@@ -60,6 +60,12 @@ export interface CommandCarrier {
|
|
|
60
60
|
}
|
|
61
61
|
/** Records what a device answered to a command; `actions.report()` by default. */
|
|
62
62
|
export type CommandReport = (db: Db, actionId: string, at: string, failure: string | null) => Promise<void>;
|
|
63
|
+
interface ConnectorRow {
|
|
64
|
+
organisation_id: string;
|
|
65
|
+
source_code: string;
|
|
66
|
+
}
|
|
67
|
+
/** The organisation's `connectors` row, checked against the source a connector reads. Refuses `connector_not_found`, `connector_source_mismatch`. */
|
|
68
|
+
export declare function connectorOf(db: Db, connectorId: string, sourceCode: string): Promise<ConnectorRow>;
|
|
63
69
|
/**
|
|
64
70
|
* The device as a platform knows it: its type and its identifier in the
|
|
65
71
|
* scheme, of the connector's organisation. Refuses `device_not_found`,
|
|
@@ -123,3 +129,4 @@ export interface CarriedCommand {
|
|
|
123
129
|
export declare function carryCommands(db: Db, carrier: CommandCarrier, request: CarryRequest): Promise<{
|
|
124
130
|
carried: CarriedCommand[];
|
|
125
131
|
}>;
|
|
132
|
+
export {};
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
// deciding when to command a device is an application's.
|
|
19
19
|
import { GestureError } from '../refusal.js';
|
|
20
20
|
import { storeSeries } from './interfaces.js';
|
|
21
|
-
|
|
21
|
+
/** The organisation's `connectors` row, checked against the source a connector reads. Refuses `connector_not_found`, `connector_source_mismatch`. */
|
|
22
|
+
export async function connectorOf(db, connectorId, sourceCode) {
|
|
22
23
|
const connector = (await db.query(`select organisation_id, source_code from connectors where id = $1`, [connectorId])).rows[0];
|
|
23
24
|
if (!connector)
|
|
24
25
|
throw new GestureError(`connector_not_found: ${connectorId}`);
|
|
@@ -1,86 +1,6 @@
|
|
|
1
1
|
import type { Db } from '../db.js';
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import type { MandateScope, MeterDataHub, MeterMandate } from './meter-data.js';
|
|
5
|
-
export interface Point {
|
|
6
|
-
lon: number;
|
|
7
|
-
lat: number;
|
|
8
|
-
}
|
|
9
|
-
/** What a connector brings back for one quantity: ready for `storeSeries()`. */
|
|
10
|
-
export interface CollectedSeries {
|
|
11
|
-
quantity_code: string;
|
|
12
|
-
/** What one value means: `interval` from `at` for one `step`, `instant` at `at`, `cumulative` register. */
|
|
13
|
-
value_kind: 'interval' | 'instant' | 'cumulative';
|
|
14
|
-
/** A Postgres interval (`1 hour`, `15 minutes`), null when irregular. */
|
|
15
|
-
step: string | null;
|
|
16
|
-
/** The unit the values are in, by QUDT code or symbol (`Wh`, `°C`); the quantity's own when absent. `store()` converts. */
|
|
17
|
-
unit?: string;
|
|
18
|
-
/** Tells apart two series of one object and quantity the connector brings at once (the dials of an index); the target's name otherwise. */
|
|
19
|
-
name?: string | null;
|
|
20
|
-
values: SeriesPoint[];
|
|
21
|
-
}
|
|
22
|
-
/** Weather observed or forecast at a point: a model, a station, an API. */
|
|
23
|
-
export interface WeatherProvider {
|
|
24
|
-
readonly source_code: string;
|
|
25
|
-
/** The forecast from now on, at the hourly step the model gives, with the time it was issued. */
|
|
26
|
-
forecast(where: Point, options?: {
|
|
27
|
-
hours?: number;
|
|
28
|
-
quantities?: string[];
|
|
29
|
-
}): Promise<{
|
|
30
|
-
issued_at: string;
|
|
31
|
-
series: CollectedSeries[];
|
|
32
|
-
}>;
|
|
33
|
-
/**
|
|
34
|
-
* Past weather, when the source has it (an archive, a reanalysis), for the
|
|
35
|
-
* days `from` to `to` included (`YYYY-MM-DD`, UTC). Irradiance on a plane
|
|
36
|
-
* (`poa_irradiance`) needs the plane: `tilt_deg` from horizontal and
|
|
37
|
-
* `azimuth_deg` clockwise from true north. `model` names one of the
|
|
38
|
-
* source's datasets (a reanalysis, a station network) when it has several.
|
|
39
|
-
*/
|
|
40
|
-
history?(where: Point, from: string, to: string, options?: {
|
|
41
|
-
quantities?: string[];
|
|
42
|
-
tilt_deg?: number;
|
|
43
|
-
azimuth_deg?: number;
|
|
44
|
-
model?: string;
|
|
45
|
-
}): Promise<CollectedSeries[]>;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* The core's azimuth (degrees clockwise from true north) counted from south,
|
|
49
|
-
* west positive, in (-180, 180]: the convention of PVGIS (`aspect`),
|
|
50
|
-
* Open-Meteo (`azimuth`) and PVsyst.
|
|
51
|
-
*/
|
|
52
|
-
export declare function azimuthFromSouth(azimuthDeg: number): number;
|
|
53
|
-
/** A meter as its operator's hub knows it: its identifier in a scheme, and what it can do. */
|
|
54
|
-
export interface MeterOfHub {
|
|
55
|
-
identifier: string;
|
|
56
|
-
scheme: string;
|
|
57
|
-
/** Take from the grid, inject into it, or both (`meters.direction`). */
|
|
58
|
-
direction?: 'offtake' | 'injection' | 'both';
|
|
59
|
-
}
|
|
60
|
-
/** What a reading of a meter is given beside the meter and the period. */
|
|
61
|
-
export interface MeterReadingOptions {
|
|
62
|
-
/** The step asked, an ISO 8601 duration (`PT30M`); the hub's own when absent. */
|
|
63
|
-
step?: string;
|
|
64
|
-
/** What is read; `load_curve` by default. */
|
|
65
|
-
scope?: MandateScope;
|
|
66
|
-
/**
|
|
67
|
-
* The mandate `grid.authorise_reading()` returned: a connector whose
|
|
68
|
-
* source reads under mandate refuses without it (`requireMandate()`).
|
|
69
|
-
*/
|
|
70
|
-
mandate?: MeterMandate | null;
|
|
71
|
-
/** The conventions of the operator's hub, from its pack row: its time zone and its quality flags. */
|
|
72
|
-
hub?: MeterDataHub;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Readings of a meter from the grid operator's hub or a data hub, by its
|
|
76
|
-
* identifier and scheme, from `from` to `to` (`YYYY-MM-DD`, `to` excluded).
|
|
77
|
-
* `collectMeterReadings()` checks the mandate, calls it and stores what it
|
|
78
|
-
* returns on the meter.
|
|
79
|
-
*/
|
|
80
|
-
export interface MeterDataProvider {
|
|
81
|
-
readonly source_code: string;
|
|
82
|
-
readings(meter: MeterOfHub, from: string, to: string, options?: MeterReadingOptions): Promise<CollectedSeries[]>;
|
|
83
|
-
}
|
|
2
|
+
export { azimuthFromSouth, type ChargerReading, type ChargingSessionRecord, type CollectedSeries, type MeterDataProvider, type MeterOfHub, type MeterReadingOptions, type Point, type SourceRow, type WeatherProvider, } from '@nexgen-os/connector';
|
|
3
|
+
import type { Address, CollectedSeries, Point, SourceRow } from '@nexgen-os/connector';
|
|
84
4
|
/** A place a register or a map knows: becomes a participant or a building, with its identifiers and attributes. */
|
|
85
5
|
export interface PlaceRecord {
|
|
86
6
|
name: string;
|
|
@@ -132,28 +52,6 @@ export declare function storeSeries(db: Db, target: SeriesTarget, series: Collec
|
|
|
132
52
|
seriesId: string;
|
|
133
53
|
inserted: number;
|
|
134
54
|
}>;
|
|
135
|
-
/**
|
|
136
|
-
* A `sources` row as a connector package ships it (decision 0011): what
|
|
137
|
-
* exists outside, who serves it, its licence and how long what it gives may
|
|
138
|
-
* be kept. The countries a source serves are data of its row, never code.
|
|
139
|
-
*/
|
|
140
|
-
export interface SourceRow {
|
|
141
|
-
code: string;
|
|
142
|
-
name: string;
|
|
143
|
-
description?: string | null;
|
|
144
|
-
kind?: 'api' | 'file' | 'registry' | 'device' | 'manual' | 'text';
|
|
145
|
-
base_url?: string | null;
|
|
146
|
-
docs_url?: string | null;
|
|
147
|
-
auth?: 'none' | 'api_key' | 'oauth2' | 'certificate' | 'mandate';
|
|
148
|
-
countries?: string[];
|
|
149
|
-
regions?: string[];
|
|
150
|
-
license?: string | null;
|
|
151
|
-
attribution?: string | null;
|
|
152
|
-
retention_days?: number | null;
|
|
153
|
-
redistributable?: boolean;
|
|
154
|
-
/** The package that brings it, `@nexgen-os/<name>`. */
|
|
155
|
-
published_by: string;
|
|
156
|
-
}
|
|
157
55
|
/**
|
|
158
56
|
* Writes the `sources` row a connector package ships, once: inserted, or
|
|
159
57
|
* updated where that package published it; a row another publisher owns is
|
|
@@ -15,17 +15,10 @@
|
|
|
15
15
|
// own data service). A private package implements the same interface and
|
|
16
16
|
// registers its `sources` rows; the core and the pages never import it.
|
|
17
17
|
import { store } from '../series.js';
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
*/
|
|
23
|
-
export function azimuthFromSouth(azimuthDeg) {
|
|
24
|
-
let a = ((azimuthDeg - 180) % 360 + 360) % 360;
|
|
25
|
-
if (a > 180)
|
|
26
|
-
a -= 360;
|
|
27
|
-
return a === -0 ? 0 : a;
|
|
28
|
-
}
|
|
18
|
+
// What a connector speaks lives in `@nexgen-os/connector`, MIT, so
|
|
19
|
+
// that a connector runs without the core (decision 0064); the core
|
|
20
|
+
// re-exports it, one definition for both.
|
|
21
|
+
export { azimuthFromSouth, } from '@nexgen-os/connector';
|
|
29
22
|
/**
|
|
30
23
|
* Writes what a connector brought back through `store()`, the one write
|
|
31
24
|
* path: the series of that object, quantity, kind, source and name, found or created once (a different value kind or step is refused
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { MeasuredObjects } from '@nexgen-os/connector';
|
|
2
|
+
import type { Db } from '../db.js';
|
|
3
|
+
export type { MeasuredFound, MeasuredObjects, MeasuredTarget } from '@nexgen-os/connector';
|
|
4
|
+
/**
|
|
5
|
+
* The meters and devices of an installation, as a stream of readings asks
|
|
6
|
+
* for them: by an identifier valid now, refused by name when it names none
|
|
7
|
+
* (`identifier_unknown`), objects in more than one organisation
|
|
8
|
+
* (`identifier_ambiguous`), something else than a meter or a device
|
|
9
|
+
* (`object_not_measured`), or an object whose participant has no time zone
|
|
10
|
+
* (`time_zone_required`); written as `measured` series of that object.
|
|
11
|
+
*/
|
|
12
|
+
export declare function measuredObjects(db: Db): MeasuredObjects;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// The meters and devices a stream of readings lands on, found and written by
|
|
2
|
+
// the core for the connector that receives them (`MeasuredObjects` of
|
|
3
|
+
// `@nexgen-os/connector`): the connector names an object by an identifier
|
|
4
|
+
// and hands its series; this reads the identifier under the connection's
|
|
5
|
+
// rights and writes through `store()`, so that the connector reaches
|
|
6
|
+
// neither the database nor the core (decision 0064).
|
|
7
|
+
import { storeSeries } from './interfaces.js';
|
|
8
|
+
/**
|
|
9
|
+
* The meters and devices of an installation, as a stream of readings asks
|
|
10
|
+
* for them: by an identifier valid now, refused by name when it names none
|
|
11
|
+
* (`identifier_unknown`), objects in more than one organisation
|
|
12
|
+
* (`identifier_ambiguous`), something else than a meter or a device
|
|
13
|
+
* (`object_not_measured`), or an object whose participant has no time zone
|
|
14
|
+
* (`time_zone_required`); written as `measured` series of that object.
|
|
15
|
+
*/
|
|
16
|
+
export function measuredObjects(db) {
|
|
17
|
+
return {
|
|
18
|
+
async byIdentifier(scheme, value, options = {}) {
|
|
19
|
+
const { rows } = await db.query(`select i.organisation_id::text, i.object_kind, i.object_id::text, p.time_zone
|
|
20
|
+
from identifiers i
|
|
21
|
+
left join participants p on p.id = participant_of(i.object_kind, i.object_id)
|
|
22
|
+
where i.scheme = $1 and i.value = $2
|
|
23
|
+
and (i.valid_from is null or i.valid_from <= now())
|
|
24
|
+
and (i.valid_to is null or i.valid_to > now())
|
|
25
|
+
and ($3::uuid is null or i.organisation_id = $3::uuid)`, [scheme, value, options.organisationId ?? null]);
|
|
26
|
+
const row = rows[0];
|
|
27
|
+
if (!row)
|
|
28
|
+
return { problem: { code: 'identifier_unknown', detail: `no meter or device is known by ${scheme} ${value}` } };
|
|
29
|
+
if (rows.length > 1) {
|
|
30
|
+
return { problem: { code: 'identifier_ambiguous', detail: `${scheme} ${value} names an object in ${rows.length} organisations; give organisationId` } };
|
|
31
|
+
}
|
|
32
|
+
if (row.object_kind !== 'meter' && row.object_kind !== 'device') {
|
|
33
|
+
return { problem: { code: 'object_not_measured', detail: `${scheme} ${value} names a ${row.object_kind}, not a meter or a device` } };
|
|
34
|
+
}
|
|
35
|
+
if (!row.time_zone) {
|
|
36
|
+
return { problem: { code: 'time_zone_required', detail: `the participant of ${row.object_kind} ${row.object_id} has no time zone: the meter's clock cannot be dated` } };
|
|
37
|
+
}
|
|
38
|
+
return { target: { organisationId: row.organisation_id, objectKind: row.object_kind, objectId: row.object_id, timeZone: row.time_zone } };
|
|
39
|
+
},
|
|
40
|
+
write(target, sourceCode, series) {
|
|
41
|
+
return storeSeries(db, { organisationId: target.organisationId, objectKind: target.objectKind, objectId: target.objectId, sourceCode, kind: 'measured' }, series);
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -1,39 +1,7 @@
|
|
|
1
1
|
import type { Db } from '../db.js';
|
|
2
2
|
import { type MeterDataProvider } from './interfaces.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/** The approved mandate `grid.authorise_reading()` found: what a connector under mandate reads with. */
|
|
6
|
-
export interface MeterMandate {
|
|
7
|
-
id: string;
|
|
8
|
-
/** The operator whose hub the data is read at (`grid.operators.code`). */
|
|
9
|
-
operator: string;
|
|
10
|
-
scope: MandateScope[];
|
|
11
|
-
/** The finest step it allows, an ISO 8601 duration. */
|
|
12
|
-
granularity: string;
|
|
13
|
-
/** The readings it covers, `YYYY-MM-DD`, null when open. */
|
|
14
|
-
data_from: string | null;
|
|
15
|
-
data_to: string | null;
|
|
16
|
-
valid_to: string | null;
|
|
17
|
-
/** The reference the operator or the intermediary gave the consent. */
|
|
18
|
-
external_reference: string | null;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* The conventions of an operator's data hub, read from its pack row
|
|
22
|
-
* (`grid.operators.meter_data`): never written in a connector.
|
|
23
|
-
*/
|
|
24
|
-
export interface MeterDataHub {
|
|
25
|
-
operator: string;
|
|
26
|
-
/** The IANA time zone the hub's files date their steps in, when they carry no offset. */
|
|
27
|
-
time_zone: string | null;
|
|
28
|
-
/** The hub's quality flags mapped to the core's codes (0–3) and the pack's (100 and up). */
|
|
29
|
-
quality_codes: Record<string, number>;
|
|
30
|
-
/** The sources that read this hub, in the pack's order. */
|
|
31
|
-
sources: string[];
|
|
32
|
-
/** How far back the hub serves readings, in months, when it says. */
|
|
33
|
-
history_months: number | null;
|
|
34
|
-
/** The steps the hub publishes load curves at, ISO 8601 durations. */
|
|
35
|
-
steps: string[];
|
|
36
|
-
}
|
|
3
|
+
export { requireMandate, type MandateScope, type MeterDataHub, type MeterMandate } from '@nexgen-os/connector';
|
|
4
|
+
import type { MandateScope, MeterDataHub, MeterMandate } from '@nexgen-os/connector';
|
|
37
5
|
export interface ReadingRequest {
|
|
38
6
|
/** The organisation's `connectors` row: its source says whether a mandate is needed. */
|
|
39
7
|
connectorId: string;
|
|
@@ -55,11 +23,6 @@ export interface ReadingRequest {
|
|
|
55
23
|
* `mandate_scope`, `connector_not_found`, `meter_not_found`).
|
|
56
24
|
*/
|
|
57
25
|
export declare function authoriseReading(db: Db, request: ReadingRequest): Promise<MeterMandate | null>;
|
|
58
|
-
/**
|
|
59
|
-
* Refuses a reading under mandate without the mandate the check returned:
|
|
60
|
-
* what every connector whose source has `auth = 'mandate'` calls first.
|
|
61
|
-
*/
|
|
62
|
-
export declare function requireMandate(sourceCode: string, mandate: MeterMandate | null | undefined, scope?: MandateScope): MeterMandate;
|
|
63
26
|
/** The conventions of the hub of the operator that runs a meter, from its pack row. */
|
|
64
27
|
export declare function meterDataHub(db: Db, meterId: string, operator?: string): Promise<MeterDataHub>;
|
|
65
28
|
/**
|
|
@@ -16,6 +16,9 @@
|
|
|
16
16
|
// database, so it holds the caller to having asked.
|
|
17
17
|
import { GestureError } from '../refusal.js';
|
|
18
18
|
import { storeSeries } from './interfaces.js';
|
|
19
|
+
// What a connector of a meter data hub speaks lives in `@nexgen-os/connector`,
|
|
20
|
+
// MIT (decision 0064); the core re-exports it.
|
|
21
|
+
export { requireMandate } from '@nexgen-os/connector';
|
|
19
22
|
/**
|
|
20
23
|
* The twin of `grid.authorise_reading()`: the approved mandate that allows
|
|
21
24
|
* the reading, null when the connector's source reads without one. Refuses
|
|
@@ -31,19 +34,6 @@ export async function authoriseReading(db, request) {
|
|
|
31
34
|
from grid.mandates where id = $1`, [id]);
|
|
32
35
|
return mandate.rows[0];
|
|
33
36
|
}
|
|
34
|
-
/**
|
|
35
|
-
* Refuses a reading under mandate without the mandate the check returned:
|
|
36
|
-
* what every connector whose source has `auth = 'mandate'` calls first.
|
|
37
|
-
*/
|
|
38
|
-
export function requireMandate(sourceCode, mandate, scope = 'load_curve') {
|
|
39
|
-
if (!mandate) {
|
|
40
|
-
throw new GestureError(`mandate_missing: ${sourceCode} reads under mandate; pass the mandate grid.authorise_reading() returned`);
|
|
41
|
-
}
|
|
42
|
-
if (!mandate.scope.includes(scope)) {
|
|
43
|
-
throw new GestureError(`mandate_scope: mandate ${mandate.id} does not allow ${scope}`);
|
|
44
|
-
}
|
|
45
|
-
return mandate;
|
|
46
|
-
}
|
|
47
37
|
/** The conventions of the hub of the operator that runs a meter, from its pack row. */
|
|
48
38
|
export async function meterDataHub(db, meterId, operator) {
|
|
49
39
|
const { rows } = await db.query(`select o.code, o.meter_data from grid.operators o
|
package/dist/files/dsmr.d.ts
CHANGED
|
@@ -1,39 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* The readings of a P1 port, once read by `@nexgen-os/dsmr`, as the series
|
|
3
|
-
* of a meter, ready for `storeSeries()`: the electricity meter on channel 0,
|
|
4
|
-
* or a gas, water or heat meter of its bus on channel 1 to 4. One mapping,
|
|
5
|
-
* whichever way the readings came (a telegram on the port, a P1M5 message
|
|
6
|
-
* through a broker, a file of them).
|
|
7
|
-
*
|
|
8
|
-
* | Reading | Quantity | Value kind | Name |
|
|
9
|
-
* |---|---|---|---|
|
|
10
|
-
* | energy register, offtake / injection | `offtake_energy` / `injection_energy` | cumulative | `tariff <n>`; none for the total |
|
|
11
|
-
* | power, offtake / injection | `offtake_power` / `injection_power` | instant | the phase, none for the connection |
|
|
12
|
-
* | voltage | `voltage` | instant | the phase |
|
|
13
|
-
* | volume of a meter of the bus | `volume` | cumulative, at the stamp that meter sent | none |
|
|
14
|
-
*
|
|
15
|
-
* Every value is dated by the meter's clock, never by its reception. A
|
|
16
|
-
* reading that cannot be dated or whose checksum does not match is left out
|
|
17
|
-
* whole and said in `skipped`, with its problems; a stream of readings is not
|
|
18
|
-
* refused for one bad telegram. Values stay in the meter's unit, which
|
|
19
|
-
* `store()` converts. The running quarter-hour demand and the month's
|
|
20
|
-
* maximum are left out: the core computes demand from the series. A current
|
|
21
|
-
* is left out until the core's vocabulary carries `current`.
|
|
22
|
-
*/
|
|
23
|
-
import type { DsmrProblem, DsmrReading } from '@nexgen-os/dsmr';
|
|
24
|
-
import type { CollectedSeries } from '../connectors/interfaces.js';
|
|
25
|
-
export interface DsmrMeterSeries {
|
|
26
|
-
series: CollectedSeries[];
|
|
27
|
-
/** The readings left out, by their index in the input, with the problems that did it. */
|
|
28
|
-
skipped: {
|
|
29
|
-
index: number;
|
|
30
|
-
problems: DsmrProblem[];
|
|
31
|
-
}[];
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* The series of one meter in readings of a P1 port: the electricity meter
|
|
35
|
-
* (`channel` 0, the default) or the meter of the bus on that channel.
|
|
36
|
-
*/
|
|
37
|
-
export declare function meterSeriesOfDsmr(readings: DsmrReading[], options?: {
|
|
38
|
-
channel?: number;
|
|
39
|
-
}): DsmrMeterSeries;
|
|
1
|
+
export { meterSeriesOfDsmr, type DsmrMeterSeries } from '@nexgen-os/connector';
|