@nexgen-os/zaptec 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 CHANGED
@@ -15,12 +15,13 @@ report to Zaptec's cloud only, as most household stations do.
15
15
  ## What it reads
16
16
 
17
17
  ```ts
18
- import { collectZaptecSessions, zaptec } from '@nexgen-os/zaptec';
18
+ import { readZaptecSessions, zaptec } from '@nexgen-os/zaptec';
19
19
 
20
20
  const cloud = zaptec({ username, password }); // the secret of the connectors row
21
21
  await cloud.chargers(); // the chargers the account reaches
22
22
  await cloud.state(chargerId); // what one reported last
23
- await collectZaptecSessions(db, cloud, { organisationId, deviceId, chargerId, from, to });
23
+ const reading = await readZaptecSessions(cloud, { chargerId, from, to });
24
+ await recordCharger(db, { organisationId, deviceId }, reading); // of the charging module
24
25
  ```
25
26
 
26
27
  | Call | Endpoint | Gives |
@@ -40,9 +41,11 @@ cloud dates in.
40
41
 
41
42
  ## Sessions and their energy
42
43
 
43
- `collectZaptecSessions(db, connector, request)` writes a window of sessions
44
- on a charger described in `charging.devices` (`charger_not_described`
45
- otherwise):
44
+ `readZaptecSessions(connector, { chargerId, from, to })` reads a window of
45
+ sessions as a `ChargerReading` of `@nexgen-os/connector`; this package
46
+ reaches neither the database nor the core. `recordCharger()` of the
47
+ `charging` module writes it on a charger described in `charging.devices`
48
+ (`charger_not_described` otherwise):
46
49
 
47
50
  - each session through `charging.record()`, once per session id: source
48
51
  `station` (the cloud is the station's central system), `source_ref` the
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type CollectedSeries, type Db, type SeriesPoint, type SourceRow } from '@nexgen-os/core';
1
+ import { type ChargerReading, type CollectedSeries, type SeriesPoint, type SourceRow } from '@nexgen-os/connector';
2
2
  /** The subset of `fetch` the connector uses, so a test or a proxy can stand in. */
