@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.
@@ -0,0 +1,412 @@
1
+ import { GestureError } from '../refusal.js';
2
+ /** The database the rows come from, as the prefix of the identifier of every object imported. */
3
+ export const WESMART_DB = 'wesmart_db';
4
+ /** The identifier scheme that records the row an object was imported from. */
5
+ export const IMPORTED_ROW_SCHEME = 'imported_row';
6
+ /**
7
+ * The device types of the WeSmart Database, by the core's `device_types`
8
+ * code. A type absent here is refused, `device_type_unmapped`.
9
+ */
10
+ export const WESMART_DEVICE_TYPES = {
11
+ solar: 'solar',
12
+ parking_canopy: 'parking_canopy',
13
+ wind: 'wind',
14
+ hydro: 'hydro',
15
+ chp: 'cogeneration',
16
+ biomass: 'biomass',
17
+ geothermal: 'geothermal',
18
+ hydrogen: 'hydrogen',
19
+ battery: 'battery',
20
+ inverter: 'inverter',
21
+ heatpump: 'heat_pump',
22
+ heat_pump: 'heat_pump',
23
+ ev: 'charger',
24
+ ev_charger: 'charger',
25
+ };
26
+ /** The device types that produce, as the WeSmart Database lists them (`PRODUCTION_DEVICE_TYPES`). */
27
+ const PRODUCING = new Set(['solar', 'parking_canopy', 'wind', 'chp', 'hydro', 'geothermal', 'biomass']);
28
+ /** A device's status, by the core's (`devices_status`). Null keeps the core's rule: planned until installed. */
29
+ const DEVICE_STATUSES = {
30
+ active: 'in_service',
31
+ inactive: 'out_of_service',
32
+ planned: 'planned',
33
+ };
34
+ /** The statuses both databases share, word for word. */
35
+ const PROJECT_STATUSES = new Set(['simulation', 'creation', 'active', 'inactive', 'archived']);
36
+ const PARTICIPANT_STATUSES = new Set(['simulation', 'creation', 'active', 'inactive']);
37
+ /** The columns each table's mapping reads; any other column holding a value is counted as not carried. */
38
+ const CARRIED = {
39
+ organisations: new Set(['id', 'name']),
40
+ projects: new Set(['id', 'name', 'organisation_id', 'owner_id', 'country', 'status', 'start_date']),
41
+ participants: new Set(['id', 'project_id', 'name', 'status', 'address', 'postal_code', 'city', 'country', 'latitude',
42
+ 'longitude', 'smart_meter_id', 'power', 'consumption', 'join_date', 'leave_date', 'archived_at']),
43
+ devices: new Set(['id', 'participant_id', 'name', 'device_type', 'manufacturer', 'model', 'power', 'installation_date', 'status']),
44
+ };
45
+ /** Bookkeeping columns: the database's, not a description. */
46
+ const BOOKKEEPING = new Set(['created_at', 'updated_at']);
47
+ /** The identifier an object imported from a row is recorded under. */
48
+ export function importedRowKey(table, id) {
49
+ return `${WESMART_DB}:${table}:${id}`;
50
+ }
51
+ /** The key of the meter a participant's `smart_meter_id` becomes. */
52
+ function meterKey(participantId) {
53
+ return `${importedRowKey('participants', participantId)}:smart_meter_id`;
54
+ }
55
+ /** The meter number as the WeSmart Database compares it: spaces removed (`normalizeMeterId`). */
56
+ export function normaliseMeterNumber(value) {
57
+ return value.replace(/\s/g, '');
58
+ }
59
+ /** A placeholder for a number not known yet: `VIRTUAL-` and letters, digits, dashes. */
60
+ const VIRTUAL_METER = /^VIRTUAL-[A-Z0-9][A-Z0-9-]*$/i;
61
+ function text(v) {
62
+ if (typeof v !== 'string')
63
+ return null;
64
+ const t = v.trim();
65
+ return t === '' ? null : t;
66
+ }
67
+ /** A number as `numeric` comes: a number or its text. Null when neither. */
68
+ function number(v) {
69
+ if (typeof v === 'number')
70
+ return Number.isFinite(v) ? v : null;
71
+ if (typeof v === 'string' && v.trim() !== '' && Number.isFinite(Number(v)))
72
+ return Number(v);
73
+ return null;
74
+ }
75
+ /** A date as `YYYY-MM-DD`, from a date or the date part of a timestamp. */
76
+ function day(v) {
77
+ const t = text(v);
78
+ const m = t === null ? null : /^(\d{4}-\d{2}-\d{2})/.exec(t);
79
+ return m ? m[1] : null;
80
+ }
81
+ function byCreation(rows) {
82
+ return [...rows].sort((a, b) => String(a.created_at ?? '').localeCompare(String(b.created_at ?? '')) || a.id.localeCompare(b.id));
83
+ }
84
+ function held(v) {
85
+ if (v === null || v === undefined)
86
+ return false;
87
+ if (typeof v === 'string')
88
+ return v.trim() !== '';
89
+ if (Array.isArray(v))
90
+ return v.length > 0;
91
+ if (typeof v === 'object')
92
+ return Object.keys(v).length > 0;
93
+ return true;
94
+ }
95
+ /**
96
+ * What every row of a WeSmart Database becomes in the core, or why it does
97
+ * not. Pure: reads the rows and the options, touches nothing.
98
+ *
99
+ * A refused row refuses what hangs from it (`parent_refused`): the
100
+ * participants of a refused project, the meter and the devices of a refused
101
+ * participant. Two participants of one project carrying one meter
102
+ * number are one connection point: the first entered carries the meter, the
103
+ * others are refused theirs (`meter_shared`) and keep their own row.
104
+ */
105
+ export function planWesmartImport(snapshot, options) {
106
+ const plan = { organisations: [], projects: [], participants: [], meters: [], devices: [], refusals: [], notCarried: [] };
107
+ const refuse = (code, table, id, detail) => plan.refusals.push({ code, table, id, detail });
108
+ const notCarried = new Map();
109
+ const count = (table, column) => {
110
+ const k = `${table}.${column}`;
111
+ const entry = notCarried.get(k) ?? { table, column, rows: 0 };
112
+ entry.rows += 1;
113
+ notCarried.set(k, entry);
114
+ };
115
+ const audit = (table, row) => {
116
+ for (const [column, value] of Object.entries(row)) {
117
+ if (!CARRIED[table].has(column) && !BOOKKEEPING.has(column) && held(value))
118
+ count(table, column);
119
+ }
120
+ };
121
+ const seen = new Map();
122
+ const firstTime = (table, row) => {
123
+ const ids = seen.get(table) ?? new Set();
124
+ seen.set(table, ids);
125
+ if (typeof row.id !== 'string' || row.id === '') {
126
+ refuse('row_id_required', table, String(row.id ?? ''), 'a row without its id cannot be found again');
127
+ return false;
128
+ }
129
+ if (ids.has(row.id)) {
130
+ refuse('row_duplicated', table, row.id, 'the snapshot holds this row twice');
131
+ return false;
132
+ }
133
+ ids.add(row.id);
134
+ return true;
135
+ };
136
+ // Organisations.
137
+ const organisations = new Set();
138
+ for (const o of byCreation(snapshot.organisations ?? [])) {
139
+ if (!firstTime('organisations', o))
140
+ continue;
141
+ audit('organisations', o);
142
+ const name = text(o.name);
143
+ if (name === null) {
144
+ refuse('organisation_name_required', 'organisations', o.id, 'an organisation has a name');
145
+ continue;
146
+ }
147
+ plan.organisations.push({ key: importedRowKey('organisations', o.id), name });
148
+ organisations.add(o.id);
149
+ }
150
+ // Projects.
151
+ const projects = new Map();
152
+ for (const p of byCreation(snapshot.projects)) {
153
+ if (!firstTime('projects', p))
154
+ continue;
155
+ audit('projects', p);
156
+ const name = text(p.name);
157
+ const country = text(p.country)?.toUpperCase() ?? null;
158
+ const status = text(p.status);
159
+ const organisationId = text(p.organisation_id);
160
+ let organisation;
161
+ if (organisationId !== null) {
162
+ if (!organisations.has(organisationId)) {
163
+ const known = (snapshot.organisations ?? []).some((o) => o.id === organisationId);
164
+ refuse(known ? 'parent_refused' : 'organisation_not_found', 'projects', p.id, `its organisation ${organisationId} is ${known ? 'refused' : 'not in the snapshot'}`);
165
+ continue;
166
+ }
167
+ organisation = { key: importedRowKey('organisations', organisationId) };
168
+ }
169
+ else if (options.organisationForOwnProjects !== undefined) {
170
+ organisation = { id: options.organisationForOwnProjects };
171
+ }
172
+ else {
173
+ refuse('project_organisation_required', 'projects', p.id, 'the project is in no organisation, and none was given for such projects');
174
+ continue;
175
+ }
176
+ if (name === null) {
177
+ refuse('project_name_required', 'projects', p.id, 'a project has a name');
178
+ continue;
179
+ }
180
+ if (country === null || !/^[A-Z]{2}$/.test(country)) {
181
+ refuse('country_required', 'projects', p.id, `a project names the ISO 3166-1 alpha-2 country whose rules apply; it has ${JSON.stringify(p.country ?? null)}`);
182
+ continue;
183
+ }
184
+ if (status !== null && !PROJECT_STATUSES.has(status)) {
185
+ refuse('project_status_unknown', 'projects', p.id, `${status} is not a status of a project`);
186
+ continue;
187
+ }
188
+ const planned = { key: importedRowKey('projects', p.id), organisation, name, country, status, start_date: day(p.start_date) };
189
+ plan.projects.push(planned);
190
+ projects.set(p.id, planned);
191
+ }
192
+ // Devices, by participant, so that a participant knows whether it produces.
193
+ const devicesOf = new Map();
194
+ for (const d of snapshot.devices ?? []) {
195
+ const pid = text(d.participant_id);
196
+ if (pid === null)
197
+ continue;
198
+ devicesOf.set(pid, [...(devicesOf.get(pid) ?? []), d]);
199
+ }
200
+ // Participants, their place and their meter.
201
+ const participants = new Map();
202
+ const meterCarriers = new Map();
203
+ for (const p of byCreation(snapshot.participants)) {
204
+ if (!firstTime('participants', p))
205
+ continue;
206
+ audit('participants', p);
207
+ const projectId = text(p.project_id);
208
+ const project = projectId === null ? undefined : projects.get(projectId);
209
+ if (project === undefined) {
210
+ const known = snapshot.projects.some((r) => r.id === projectId);
211
+ refuse(known ? 'parent_refused' : 'project_not_found', 'participants', p.id, `its project ${projectId ?? 'is not given'}${projectId === null ? '' : known ? ' is refused' : ' is not in the snapshot'}`);
212
+ continue;
213
+ }
214
+ const name = text(p.name);
215
+ if (name === null) {
216
+ refuse('participant_name_required', 'participants', p.id, 'a participant has a name');
217
+ continue;
218
+ }
219
+ const status = p.archived_at ? 'inactive' : (text(p.status) ?? 'simulation');
220
+ if (!PARTICIPANT_STATUSES.has(status)) {
221
+ refuse('participant_status_unknown', 'participants', p.id, `${status} is not a status of a participant`);
222
+ continue;
223
+ }
224
+ // The place: what the row says, in the country of the participant, or
225
+ // of its project when the row names none.
226
+ const country = text(p.country)?.toUpperCase() ?? project.country;
227
+ if (!/^[A-Z]{2}$/.test(country)) {
228
+ refuse('country_invalid', 'participants', p.id, `${JSON.stringify(p.country)} is not an ISO 3166-1 alpha-2 code`);
229
+ continue;
230
+ }
231
+ const place = { country };
232
+ const line = text(p.address);
233
+ const postal = text(p.postal_code);
234
+ const city = text(p.city);
235
+ if (line !== null)
236
+ place.address_line1 = line;
237
+ if (postal !== null)
238
+ place.postal_code = postal;
239
+ if (city !== null)
240
+ place.city = city;
241
+ const lat = number(p.latitude);
242
+ const lon = number(p.longitude);
243
+ if (lat !== null && lon !== null && Math.abs(lat) <= 90 && Math.abs(lon) <= 180) {
244
+ place.geometry = { type: 'Point', coordinates: [lon, lat, 0] };
245
+ }
246
+ else if (held(p.latitude) || held(p.longitude)) {
247
+ count('participants', 'latitude, longitude (not a point)');
248
+ }
249
+ // What it does: it produces when it carries a power or a producing
250
+ // device, it consumes when it carries a consumption.
251
+ const devices = devicesOf.get(p.id) ?? [];
252
+ const produces = (number(p.power) ?? 0) > 0 || devices.some((d) => PRODUCING.has(text(d.device_type) ?? ''));
253
+ const consumes = (number(p.consumption) ?? 0) > 0;
254
+ const category = produces ? (consumes ? 'prosumer' : 'producer') : 'consumer';
255
+ const planned = {
256
+ key: importedRowKey('participants', p.id), project_key: project.key, name, category, status,
257
+ join_date: day(p.join_date), leave_date: day(p.leave_date), place,
258
+ };
259
+ plan.participants.push(planned);
260
+ participants.set(p.id, planned);
261
+ // Its meter.
262
+ const raw = text(p.smart_meter_id);
263
+ if (raw === null)
264
+ continue;
265
+ const number_ = normaliseMeterNumber(raw);
266
+ if (VIRTUAL_METER.test(number_)) {
267
+ refuse('meter_virtual', 'participants', p.id, `${number_} is a placeholder, not a connection point an operator knows`);
268
+ continue;
269
+ }
270
+ const schemes = options.meterSchemes.filter((s) => s.country.toUpperCase() === country && (s.pattern === null || new RegExp(s.pattern).test(number_)));
271
+ const distinct = [...new Set(schemes.map((s) => s.scheme))];
272
+ if (distinct.length !== 1) {
273
+ refuse(distinct.length === 0 ? 'meter_scheme_unknown' : 'meter_scheme_ambiguous', 'participants', p.id, distinct.length === 0
274
+ ? `${number_} matches no scheme of the connection points of ${country}`
275
+ : `${number_} matches ${distinct.join(', ')} in ${country}`);
276
+ continue;
277
+ }
278
+ // One open participant per connection point within a project.
279
+ const point = `${project.key}|${distinct[0]}|${number_}`;
280
+ const carrier = meterCarriers.get(point);
281
+ if (carrier !== undefined) {
282
+ refuse('meter_shared', 'participants', p.id, `${number_} is carried by ${carrier} in the same project; one connection point is one meter`);
283
+ continue;
284
+ }
285
+ meterCarriers.set(point, planned.key);
286
+ plan.meters.push({ key: meterKey(p.id), participant_key: planned.key, identifier: number_, scheme: distinct[0] });
287
+ }
288
+ // Devices: each stays with its participant.
289
+ for (const d of byCreation(snapshot.devices ?? [])) {
290
+ if (!firstTime('devices', d))
291
+ continue;
292
+ audit('devices', d);
293
+ const pid = text(d.participant_id);
294
+ const participant = pid === null ? undefined : participants.get(pid);
295
+ if (participant === undefined) {
296
+ const known = snapshot.participants.some((r) => r.id === pid);
297
+ refuse(known ? 'parent_refused' : 'participant_not_found', 'devices', d.id, `its participant ${pid ?? 'is not given'}${pid === null ? '' : known ? ' is refused' : ' is not in the snapshot'}`);
298
+ continue;
299
+ }
300
+ const source = text(d.device_type);
301
+ const type = source === null ? undefined : WESMART_DEVICE_TYPES[source];
302
+ if (type === undefined) {
303
+ refuse('device_type_unmapped', 'devices', d.id, `${JSON.stringify(source)} has no device type of the core`);
304
+ continue;
305
+ }
306
+ const manufacturer = text(d.manufacturer);
307
+ const model = text(d.model);
308
+ const name = text(d.name) ?? ([manufacturer, model].filter(Boolean).join(' ') || null);
309
+ if (name === null) {
310
+ refuse('device_name_required', 'devices', d.id, 'a device has a name, or a manufacturer and a model');
311
+ continue;
312
+ }
313
+ const status = text(d.status);
314
+ if (status !== null && DEVICE_STATUSES[status] === undefined) {
315
+ refuse('device_status_unknown', 'devices', d.id, `${status} is not a status of a device`);
316
+ continue;
317
+ }
318
+ const power = number(d.power);
319
+ if (power !== null && power < 0) {
320
+ refuse('device_power_negative', 'devices', d.id, `${power} kW`);
321
+ continue;
322
+ }
323
+ plan.devices.push({
324
+ key: importedRowKey('devices', d.id), participant_key: participant.key, device_type: type, name,
325
+ rated_power_kw: power, installed_on: day(d.installation_date), status: status === null ? null : DEVICE_STATUSES[status],
326
+ nameplate: { ...(manufacturer ? { manufacturer } : {}), ...(model ? { model } : {}) },
327
+ });
328
+ }
329
+ plan.notCarried = [...notCarried.values()].sort((a, b) => a.table.localeCompare(b.table) || a.column.localeCompare(b.column));
330
+ return plan;
331
+ }
332
+ /**
333
+ * The schemes of the connection points by country: those the grid operators
334
+ * of the `grid` module issue, with their pattern. Refuses
335
+ * `module_not_installed` when the module is not.
336
+ */
337
+ export async function meterSchemesOf(db) {
338
+ const { rows: [installed] } = await db.query(`select to_regclass('grid.operators') is not null ok`);
339
+ if (!installed?.ok)
340
+ throw new GestureError('module_not_installed: grid — the schemes of the connection points are those its operators issue');
341
+ const { rows } = await db.query(`select distinct o.country, o.identifier_scheme scheme, s.pattern
342
+ from grid.operators o join identifier_schemes s on s.code = o.identifier_scheme
343
+ order by 1, 2`);
344
+ return rows;
345
+ }
346
+ /**
347
+ * Writes a plan, as the person the connection acts as, each object through
348
+ * the gesture of its kind; finds first, by the row it came from, what a
349
+ * previous import wrote, and writes it no second time. Run it in one
350
+ * transaction (`Session.run()`): a refusal of the database stops it whole,
351
+ * with the name of the refusal and the row it was writing.
352
+ */
353
+ export async function importWesmartHistory(db, plan) {
354
+ const ids = {};
355
+ const zero = () => ({ organisations: 0, projects: 0, participants: 0, meters: 0, devices: 0 });
356
+ const result = { ids, created: zero(), alreadyImported: zero() };
357
+ const found = async (key) => {
358
+ const { rows } = await db.query('select object_id from identifiers where scheme = $1 and value = $2', [IMPORTED_ROW_SCHEME, key]);
359
+ return rows[0]?.object_id;
360
+ };
361
+ const idOf = (key) => {
362
+ const id = ids[key];
363
+ if (id === undefined)
364
+ throw new GestureError(`parent_not_imported: ${key} is not in the plan`);
365
+ return id;
366
+ };
367
+ const write = async (kind, key, sql, params, after) => {
368
+ const existing = await found(key);
369
+ if (existing !== undefined) {
370
+ ids[key] = existing;
371
+ result.alreadyImported[kind] += 1;
372
+ return;
373
+ }
374
+ try {
375
+ const { rows: [row] } = await db.query(sql, params);
376
+ ids[key] = row.id;
377
+ if (after)
378
+ await after(row.id);
379
+ }
380
+ catch (error) {
381
+ throw new GestureError(`${String(error?.message ?? error)} (while importing ${key})`);
382
+ }
383
+ result.created[kind] += 1;
384
+ };
385
+ // `add_identifier()` records the row in the statement that creates the
386
+ // object, so that an object is never written without it.
387
+ for (const o of plan.organisations) {
388
+ await write('organisations', o.key, `with c as (select create_organisation($1) id) select c.id, add_identifier('organisation', c.id, $2, $3) from c`, [o.name, IMPORTED_ROW_SCHEME, o.key]);
389
+ }
390
+ for (const p of plan.projects) {
391
+ const organisation = 'id' in p.organisation ? p.organisation.id : idOf(p.organisation.key);
392
+ await write('projects', p.key, `with c as (select create_project($1, $2, $3, null, $4) id) select c.id, add_identifier('project', c.id, $5, $6) from c`, [organisation, p.name, p.country, p.start_date, IMPORTED_ROW_SCHEME, p.key], async (id) => {
393
+ if (p.status !== null)
394
+ await db.query('update projects set status = $2 where id = $1', [id, p.status]);
395
+ });
396
+ }
397
+ for (const p of plan.participants) {
398
+ const place = { ...p.place, identifiers: [{ scheme: IMPORTED_ROW_SCHEME, value: p.key }] };
399
+ await write('participants', p.key, `select join_project($1, $2, $3::jsonb, $4, null, $5, $6) id`, [idOf(p.project_key), p.name, JSON.stringify(place), p.category, p.join_date, p.status], async (id) => {
400
+ if (p.leave_date !== null)
401
+ await db.query('select leave_project($1, $2)', [id, p.leave_date]);
402
+ });
403
+ }
404
+ for (const m of plan.meters) {
405
+ await write('meters', m.key, `with c as (select declare_meter($1, $2, $3, 'electricity', 'both', null, null) id) select c.id, add_identifier('meter', c.id, $4, $5) from c`, [idOf(m.participant_key), m.identifier, m.scheme, IMPORTED_ROW_SCHEME, m.key]);
406
+ }
407
+ for (const d of plan.devices) {
408
+ await write('devices', d.key, `with c as (select install_device($1, $2, $3, null, $4, $5, null, 1, $6::jsonb, $7) id) select c.id, add_identifier('device', c.id, $8, $9) from c`, [idOf(d.participant_key), d.device_type, d.name, d.rated_power_kw, d.installed_on, JSON.stringify(d.nameplate), d.status,
409
+ IMPORTED_ROW_SCHEME, d.key]);
410
+ }
411
+ return result;
412
+ }
@@ -127,6 +127,25 @@ export interface DeviceInput {
127
127
  export declare function installDevice(db: Db, d: DeviceInput): Promise<{
128
128
  id: string;
129
129
  }>;
130
+ /** The statuses a device moves to through the works and its life; `removed` is `retire_device()`'s. */
131
+ export type DeviceStatus = 'installed' | 'in_service' | 'out_of_service';
132
+ /**
133
+ * Moves a device on a day by a named transition (`set_device_status()`):
134
+ * planned to installed or in service, the day becoming its installation
135
+ * date; installed to in service, or out of service when it failed its
136
+ * commissioning; in service to out of service and back. Refuses
137
+ * `device_status_invalid`, `device_day_invalid`, `device_retired`,
138
+ * `device_not_found`, `not_allowed`. Returns where the device stands.
139
+ */
140
+ export declare function setDeviceStatus(db: Db, d: {
141
+ device_id: string;
142
+ status: DeviceStatus;
143
+ on?: string;
144
+ }): Promise<{
145
+ id: string;
146
+ status: string;
147
+ installation_date: string | null;
148
+ }>;
130
149
  export interface PvArrayInput {
131
150
  name: string;
132
151
  /** Degrees clockwise from true north: 180 faces south. */
package/dist/gestures.js CHANGED
@@ -167,6 +167,18 @@ export async function installDevice(db, d) {
167
167
  return first(db, `select install_device($1, $2, $3, $4, $5, $6, $7, coalesce($8, 1)) id`, [d.participant_id, d.device_type, d.name, d.product_id ?? null, d.rated_power_kw ?? null, d.installed_on ?? null,
168
168
  d.source_code ?? null, d.unit_count ?? null]);
169
169
  }
170
+ /**
171
+ * Moves a device on a day by a named transition (`set_device_status()`):
172
+ * planned to installed or in service, the day becoming its installation
173
+ * date; installed to in service, or out of service when it failed its
174
+ * commissioning; in service to out of service and back. Refuses
175
+ * `device_status_invalid`, `device_day_invalid`, `device_retired`,
176
+ * `device_not_found`, `not_allowed`. Returns where the device stands.
177
+ */
178
+ export async function setDeviceStatus(db, d) {
179
+ await db.query('select set_device_status($1, $2, coalesce($3::date, current_date))', [d.device_id, d.status, d.on ?? null]);
180
+ return first(db, 'select id::text, status, installation_date::text from devices where id = $1', [d.device_id]);
181
+ }
170
182
  /**
171
183
  * A PV system of a participant, described as the `solar` module reads it: a
172
184
  * device of the core (`install_device()`, type `solar`), its row of
package/dist/index.d.ts CHANGED
@@ -12,8 +12,10 @@ export * from './address.js';
12
12
  export * from './series.js';
13
13
  export * from './time.js';
14
14
  export * from './connectors/interfaces.js';
15
+ export * from './connectors/measured.js';
15
16
  export * from './connectors/meter-data.js';
16
17
  export * from './connectors/devices.js';
18
+ export * from './connectors/activations.js';
17
19
  export * from './connectors/runner.js';
18
20
  export * from './connectors/pvlib.js';
19
21
  export * from './connectors/settlement.js';
package/dist/index.js CHANGED
@@ -12,8 +12,10 @@ export * from './address.js';
12
12
  export * from './series.js';
13
13
  export * from './time.js';
14
14
  export * from './connectors/interfaces.js';
15
+ export * from './connectors/measured.js';
15
16
  export * from './connectors/meter-data.js';
16
17
  export * from './connectors/devices.js';
18
+ export * from './connectors/activations.js';
17
19
  export * from './connectors/runner.js';
18
20
  export * from './connectors/pvlib.js';
19
21
  export * from './connectors/settlement.js';
package/dist/refusal.d.ts CHANGED
@@ -7,8 +7,8 @@
7
7
  * a name is and on what counts as the database saying no. The shape is Ekwo
8
8
  * OS's (`refusal.ts`), so an agent reads both projects the same way.
9
9
  */
10
- /** The name of a raise like `time_zone_required: the participant …`, or undefined. */
11
- export declare function refusalCode(message: string): string | undefined;
10
+ import { GestureError } from '@nexgen-os/connector';
11
+ export { GestureError, refusalCode } from '@nexgen-os/connector';
12
12
  /**
13
13
  * Whether an SQLSTATE is the database declining, as opposed to failing: a
14
14
  * raise of its own (`P0001`, `P0002`), a right the caller does not hold or a
@@ -16,14 +16,5 @@ export declare function refusalCode(message: string): string | undefined;
16
16
  * (`25006`), a constraint (class `23`).
17
17
  */
18
18
  export declare function isRefusalState(sqlstate: string | undefined): boolean;
19
- /** An error of a gesture: its name, its message, and the sentence that explains it when there is one. */
20
- export declare class GestureError extends Error {
21
- readonly code: string | undefined;
22
- readonly hint: string | undefined;
23
- constructor(message: string, options?: {
24
- code?: string;
25
- hint?: string;
26
- });
27
- }
28
19
  /** The error a surface reports for whatever the database or a gesture threw. */
29
20
  export declare function explain(error: unknown): GestureError;
package/dist/refusal.js CHANGED
@@ -7,11 +7,11 @@
7
7
  * a name is and on what counts as the database saying no. The shape is Ekwo
8
8
  * OS's (`refusal.ts`), so an agent reads both projects the same way.
9
9
  */
10
- /** The name of a raise like `time_zone_required: the participant …`, or undefined. */
11
- export function refusalCode(message) {
12
- const match = /^([a-z][a-z0-9_]{3,}):/.exec(message.trim());
13
- return match?.[1];
14
- }
10
+ import { GestureError, refusalCode } from '@nexgen-os/connector';
11
+ // The name of a refusal and the error that carries it live in
12
+ // `@nexgen-os/connector`, MIT, so that a connector refuses by name without
13
+ // the core (decision 0064); the core re-exports them.
14
+ export { GestureError, refusalCode } from '@nexgen-os/connector';
15
15
  /**
16
16
  * Whether an SQLSTATE is the database declining, as opposed to failing: a
17
17
  * raise of its own (`P0001`, `P0002`), a right the caller does not hold or a
@@ -25,30 +25,6 @@ export function isRefusalState(sqlstate) {
25
25
  return true;
26
26
  return sqlstate.startsWith('23');
27
27
  }
28
- /** What a few refusals mean, for whoever reads them next. An unknown name keeps its message. */
29
- const HINTS = {
30
- read_only: 'This connection reads. Writing is allowed by the person: NEXGEN_WRITE=1 in the environment they wrote, a key with write, or the scope energy.write on the connection they approved.',
31
- not_allowed: 'The person this acts as does not hold that right. Ask them, or an owner of the organisation.',
32
- api_key_revoked: 'The key was withdrawn. Its owner issues another one.',
33
- api_key_expired: 'The key expired. Its owner issues another one.',
34
- api_key_unknown: 'The key was not issued by this installation. Check the variable it was put in.',
35
- api_key_issuer_left: 'The person who issued the key left the organisation. An owner issues another one.',
36
- time_zone_required: 'The participant has no time zone: give it one before reading or storing daily values.',
37
- geocoder_not_configured: 'No geocoder was configured by the person. Give the place as a point, or ask them to configure one.',
38
- module_not_installed: 'The module is not installed on this database: nexgen-os init --modules adds it.',
39
- computation_not_loaded: 'This server was started without the code of the module that computes this. Start it from a checkout of the repository, where the modules are.',
40
- };
41
- /** An error of a gesture: its name, its message, and the sentence that explains it when there is one. */
42
- export class GestureError extends Error {
43
- code;
44
- hint;
45
- constructor(message, options = {}) {
46
- super(message);
47
- this.name = 'GestureError';
48
- this.code = options.code ?? refusalCode(message);
49
- this.hint = options.hint ?? (this.code === undefined ? undefined : HINTS[this.code]);
50
- }
51
- }
52
28
  /** The error a surface reports for whatever the database or a gesture threw. */
53
29
  export function explain(error) {
54
30
  if (error instanceof GestureError)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nexgen-os/core",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "What the code of Nexgen OS shares: the database seam, the standard place and the geocoder chain, the series API, the connector interfaces, the engine glue, and the importers and exporters of the file formats.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "repository": {
@@ -33,16 +33,17 @@
33
33
  "build": "tsc -p tsconfig.build.json && node ../../scripts/copy-license.mjs"
34
34
  },
35
35
  "dependencies": {
36
- "@nexgen-os/dsmr": "^0.5.0",
37
- "@nexgen-os/enedis-r6x": "^0.5.0",
38
- "@nexgen-os/energy-bill": "^0.5.0",
39
- "@nexgen-os/evcc": "^0.5.0",
40
- "@nexgen-os/geojson": "^0.5.0",
41
- "@nexgen-os/homeassistant": "^0.5.0",
42
- "@nexgen-os/project-sheet": "^0.5.0",
43
- "@nexgen-os/pvsol": "^0.5.0",
44
- "@nexgen-os/solar": "^0.5.0",
45
- "@nexgen-os/weather-files": "^0.5.0"
36
+ "@nexgen-os/connector": "^0.6.0",
37
+ "@nexgen-os/dsmr": "^0.6.0",
38
+ "@nexgen-os/enedis-r6x": "^0.6.0",
39
+ "@nexgen-os/energy-bill": "^0.6.0",
40
+ "@nexgen-os/evcc": "^0.6.0",
41
+ "@nexgen-os/geojson": "^0.6.0",
42
+ "@nexgen-os/homeassistant": "^0.6.0",
43
+ "@nexgen-os/project-sheet": "^0.6.0",
44
+ "@nexgen-os/pvsol": "^0.6.0",
45
+ "@nexgen-os/solar": "^0.6.0",
46
+ "@nexgen-os/weather-files": "^0.6.0"
46
47
  },
47
48
  "peerDependencies": {
48
49
  "pg": "^8.23.1"