homematic-manager 3.5.0 → 3.5.1
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/node_modules/@homematic-manager/backend/README.md +8 -1
- package/node_modules/@homematic-manager/backend/dist/cache/descriptions.d.ts +21 -2
- package/node_modules/@homematic-manager/backend/dist/cache/descriptions.js +50 -16
- package/node_modules/@homematic-manager/backend/dist/cache/store.d.ts +12 -2
- package/node_modules/@homematic-manager/backend/dist/cache/store.js +126 -11
- package/node_modules/@homematic-manager/backend/dist/meta/occuliteProvider.d.ts +8 -0
- package/node_modules/@homematic-manager/backend/dist/meta/occuliteProvider.js +20 -17
- package/node_modules/@homematic-manager/backend/dist/util/jsonFile.d.ts +9 -2
- package/node_modules/@homematic-manager/backend/dist/util/jsonFile.js +39 -2
- package/node_modules/@homematic-manager/backend/package.json +1 -1
- package/node_modules/@homematic-manager/core/package.json +1 -1
- package/package.json +3 -3
|
@@ -103,10 +103,17 @@ own directory; a test: a temporary one).
|
|
|
103
103
|
| --- | --- | --- |
|
|
104
104
|
| `config.json` | the `AppConfig` (connection only; the rest is derived) | on every `config.set`, atomically |
|
|
105
105
|
| `cache/<host>/devices.json` | device descriptions per interface | debounced, on every change |
|
|
106
|
-
| `cache/<host>/descriptions
|
|
106
|
+
| `cache/<host>/descriptions/<interface>/<type>.<hash>.json` | paramset descriptions by identity, one file per device type | debounced, only the type that gained one |
|
|
107
107
|
| `cache/<host>/names.json` | the local names and the ReGa object ids | debounced, on every rename |
|
|
108
|
+
| `cache/<host>/unreach.json` | how often each device went unreachable | a minute after an outage, and on stop |
|
|
109
|
+
| `cache/<host>/occulite-meta.json` | the last metadata snapshot of openccu-lite | 5 s after a change, and on stop |
|
|
108
110
|
| `cache/<host>/write-log.json` | the write log of the session | debounced, on every write |
|
|
109
111
|
|
|
112
|
+
A file whose content did not change is not written again (the device list after every `init`, the
|
|
113
|
+
names at every start, the metadata snapshot after every reconnect): on the addon the data directory
|
|
114
|
+
is the CCU's SD card. The single `descriptions.json` of 3.5 and older is split into the per-type
|
|
115
|
+
files on the first start and removed.
|
|
116
|
+
|
|
110
117
|
The cache directory is keyed by host, so switching between two CCUs cannot mix their device lists.
|
|
111
118
|
Every write goes through a temporary file and a rename: 2.x used `persist-json` without its
|
|
112
119
|
`{secure: true}` option, so an interrupted write lost the configuration.
|
|
@@ -10,16 +10,32 @@
|
|
|
10
10
|
* with 100 devices is a minute of RPC traffic, and it is the same answer every time. The identity
|
|
11
11
|
* carries the firmware and the version, so a device that was updated gets a new key rather than a
|
|
12
12
|
* stale description - and it is the same key `multiApplyEligibility()` compares (task 6.3).
|
|
13
|
+
*
|
|
14
|
+
* **Stored in shards, one per interface and device type.** A CCU with a few dozen device types
|
|
15
|
+
* has megabytes of descriptions; kept in one file, every description fetched for a type not seen
|
|
16
|
+
* before rewrote all of them, on the addon to the CCU's SD card. Each shard is its own file now
|
|
17
|
+
* (`CacheStore` decides where), and only the shards that gained something are written. A device
|
|
18
|
+
* of a type that is cached already writes nothing at all.
|
|
13
19
|
*/
|
|
14
20
|
import { type DeviceDescription, type ParamsetDescription } from '@homematic-manager/core';
|
|
15
21
|
/** What is written to `<cache>/descriptions.json`: identity -> description. */
|
|
16
22
|
export type DescriptionCacheSnapshot = Record<string, ParamsetDescription>;
|
|
23
|
+
/**
|
|
24
|
+
* The shard an identity belongs to: its interface and device type (`BidCos-RF/HM-LC-Sw1-Pl`).
|
|
25
|
+
* Only a bucket - the identity itself is what a shard file is keyed by.
|
|
26
|
+
*/
|
|
27
|
+
export declare function shardOf(identity: string): string;
|
|
17
28
|
export declare class ParamsetDescriptionCache {
|
|
18
29
|
#private;
|
|
19
30
|
get size(): number;
|
|
20
|
-
/** True when something was stored since the last `
|
|
31
|
+
/** True when something was stored since the last `takeDirty()`. */
|
|
21
32
|
get dirty(): boolean;
|
|
22
|
-
|
|
33
|
+
/** The shards that changed since the last call, and forgets them. */
|
|
34
|
+
takeDirty(): string[];
|
|
35
|
+
/** Every shard that holds something. */
|
|
36
|
+
shards(): string[];
|
|
37
|
+
/** What is written to the file of one shard: identity -> description; empty when it holds nothing. */
|
|
38
|
+
shardSnapshot(shard: string): DescriptionCacheSnapshot;
|
|
23
39
|
/**
|
|
24
40
|
* The identity of a paramset, or `undefined` when the device is not in the index - which is the
|
|
25
41
|
* case for a channel whose parent has not been listed yet, and means "do not cache this".
|
|
@@ -31,6 +47,9 @@ export declare class ParamsetDescriptionCache {
|
|
|
31
47
|
/** Drops everything, or everything of one interface (the identity starts with its name). */
|
|
32
48
|
clear(interfaceName?: string): void;
|
|
33
49
|
toJSON(): DescriptionCacheSnapshot;
|
|
50
|
+
/** Replaces everything with one snapshot (a shard file, or the single file of 3.5 and older). */
|
|
34
51
|
load(snapshot: unknown): void;
|
|
52
|
+
/** Adds a snapshot to what is there, without marking anything changed: it came from disk. */
|
|
53
|
+
merge(snapshot: unknown): void;
|
|
35
54
|
}
|
|
36
55
|
//# sourceMappingURL=descriptions.d.ts.map
|
|
@@ -10,20 +10,51 @@
|
|
|
10
10
|
* with 100 devices is a minute of RPC traffic, and it is the same answer every time. The identity
|
|
11
11
|
* carries the firmware and the version, so a device that was updated gets a new key rather than a
|
|
12
12
|
* stale description - and it is the same key `multiApplyEligibility()` compares (task 6.3).
|
|
13
|
+
*
|
|
14
|
+
* **Stored in shards, one per interface and device type.** A CCU with a few dozen device types
|
|
15
|
+
* has megabytes of descriptions; kept in one file, every description fetched for a type not seen
|
|
16
|
+
* before rewrote all of them, on the addon to the CCU's SD card. Each shard is its own file now
|
|
17
|
+
* (`CacheStore` decides where), and only the shards that gained something are written. A device
|
|
18
|
+
* of a type that is cached already writes nothing at all.
|
|
13
19
|
*/
|
|
14
20
|
import { paramsetIdentity } from '@homematic-manager/core';
|
|
21
|
+
/**
|
|
22
|
+
* The shard an identity belongs to: its interface and device type (`BidCos-RF/HM-LC-Sw1-Pl`).
|
|
23
|
+
* Only a bucket - the identity itself is what a shard file is keyed by.
|
|
24
|
+
*/
|
|
25
|
+
export function shardOf(identity) {
|
|
26
|
+
const [interfaceName = '', deviceType = ''] = identity.split('/');
|
|
27
|
+
return `${interfaceName}/${deviceType}`;
|
|
28
|
+
}
|
|
15
29
|
export class ParamsetDescriptionCache {
|
|
16
30
|
#byIdentity = new Map();
|
|
17
|
-
#
|
|
31
|
+
#dirtyShards = new Set();
|
|
18
32
|
get size() {
|
|
19
33
|
return this.#byIdentity.size;
|
|
20
34
|
}
|
|
21
|
-
/** True when something was stored since the last `
|
|
35
|
+
/** True when something was stored since the last `takeDirty()`. */
|
|
22
36
|
get dirty() {
|
|
23
|
-
return this.#
|
|
37
|
+
return this.#dirtyShards.size > 0;
|
|
38
|
+
}
|
|
39
|
+
/** The shards that changed since the last call, and forgets them. */
|
|
40
|
+
takeDirty() {
|
|
41
|
+
const shards = [...this.#dirtyShards];
|
|
42
|
+
this.#dirtyShards.clear();
|
|
43
|
+
return shards;
|
|
44
|
+
}
|
|
45
|
+
/** Every shard that holds something. */
|
|
46
|
+
shards() {
|
|
47
|
+
return [...new Set([...this.#byIdentity.keys()].map((identity) => shardOf(identity)))];
|
|
24
48
|
}
|
|
25
|
-
|
|
26
|
-
|
|
49
|
+
/** What is written to the file of one shard: identity -> description; empty when it holds nothing. */
|
|
50
|
+
shardSnapshot(shard) {
|
|
51
|
+
const snapshot = {};
|
|
52
|
+
for (const [identity, description] of this.#byIdentity) {
|
|
53
|
+
if (shardOf(identity) === shard) {
|
|
54
|
+
snapshot[identity] = description;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return snapshot;
|
|
27
58
|
}
|
|
28
59
|
/**
|
|
29
60
|
* The identity of a paramset, or `undefined` when the device is not in the index - which is the
|
|
@@ -44,31 +75,35 @@ export class ParamsetDescriptionCache {
|
|
|
44
75
|
if (identity === undefined) {
|
|
45
76
|
return;
|
|
46
77
|
}
|
|
78
|
+
const known = this.#byIdentity.get(identity);
|
|
47
79
|
this.#byIdentity.set(identity, description);
|
|
48
|
-
|
|
80
|
+
if (known === undefined || JSON.stringify(known) !== JSON.stringify(description)) {
|
|
81
|
+
this.#dirtyShards.add(shardOf(identity));
|
|
82
|
+
}
|
|
49
83
|
}
|
|
50
84
|
has(identity) {
|
|
51
85
|
return identity !== undefined && this.#byIdentity.has(identity);
|
|
52
86
|
}
|
|
53
87
|
/** Drops everything, or everything of one interface (the identity starts with its name). */
|
|
54
88
|
clear(interfaceName) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
for (const identity of [...this.#byIdentity.keys()]) {
|
|
60
|
-
if (identity.startsWith(`${interfaceName}/`)) {
|
|
61
|
-
this.#byIdentity.delete(identity);
|
|
62
|
-
}
|
|
89
|
+
for (const identity of [...this.#byIdentity.keys()]) {
|
|
90
|
+
if (interfaceName === undefined || identity.startsWith(`${interfaceName}/`)) {
|
|
91
|
+
this.#byIdentity.delete(identity);
|
|
92
|
+
this.#dirtyShards.add(shardOf(identity));
|
|
63
93
|
}
|
|
64
94
|
}
|
|
65
|
-
this.#dirty = true;
|
|
66
95
|
}
|
|
67
96
|
toJSON() {
|
|
68
97
|
return Object.fromEntries(this.#byIdentity);
|
|
69
98
|
}
|
|
99
|
+
/** Replaces everything with one snapshot (a shard file, or the single file of 3.5 and older). */
|
|
70
100
|
load(snapshot) {
|
|
71
101
|
this.#byIdentity.clear();
|
|
102
|
+
this.#dirtyShards.clear();
|
|
103
|
+
this.merge(snapshot);
|
|
104
|
+
}
|
|
105
|
+
/** Adds a snapshot to what is there, without marking anything changed: it came from disk. */
|
|
106
|
+
merge(snapshot) {
|
|
72
107
|
if (typeof snapshot === 'object' && snapshot !== null) {
|
|
73
108
|
for (const [identity, description] of Object.entries(snapshot)) {
|
|
74
109
|
if (typeof description === 'object' && description !== null && !Array.isArray(description)) {
|
|
@@ -76,7 +111,6 @@ export class ParamsetDescriptionCache {
|
|
|
76
111
|
}
|
|
77
112
|
}
|
|
78
113
|
}
|
|
79
|
-
this.#dirty = false;
|
|
80
114
|
}
|
|
81
115
|
}
|
|
82
116
|
//# sourceMappingURL=descriptions.js.map
|
|
@@ -8,7 +8,12 @@
|
|
|
8
8
|
* that is thrown away on restart answers no question at all.
|
|
9
9
|
*
|
|
10
10
|
* The files are written debounced (`DebouncedJsonFile`), so a CCU that reports 400 devices in one
|
|
11
|
-
* burst causes one write, not 400 as 2.x did
|
|
11
|
+
* burst causes one write, not 400 as 2.x did, and a file whose content did not change is not
|
|
12
|
+
* written at all (`writeJsonFile`). The paramset descriptions are one file per interface and device
|
|
13
|
+
* type under `descriptions/`; the single `descriptions.json` of 3.5 and older is read once, split
|
|
14
|
+
* and removed. The unreach counters wait a minute before they are written: a device that flaps
|
|
15
|
+
* produces an edge per minute or more, and losing the last minute of counts to a power cut costs
|
|
16
|
+
* less than writing the file after each one.
|
|
12
17
|
*/
|
|
13
18
|
import { RingBuffer, RssiStore, ServiceMessageStore, type EventRecord, type ServiceMessage } from '@homematic-manager/core';
|
|
14
19
|
import { DeviceCache } from './devices.js';
|
|
@@ -24,9 +29,13 @@ export interface CacheStoreOptions {
|
|
|
24
29
|
readonly eventBufferSize?: number;
|
|
25
30
|
/** Debounce of the cache files. */
|
|
26
31
|
readonly writeDelayMs?: number;
|
|
32
|
+
/** Debounce of `unreach.json`; a minute by default, `writeDelayMs` when only that is given. */
|
|
33
|
+
readonly unreachWriteDelayMs?: number;
|
|
27
34
|
/** A cache that cannot be written is reported, never thrown. */
|
|
28
35
|
readonly onError?: (error: unknown) => void;
|
|
29
36
|
}
|
|
37
|
+
/** How long the unreach counters wait before they are written. */
|
|
38
|
+
export declare const UNREACH_WRITE_DELAY_MS = 60000;
|
|
30
39
|
/** The caches of one connection. */
|
|
31
40
|
export declare class CacheStore {
|
|
32
41
|
#private;
|
|
@@ -40,9 +49,10 @@ export declare class CacheStore {
|
|
|
40
49
|
constructor(options: CacheStoreOptions);
|
|
41
50
|
/** The RSSI matrix of one interface; created on first use. */
|
|
42
51
|
rssi(interfaceName: string): RssiStore;
|
|
43
|
-
/** Reads the
|
|
52
|
+
/** Reads the persisted caches. Never throws; a missing file means "nothing cached". */
|
|
44
53
|
load(): Promise<void>;
|
|
45
54
|
saveDevices(): void;
|
|
55
|
+
/** Writes the shards that gained (or lost) something, and only those. */
|
|
46
56
|
saveDescriptions(): void;
|
|
47
57
|
saveNames(): void;
|
|
48
58
|
saveUnreach(): void;
|
|
@@ -8,14 +8,60 @@
|
|
|
8
8
|
* that is thrown away on restart answers no question at all.
|
|
9
9
|
*
|
|
10
10
|
* The files are written debounced (`DebouncedJsonFile`), so a CCU that reports 400 devices in one
|
|
11
|
-
* burst causes one write, not 400 as 2.x did
|
|
11
|
+
* burst causes one write, not 400 as 2.x did, and a file whose content did not change is not
|
|
12
|
+
* written at all (`writeJsonFile`). The paramset descriptions are one file per interface and device
|
|
13
|
+
* type under `descriptions/`; the single `descriptions.json` of 3.5 and older is read once, split
|
|
14
|
+
* and removed. The unreach counters wait a minute before they are written: a device that flaps
|
|
15
|
+
* produces an edge per minute or more, and losing the last minute of counts to a power cut costs
|
|
16
|
+
* less than writing the file after each one.
|
|
12
17
|
*/
|
|
18
|
+
import { createHash } from 'node:crypto';
|
|
19
|
+
import fs from 'node:fs/promises';
|
|
20
|
+
import path from 'node:path';
|
|
13
21
|
import { RingBuffer, RssiStore, ServiceMessageStore, } from '@homematic-manager/core';
|
|
14
|
-
import { DebouncedJsonFile } from '../util/jsonFile.js';
|
|
22
|
+
import { DebouncedJsonFile, readJsonFile, removeFile, writeJsonFile } from '../util/jsonFile.js';
|
|
15
23
|
import { DeviceCache } from './devices.js';
|
|
16
24
|
import { ParamsetDescriptionCache } from './descriptions.js';
|
|
17
25
|
import { NameStore } from './names.js';
|
|
18
26
|
import { UnreachCache } from './unreach.js';
|
|
27
|
+
/** How long the unreach counters wait before they are written. */
|
|
28
|
+
export const UNREACH_WRITE_DELAY_MS = 60_000;
|
|
29
|
+
/** A file name made of anything: letters, digits, `-` and `_` stay, the rest is `%xx`, lower-cased. */
|
|
30
|
+
function safeName(name) {
|
|
31
|
+
const safe = [...Buffer.from(name, 'utf8')]
|
|
32
|
+
.map((byte) => {
|
|
33
|
+
const char = String.fromCharCode(byte);
|
|
34
|
+
return /[A-Za-z0-9_-]/.test(char) ? char : `%${byte.toString(16).padStart(2, '0')}`;
|
|
35
|
+
})
|
|
36
|
+
.join('');
|
|
37
|
+
return (safe === '' ? '%' : safe).toLowerCase();
|
|
38
|
+
}
|
|
39
|
+
/** The `.json` files one level below the shard directory; none when it does not exist. */
|
|
40
|
+
async function shardFiles(dir) {
|
|
41
|
+
const files = [];
|
|
42
|
+
let interfaces;
|
|
43
|
+
try {
|
|
44
|
+
interfaces = await fs.readdir(dir);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return files;
|
|
48
|
+
}
|
|
49
|
+
for (const interfaceDir of interfaces.sort()) {
|
|
50
|
+
let names;
|
|
51
|
+
try {
|
|
52
|
+
names = await fs.readdir(path.join(dir, interfaceDir));
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
for (const name of names.sort()) {
|
|
58
|
+
if (name.endsWith('.json')) {
|
|
59
|
+
files.push(path.join(dir, interfaceDir, name));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return files;
|
|
64
|
+
}
|
|
19
65
|
/** The caches of one connection. */
|
|
20
66
|
export class CacheStore {
|
|
21
67
|
devices = new DeviceCache();
|
|
@@ -28,9 +74,11 @@ export class CacheStore {
|
|
|
28
74
|
#rssi = new Map();
|
|
29
75
|
#now;
|
|
30
76
|
#deviceFile;
|
|
31
|
-
#
|
|
77
|
+
#descriptionFiles = new Map();
|
|
32
78
|
#nameFile;
|
|
33
79
|
#unreachFile;
|
|
80
|
+
#cacheDir;
|
|
81
|
+
#fileOptions;
|
|
34
82
|
constructor(options) {
|
|
35
83
|
this.#now = options.now ?? (() => Date.now());
|
|
36
84
|
this.serviceMessages = new ServiceMessageStore({ now: this.#now });
|
|
@@ -39,10 +87,40 @@ export class CacheStore {
|
|
|
39
87
|
delayMs: options.writeDelayMs ?? 500,
|
|
40
88
|
...(options.onError ? { onError: options.onError } : {}),
|
|
41
89
|
};
|
|
90
|
+
this.#cacheDir = options.cacheDir;
|
|
91
|
+
this.#fileOptions = fileOptions;
|
|
42
92
|
this.#deviceFile = new DebouncedJsonFile(`${options.cacheDir}/devices.json`, fileOptions);
|
|
43
|
-
this.#descriptionFile = new DebouncedJsonFile(`${options.cacheDir}/descriptions.json`, fileOptions);
|
|
44
93
|
this.#nameFile = new DebouncedJsonFile(`${options.cacheDir}/names.json`, fileOptions);
|
|
45
|
-
this.#unreachFile = new DebouncedJsonFile(`${options.cacheDir}/unreach.json`,
|
|
94
|
+
this.#unreachFile = new DebouncedJsonFile(`${options.cacheDir}/unreach.json`, {
|
|
95
|
+
...fileOptions,
|
|
96
|
+
delayMs: options.unreachWriteDelayMs ?? options.writeDelayMs ?? UNREACH_WRITE_DELAY_MS,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/** The directory of the description shards. */
|
|
100
|
+
get #descriptionDir() {
|
|
101
|
+
return path.join(this.#cacheDir, 'descriptions');
|
|
102
|
+
}
|
|
103
|
+
/** The single description file of 3.5 and older. */
|
|
104
|
+
get #legacyDescriptionFile() {
|
|
105
|
+
return path.join(this.#cacheDir, 'descriptions.json');
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The file of one shard: `descriptions/<interface>/<type>.<hash>.json`. The name is the device
|
|
109
|
+
* type made safe for every file system, lower-cased, and the hash of the exact type keeps two
|
|
110
|
+
* types that differ only in case (`HMIP-eTRV`, `HmIP-eTRV`) apart where names do not.
|
|
111
|
+
*/
|
|
112
|
+
#descriptionFile(shard) {
|
|
113
|
+
const existing = this.#descriptionFiles.get(shard);
|
|
114
|
+
if (existing) {
|
|
115
|
+
return existing;
|
|
116
|
+
}
|
|
117
|
+
const separator = shard.indexOf('/');
|
|
118
|
+
const interfaceName = shard.slice(0, separator);
|
|
119
|
+
const deviceType = shard.slice(separator + 1);
|
|
120
|
+
const hash = createHash('sha256').update(deviceType).digest('hex').slice(0, 8);
|
|
121
|
+
const file = new DebouncedJsonFile(path.join(this.#descriptionDir, safeName(interfaceName), `${safeName(deviceType)}.${hash}.json`), this.#fileOptions);
|
|
122
|
+
this.#descriptionFiles.set(shard, file);
|
|
123
|
+
return file;
|
|
46
124
|
}
|
|
47
125
|
/** The RSSI matrix of one interface; created on first use. */
|
|
48
126
|
rssi(interfaceName) {
|
|
@@ -54,19 +132,51 @@ export class CacheStore {
|
|
|
54
132
|
this.#rssi.set(interfaceName, store);
|
|
55
133
|
return store;
|
|
56
134
|
}
|
|
57
|
-
/** Reads the
|
|
135
|
+
/** Reads the persisted caches. Never throws; a missing file means "nothing cached". */
|
|
58
136
|
async load() {
|
|
59
137
|
this.devices.load(await this.#deviceFile.read());
|
|
60
|
-
|
|
138
|
+
await this.#loadDescriptions();
|
|
61
139
|
this.names.load(await this.#nameFile.read());
|
|
62
140
|
this.unreach.load(await this.#unreachFile.read());
|
|
63
141
|
}
|
|
142
|
+
/** Every shard file, then the single file of 3.5 and older, which is split once and removed. */
|
|
143
|
+
async #loadDescriptions() {
|
|
144
|
+
this.descriptions.load(undefined);
|
|
145
|
+
for (const file of await shardFiles(this.#descriptionDir)) {
|
|
146
|
+
this.descriptions.merge(await readJsonFile(file));
|
|
147
|
+
}
|
|
148
|
+
const legacy = await readJsonFile(this.#legacyDescriptionFile);
|
|
149
|
+
if (legacy === undefined) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
this.descriptions.merge(legacy);
|
|
153
|
+
this.descriptions.takeDirty();
|
|
154
|
+
try {
|
|
155
|
+
await Promise.all(this.descriptions
|
|
156
|
+
.shards()
|
|
157
|
+
.map((shard) => writeJsonFile(this.#descriptionFile(shard).file, this.descriptions.shardSnapshot(shard))));
|
|
158
|
+
await removeFile(this.#legacyDescriptionFile);
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
// the old file stays and is split again next time; the cache in memory is complete
|
|
162
|
+
this.#fileOptions.onError?.(error);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
64
165
|
saveDevices() {
|
|
65
166
|
this.#deviceFile.save(this.devices.toJSON());
|
|
66
167
|
}
|
|
168
|
+
/** Writes the shards that gained (or lost) something, and only those. */
|
|
67
169
|
saveDescriptions() {
|
|
68
|
-
|
|
69
|
-
|
|
170
|
+
for (const shard of this.descriptions.takeDirty()) {
|
|
171
|
+
const snapshot = this.descriptions.shardSnapshot(shard);
|
|
172
|
+
const file = this.#descriptionFile(shard);
|
|
173
|
+
if (Object.keys(snapshot).length === 0) {
|
|
174
|
+
void file.remove();
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
file.save(snapshot);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
70
180
|
}
|
|
71
181
|
saveNames() {
|
|
72
182
|
this.#nameFile.save(this.names.toJSON());
|
|
@@ -78,7 +188,7 @@ export class CacheStore {
|
|
|
78
188
|
async flush() {
|
|
79
189
|
await Promise.all([
|
|
80
190
|
this.#deviceFile.flush(),
|
|
81
|
-
this.#
|
|
191
|
+
...[...this.#descriptionFiles.values()].map((file) => file.flush()),
|
|
82
192
|
this.#nameFile.flush(),
|
|
83
193
|
this.#unreachFile.flush(),
|
|
84
194
|
]);
|
|
@@ -94,12 +204,17 @@ export class CacheStore {
|
|
|
94
204
|
for (const interfaceName of this.serviceMessages.list().map((message) => message.interfaceName)) {
|
|
95
205
|
this.serviceMessages.replaceInterface(interfaceName, []);
|
|
96
206
|
}
|
|
207
|
+
this.descriptions.takeDirty();
|
|
97
208
|
await Promise.all([
|
|
98
209
|
this.#deviceFile.remove(),
|
|
99
|
-
this.#
|
|
210
|
+
...[...this.#descriptionFiles.values()].map((file) => file.remove()),
|
|
100
211
|
this.#nameFile.remove(),
|
|
101
212
|
this.#unreachFile.remove(),
|
|
213
|
+
removeFile(this.#legacyDescriptionFile),
|
|
102
214
|
]);
|
|
215
|
+
this.#descriptionFiles.clear();
|
|
216
|
+
// shards of another session (a type this one never touched) go with the directory
|
|
217
|
+
await fs.rm(this.#descriptionDir, { recursive: true, force: true }).catch(() => undefined);
|
|
103
218
|
}
|
|
104
219
|
/** The service messages in the shape the contract asks for. */
|
|
105
220
|
listServiceMessages(interfaceName) {
|
|
@@ -38,6 +38,12 @@ export interface OcculiteProviderOptions extends MetaProviderEvents {
|
|
|
38
38
|
readonly system: SystemLink;
|
|
39
39
|
/** `<cacheDir>/occulite-meta.json`: the last snapshot, so a restart without the box has names. */
|
|
40
40
|
readonly cacheFile?: string;
|
|
41
|
+
/**
|
|
42
|
+
* How long changes to the cache file are collected before it is written. The box holds the
|
|
43
|
+
* store; the file is only what a restart without the box shows, so a burst of changes (an
|
|
44
|
+
* import, a room assigned to fifty channels) is one write, not fifty. `stop()` writes it.
|
|
45
|
+
*/
|
|
46
|
+
readonly cacheWriteDelayMs?: number;
|
|
41
47
|
/** `occulited 0.1.0`, from the detection call. */
|
|
42
48
|
readonly implementation?: string;
|
|
43
49
|
readonly reconnectMinMs?: number;
|
|
@@ -45,6 +51,8 @@ export interface OcculiteProviderOptions extends MetaProviderEvents {
|
|
|
45
51
|
/** Injected by the tests in place of `setTimeout`. */
|
|
46
52
|
readonly wait?: (ms: number, signal: AbortSignal) => Promise<void>;
|
|
47
53
|
}
|
|
54
|
+
/** How long changes to the cache file are collected; see `cacheWriteDelayMs`. */
|
|
55
|
+
export declare const CACHE_WRITE_DELAY_MS = 5000;
|
|
48
56
|
export declare class OcculiteProvider implements MetadataProvider {
|
|
49
57
|
#private;
|
|
50
58
|
readonly kind: "occulite";
|
|
@@ -28,11 +28,13 @@
|
|
|
28
28
|
*/
|
|
29
29
|
import { applyEvent, emptyDocument, isMetaError, parseDocument, summariseAll, } from '@homematic-manager/core';
|
|
30
30
|
import { errorMessage } from '../errors.js';
|
|
31
|
-
import {
|
|
31
|
+
import { DebouncedJsonFile } from '../util/jsonFile.js';
|
|
32
32
|
/** First wait before the event stream is opened again, in milliseconds. */
|
|
33
33
|
export const RECONNECT_MIN_MS = 2000;
|
|
34
34
|
/** Longest wait between two attempts. A box that is being updated is back well inside a minute. */
|
|
35
35
|
export const RECONNECT_MAX_MS = 60_000;
|
|
36
|
+
/** How long changes to the cache file are collected; see `cacheWriteDelayMs`. */
|
|
37
|
+
export const CACHE_WRITE_DELAY_MS = 5000;
|
|
36
38
|
export class OcculiteProvider {
|
|
37
39
|
kind = 'occulite';
|
|
38
40
|
#options;
|
|
@@ -47,9 +49,19 @@ export class OcculiteProvider {
|
|
|
47
49
|
#lastNotice = '';
|
|
48
50
|
/** Ends the wait between two attempts at the event stream; set while the loop is sleeping. */
|
|
49
51
|
#wake;
|
|
52
|
+
#cache;
|
|
50
53
|
constructor(options) {
|
|
51
54
|
this.#options = options;
|
|
52
55
|
this.#system = options.system;
|
|
56
|
+
this.#cache =
|
|
57
|
+
options.cacheFile === undefined
|
|
58
|
+
? undefined
|
|
59
|
+
: new DebouncedJsonFile(options.cacheFile, {
|
|
60
|
+
delayMs: options.cacheWriteDelayMs ?? CACHE_WRITE_DELAY_MS,
|
|
61
|
+
onError: (error) => {
|
|
62
|
+
options.onNotice('warn', `the metadata cache could not be written: ${errorMessage(error)}`);
|
|
63
|
+
},
|
|
64
|
+
});
|
|
53
65
|
}
|
|
54
66
|
state() {
|
|
55
67
|
return {
|
|
@@ -90,6 +102,7 @@ export class OcculiteProvider {
|
|
|
90
102
|
// the loop ends by being aborted; that is not a failure
|
|
91
103
|
}
|
|
92
104
|
this.#loop = undefined;
|
|
105
|
+
await this.#cache?.flush();
|
|
93
106
|
}
|
|
94
107
|
/**
|
|
95
108
|
* A fresh snapshot, on demand - the credential changed, or a host asked for one.
|
|
@@ -196,7 +209,7 @@ export class OcculiteProvider {
|
|
|
196
209
|
// change nobody announced
|
|
197
210
|
this.#options.onStateChanged(this.state());
|
|
198
211
|
this.#options.onChanged();
|
|
199
|
-
|
|
212
|
+
this.#saveCache();
|
|
200
213
|
}
|
|
201
214
|
catch (error) {
|
|
202
215
|
this.#fail(error);
|
|
@@ -235,7 +248,7 @@ export class OcculiteProvider {
|
|
|
235
248
|
this.#document = next;
|
|
236
249
|
this.#options.onStateChanged(this.state());
|
|
237
250
|
this.#options.onChanged();
|
|
238
|
-
|
|
251
|
+
this.#saveCache();
|
|
239
252
|
}
|
|
240
253
|
}
|
|
241
254
|
catch (error) {
|
|
@@ -267,10 +280,7 @@ export class OcculiteProvider {
|
|
|
267
280
|
}
|
|
268
281
|
}
|
|
269
282
|
async #loadCache() {
|
|
270
|
-
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
273
|
-
const raw = await readJsonFile(this.#options.cacheFile);
|
|
283
|
+
const raw = await this.#cache?.read();
|
|
274
284
|
if (raw === undefined) {
|
|
275
285
|
return;
|
|
276
286
|
}
|
|
@@ -283,16 +293,9 @@ export class OcculiteProvider {
|
|
|
283
293
|
// next snapshot and nobody needs to hear about it
|
|
284
294
|
}
|
|
285
295
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
}
|
|
290
|
-
try {
|
|
291
|
-
await writeJsonFile(this.#options.cacheFile, this.#document);
|
|
292
|
-
}
|
|
293
|
-
catch (error) {
|
|
294
|
-
this.#options.onNotice('warn', `the metadata cache could not be written: ${errorMessage(error)}`);
|
|
295
|
-
}
|
|
296
|
+
/** Debounced; a snapshot that equals the file (every reconnect) is not written at all. */
|
|
297
|
+
#saveCache() {
|
|
298
|
+
this.#cache?.save(this.#document);
|
|
296
299
|
}
|
|
297
300
|
#sleep(ms, signal) {
|
|
298
301
|
if (this.#options.wait) {
|
|
@@ -9,6 +9,12 @@
|
|
|
9
9
|
* rewrote the whole device cache on every `newDevices` callback; a CCU that reports 400 devices in
|
|
10
10
|
* a burst wrote the file 400 times.
|
|
11
11
|
*
|
|
12
|
+
* **Unchanged content is not written again.** On the addon the data directory is the CCU's SD
|
|
13
|
+
* card, and most saves repeat what is there already: the device list after every `init`, the names
|
|
14
|
+
* and the configuration at every start, the metadata snapshot after every reconnect. The hash of
|
|
15
|
+
* what was last read or written is kept per file, and a write whose text hashes the same - while
|
|
16
|
+
* the file is still there with that size - is skipped.
|
|
17
|
+
*
|
|
12
18
|
* A read failure is never fatal: a missing or corrupt file means "no cache yet".
|
|
13
19
|
*/
|
|
14
20
|
/** Reads and parses a JSON file; `undefined` when it is missing, unreadable or not JSON. */
|
|
@@ -16,9 +22,10 @@ export declare function readJsonFile<T>(file: string): Promise<T | undefined>;
|
|
|
16
22
|
/**
|
|
17
23
|
* Writes a JSON file atomically, creating the directory if it does not exist. Writes of the same
|
|
18
24
|
* file run one after another in call order, so the last call's value is what stays; the value is
|
|
19
|
-
* serialised at the call.
|
|
25
|
+
* serialised at the call. Resolves to `false` when the file already held exactly this text and
|
|
26
|
+
* nothing was written.
|
|
20
27
|
*/
|
|
21
|
-
export declare function writeJsonFile(file: string, value: unknown): Promise<
|
|
28
|
+
export declare function writeJsonFile(file: string, value: unknown): Promise<boolean>;
|
|
22
29
|
/** Removes a file; a missing file is not an error. */
|
|
23
30
|
export declare function removeFile(file: string): Promise<void>;
|
|
24
31
|
export interface DebouncedJsonFileOptions {
|
|
@@ -9,10 +9,35 @@
|
|
|
9
9
|
* rewrote the whole device cache on every `newDevices` callback; a CCU that reports 400 devices in
|
|
10
10
|
* a burst wrote the file 400 times.
|
|
11
11
|
*
|
|
12
|
+
* **Unchanged content is not written again.** On the addon the data directory is the CCU's SD
|
|
13
|
+
* card, and most saves repeat what is there already: the device list after every `init`, the names
|
|
14
|
+
* and the configuration at every start, the metadata snapshot after every reconnect. The hash of
|
|
15
|
+
* what was last read or written is kept per file, and a write whose text hashes the same - while
|
|
16
|
+
* the file is still there with that size - is skipped.
|
|
17
|
+
*
|
|
12
18
|
* A read failure is never fatal: a missing or corrupt file means "no cache yet".
|
|
13
19
|
*/
|
|
20
|
+
import { createHash } from 'node:crypto';
|
|
14
21
|
import fs from 'node:fs/promises';
|
|
15
22
|
import path from 'node:path';
|
|
23
|
+
const knownContent = new Map();
|
|
24
|
+
function contentOf(text) {
|
|
25
|
+
return { hash: createHash('sha256').update(text).digest('base64'), bytes: Buffer.byteLength(text) };
|
|
26
|
+
}
|
|
27
|
+
/** Is the file on disk still the text that hashes to `content`? A missing file is not. */
|
|
28
|
+
async function holds(file, content) {
|
|
29
|
+
const known = knownContent.get(file);
|
|
30
|
+
if (known?.hash !== content.hash || known.bytes !== content.bytes) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
// somebody may have deleted or replaced the file behind our back; the size is the cheap check
|
|
35
|
+
return (await fs.stat(file)).size === content.bytes;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
16
41
|
/** Reads and parses a JSON file; `undefined` when it is missing, unreadable or not JSON. */
|
|
17
42
|
export async function readJsonFile(file) {
|
|
18
43
|
let text;
|
|
@@ -20,8 +45,10 @@ export async function readJsonFile(file) {
|
|
|
20
45
|
text = await fs.readFile(file, 'utf8');
|
|
21
46
|
}
|
|
22
47
|
catch {
|
|
48
|
+
knownContent.delete(file);
|
|
23
49
|
return undefined;
|
|
24
50
|
}
|
|
51
|
+
knownContent.set(file, contentOf(text));
|
|
25
52
|
try {
|
|
26
53
|
return JSON.parse(text);
|
|
27
54
|
}
|
|
@@ -38,16 +65,24 @@ const writesInProgress = new Map();
|
|
|
38
65
|
/**
|
|
39
66
|
* Writes a JSON file atomically, creating the directory if it does not exist. Writes of the same
|
|
40
67
|
* file run one after another in call order, so the last call's value is what stays; the value is
|
|
41
|
-
* serialised at the call.
|
|
68
|
+
* serialised at the call. Resolves to `false` when the file already held exactly this text and
|
|
69
|
+
* nothing was written.
|
|
42
70
|
*/
|
|
43
71
|
export async function writeJsonFile(file, value) {
|
|
44
72
|
const text = `${JSON.stringify(value, null, 2)}\n`;
|
|
73
|
+
const content = contentOf(text);
|
|
45
74
|
const previous = writesInProgress.get(file) ?? Promise.resolve();
|
|
46
75
|
const write = previous.then(async () => {
|
|
76
|
+
if (await holds(file, content)) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
knownContent.delete(file);
|
|
47
80
|
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
48
81
|
const temporary = `${file}.tmp`;
|
|
49
82
|
await fs.writeFile(temporary, text, 'utf8');
|
|
50
83
|
await fs.rename(temporary, file);
|
|
84
|
+
knownContent.set(file, content);
|
|
85
|
+
return true;
|
|
51
86
|
});
|
|
52
87
|
// the next write waits for this one whether it worked or not; the caller hears the error
|
|
53
88
|
const settled = write.catch(() => undefined);
|
|
@@ -61,6 +96,7 @@ export async function writeJsonFile(file, value) {
|
|
|
61
96
|
}
|
|
62
97
|
/** Removes a file; a missing file is not an error. */
|
|
63
98
|
export async function removeFile(file) {
|
|
99
|
+
knownContent.delete(file);
|
|
64
100
|
try {
|
|
65
101
|
await fs.rm(file, { force: true });
|
|
66
102
|
}
|
|
@@ -137,8 +173,9 @@ export class DebouncedJsonFile {
|
|
|
137
173
|
this.#timer = undefined;
|
|
138
174
|
}
|
|
139
175
|
this.#pending = undefined;
|
|
176
|
+
// in the same chain as the writes, so a save that follows the removal is not undone by it
|
|
177
|
+
this.#writing = this.#writing.then(() => removeFile(this.file));
|
|
140
178
|
await this.#writing;
|
|
141
|
-
await removeFile(this.file);
|
|
142
179
|
}
|
|
143
180
|
}
|
|
144
181
|
//# sourceMappingURL=jsonFile.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "homematic-manager",
|
|
3
|
-
"version": "3.5.
|
|
3
|
+
"version": "3.5.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Configure and administer Homematic and HomematicIP devices - devices and channels, direct links, paramsets, RSSI, service messages, events and an RPC console - from a local HTTP/WebSocket server that also serves the web UI",
|
|
6
6
|
"keywords": [
|
|
@@ -67,8 +67,8 @@
|
|
|
67
67
|
"hm-simulator": "^1.5.0"
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
|
-
"@homematic-manager/backend": "3.5.
|
|
71
|
-
"@homematic-manager/core": "3.5.
|
|
70
|
+
"@homematic-manager/backend": "3.5.1",
|
|
71
|
+
"@homematic-manager/core": "3.5.1",
|
|
72
72
|
"binrpc": "^4.2.0",
|
|
73
73
|
"homematic-rega": "^2.0.0",
|
|
74
74
|
"homematic-xmlrpc": "^2.0.0",
|