3
3
  export type HttpFetch = (url: string, init?: {
4
4
  method?: string;
@@ -125,29 +125,21 @@ export declare function stateOfZaptec(observations: unknown): ZaptecState;
125
125
  * below the one before it is skipped (`value_invalid`).
126
126
  */
127
127
  export declare function sessionsOfZaptec(archived: unknown[]): ZaptecSessions;
128
- export interface CollectedSessions {
129
- /** The sessions recorded, by id in `charging.sessions`, in the order of the cloud. */
130
- sessions: string[];
131
- /** The energy series of the charger, and the readings it received. */
132
- energy: {
133
- seriesId: string;
134
- inserted: number;
135
- } | null;
128
+ export interface ReadZaptecSessions extends ChargerReading {
129
+ source_code: 'zaptec';
136
130
  skipped: ZaptecSessions['skipped'];
137
131
  }
138
132
  /**
139
- * The sessions of a Zaptec charger that ended in a window, written on its
140
- * device described in `charging.devices`: each session once by
141
- * `charging.record()` (source `station`, `source_ref` the station's serial),
142
- * the energy as one `consumption_energy` register of the device, `measured`,
143
- * source `zaptec`. Refuses as `charging.record()` does (`charger_not_described`).
133
+ * The sessions of a Zaptec charger that ended in a window, ready for
134
+ * `recordCharger()` of the `charging` module: each session with the serial
135
+ * of the station that delivered it as its `source_ref`, and their energy as
136
+ * one `consumption_energy` register of the charger (none when no session
137
+ * brought a reading), and what was left out.
144
138
  */
145
- export declare function collectZaptecSessions(db: Db, connector: Pick<ZaptecConnector, 'sessions'>, request: {
146
- organisationId: string;
147
- deviceId: string;
139
+ export declare function readZaptecSessions(connector: Pick<ZaptecConnector, 'sessions'>, window: {
148
140
  chargerId: string;
149
141
  from: string;
150
142
  to: string;
151
- }): Promise<CollectedSessions>;
143
+ }): Promise<ReadZaptecSessions>;
152
144
  /** The `sources` row this package ships (`source.json`): write it with `registerSource()`. */
153
145
  export declare const zaptecSource: SourceRow;
package/dist/index.js CHANGED
@@ -21,11 +21,11 @@
21
21
  // register that falls back as a reset, so the sessions of a charger make
22
22
  // one series: `resample()` gives the energy of each hour or day.
23
23
  //
24
- // `collectZaptecSessions()` writes a window of sessions on a charger
25
- // described in `charging.devices`: the sessions through `charging.record()`
26
- // (source `station`, the charger's serial as `source_ref`), once per session
27
- // id, and the energy through `storeSeries()`; a second collection of the
28
- // same window changes nothing.
24
+ // `readZaptecSessions()` reads a window of sessions of a charger as the
25
+ // `ChargerReading` of `@nexgen-os/connector`: the sessions, the station's
26
+ // serial as their `source_ref`, and their energy as one register. The caller
27
+ // writes it (`recordCharger()` of the `charging` module): this package
28
+ // reaches neither the database nor the core (decision 0064).
29
29
  //
30
30
  // Left out, on purpose: the user who authorised a session (a person's name
31
31
  // and e-mail: the token name the station read is kept, as for OCPP), the
@@ -38,7 +38,7 @@
38
38
  // from the secret the organisation's `connectors` row names, resolved by the
39
39
  // installation's vault.
40
40
  import { readFileSync } from 'node:fs';
41
- import { GestureError, storeSeries } from '@nexgen-os/core';
41
+ import { GestureError } from '@nexgen-os/connector';
42
42
  /** The operation modes a charger reports (observation 710), in words. */
43
43
  export const OPERATION_MODES = {
44
44
  0: 'unknown',
@@ -269,21 +269,14 @@ export function sessionsOfZaptec(archived) {
269
269
  return out;
270
270
  }
271
271
  /**
272
- * The sessions of a Zaptec charger that ended in a window, written on its
273
- * device described in `charging.devices`: each session once by
274
- * `charging.record()` (source `station`, `source_ref` the station's serial),
275
- * the energy as one `consumption_energy` register of the device, `measured`,
276
- * source `zaptec`. Refuses as `charging.record()` does (`charger_not_described`).
272
+ * The sessions of a Zaptec charger that ended in a window, ready for
273
+ * `recordCharger()` of the `charging` module: each session with the serial
274
+ * of the station that delivered it as its `source_ref`, and their energy as
275
+ * one `consumption_energy` register of the charger (none when no session
276
+ * brought a reading), and what was left out.
277
277
  */
278
- export async function collectZaptecSessions(db, connector, request) {
279
- const read = await connector.sessions({ chargerId: request.chargerId, from: request.from, to: request.to });
280
- const sessions = [];
281
- for (const s of read.sessions) {
282
- const r = await db.query('select charging.record($1, $2::jsonb, $3, $4) id', [
283
- request.deviceId, JSON.stringify(s.record), 'station', s.record.metadata.device_id,
284
- ]);
285
- sessions.push(r.rows[0].id);
286
- }
278
+ export async function readZaptecSessions(connector, window) {
279
+ const read = await connector.sessions(window);
287
280
  // A session that starts at the instant the one before it ended keeps the
288
281
  // end of the earlier one: the core counts the next reading below it as a
289
282
  // reset, the energy of the new session from zero.
@@ -291,10 +284,15 @@ export async function collectZaptecSessions(db, connector, request) {
291
284
  for (const p of read.sessions.flatMap((s) => s.energy))
292
285
  byInstant.set(p.at, Math.max(byInstant.get(p.at) ?? -Infinity, p.value));
293
286
  const values = [...byInstant].sort(([a], [b]) => a.localeCompare(b)).map(([at, value]) => ({ at, value }));
294
- const energy = values.length
295
- ? await storeSeries(db, { organisationId: request.organisationId, objectKind: 'device', objectId: request.deviceId, sourceCode: 'zaptec', kind: 'measured' }, { quantity_code: 'consumption_energy', value_kind: 'cumulative', step: null, unit: 'KiloW-HR', values })
296
- : null;
297
- return { sessions, energy, skipped: read.skipped };
287
+ const series = values.length
288
+ ? [{ quantity_code: 'consumption_energy', value_kind: 'cumulative', step: null, unit: 'KiloW-HR', values }]
289
+ : [];
290
+ return {
291
+ source_code: 'zaptec',
292
+ sessions: read.sessions.map((s) => ({ record: s.record, source_ref: s.record.metadata.device_id })),
293
+ series,
294
+ skipped: read.skipped,
295
+ };
298
296
  }
299
297
  /** The `sources` row this package ships (`source.json`): write it with `registerSource()`. */
300
298
  export const zaptecSource = JSON.parse(readFileSync(new URL('../source.json', import.meta.url), 'utf8'));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nexgen-os/zaptec",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "Zaptec charging stations read from their cloud as a Nexgen OS connector: the chargers of an account, their state, and their completed sessions with the energy delivered, as sessions and series of the charger.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -33,6 +33,6 @@
33
33
  "build": "tsc -p tsconfig.build.json"
34
34
  },
35
35
  "dependencies": {
36
- "@nexgen-os/core": "^0.5.0"
36
+ "@nexgen-os/connector": "^0.6.0"
37
37
  }
38
38
  }