@nexgen-os/easee 0.0.0-stage → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +89 -2
- package/dist/index.d.ts +135 -0
- package/dist/index.js +224 -0
- package/package.json +36 -4
- package/source.json +14 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nexgen OS contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@nexgen-os/easee`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Easee](https://easee.com) charging stations, read from their maker's cloud
|
|
4
|
+
on its public REST API (`https://api.easee.com`;
|
|
5
|
+
[documentation](https://developer.easee.com)), with the account of the
|
|
6
|
+
charger's owner. What it reads lands on a charger of the
|
|
7
|
+
[`charging`](../../../modules/charging/README.md) module: its sessions and
|
|
8
|
+
the series of the device. MIT.
|
|
9
|
+
|
|
10
|
+
A station that speaks OCPP to a central system of the installation is read
|
|
11
|
+
by its log instead ([`@nexgen-os/ocpp`](../../formats/ocpp/README.md) and
|
|
12
|
+
`importStationLog()` of `charging`); this connector is for the stations that
|
|
13
|
+
report to Easee's cloud.
|
|
14
|
+
|
|
15
|
+
## What it reads
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { collectEasee, easee } from '@nexgen-os/easee';
|
|
19
|
+
|
|
20
|
+
const cloud = easee({ username, password }); // the secret of the connectors row
|
|
21
|
+
await cloud.chargers(); // the chargers the account reaches
|
|
22
|
+
await collectEasee(db, cloud, { organisationId, deviceId, chargerId, from, to });
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
| Call | Endpoint | Gives |
|
|
26
|
+
|---|---|---|
|
|
27
|
+
| `chargers()` | `GET /api/chargers` | each charger by its serial number (what the other calls take) and its name |
|
|
28
|
+
| `observations({ chargerId, from, to })` | `GET /api/chargers/{id}/observations/{observation}/{from}/{to}` | the history of the observations below, each value at its own stamp |
|
|
29
|
+
| `sessions({ chargerId, from, to })` | `GET /api/sessions/charger/{id}/sessions/{from}/{to}` | the sessions of the window, each as the record `charging.record()` takes |
|
|
30
|
+
|
|
31
|
+
| Observation | Series of the charger |
|
|
32
|
+
|---|---|
|
|
33
|
+
| `LIFETIME ENERGY` (124) | `consumption_energy`, kWh, cumulative: the charger's own register, so `resample()` gives the energy of each hour or day |
|
|
34
|
+
| `TOTAL POWER` (120) | `consumption_power`, kW, instant |
|
|
35
|
+
|
|
36
|
+
A stamp the cloud gives without an offset is read as UTC, the time the
|
|
37
|
+
cloud dates in.
|
|
38
|
+
|
|
39
|
+
## Sessions
|
|
40
|
+
|
|
41
|
+
`collectEasee(db, connector, request)` writes a window on a charger
|
|
42
|
+
described in `charging.devices` (`charger_not_described` otherwise):
|
|
43
|
+
|
|
44
|
+
- each session through `charging.record()`, once per session id: source
|
|
45
|
+
`station` (the cloud is the station's central system), `source_ref` the
|
|
46
|
+
charger's serial, started when the car was connected and ended when it
|
|
47
|
+
was disconnected (open while it is), its energy, the token the station
|
|
48
|
+
read; in `metadata` when energy started and stopped flowing, how long it
|
|
49
|
+
flowed, and whether the cloud holds the session complete;
|
|
50
|
+
- the observations as `measured` series of the device, source `easee`.
|
|
51
|
+
|
|
52
|
+
A second collection of the same window changes nothing. What is left out is
|
|
53
|
+
named in `skipped`: a session without an id or a start, a stamp that is not
|
|
54
|
+
a date (`stamp_invalid`), a value that is not a number (`value_invalid`).
|
|
55
|
+
|
|
56
|
+
The cloud limits how often it is asked: two lists of chargers a minute, ten
|
|
57
|
+
lists of sessions an hour. A collection asks each endpoint once per charger
|
|
58
|
+
and window; a refusal for asking too often is named (`source_rate_limited`)
|
|
59
|
+
and never retried here: the job that schedules collections waits.
|
|
60
|
+
|
|
61
|
+
## Configuration
|
|
62
|
+
|
|
63
|
+
An organisation's `connectors` row names the source `easee` and the account
|
|
64
|
+
by `secret_ref` (the vault holds it; the caller resolves it and passes
|
|
65
|
+
`username` and `password`, the connector reads no environment), and in
|
|
66
|
+
`config` the chargers it collects. The access token is renewed with its
|
|
67
|
+
refresh token, and by signing in again when that is refused.
|
|
68
|
+
|
|
69
|
+
## Its source
|
|
70
|
+
|
|
71
|
+
The package ships its `sources` row, [`source.json`](source.json)
|
|
72
|
+
(`easeeSource`; write it with `registerSource()` of the core): kind `api`,
|
|
73
|
+
`auth = 'oauth2'`, worldwide, not redistributable: the data is the charger
|
|
74
|
+
owner's, read with their account.
|
|
75
|
+
|
|
76
|
+
## What is not done yet
|
|
77
|
+
|
|
78
|
+
- The answers in `tests/fixtures` are built on the shapes of the published
|
|
79
|
+
OpenAPI description, not recorded: record them with a real account at the
|
|
80
|
+
first run and keep them beside these; the tests must pass unchanged.
|
|
81
|
+
- Commands (start, stop, a current limit) wait for the device connector
|
|
82
|
+
contract of the core, through which a command a person validated is
|
|
83
|
+
carried.
|
|
84
|
+
- Left out on purpose: the price and the cost of a session (a price is the
|
|
85
|
+
data of a contract), the signed payload (OCMF, kept by Easee), the
|
|
86
|
+
operation mode, schedules and settings.
|
|
87
|
+
|
|
88
|
+
## Licence
|
|
89
|
+
|
|
90
|
+
MIT.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { type CollectedSeries, type Db, type SourceRow } from '@nexgen-os/core';
|
|
2
|
+
/** The subset of `fetch` the connector uses, so a test or a proxy can stand in. */
|
|
3
|
+
export type HttpFetch = (url: string, init?: {
|
|
4
|
+
method?: string;
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
body?: string;
|
|
7
|
+
}) => Promise<{
|
|
8
|
+
ok: boolean;
|
|
9
|
+
status: number;
|
|
10
|
+
json(): Promise<unknown>;
|
|
11
|
+
text(): Promise<string>;
|
|
12
|
+
}>;
|
|
13
|
+
export interface EaseeOptions {
|
|
14
|
+
/** The account of the charger's owner, from the secret of the `connectors` row. */
|
|
15
|
+
username: string;
|
|
16
|
+
password: string;
|
|
17
|
+
/** `https://api.easee.com` by default. */
|
|
18
|
+
baseUrl?: string;
|
|
19
|
+
fetch?: HttpFetch;
|
|
20
|
+
/** The clock, in UTC milliseconds, by which the access token expires; `Date.now` by default. */
|
|
21
|
+
now?: () => number;
|
|
22
|
+
}
|
|
23
|
+
/** A charger as the cloud lists it. */
|
|
24
|
+
export interface EaseeCharger {
|
|
25
|
+
/** Its serial number: the id the other calls take. */
|
|
26
|
+
id: string;
|
|
27
|
+
name: string | null;
|
|
28
|
+
}
|
|
29
|
+
/** The observations read into series: their id in the cloud, the quantity, its kind and the unit the cloud gives. */
|
|
30
|
+
export declare const OBSERVATIONS: Record<number, {
|
|
31
|
+
quantity: string;
|
|
32
|
+
value_kind: 'cumulative' | 'instant';
|
|
33
|
+
unit: string;
|
|
34
|
+
}>;
|
|
35
|
+
/** A session in the shape `charging.record()` takes. */
|
|
36
|
+
export interface EaseeSessionRecord {
|
|
37
|
+
/** The session's id in the cloud. */
|
|
38
|
+
transaction_id: string;
|
|
39
|
+
/** When the car was connected, and disconnected (absent while it is). */
|
|
40
|
+
started_at: string;
|
|
41
|
+
ended_at?: string;
|
|
42
|
+
energy_kwh: number | null;
|
|
43
|
+
/** The token the station read (an RFID card), as given: it may name a person. */
|
|
44
|
+
id_token?: string;
|
|
45
|
+
metadata: {
|
|
46
|
+
/** When energy started and stopped flowing, inside the connection. */
|
|
47
|
+
energy_from: string | null;
|
|
48
|
+
energy_to: string | null;
|
|
49
|
+
/** How long energy flowed, in seconds. */
|
|
50
|
+
charging_s: number | null;
|
|
51
|
+
complete: boolean;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export interface EaseeReadings {
|
|
55
|
+
series: CollectedSeries[];
|
|
56
|
+
/** Values left out, with the observation and the name of the reason. */
|
|
57
|
+
skipped: {
|
|
58
|
+
observation: number;
|
|
59
|
+
at: unknown;
|
|
60
|
+
reason: string;
|
|
61
|
+
}[];
|
|
62
|
+
}
|
|
63
|
+
export interface EaseeSessions {
|
|
64
|
+
sessions: EaseeSessionRecord[];
|
|
65
|
+
skipped: {
|
|
66
|
+
session: string;
|
|
67
|
+
reason: string;
|
|
68
|
+
}[];
|
|
69
|
+
}
|
|
70
|
+
export interface EaseeConnector {
|
|
71
|
+
readonly source_code: 'easee';
|
|
72
|
+
chargers(): Promise<EaseeCharger[]>;
|
|
73
|
+
/** The history of the observations of `OBSERVATIONS` from `from` to `to`, ISO 8601. */
|
|
74
|
+
observations(request: {
|
|
75
|
+
chargerId: string;
|
|
76
|
+
from: string;
|
|
77
|
+
to: string;
|
|
78
|
+
}): Promise<EaseeReadings>;
|
|
79
|
+
/** The sessions of a charger from `from` to `to`, ISO 8601. */
|
|
80
|
+
sessions(request: {
|
|
81
|
+
chargerId: string;
|
|
82
|
+
from: string;
|
|
83
|
+
to: string;
|
|
84
|
+
}): Promise<EaseeSessions>;
|
|
85
|
+
}
|
|
86
|
+
/** A connector on the cloud API of Easee, with the account of the charger's owner. `source_code` is `easee`. */
|
|
87
|
+
export declare function easee(options: EaseeOptions): EaseeConnector;
|
|
88
|
+
/**
|
|
89
|
+
* A stamp of the cloud as ISO 8601 UTC. The cloud dates in UTC; a stamp
|
|
90
|
+
* without an offset is read as UTC. Null when it is not a date.
|
|
91
|
+
*/
|
|
92
|
+
export declare function instantOf(stamp: unknown): string | null;
|
|
93
|
+
/**
|
|
94
|
+
* The history of one observation of `OBSERVATIONS` as one series, sorted
|
|
95
|
+
* and once per instant. A value that is not a number is skipped
|
|
96
|
+
* (`value_invalid`), and so is a stamp that is not a date (`stamp_invalid`).
|
|
97
|
+
*/
|
|
98
|
+
export declare function seriesOfEasee(observation: number, history: unknown): EaseeReadings;
|
|
99
|
+
/**
|
|
100
|
+
* Sessions of `GET /api/sessions/charger/{id}/sessions/{from}/{to}` as
|
|
101
|
+
* records: started when the car was connected, ended when it was
|
|
102
|
+
* disconnected. A session without an id or a start is skipped
|
|
103
|
+
* (`stamp_invalid`).
|
|
104
|
+
*/
|
|
105
|
+
export declare function sessionsOfEasee(sessions: unknown[]): EaseeSessions;
|
|
106
|
+
export interface CollectedEasee {
|
|
107
|
+
/** The sessions recorded, by id in `charging.sessions`, in the order of the cloud. */
|
|
108
|
+
sessions: string[];
|
|
109
|
+
/** The series of the charger written, with the readings each received. */
|
|
110
|
+
series: {
|
|
111
|
+
quantity: string;
|
|
112
|
+
seriesId: string;
|
|
113
|
+
inserted: number;
|
|
114
|
+
}[];
|
|
115
|
+
skipped: {
|
|
116
|
+
observations: EaseeReadings['skipped'];
|
|
117
|
+
sessions: EaseeSessions['skipped'];
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* A window of an Easee charger written on its device described in
|
|
122
|
+
* `charging.devices`: each session once by `charging.record()` (source
|
|
123
|
+
* `station`, `source_ref` the charger's serial), the lifetime energy
|
|
124
|
+
* register and the power as `measured` series of the device, source `easee`.
|
|
125
|
+
* Refuses as `charging.record()` does (`charger_not_described`).
|
|
126
|
+
*/
|
|
127
|
+
export declare function collectEasee(db: Db, connector: Pick<EaseeConnector, 'observations' | 'sessions'>, request: {
|
|
128
|
+
organisationId: string;
|
|
129
|
+
deviceId: string;
|
|
130
|
+
chargerId: string;
|
|
131
|
+
from: string;
|
|
132
|
+
to: string;
|
|
133
|
+
}): Promise<CollectedEasee>;
|
|
134
|
+
/** The `sources` row this package ships (`source.json`): write it with `registerSource()`. */
|
|
135
|
+
export declare const easeeSource: SourceRow;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// Easee — https://easee.com, charging stations reported to their maker's
|
|
2
|
+
// cloud, read on its public REST API (https://developer.easee.com) with the
|
|
3
|
+
// account of the charger's owner. Source `easee`, shipped with the package.
|
|
4
|
+
//
|
|
5
|
+
// Three reads, in the core's terms:
|
|
6
|
+
//
|
|
7
|
+
// - `chargers()`: the chargers the account reaches (`GET /api/chargers`),
|
|
8
|
+
// each by the id the cloud knows it by, its serial number;
|
|
9
|
+
// - `observations({ chargerId, from, to })`: the history of what the charger
|
|
10
|
+
// observed (`GET /api/chargers/{id}/observations/{observation}/{from}/{to}`)
|
|
11
|
+
// for the observations of `OBSERVATIONS`: its lifetime energy register
|
|
12
|
+
// (`consumption_energy`, cumulative, kWh) and the power it delivered
|
|
13
|
+
// (`consumption_power`, kW), each value at its own stamp;
|
|
14
|
+
// - `sessions({ chargerId, from, to })`: its charging sessions
|
|
15
|
+
// (`GET /api/sessions/charger/{id}/sessions/{from}/{to}`), each as the
|
|
16
|
+
// record `charging.record()` takes.
|
|
17
|
+
//
|
|
18
|
+
// `collectEasee()` writes a window on a charger described in
|
|
19
|
+
// `charging.devices`: the sessions through `charging.record()` (source
|
|
20
|
+
// `station`, the charger's serial as `source_ref`), once per session id, and
|
|
21
|
+
// the series through `storeSeries()`; a second collection of the same window
|
|
22
|
+
// changes nothing.
|
|
23
|
+
//
|
|
24
|
+
// The cloud limits how often it is asked (two lists of chargers a minute, ten
|
|
25
|
+
// lists of sessions an hour): a collection asks each endpoint once per
|
|
26
|
+
// charger and window, and a refusal for asking too often is named
|
|
27
|
+
// (`source_rate_limited`), never retried here.
|
|
28
|
+
//
|
|
29
|
+
// Left out, on purpose: the price and the cost of a session (a price is the
|
|
30
|
+
// data of a contract, not of the station), the signed payload of a session
|
|
31
|
+
// (OCMF, kept by Easee), the operation mode, schedules and settings.
|
|
32
|
+
// Commands (start, stop, a current limit) wait for the device connector
|
|
33
|
+
// contract of the core, through which a person's validated command is
|
|
34
|
+
// carried.
|
|
35
|
+
//
|
|
36
|
+
// Nothing here reads the environment or the database: the account comes
|
|
37
|
+
// from the secret the organisation's `connectors` row names, resolved by the
|
|
38
|
+
// installation's vault.
|
|
39
|
+
import { readFileSync } from 'node:fs';
|
|
40
|
+
import { GestureError, storeSeries } from '@nexgen-os/core';
|
|
41
|
+
/** The observations read into series: their id in the cloud, the quantity, its kind and the unit the cloud gives. */
|
|
42
|
+
export const OBSERVATIONS = {
|
|
43
|
+
124: { quantity: 'consumption_energy', value_kind: 'cumulative', unit: 'KiloW-HR' },
|
|
44
|
+
120: { quantity: 'consumption_power', value_kind: 'instant', unit: 'KiloW' },
|
|
45
|
+
};
|
|
46
|
+
/** A connector on the cloud API of Easee, with the account of the charger's owner. `source_code` is `easee`. */
|
|
47
|
+
export function easee(options) {
|
|
48
|
+
if (!options.username || !options.password)
|
|
49
|
+
throw new GestureError('secret_not_found: easee needs the account of the charger\'s owner (the secret of its connectors row)');
|
|
50
|
+
const base = (options.baseUrl ?? 'https://api.easee.com').replace(/\/+$/, '');
|
|
51
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
52
|
+
const now = options.now ?? Date.now;
|
|
53
|
+
let token = null;
|
|
54
|
+
const send = async (path, init) => {
|
|
55
|
+
try {
|
|
56
|
+
return await doFetch(`${base}${path}`, init);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
throw new GestureError(`source_unavailable: easee does not answer on ${path}: ${error.message}`);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const refused = async (response, path) => {
|
|
63
|
+
const detail = (await response.text().catch(() => '')).slice(0, 300);
|
|
64
|
+
if (response.status === 429)
|
|
65
|
+
throw new GestureError(`source_rate_limited: easee asks to wait before ${path} again: ${detail}`);
|
|
66
|
+
if (response.status === 401 || response.status === 403 || (response.status === 400 && path.startsWith('/api/accounts/'))) {
|
|
67
|
+
throw new GestureError(`source_unauthorised: easee ${response.status} on ${path}: ${detail}`);
|
|
68
|
+
}
|
|
69
|
+
throw new GestureError(`source_unavailable: easee ${response.status} on ${path}: ${detail}`);
|
|
70
|
+
};
|
|
71
|
+
const post = (path, body) => send(path, { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, body: JSON.stringify(body) });
|
|
72
|
+
const signIn = async () => {
|
|
73
|
+
let response = token?.refresh ? await post('/api/accounts/refresh_token', { refreshToken: token.refresh }) : null;
|
|
74
|
+
// A refresh token refused or absent: signed in again.
|
|
75
|
+
if (!response || !response.ok)
|
|
76
|
+
response = await post('/api/accounts/login', { userName: options.username, password: options.password });
|
|
77
|
+
if (!response.ok)
|
|
78
|
+
return refused(response, '/api/accounts/login');
|
|
79
|
+
const r = (await response.json());
|
|
80
|
+
if (typeof r.accessToken !== 'string' || !r.accessToken)
|
|
81
|
+
throw new GestureError('source_unauthorised: easee gave no access token');
|
|
82
|
+
// Renewed a minute before it expires.
|
|
83
|
+
const lifetime = typeof r.expiresIn === 'number' ? r.expiresIn : 3600;
|
|
84
|
+
token = { value: r.accessToken, refresh: typeof r.refreshToken === 'string' ? r.refreshToken : null, expires: now() + Math.max(lifetime - 60, 0) * 1000 };
|
|
85
|
+
return token.value;
|
|
86
|
+
};
|
|
87
|
+
const accessToken = async () => (token && token.expires > now() ? token.value : signIn());
|
|
88
|
+
const get = async (path) => {
|
|
89
|
+
const call = async () => send(path, { method: 'GET', headers: { Authorization: `Bearer ${await accessToken()}`, Accept: 'application/json' } });
|
|
90
|
+
let response = await call();
|
|
91
|
+
// A token revoked before its time: renewed once.
|
|
92
|
+
if (response.status === 401 && token) {
|
|
93
|
+
token.expires = 0;
|
|
94
|
+
response = await call();
|
|
95
|
+
}
|
|
96
|
+
if (!response.ok)
|
|
97
|
+
return refused(response, path.replace(/\?.*$/, ''));
|
|
98
|
+
return response.json();
|
|
99
|
+
};
|
|
100
|
+
const window = (from, to) => `${encodeURIComponent(from)}/${encodeURIComponent(to)}`;
|
|
101
|
+
return {
|
|
102
|
+
source_code: 'easee',
|
|
103
|
+
async chargers() {
|
|
104
|
+
const list = (await get('/api/chargers'));
|
|
105
|
+
return (Array.isArray(list) ? list : []).map((c) => ({ id: String(c.id), name: text(c.name) }));
|
|
106
|
+
},
|
|
107
|
+
async observations({ chargerId, from, to }) {
|
|
108
|
+
const out = { series: [], skipped: [] };
|
|
109
|
+
for (const id of Object.keys(OBSERVATIONS).map(Number)) {
|
|
110
|
+
const history = await get(`/api/chargers/${encodeURIComponent(chargerId)}/observations/${id}/${window(from, to)}`);
|
|
111
|
+
const read = seriesOfEasee(id, history);
|
|
112
|
+
out.series.push(...read.series);
|
|
113
|
+
out.skipped.push(...read.skipped);
|
|
114
|
+
}
|
|
115
|
+
return out;
|
|
116
|
+
},
|
|
117
|
+
async sessions({ chargerId, from, to }) {
|
|
118
|
+
return sessionsOfEasee((await get(`/api/sessions/charger/${encodeURIComponent(chargerId)}/sessions/${window(from, to)}`)));
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const text = (v) => (typeof v === 'string' && v !== '' ? v : null);
|
|
123
|
+
/**
|
|
124
|
+
* A stamp of the cloud as ISO 8601 UTC. The cloud dates in UTC; a stamp
|
|
125
|
+
* without an offset is read as UTC. Null when it is not a date.
|
|
126
|
+
*/
|
|
127
|
+
export function instantOf(stamp) {
|
|
128
|
+
if (typeof stamp !== 'string' || stamp === '')
|
|
129
|
+
return null;
|
|
130
|
+
const s = /(?:Z|[+-]\d{2}:?\d{2})$/i.test(stamp) ? stamp : `${stamp}Z`;
|
|
131
|
+
const ms = Date.parse(s);
|
|
132
|
+
return Number.isNaN(ms) ? null : new Date(ms).toISOString();
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* The history of one observation of `OBSERVATIONS` as one series, sorted
|
|
136
|
+
* and once per instant. A value that is not a number is skipped
|
|
137
|
+
* (`value_invalid`), and so is a stamp that is not a date (`stamp_invalid`).
|
|
138
|
+
*/
|
|
139
|
+
export function seriesOfEasee(observation, history) {
|
|
140
|
+
const known = OBSERVATIONS[observation];
|
|
141
|
+
if (!known)
|
|
142
|
+
throw new GestureError(`observation_unknown: easee reads the observations ${Object.keys(OBSERVATIONS).join(', ')}, not ${observation}`);
|
|
143
|
+
const skipped = [];
|
|
144
|
+
const values = new Map();
|
|
145
|
+
for (const o of Array.isArray(history) ? history : []) {
|
|
146
|
+
const at = instantOf(o.timestamp);
|
|
147
|
+
if (at === null) {
|
|
148
|
+
skipped.push({ observation, at: o.timestamp, reason: 'stamp_invalid' });
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const value = typeof o.value === 'number' ? o.value : typeof o.value === 'string' && o.value.trim() !== '' ? Number(o.value) : NaN;
|
|
152
|
+
if (!Number.isFinite(value)) {
|
|
153
|
+
skipped.push({ observation, at, reason: 'value_invalid' });
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
values.set(at, value);
|
|
157
|
+
}
|
|
158
|
+
const points = [...values].sort(([a], [b]) => a.localeCompare(b)).map(([at, value]) => ({ at, value }));
|
|
159
|
+
return {
|
|
160
|
+
series: points.length ? [{ quantity_code: known.quantity, value_kind: known.value_kind, step: null, unit: known.unit, name: null, values: points }] : [],
|
|
161
|
+
skipped,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Sessions of `GET /api/sessions/charger/{id}/sessions/{from}/{to}` as
|
|
166
|
+
* records: started when the car was connected, ended when it was
|
|
167
|
+
* disconnected. A session without an id or a start is skipped
|
|
168
|
+
* (`stamp_invalid`).
|
|
169
|
+
*/
|
|
170
|
+
export function sessionsOfEasee(sessions) {
|
|
171
|
+
const out = { sessions: [], skipped: [] };
|
|
172
|
+
for (const s of (Array.isArray(sessions) ? sessions : [])) {
|
|
173
|
+
const id = s.id === undefined || s.id === null ? '' : String(s.id);
|
|
174
|
+
const started = instantOf(s.carConnected);
|
|
175
|
+
if (!id || started === null) {
|
|
176
|
+
out.skipped.push({ session: id, reason: 'stamp_invalid' });
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const ended = instantOf(s.carDisconnected);
|
|
180
|
+
const record = {
|
|
181
|
+
transaction_id: id,
|
|
182
|
+
started_at: started,
|
|
183
|
+
energy_kwh: typeof s.kiloWattHours === 'number' && Number.isFinite(s.kiloWattHours) ? s.kiloWattHours : null,
|
|
184
|
+
metadata: {
|
|
185
|
+
energy_from: instantOf(s.firstEnergyTransferPeriodStarted),
|
|
186
|
+
energy_to: instantOf(s.lastEnergyTransferPeriodEnd),
|
|
187
|
+
charging_s: typeof s.actualDurationSeconds === 'number' ? s.actualDurationSeconds : null,
|
|
188
|
+
complete: s.isComplete === true,
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
if (ended !== null)
|
|
192
|
+
record.ended_at = ended;
|
|
193
|
+
const tokenRead = text(s.authToken);
|
|
194
|
+
if (tokenRead)
|
|
195
|
+
record.id_token = tokenRead;
|
|
196
|
+
out.sessions.push(record);
|
|
197
|
+
}
|
|
198
|
+
return out;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* A window of an Easee charger written on its device described in
|
|
202
|
+
* `charging.devices`: each session once by `charging.record()` (source
|
|
203
|
+
* `station`, `source_ref` the charger's serial), the lifetime energy
|
|
204
|
+
* register and the power as `measured` series of the device, source `easee`.
|
|
205
|
+
* Refuses as `charging.record()` does (`charger_not_described`).
|
|
206
|
+
*/
|
|
207
|
+
export async function collectEasee(db, connector, request) {
|
|
208
|
+
const window = { chargerId: request.chargerId, from: request.from, to: request.to };
|
|
209
|
+
const sessions = await connector.sessions(window);
|
|
210
|
+
const recorded = [];
|
|
211
|
+
for (const s of sessions.sessions) {
|
|
212
|
+
const r = await db.query('select charging.record($1, $2::jsonb, $3, $4) id', [request.deviceId, JSON.stringify(s), 'station', request.chargerId]);
|
|
213
|
+
recorded.push(r.rows[0].id);
|
|
214
|
+
}
|
|
215
|
+
const readings = await connector.observations(window);
|
|
216
|
+
const series = [];
|
|
217
|
+
for (const s of readings.series) {
|
|
218
|
+
const stored = await storeSeries(db, { organisationId: request.organisationId, objectKind: 'device', objectId: request.deviceId, sourceCode: 'easee', kind: 'measured' }, s);
|
|
219
|
+
series.push({ quantity: s.quantity_code, ...stored });
|
|
220
|
+
}
|
|
221
|
+
return { sessions: recorded, series, skipped: { observations: readings.skipped, sessions: sessions.skipped } };
|
|
222
|
+
}
|
|
223
|
+
/** The `sources` row this package ships (`source.json`): write it with `registerSource()`. */
|
|
224
|
+
export const easeeSource = JSON.parse(readFileSync(new URL('../source.json', import.meta.url), 'utf8'));
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexgen-os/easee",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Easee charging stations read from their cloud as a Nexgen OS connector: the chargers of an account, their lifetime energy register and power, and their sessions, as sessions and series of the charger.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/nexgen-os/nexgen-os.git",
|
|
9
|
+
"directory": "packages/connectors/easee"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=20"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"source.json",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc -p tsconfig.build.json"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@nexgen-os/core": "^0.5.0"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/source.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"code": "easee",
|
|
3
|
+
"name": "Easee",
|
|
4
|
+
"description": "The cloud of Easee charging stations: the chargers an account reaches, the history of what each observed (its lifetime energy register, the power it delivered) and its charging sessions with the energy of each. Read by @nexgen-os/easee with the account of the charger's owner.",
|
|
5
|
+
"kind": "api",
|
|
6
|
+
"base_url": "https://api.easee.com",
|
|
7
|
+
"docs_url": "https://developer.easee.com",
|
|
8
|
+
"auth": "oauth2",
|
|
9
|
+
"license": "Easee's terms of use; the data is the charger owner's, read with their account",
|
|
10
|
+
"attribution": "Easee",
|
|
11
|
+
"retention_days": null,
|
|
12
|
+
"redistributable": false,
|
|
13
|
+
"published_by": "@nexgen-os/easee"
|
|
14
|
+
}
|