@nexgen-os/core 0.4.2 → 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 +2 -2
- 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 +132 -0
- package/dist/connectors/devices.js +125 -0
- 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/connectors/settlement.js +2 -2
- package/dist/db.d.ts +8 -0
- package/dist/db.js +32 -1
- package/dist/files/dsmr.d.ts +1 -0
- package/dist/files/dsmr.js +5 -0
- package/dist/files/enedis-r6x.d.ts +1 -32
- package/dist/files/enedis-r6x.js +5 -86
- package/dist/files/energy-bill.d.ts +4 -3
- package/dist/files/energy-bill.js +11 -6
- package/dist/files/evcc.d.ts +102 -0
- package/dist/files/evcc.js +76 -0
- package/dist/files/geojson.d.ts +41 -2
- package/dist/files/geojson.js +61 -1
- package/dist/files/index.d.ts +6 -0
- package/dist/files/index.js +6 -0
- package/dist/files/project-sheet.d.ts +44 -0
- package/dist/files/project-sheet.js +102 -0
- package/dist/files/project.d.ts +108 -0
- package/dist/files/project.js +104 -0
- package/dist/files/pvsol.d.ts +64 -0
- package/dist/files/pvsol.js +99 -0
- package/dist/files/weather-files.d.ts +3 -2
- package/dist/files/weather-files.js +11 -7
- package/dist/files/wesmart-db.d.ts +229 -0
- package/dist/files/wesmart-db.js +412 -0
- package/dist/gestures.d.ts +135 -0
- package/dist/gestures.js +99 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +12 -0
- package/dist/refusal.d.ts +2 -11
- package/dist/refusal.js +5 -29
- package/dist/series.d.ts +11 -6
- package/dist/series.js +6 -1
- package/dist/time.d.ts +1 -6
- package/dist/time.js +4 -25
- package/package.json +13 -6
- package/schema/geojson-export.1.json +605 -0
|
@@ -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
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// The `settlement` request kind (packages/connectors/README.md): the contracts in force
|
|
2
|
-
// over a period, each with its price signals
|
|
3
|
-
//
|
|
2
|
+
// over a period, each with its price signals as the `energy_contracts` module
|
|
3
|
+
// keeps them (modules/energy_contracts/README.md) and the series its
|
|
4
4
|
// volume is read from; back, the
|
|
5
5
|
// settlement lines of each contract — the energy, the price and the amount
|
|
6
6
|
// over each interval a price holds, who pays and who is paid. Any engine
|
package/dist/db.d.ts
CHANGED
|
@@ -9,3 +9,11 @@ export interface Db {
|
|
|
9
9
|
rows: T[];
|
|
10
10
|
}>;
|
|
11
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Runs `fn` so that what it writes lands whole or not at all. Inside the
|
|
14
|
+
* caller's transaction it holds a savepoint, released when `fn` returns and
|
|
15
|
+
* rolled back to when it throws, so the caller's transaction goes on; outside
|
|
16
|
+
* of one it is a transaction of its own. The throw is re-thrown either way.
|
|
17
|
+
* As everywhere in the core, the `Db` is one connection.
|
|
18
|
+
*/
|
|
19
|
+
export declare function atomically<T>(db: Db, fn: () => Promise<T>): Promise<T>;
|
package/dist/db.js
CHANGED
|
@@ -1 +1,32 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Runs `fn` so that what it writes lands whole or not at all. Inside the
|
|
3
|
+
* caller's transaction it holds a savepoint, released when `fn` returns and
|
|
4
|
+
* rolled back to when it throws, so the caller's transaction goes on; outside
|
|
5
|
+
* of one it is a transaction of its own. The throw is re-thrown either way.
|
|
6
|
+
* As everywhere in the core, the `Db` is one connection.
|
|
7
|
+
*/
|
|
8
|
+
export async function atomically(db, fn) {
|
|
9
|
+
let own = false;
|
|
10
|
+
try {
|
|
11
|
+
await db.query('savepoint nexgen_atomically');
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
// 25P01, no_active_sql_transaction: there is no transaction to nest in.
|
|
15
|
+
if (error?.code !== '25P01')
|
|
16
|
+
throw error;
|
|
17
|
+
await db.query('begin');
|
|
18
|
+
own = true;
|
|
19
|
+
}
|
|
20
|
+
let result;
|
|
21
|
+
try {
|
|
22
|
+
result = await fn();
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
await db.query(own ? 'rollback' : 'rollback to savepoint nexgen_atomically');
|
|
26
|
+
if (!own)
|
|
27
|
+
await db.query('release savepoint nexgen_atomically');
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
await db.query(own ? 'commit' : 'release savepoint nexgen_atomically');
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { meterSeriesOfDsmr, type DsmrMeterSeries } from '@nexgen-os/connector';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// The readings of a P1 port as the series of a meter: `meterSeriesOfDsmr()`
|
|
2
|
+
// lives in `@nexgen-os/connector`, MIT, so that a connector that receives
|
|
3
|
+
// them (`mqtt`) maps them without the core (decision 0064); the core
|
|
4
|
+
// re-exports it, one definition for both.
|
|
5
|
+
export { meterSeriesOfDsmr } from '@nexgen-os/connector';
|
|
@@ -1,32 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* An R63 or R64 file of Enedis, once read, as the series of a meter.
|
|
3
|
-
*
|
|
4
|
-
* `@nexgen-os/enedis-r6x` reads the file with the zone and the quality table
|
|
5
|
-
* of the operator's hub (`grid.operators.meter_data`); `meterSeriesOfR6x()`
|
|
6
|
-
* names what it read in the core's terms, ready for `storeSeries()` or
|
|
7
|
-
* `collectMeterReadings()`. One reading, one mapping, whichever way the file
|
|
8
|
-
* came: through an intermediary (Switchgrid), from the operator's own
|
|
9
|
-
* channel, or uploaded by a person.
|
|
10
|
-
*
|
|
11
|
-
* | File | Quantity | Value kind |
|
|
12
|
-
* |---|---|---|
|
|
13
|
-
* | `PA` `CONS` / `PROD` | `offtake_power` / `injection_power` | interval, the file's step |
|
|
14
|
-
* | `PRI` / `PRC` `CONS` | `reactive_power_inductive` / `_capacitive` | interval |
|
|
15
|
-
* | `E` | `voltage` | interval |
|
|
16
|
-
* | `EA` index `CONS` / `PROD` | `offtake_energy` / `injection_energy` | cumulative, one series per dial named `calendar/time class` |
|
|
17
|
-
*
|
|
18
|
-
* Values stay in the file's unit, which `store()` converts. A problem that
|
|
19
|
-
* would make a value wrong (no zone, a letter without a code, a date not
|
|
20
|
-
* read) refuses the file by name; nothing is stored half.
|
|
21
|
-
*/
|
|
22
|
-
import { type R6xFile } from '@nexgen-os/enedis-r6x';
|
|
23
|
-
import type { CollectedSeries } from '../connectors/interfaces.js';
|
|
24
|
-
import type { MeterDataHub } from '../connectors/meter-data.js';
|
|
25
|
-
/** Reads a file with the conventions of the hub it came from. */
|
|
26
|
-
export declare function readMeterFile(text: string | Record<string, unknown>, hub: Pick<MeterDataHub, 'time_zone' | 'quality_codes'>): R6xFile;
|
|
27
|
-
/**
|
|
28
|
-
* The series of one metering point in a file read by `@nexgen-os/enedis-r6x`,
|
|
29
|
-
* as `CollectedSeries`. Refuses `meter_file_unreadable` with the problems
|
|
30
|
-
* that make it so, `meter_file_empty` when the point is not in the file.
|
|
31
|
-
*/
|
|
32
|
-
export declare function meterSeriesOfR6x(file: R6xFile, prm: string): CollectedSeries[];
|
|
1
|
+
export { meterSeriesOfR6x, readMeterFile } from '@nexgen-os/connector';
|
package/dist/files/enedis-r6x.js
CHANGED
|
@@ -1,86 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* names what it read in the core's terms, ready for `storeSeries()` or
|
|
7
|
-
* `collectMeterReadings()`. One reading, one mapping, whichever way the file
|
|
8
|
-
* came: through an intermediary (Switchgrid), from the operator's own
|
|
9
|
-
* channel, or uploaded by a person.
|
|
10
|
-
*
|
|
11
|
-
* | File | Quantity | Value kind |
|
|
12
|
-
* |---|---|---|
|
|
13
|
-
* | `PA` `CONS` / `PROD` | `offtake_power` / `injection_power` | interval, the file's step |
|
|
14
|
-
* | `PRI` / `PRC` `CONS` | `reactive_power_inductive` / `_capacitive` | interval |
|
|
15
|
-
* | `E` | `voltage` | interval |
|
|
16
|
-
* | `EA` index `CONS` / `PROD` | `offtake_energy` / `injection_energy` | cumulative, one series per dial named `calendar/time class` |
|
|
17
|
-
*
|
|
18
|
-
* Values stay in the file's unit, which `store()` converts. A problem that
|
|
19
|
-
* would make a value wrong (no zone, a letter without a code, a date not
|
|
20
|
-
* read) refuses the file by name; nothing is stored half.
|
|
21
|
-
*/
|
|
22
|
-
import { readR6x, durationMs } from '@nexgen-os/enedis-r6x';
|
|
23
|
-
import { GestureError } from '../refusal.js';
|
|
24
|
-
/** Problems of a reading that refuse the file: what is left would be wrong. */
|
|
25
|
-
const BLOCKING = new Set(['file_unreadable', 'time_zone_required', 'local_time_nonexistent', 'date_unreadable', 'quality_unknown']);
|
|
26
|
-
/** The file's units by the `units` vocabulary's codes, where its spelling is not one. */
|
|
27
|
-
const UNITS = { VAr: 'V-A_Reactive', var: 'V-A_Reactive', kVAr: 'KiloV-A_Reactive' };
|
|
28
|
-
/** Reads a file with the conventions of the hub it came from. */
|
|
29
|
-
export function readMeterFile(text, hub) {
|
|
30
|
-
const options = { qualityCodes: hub.quality_codes, ...(hub.time_zone ? { timeZone: hub.time_zone } : {}) };
|
|
31
|
-
return readR6x(text, options);
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* The series of one metering point in a file read by `@nexgen-os/enedis-r6x`,
|
|
35
|
-
* as `CollectedSeries`. Refuses `meter_file_unreadable` with the problems
|
|
36
|
-
* that make it so, `meter_file_empty` when the point is not in the file.
|
|
37
|
-
*/
|
|
38
|
-
export function meterSeriesOfR6x(file, prm) {
|
|
39
|
-
const blocking = file.problems.filter((p) => BLOCKING.has(p.code));
|
|
40
|
-
if (blocking.length) {
|
|
41
|
-
throw new GestureError(`meter_file_unreadable: ${blocking.slice(0, 3).map((p) => `${p.code} (${p.detail})`).join('; ')}`);
|
|
42
|
-
}
|
|
43
|
-
const ours = file.series.filter((s) => s.prm === prm);
|
|
44
|
-
if (ours.length === 0)
|
|
45
|
-
throw new GestureError(`meter_file_empty: the file holds no measures of ${prm}`);
|
|
46
|
-
const out = [];
|
|
47
|
-
for (const s of ours) {
|
|
48
|
-
const quantity = quantityOf(s);
|
|
49
|
-
if (!quantity)
|
|
50
|
-
continue;
|
|
51
|
-
out.push({
|
|
52
|
-
quantity_code: quantity,
|
|
53
|
-
value_kind: s.kind === 'index' ? 'cumulative' : 'interval',
|
|
54
|
-
step: s.kind === 'index' ? null : postgresStep(s.step),
|
|
55
|
-
...(s.unit ? { unit: UNITS[s.unit] ?? s.unit } : {}),
|
|
56
|
-
...(s.kind === 'index' ? { name: [s.calendar, s.time_class].filter(Boolean).join('/') || null } : {}),
|
|
57
|
-
values: s.points.map((p) => ({ at: p.start, value: p.value, quality: p.quality })),
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
return out;
|
|
61
|
-
}
|
|
62
|
-
function quantityOf(s) {
|
|
63
|
-
const offtake = s.business === 'CONS';
|
|
64
|
-
const injection = s.business === 'PROD';
|
|
65
|
-
if (!offtake && !injection)
|
|
66
|
-
return null;
|
|
67
|
-
if (s.kind === 'index')
|
|
68
|
-
return s.physical === 'EA' ? (offtake ? 'offtake_energy' : 'injection_energy') : null;
|
|
69
|
-
switch (s.physical) {
|
|
70
|
-
case 'PA': return offtake ? 'offtake_power' : 'injection_power';
|
|
71
|
-
// Reactive power and voltage are read on the offtake side; the core has
|
|
72
|
-
// one series of each per meter.
|
|
73
|
-
case 'PRI': return offtake ? 'reactive_power_inductive' : null;
|
|
74
|
-
case 'PRC': return offtake ? 'reactive_power_capacitive' : null;
|
|
75
|
-
case 'E': return offtake ? 'voltage' : null;
|
|
76
|
-
default: return null;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
/** `PT5M` as Postgres writes an interval, `5 minutes`; null for an irregular curve. */
|
|
80
|
-
function postgresStep(step) {
|
|
81
|
-
const ms = step ? durationMs(step) : null;
|
|
82
|
-
if (!ms)
|
|
83
|
-
return null;
|
|
84
|
-
const minutes = ms / 60_000;
|
|
85
|
-
return minutes % 60 === 0 ? `${minutes / 60} hour${minutes === 60 ? '' : 's'}` : `${minutes} minutes`;
|
|
86
|
-
}
|
|
1
|
+
// An R63 or R64 file of Enedis as the series of a meter: `readMeterFile()`
|
|
2
|
+
// and `meterSeriesOfR6x()` live in `@nexgen-os/connector`, MIT, so that a
|
|
3
|
+
// connector relaying the files reads them without the core (decision 0064);
|
|
4
|
+
// the core re-exports them, one definition for both.
|
|
5
|
+
export { meterSeriesOfR6x, readMeterFile } from '@nexgen-os/connector';
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* participant's country, which must be the bill's.
|
|
20
20
|
*/
|
|
21
21
|
import { type EnergyBillReading } from '@nexgen-os/energy-bill';
|
|
22
|
-
import type
|
|
22
|
+
import { type Db } from '../db.js';
|
|
23
23
|
/** The file of the bill: where it is kept and its checksum, `sha256:<hex>`. */
|
|
24
24
|
export interface BillFile {
|
|
25
25
|
name: string;
|
|
@@ -72,8 +72,9 @@ export declare function tariffOfBill(db: Db, request: {
|
|
|
72
72
|
}): Promise<BillAttribute[]>;
|
|
73
73
|
/**
|
|
74
74
|
* The three gestures of the core in their order: the bill attached, its
|
|
75
|
-
* meter found or declared, its contract priced
|
|
76
|
-
*
|
|
75
|
+
* meter found or declared, its contract priced, in one transaction (or
|
|
76
|
+
* under a savepoint of the caller's): a bill refused writes nothing. What
|
|
77
|
+
* `tariffOfBill()` refuses is checked first. Returns what was written.
|
|
77
78
|
*/
|
|
78
79
|
export declare function importEnergyBill(db: Db, request: {
|
|
79
80
|
participantId: string;
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
* participant's country, which must be the bill's.
|
|
20
20
|
*/
|
|
21
21
|
import { meanProjectedPrice } from '@nexgen-os/energy-bill';
|
|
22
|
+
import { atomically } from '../db.js';
|
|
22
23
|
const CHECKSUM = /^sha256:[0-9a-f]{64}$/;
|
|
23
24
|
/**
|
|
24
25
|
* Attaches the bill to the participant, with its checksum and what the
|
|
@@ -132,14 +133,18 @@ export async function tariffOfBill(db, request) {
|
|
|
132
133
|
}
|
|
133
134
|
/**
|
|
134
135
|
* The three gestures of the core in their order: the bill attached, its
|
|
135
|
-
* meter found or declared, its contract priced
|
|
136
|
-
*
|
|
136
|
+
* meter found or declared, its contract priced, in one transaction (or
|
|
137
|
+
* under a savepoint of the caller's): a bill refused writes nothing. What
|
|
138
|
+
* `tariffOfBill()` refuses is checked first. Returns what was written.
|
|
137
139
|
*/
|
|
138
140
|
export async function importEnergyBill(db, request) {
|
|
139
141
|
// Checked before anything is written: a bill refused writes nothing.
|
|
140
142
|
await placeOfBill(db, request.participantId, request.reading);
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
143
|
+
// And one refused half way neither: its document would refuse it again as already imported.
|
|
144
|
+
return atomically(db, async () => {
|
|
145
|
+
const documentId = await attachBill(db, request.participantId, request.reading, request.file);
|
|
146
|
+
const meter = request.reading.meter ? await meterOfBill(db, request.participantId, request.reading) : null;
|
|
147
|
+
const attributes = await tariffOfBill(db, { participantId: request.participantId, meterId: meter?.meterId ?? null, reading: request.reading, documentId });
|
|
148
|
+
return { documentId, meterId: meter?.meterId ?? null, meterFound: meter?.found ?? false, attributes };
|
|
149
|
+
});
|
|
145
150
|
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The products evcc can drive, in the catalogue.
|
|
3
|
+
*
|
|
4
|
+
* `@nexgen-os/evcc` reads the templates of evcc; these functions turn the
|
|
5
|
+
* products they name into catalogue entries of the core
|
|
6
|
+
* ([0027](../../../../docs/decisions/0027-a-product-is-a-catalogue-entry.md)),
|
|
7
|
+
* each carrying, under `metadata.evcc`, the templates a device of that
|
|
8
|
+
* model can be driven through: the connector proposed when a device of the
|
|
9
|
+
* model is installed.
|
|
10
|
+
*
|
|
11
|
+
* - `catalogueOfEvcc()` the entries the templates give, and what they
|
|
12
|
+
* leave out and why, with no database;
|
|
13
|
+
* - `importEvccCatalogue()` the entries written, an entry the catalogue
|
|
14
|
+
* already holds gaining its templates.
|
|
15
|
+
*
|
|
16
|
+
* A template evcc drives only for its sponsors is left out: what it drives
|
|
17
|
+
* is not open to every installation.
|
|
18
|
+
*/
|
|
19
|
+
import type { EvccTemplate } from '@nexgen-os/evcc';
|
|
20
|
+
import type { Db } from '../db.js';
|
|
21
|
+
/**
|
|
22
|
+
* Which device type of the core a template's products are, by class: for
|
|
23
|
+
* `charger`, by the template's group (`''` without one); for `meter`, by
|
|
24
|
+
* the first of the uses the template serves found in the table, in the
|
|
25
|
+
* table's order (`''` for a template that names no use). A class or a group
|
|
26
|
+
* absent from the table, or mapped to null, gives no catalogue entry.
|
|
27
|
+
*/
|
|
28
|
+
export interface EvccDeviceTypes {
|
|
29
|
+
charger: Record<string, string | null>;
|
|
30
|
+
meter: Array<[usage: string, deviceType: string | null]>;
|
|
31
|
+
[evccClass: string]: Record<string, string | null> | Array<[string, string | null]>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The device types evcc's templates are read as by default: a charging
|
|
35
|
+
* station; a heating template (heat pumps and heating rods) a heat pump; a
|
|
36
|
+
* switch socket a controller; a meter template serving `pv` an inverter
|
|
37
|
+
* (a hybrid inverter included), else `battery` a battery, else a meter.
|
|
38
|
+
* Vehicles are not devices of a place: no entry.
|
|
39
|
+
*/
|
|
40
|
+
export declare const EVCC_DEVICE_TYPES: EvccDeviceTypes;
|
|
41
|
+
/** The device type of a template's products, or null when the table gives none. */
|
|
42
|
+
export declare function deviceTypeOfEvcc(template: EvccTemplate, table?: EvccDeviceTypes): string | null;
|
|
43
|
+
/** A template a device of a model can be driven through. */
|
|
44
|
+
export interface EvccConnectorOfProduct {
|
|
45
|
+
class: string;
|
|
46
|
+
template: string;
|
|
47
|
+
group: string | null;
|
|
48
|
+
capabilities: string[];
|
|
49
|
+
usages: string[];
|
|
50
|
+
discovery: EvccTemplate['discovery'];
|
|
51
|
+
}
|
|
52
|
+
/** A catalogue entry evcc's templates give. */
|
|
53
|
+
export interface EvccCatalogueEntry {
|
|
54
|
+
manufacturer: string;
|
|
55
|
+
model: string;
|
|
56
|
+
deviceType: string;
|
|
57
|
+
/** Every template naming this model, in the order read. */
|
|
58
|
+
connectors: EvccConnectorOfProduct[];
|
|
59
|
+
}
|
|
60
|
+
/** A product left out of the catalogue, and why. */
|
|
61
|
+
export interface EvccLeftOut {
|
|
62
|
+
template: string;
|
|
63
|
+
brand: string;
|
|
64
|
+
model: string | null;
|
|
65
|
+
reason: 'sponsored' | 'model_missing' | 'device_type_unknown';
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The catalogue entries the templates give: one per manufacturer and
|
|
69
|
+
* model, the device type of the first template naming it, every template
|
|
70
|
+
* naming it as its connectors. Left out: the products of a sponsored
|
|
71
|
+
* template, a brand named without a model, a template the table gives no
|
|
72
|
+
* device type.
|
|
73
|
+
*/
|
|
74
|
+
export declare function catalogueOfEvcc(templates: readonly EvccTemplate[], options?: {
|
|
75
|
+
deviceTypes?: EvccDeviceTypes;
|
|
76
|
+
}): {
|
|
77
|
+
entries: EvccCatalogueEntry[];
|
|
78
|
+
leftOut: EvccLeftOut[];
|
|
79
|
+
};
|
|
80
|
+
/** Where the templates were read from: the repository and the commit. */
|
|
81
|
+
export interface EvccRevision {
|
|
82
|
+
repository: string;
|
|
83
|
+
commit: string;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Writes the catalogue entries of the templates: an organisation's own
|
|
87
|
+
* (`organisationId`), or shared by every organisation (null, for whoever
|
|
88
|
+
* installs). An entry the catalogue already holds by manufacturer and
|
|
89
|
+
* model keeps its values and gains `metadata.evcc`, replaced as a whole at
|
|
90
|
+
* each import, so importing the same templates again changes nothing. A
|
|
91
|
+
* new entry is published by `evcc`. Every entry says the commit it was
|
|
92
|
+
* read at and evcc's licence.
|
|
93
|
+
*/
|
|
94
|
+
export declare function importEvccCatalogue(db: Db, templates: readonly EvccTemplate[], options: {
|
|
95
|
+
organisationId: string | null;
|
|
96
|
+
revision: EvccRevision;
|
|
97
|
+
deviceTypes?: EvccDeviceTypes;
|
|
98
|
+
}): Promise<{
|
|
99
|
+
created: number;
|
|
100
|
+
updated: number;
|
|
101
|
+
leftOut: EvccLeftOut[];
|
|
102
|
+
}>;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The device types evcc's templates are read as by default: a charging
|
|
3
|
+
* station; a heating template (heat pumps and heating rods) a heat pump; a
|
|
4
|
+
* switch socket a controller; a meter template serving `pv` an inverter
|
|
5
|
+
* (a hybrid inverter included), else `battery` a battery, else a meter.
|
|
6
|
+
* Vehicles are not devices of a place: no entry.
|
|
7
|
+
*/
|
|
8
|
+
export const EVCC_DEVICE_TYPES = {
|
|
9
|
+
charger: { '': 'charger', heating: 'heat_pump', switchsockets: 'controller' },
|
|
10
|
+
meter: [['pv', 'inverter'], ['battery', 'battery'], ['grid', 'meter'], ['charge', 'meter'], ['aux', 'meter'], ['', 'meter']],
|
|
11
|
+
};
|
|
12
|
+
/** The device type of a template's products, or null when the table gives none. */
|
|
13
|
+
export function deviceTypeOfEvcc(template, table = EVCC_DEVICE_TYPES) {
|
|
14
|
+
const rule = table[template.class];
|
|
15
|
+
if (!rule)
|
|
16
|
+
return null;
|
|
17
|
+
if (Array.isArray(rule)) {
|
|
18
|
+
const usages = template.usages.length > 0 ? template.usages : [''];
|
|
19
|
+
return rule.find(([usage]) => usages.includes(usage))?.[1] ?? null;
|
|
20
|
+
}
|
|
21
|
+
return rule[template.group ?? ''] ?? null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The catalogue entries the templates give: one per manufacturer and
|
|
25
|
+
* model, the device type of the first template naming it, every template
|
|
26
|
+
* naming it as its connectors. Left out: the products of a sponsored
|
|
27
|
+
* template, a brand named without a model, a template the table gives no
|
|
28
|
+
* device type.
|
|
29
|
+
*/
|
|
30
|
+
export function catalogueOfEvcc(templates, options = {}) {
|
|
31
|
+
const entries = new Map();
|
|
32
|
+
const leftOut = [];
|
|
33
|
+
for (const t of templates) {
|
|
34
|
+
const deviceType = deviceTypeOfEvcc(t, options.deviceTypes);
|
|
35
|
+
for (const p of t.products) {
|
|
36
|
+
const reason = t.sponsored ? 'sponsored' : p.model === null ? 'model_missing' : deviceType === null ? 'device_type_unknown' : null;
|
|
37
|
+
if (reason) {
|
|
38
|
+
leftOut.push({ template: t.template, brand: p.brand, model: p.model, reason });
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const key = `${p.brand}\u0000${p.model}`;
|
|
42
|
+
const entry = entries.get(key) ?? { manufacturer: p.brand, model: p.model, deviceType: deviceType, connectors: [] };
|
|
43
|
+
entries.set(key, entry);
|
|
44
|
+
if (!entry.connectors.some((c) => c.class === t.class && c.template === t.template)) {
|
|
45
|
+
entry.connectors.push({ class: t.class, template: t.template, group: t.group, capabilities: t.capabilities, usages: t.usages, discovery: t.discovery });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return { entries: [...entries.values()], leftOut };
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Writes the catalogue entries of the templates: an organisation's own
|
|
53
|
+
* (`organisationId`), or shared by every organisation (null, for whoever
|
|
54
|
+
* installs). An entry the catalogue already holds by manufacturer and
|
|
55
|
+
* model keeps its values and gains `metadata.evcc`, replaced as a whole at
|
|
56
|
+
* each import, so importing the same templates again changes nothing. A
|
|
57
|
+
* new entry is published by `evcc`. Every entry says the commit it was
|
|
58
|
+
* read at and evcc's licence.
|
|
59
|
+
*/
|
|
60
|
+
export async function importEvccCatalogue(db, templates, options) {
|
|
61
|
+
if (!options.revision?.commit)
|
|
62
|
+
throw new Error('revision_required: the commit of evcc the templates were read at');
|
|
63
|
+
const { entries, leftOut } = catalogueOfEvcc(templates, { deviceTypes: options.deviceTypes });
|
|
64
|
+
let created = 0;
|
|
65
|
+
for (const e of entries) {
|
|
66
|
+
const evcc = { connectors: e.connectors, repository: options.revision.repository, commit: options.revision.commit, licence: 'MIT' };
|
|
67
|
+
const row = (await db.query(`insert into products (organisation_id, device_type, manufacturer, model, metadata, published_by)
|
|
68
|
+
values ($1, $2, $3, $4, jsonb_build_object('evcc', $5::jsonb), 'evcc')
|
|
69
|
+
on conflict (organisation_id, manufacturer, model)
|
|
70
|
+
do update set metadata = products.metadata || jsonb_build_object('evcc', $5::jsonb)
|
|
71
|
+
returning (xmax = 0) as created`, [options.organisationId, e.deviceType, e.manufacturer, e.model, JSON.stringify(evcc)])).rows[0];
|
|
72
|
+
if (row.created)
|
|
73
|
+
created += 1;
|
|
74
|
+
}
|
|
75
|
+
return { created, updated: entries.length - created, leftOut };
|
|
76
|
+
}
|
package/dist/files/geojson.d.ts
CHANGED
|
@@ -16,9 +16,13 @@
|
|
|
16
16
|
* `open: true` is the export meant for the public: no street line, no
|
|
17
17
|
* serial number, no metadata of a described object, and nothing that came from a source whose
|
|
18
18
|
* licence forbids passing it on.
|
|
19
|
+
*
|
|
20
|
+
* `importGeoJsonParticipants()` reads the other way: the participants of a
|
|
21
|
+
* FeatureCollection — this export's, or any file whose features carry the
|
|
22
|
+
* same properties — join a project at their place.
|
|
19
23
|
*/
|
|
20
|
-
import { type FeatureCollection } from '@nexgen-os/geojson';
|
|
21
|
-
import type
|
|
24
|
+
import { type Feature, type FeatureCollection } from '@nexgen-os/geojson';
|
|
25
|
+
import { type Db } from '../db.js';
|
|
22
26
|
/** A figure a module computed on the object, as the module's table keeps it. */
|
|
23
27
|
export interface CalculationSummary {
|
|
24
28
|
id: string;
|
|
@@ -77,3 +81,38 @@ export interface GeoJsonExportOptions {
|
|
|
77
81
|
calculations?: boolean;
|
|
78
82
|
}
|
|
79
83
|
export declare function exportGeoJson(db: Db, target: GeoJsonTarget, options?: GeoJsonExportOptions): Promise<GeoJsonExport>;
|
|
84
|
+
/** The file a collection was read from: where it is kept and its checksum, `sha256:<hex>`. */
|
|
85
|
+
export interface GeoJsonFile {
|
|
86
|
+
name: string;
|
|
87
|
+
uri: string;
|
|
88
|
+
checksum: string;
|
|
89
|
+
byteSize?: number | null;
|
|
90
|
+
}
|
|
91
|
+
export interface GeoJsonImport {
|
|
92
|
+
/** The file, a document of the project. */
|
|
93
|
+
documentId: string;
|
|
94
|
+
/** The participants that joined, by the index of their feature. */
|
|
95
|
+
participants: {
|
|
96
|
+
index: number;
|
|
97
|
+
id: string;
|
|
98
|
+
name: string;
|
|
99
|
+
}[];
|
|
100
|
+
/** The features of another kind (`project`, `building`, `device`…), left out. */
|
|
101
|
+
skipped: {
|
|
102
|
+
index: number;
|
|
103
|
+
kind: string;
|
|
104
|
+
}[];
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The participants of a FeatureCollection read by `@nexgen-os/geojson`
|
|
108
|
+
* join a project, each at its place: the feature's geometry, and its
|
|
109
|
+
* properties named as the export writes them — `name`, `category`,
|
|
110
|
+
* `country`, the address, `region`, `municipality`, `time_zone`,
|
|
111
|
+
* `location_precision`, `location_source_code`, `identifiers`. A feature
|
|
112
|
+
* without `kind`, or of kind `participant`, is one; the others are left
|
|
113
|
+
* out and listed. The file is attached to the project with its checksum
|
|
114
|
+
* and imported once (`already_imported`, decision 0036), whole or not at
|
|
115
|
+
* all. Refuses `checksum_required`, and by the index of the feature
|
|
116
|
+
* `name_required` and `country_required`: a country is never assumed.
|
|
117
|
+
*/
|
|
118
|
+
export declare function importGeoJsonParticipants(db: Db, projectId: string, features: Feature[], file: GeoJsonFile): Promise<GeoJsonImport>;
|