matterbridge 3.0.1 → 3.0.2-dev-20250508-7214e17
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/CHANGELOG.md +25 -0
- package/README.md +1 -1
- package/dist/cli.js +2 -37
- package/dist/cluster/export.js +0 -2
- package/dist/defaultConfigSchema.js +0 -23
- package/dist/deviceManager.js +1 -94
- package/dist/frontend.js +15 -328
- package/dist/index.js +2 -28
- package/dist/logger/export.js +0 -1
- package/dist/matter/behaviors.js +0 -2
- package/dist/matter/clusters.js +0 -2
- package/dist/matter/devices.js +0 -2
- package/dist/matter/endpoints.js +0 -2
- package/dist/matter/export.js +0 -2
- package/dist/matter/types.js +0 -2
- package/dist/matterbridge.js +80 -747
- package/dist/matterbridgeAccessoryPlatform.js +0 -34
- package/dist/matterbridgeBehaviors.js +109 -48
- package/dist/matterbridgeDeviceTypes.js +12 -431
- package/dist/matterbridgeDynamicPlatform.js +0 -34
- package/dist/matterbridgeEndpoint.js +13 -814
- package/dist/matterbridgeEndpointHelpers.js +29 -148
- package/dist/matterbridgePlatform.js +7 -225
- package/dist/matterbridgeTypes.js +0 -24
- package/dist/pluginManager.js +3 -264
- package/dist/roboticVacuumCleaner.js +87 -0
- package/dist/shelly.js +6 -146
- package/dist/storage/export.js +0 -1
- package/dist/update.js +1 -53
- package/dist/utils/colorUtils.js +2 -205
- package/dist/utils/{parameter.js → commandLine.js} +1 -54
- package/dist/utils/copyDirectory.js +1 -37
- package/dist/utils/createZip.js +2 -42
- package/dist/utils/deepCopy.js +8 -43
- package/dist/utils/deepEqual.js +7 -69
- package/dist/utils/export.js +2 -2
- package/dist/utils/hex.js +27 -0
- package/dist/utils/isvalid.js +3 -93
- package/dist/utils/network.js +7 -78
- package/dist/utils/wait.js +5 -48
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -2
- package/dist/cli.d.ts +0 -29
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/cluster/export.d.ts +0 -2
- package/dist/cluster/export.d.ts.map +0 -1
- package/dist/cluster/export.js.map +0 -1
- package/dist/defaultConfigSchema.d.ts +0 -27
- package/dist/defaultConfigSchema.d.ts.map +0 -1
- package/dist/defaultConfigSchema.js.map +0 -1
- package/dist/deviceManager.d.ts +0 -114
- package/dist/deviceManager.d.ts.map +0 -1
- package/dist/deviceManager.js.map +0 -1
- package/dist/frontend.d.ts +0 -240
- package/dist/frontend.d.ts.map +0 -1
- package/dist/frontend.js.map +0 -1
- package/dist/index.d.ts +0 -35
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/logger/export.d.ts +0 -2
- package/dist/logger/export.d.ts.map +0 -1
- package/dist/logger/export.js.map +0 -1
- package/dist/matter/behaviors.d.ts +0 -2
- package/dist/matter/behaviors.d.ts.map +0 -1
- package/dist/matter/behaviors.js.map +0 -1
- package/dist/matter/clusters.d.ts +0 -2
- package/dist/matter/clusters.d.ts.map +0 -1
- package/dist/matter/clusters.js.map +0 -1
- package/dist/matter/devices.d.ts +0 -2
- package/dist/matter/devices.d.ts.map +0 -1
- package/dist/matter/devices.js.map +0 -1
- package/dist/matter/endpoints.d.ts +0 -2
- package/dist/matter/endpoints.d.ts.map +0 -1
- package/dist/matter/endpoints.js.map +0 -1
- package/dist/matter/export.d.ts +0 -5
- package/dist/matter/export.d.ts.map +0 -1
- package/dist/matter/export.js.map +0 -1
- package/dist/matter/types.d.ts +0 -3
- package/dist/matter/types.d.ts.map +0 -1
- package/dist/matter/types.js.map +0 -1
- package/dist/matterbridge.d.ts +0 -433
- package/dist/matterbridge.d.ts.map +0 -1
- package/dist/matterbridge.js.map +0 -1
- package/dist/matterbridgeAccessoryPlatform.d.ts +0 -40
- package/dist/matterbridgeAccessoryPlatform.d.ts.map +0 -1
- package/dist/matterbridgeAccessoryPlatform.js.map +0 -1
- package/dist/matterbridgeBehaviors.d.ts +0 -1166
- package/dist/matterbridgeBehaviors.d.ts.map +0 -1
- package/dist/matterbridgeBehaviors.js.map +0 -1
- package/dist/matterbridgeDeviceTypes.d.ts +0 -494
- package/dist/matterbridgeDeviceTypes.d.ts.map +0 -1
- package/dist/matterbridgeDeviceTypes.js.map +0 -1
- package/dist/matterbridgeDynamicPlatform.d.ts +0 -40
- package/dist/matterbridgeDynamicPlatform.d.ts.map +0 -1
- package/dist/matterbridgeDynamicPlatform.js.map +0 -1
- package/dist/matterbridgeEndpoint.d.ts +0 -956
- package/dist/matterbridgeEndpoint.d.ts.map +0 -1
- package/dist/matterbridgeEndpoint.js.map +0 -1
- package/dist/matterbridgeEndpointHelpers.d.ts +0 -2706
- package/dist/matterbridgeEndpointHelpers.d.ts.map +0 -1
- package/dist/matterbridgeEndpointHelpers.js.map +0 -1
- package/dist/matterbridgePlatform.d.ts +0 -294
- package/dist/matterbridgePlatform.d.ts.map +0 -1
- package/dist/matterbridgePlatform.js.map +0 -1
- package/dist/matterbridgeTypes.d.ts +0 -187
- package/dist/matterbridgeTypes.d.ts.map +0 -1
- package/dist/matterbridgeTypes.js.map +0 -1
- package/dist/pluginManager.d.ts +0 -273
- package/dist/pluginManager.d.ts.map +0 -1
- package/dist/pluginManager.js.map +0 -1
- package/dist/shelly.d.ts +0 -92
- package/dist/shelly.d.ts.map +0 -1
- package/dist/shelly.js.map +0 -1
- package/dist/storage/export.d.ts +0 -2
- package/dist/storage/export.d.ts.map +0 -1
- package/dist/storage/export.js.map +0 -1
- package/dist/update.d.ts +0 -32
- package/dist/update.d.ts.map +0 -1
- package/dist/update.js.map +0 -1
- package/dist/utils/colorUtils.d.ts +0 -61
- package/dist/utils/colorUtils.d.ts.map +0 -1
- package/dist/utils/colorUtils.js.map +0 -1
- package/dist/utils/copyDirectory.d.ts +0 -32
- package/dist/utils/copyDirectory.d.ts.map +0 -1
- package/dist/utils/copyDirectory.js.map +0 -1
- package/dist/utils/createZip.d.ts +0 -38
- package/dist/utils/createZip.d.ts.map +0 -1
- package/dist/utils/createZip.js.map +0 -1
- package/dist/utils/deepCopy.d.ts +0 -31
- package/dist/utils/deepCopy.d.ts.map +0 -1
- package/dist/utils/deepCopy.js.map +0 -1
- package/dist/utils/deepEqual.d.ts +0 -53
- package/dist/utils/deepEqual.d.ts.map +0 -1
- package/dist/utils/deepEqual.js.map +0 -1
- package/dist/utils/export.d.ts +0 -10
- package/dist/utils/export.d.ts.map +0 -1
- package/dist/utils/export.js.map +0 -1
- package/dist/utils/isvalid.d.ts +0 -95
- package/dist/utils/isvalid.d.ts.map +0 -1
- package/dist/utils/isvalid.js.map +0 -1
- package/dist/utils/network.d.ts +0 -69
- package/dist/utils/network.d.ts.map +0 -1
- package/dist/utils/network.js.map +0 -1
- package/dist/utils/parameter.d.ts +0 -58
- package/dist/utils/parameter.d.ts.map +0 -1
- package/dist/utils/parameter.js.map +0 -1
- package/dist/utils/wait.d.ts +0 -43
- package/dist/utils/wait.d.ts.map +0 -1
- package/dist/utils/wait.js.map +0 -1
package/dist/shelly.js
CHANGED
|
@@ -1,33 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file contains the shelly api functions.
|
|
3
|
-
*
|
|
4
|
-
* @file shelly.ts
|
|
5
|
-
* @author Luca Liguori
|
|
6
|
-
* @date 2025-02-19
|
|
7
|
-
* @version 1.0.3
|
|
8
|
-
*
|
|
9
|
-
* Copyright 2025, 2026, 2027 Luca Liguori.
|
|
10
|
-
*
|
|
11
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
12
|
-
* you may not use this file except in compliance with the License.
|
|
13
|
-
* You may obtain a copy of the License at
|
|
14
|
-
*
|
|
15
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
-
*
|
|
17
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
18
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
19
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
-
* See the License for the specific language governing permissions and
|
|
21
|
-
* limitations under the License. *
|
|
22
|
-
*/
|
|
23
1
|
import { WS_ID_SHELLY_MAIN_UPDATE, WS_ID_SHELLY_SYS_UPDATE } from './frontend.js';
|
|
24
2
|
import { debugStringify } from './logger/export.js';
|
|
25
|
-
/**
|
|
26
|
-
* Fetches Shelly system updates. If available: logs the result, sends a snackbar message, and broadcasts the message.
|
|
27
|
-
*
|
|
28
|
-
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
29
|
-
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
30
|
-
*/
|
|
31
3
|
export async function getShellySysUpdate(matterbridge) {
|
|
32
4
|
getShelly('/api/updates/sys/check', 60 * 1000)
|
|
33
5
|
.then(async (data) => {
|
|
@@ -46,12 +18,6 @@ export async function getShellySysUpdate(matterbridge) {
|
|
|
46
18
|
matterbridge.log.warn(`Error getting Shelly system updates: ${error instanceof Error ? error.message : error}`);
|
|
47
19
|
});
|
|
48
20
|
}
|
|
49
|
-
/**
|
|
50
|
-
* Triggers Shelly system updates.
|
|
51
|
-
*
|
|
52
|
-
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
53
|
-
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
54
|
-
*/
|
|
55
21
|
export async function triggerShellySysUpdate(matterbridge) {
|
|
56
22
|
getShelly('/api/updates/sys/perform', 10 * 1000)
|
|
57
23
|
.then(async () => {
|
|
@@ -68,12 +34,6 @@ export async function triggerShellySysUpdate(matterbridge) {
|
|
|
68
34
|
verifyShellyUpdate(matterbridge, '/api/updates/sys/status', 'Shelly system update');
|
|
69
35
|
});
|
|
70
36
|
}
|
|
71
|
-
/**
|
|
72
|
-
* Fetches Shelly main updates. If available: logs the result, sends a snackbar message, and broadcasts the message.
|
|
73
|
-
*
|
|
74
|
-
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
75
|
-
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
76
|
-
*/
|
|
77
37
|
export async function getShellyMainUpdate(matterbridge) {
|
|
78
38
|
getShelly('/api/updates/main/check', 60 * 1000)
|
|
79
39
|
.then(async (data) => {
|
|
@@ -92,15 +52,9 @@ export async function getShellyMainUpdate(matterbridge) {
|
|
|
92
52
|
matterbridge.log.warn(`Error getting Shelly main updates: ${error instanceof Error ? error.message : error}`);
|
|
93
53
|
});
|
|
94
54
|
}
|
|
95
|
-
/**
|
|
96
|
-
* Triggers Shelly main updates.
|
|
97
|
-
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
98
|
-
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
99
|
-
*/
|
|
100
55
|
export async function triggerShellyMainUpdate(matterbridge) {
|
|
101
56
|
getShelly('/api/updates/main/perform', 10 * 1000)
|
|
102
57
|
.then(async () => {
|
|
103
|
-
// {"updatingInProgress":true} or {"updatingInProgress":false}
|
|
104
58
|
matterbridge.log.debug(`Triggered Shelly main updates`);
|
|
105
59
|
})
|
|
106
60
|
.catch((error) => {
|
|
@@ -114,20 +68,13 @@ export async function triggerShellyMainUpdate(matterbridge) {
|
|
|
114
68
|
verifyShellyUpdate(matterbridge, '/api/updates/main/status', 'Shelly software update');
|
|
115
69
|
});
|
|
116
70
|
}
|
|
117
|
-
/**
|
|
118
|
-
* Verifies Shelly update.
|
|
119
|
-
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
120
|
-
* @param {string} api - The api to call: /api/updates/sys/status or /api/updates/main/status
|
|
121
|
-
* @param {string} name - The name of the update.
|
|
122
|
-
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
123
|
-
*/
|
|
124
71
|
async function verifyShellyUpdate(matterbridge, api, name) {
|
|
125
72
|
return new Promise((resolve) => {
|
|
126
73
|
const timeout = setTimeout(() => {
|
|
127
74
|
matterbridge.log.warn(`${name} check timed out`);
|
|
128
75
|
clearInterval(interval);
|
|
129
76
|
resolve();
|
|
130
|
-
}, 600 * 1000);
|
|
77
|
+
}, 600 * 1000);
|
|
131
78
|
const interval = setInterval(() => {
|
|
132
79
|
getShelly(api, 10 * 1000)
|
|
133
80
|
.then(async (data) => {
|
|
@@ -149,15 +96,9 @@ async function verifyShellyUpdate(matterbridge, api, name) {
|
|
|
149
96
|
clearTimeout(timeout);
|
|
150
97
|
resolve();
|
|
151
98
|
});
|
|
152
|
-
}, 15 * 1000);
|
|
99
|
+
}, 15 * 1000);
|
|
153
100
|
});
|
|
154
101
|
}
|
|
155
|
-
/**
|
|
156
|
-
* Triggers Shelly change network configuration.
|
|
157
|
-
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
158
|
-
* @param {object} config - The network configuration.
|
|
159
|
-
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
160
|
-
*/
|
|
161
102
|
export async function triggerShellyChangeIp(matterbridge, config) {
|
|
162
103
|
const api = config.type === 'static' ? '/api/network/connection/static' : '/api/network/connection/dynamic';
|
|
163
104
|
const data = { interface: 'end0' };
|
|
@@ -180,15 +121,8 @@ export async function triggerShellyChangeIp(matterbridge, config) {
|
|
|
180
121
|
matterbridge.frontend.wssSendSnackbarMessage('Error changing Shelly network configuration', 10, 'error');
|
|
181
122
|
})
|
|
182
123
|
.finally(() => {
|
|
183
|
-
// matterbridge.log.notice(`Changed Shelly network configuration`);
|
|
184
|
-
// matterbridge.frontend.wssSendSnackbarMessage('Changed Shelly network configuration');
|
|
185
124
|
});
|
|
186
125
|
}
|
|
187
|
-
/**
|
|
188
|
-
* Triggers Shelly system reboot.
|
|
189
|
-
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
190
|
-
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
191
|
-
*/
|
|
192
126
|
export async function triggerShellyReboot(matterbridge) {
|
|
193
127
|
matterbridge.log.debug(`Triggering Shelly system reboot`);
|
|
194
128
|
postShelly('/api/system/reboot', {}, 60 * 1000)
|
|
@@ -203,17 +137,8 @@ export async function triggerShellyReboot(matterbridge) {
|
|
|
203
137
|
matterbridge.frontend.wssSendSnackbarMessage('Error rebooting Shelly board', 10, 'error');
|
|
204
138
|
})
|
|
205
139
|
.finally(() => {
|
|
206
|
-
// matterbridge.log.notice(`Rebooting Shelly board...`);
|
|
207
|
-
// matterbridge.frontend.wssSendSnackbarMessage('Rebooting Shelly board...');
|
|
208
140
|
});
|
|
209
141
|
}
|
|
210
|
-
/**
|
|
211
|
-
* Triggers Shelly soft reset.
|
|
212
|
-
* It will replaces network config with default one (edn0 on dhcp).
|
|
213
|
-
*
|
|
214
|
-
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
215
|
-
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
216
|
-
*/
|
|
217
142
|
export async function triggerShellySoftReset(matterbridge) {
|
|
218
143
|
matterbridge.log.debug(`Triggering Shelly soft reset`);
|
|
219
144
|
getShelly('/api/reset/soft', 60 * 1000)
|
|
@@ -228,16 +153,8 @@ export async function triggerShellySoftReset(matterbridge) {
|
|
|
228
153
|
matterbridge.frontend.wssSendSnackbarMessage('Error resetting the network parameters on Shelly board', 10, 'error');
|
|
229
154
|
})
|
|
230
155
|
.finally(() => {
|
|
231
|
-
// matterbridge.log.notice(`Resetting the network parameters on Shelly board...`);
|
|
232
|
-
// matterbridge.frontend.wssSendSnackbarMessage('Resetting the network parameters on Shelly board...');
|
|
233
156
|
});
|
|
234
157
|
}
|
|
235
|
-
/**
|
|
236
|
-
* Triggers Shelly hard reset.
|
|
237
|
-
* It will do a hard reset and will remove both directories .matterbridge Matterbridge.
|
|
238
|
-
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
239
|
-
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
240
|
-
*/
|
|
241
158
|
export async function triggerShellyHardReset(matterbridge) {
|
|
242
159
|
matterbridge.log.debug(`Triggering Shelly hard reset`);
|
|
243
160
|
getShelly('/api/reset/hard', 60 * 1000)
|
|
@@ -252,16 +169,8 @@ export async function triggerShellyHardReset(matterbridge) {
|
|
|
252
169
|
matterbridge.frontend.wssSendSnackbarMessage('Error while factory resetting the Shelly board', 10, 'error');
|
|
253
170
|
})
|
|
254
171
|
.finally(() => {
|
|
255
|
-
// matterbridge.log.notice(`Factory resetting Shelly board...`);
|
|
256
|
-
// matterbridge.frontend.wssSendSnackbarMessage('Factory resetting Shelly board...');
|
|
257
172
|
});
|
|
258
173
|
}
|
|
259
|
-
/**
|
|
260
|
-
* Fetches Shelly system log and write it to shelly.log.
|
|
261
|
-
*
|
|
262
|
-
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
263
|
-
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
264
|
-
*/
|
|
265
174
|
export async function createShellySystemLog(matterbridge) {
|
|
266
175
|
const { promises: fs } = await import('node:fs');
|
|
267
176
|
const path = await import('node:path');
|
|
@@ -281,27 +190,6 @@ export async function createShellySystemLog(matterbridge) {
|
|
|
281
190
|
matterbridge.log.warn(`Error getting Shelly system log: ${error instanceof Error ? error.message : error}`);
|
|
282
191
|
});
|
|
283
192
|
}
|
|
284
|
-
/**
|
|
285
|
-
* Perform a GET to Shelly board apis.
|
|
286
|
-
* @param {string} api - The api to call:
|
|
287
|
-
*
|
|
288
|
-
* /api/updates/sys/check => [{name:string; ...}]
|
|
289
|
-
* /api/updates/sys/perform => {"updatingInProgress":true} or {"updatingInProgress":false}
|
|
290
|
-
* /api/updates/sys/status => {"updatingInProgress":true} or {"updatingInProgress":false}
|
|
291
|
-
* /api/updates/main/check => [{name:string; ...}]
|
|
292
|
-
* /api/updates/main/perform => {"updatingInProgress":true} or {"updatingInProgress":false}
|
|
293
|
-
* /api/updates/main/status => {"updatingInProgress":true} or {"updatingInProgress":false}
|
|
294
|
-
*
|
|
295
|
-
* /api/logs/system => text
|
|
296
|
-
*
|
|
297
|
-
* /api/reset/soft => "ok" Replaces network config with default one (edn0 on dhcp)
|
|
298
|
-
* /api/reset/hard => reboot on success Hard reset makes soft reset + removing both directories .matterbridge Matterbridge + reboot
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
* @param {number} [timeout=5000] - The timeout duration in milliseconds (default is 60000ms).
|
|
302
|
-
* @returns {Promise<any>} A promise that resolves to the response.
|
|
303
|
-
* @throws {Error} If the request fails.
|
|
304
|
-
*/
|
|
305
193
|
async function getShelly(api, timeout = 60000) {
|
|
306
194
|
const http = await import('node:http');
|
|
307
195
|
return new Promise((resolve, reject) => {
|
|
@@ -315,13 +203,12 @@ async function getShelly(api, timeout = 60000) {
|
|
|
315
203
|
let data = '';
|
|
316
204
|
if (res.statusCode !== 200) {
|
|
317
205
|
clearTimeout(timeoutId);
|
|
318
|
-
res.resume();
|
|
319
|
-
req.destroy();
|
|
206
|
+
res.resume();
|
|
207
|
+
req.destroy();
|
|
320
208
|
reject(new Error(`Failed to fetch data. Status code: ${res.statusCode}`));
|
|
321
209
|
return;
|
|
322
210
|
}
|
|
323
211
|
res.on('data', (chunk) => {
|
|
324
|
-
// console.log(chunk);
|
|
325
212
|
data += chunk;
|
|
326
213
|
});
|
|
327
214
|
res.on('end', () => {
|
|
@@ -336,7 +223,6 @@ async function getShelly(api, timeout = 60000) {
|
|
|
336
223
|
}
|
|
337
224
|
}
|
|
338
225
|
else {
|
|
339
|
-
// console.log(data);
|
|
340
226
|
resolve(data);
|
|
341
227
|
}
|
|
342
228
|
});
|
|
@@ -347,29 +233,6 @@ async function getShelly(api, timeout = 60000) {
|
|
|
347
233
|
});
|
|
348
234
|
});
|
|
349
235
|
}
|
|
350
|
-
/**
|
|
351
|
-
* Perform a POST request to Shelly board apis.
|
|
352
|
-
* @param {string} api - The api to call:
|
|
353
|
-
*
|
|
354
|
-
* Set static ip
|
|
355
|
-
* /api/network/connection/static -d '{"interface": "end0", "addr": "10.11.12.101", "mask": "255.255.255.0", "gw": "10.11.12.1", "dns": "1.1.1.1"}' => {}
|
|
356
|
-
*
|
|
357
|
-
* Set dhcp
|
|
358
|
-
* /api/network/connection/dynamic -d '{"interface": "end0"}' => {}
|
|
359
|
-
*
|
|
360
|
-
* Reboot
|
|
361
|
-
* /api/system/reboot => {"success":true}
|
|
362
|
-
*
|
|
363
|
-
* curl -H "Content-Type: application/json" -X POST http://127.0.0.1:8101/api/network/connection/dynamic
|
|
364
|
-
* -d '{"interface": "end0"}'
|
|
365
|
-
*
|
|
366
|
-
* curl -H "Content-Type: application/json" -X POST http://127.0.0.1:8101/api/network/connection/static
|
|
367
|
-
* -d '{"interface": "end0", "addr": "192.168.1.64", "mask": "255.255.255.0", "gw": "192.168.1.1", "dns": "192.168.1.1"}'
|
|
368
|
-
*
|
|
369
|
-
* @param {number} [timeout=5000] - The timeout duration in milliseconds (default is 60000ms).
|
|
370
|
-
* @returns {Promise<any>} A promise that resolves to the response.
|
|
371
|
-
* @throws {Error} If the request fails.
|
|
372
|
-
*/
|
|
373
236
|
async function postShelly(api, data, timeout = 60000) {
|
|
374
237
|
const http = await import('node:http');
|
|
375
238
|
return new Promise((resolve, reject) => {
|
|
@@ -390,11 +253,10 @@ async function postShelly(api, data, timeout = 60000) {
|
|
|
390
253
|
};
|
|
391
254
|
const req = http.request(url, options, (res) => {
|
|
392
255
|
let responseData = '';
|
|
393
|
-
// Check for non-success status codes (e.g., 300+)
|
|
394
256
|
if (res.statusCode && res.statusCode >= 300) {
|
|
395
257
|
clearTimeout(timeoutId);
|
|
396
|
-
res.resume();
|
|
397
|
-
req.destroy();
|
|
258
|
+
res.resume();
|
|
259
|
+
req.destroy();
|
|
398
260
|
return reject(new Error(`Failed to post data. Status code: ${res.statusCode}`));
|
|
399
261
|
}
|
|
400
262
|
res.on('data', (chunk) => {
|
|
@@ -415,9 +277,7 @@ async function postShelly(api, data, timeout = 60000) {
|
|
|
415
277
|
clearTimeout(timeoutId);
|
|
416
278
|
reject(new Error(`Request failed: ${error instanceof Error ? error.message : error}`));
|
|
417
279
|
});
|
|
418
|
-
// Send the JSON data
|
|
419
280
|
req.write(jsonData);
|
|
420
281
|
req.end();
|
|
421
282
|
});
|
|
422
283
|
}
|
|
423
|
-
//# sourceMappingURL=shelly.js.map
|
package/dist/storage/export.js
CHANGED
package/dist/update.js
CHANGED
|
@@ -1,36 +1,7 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file contains the check updates functions.
|
|
3
|
-
*
|
|
4
|
-
* @file update.ts
|
|
5
|
-
* @author Luca Liguori
|
|
6
|
-
* @date 2025-02-24
|
|
7
|
-
* @version 1.0.0
|
|
8
|
-
*
|
|
9
|
-
* Copyright 2025, 2026, 2027 Luca Liguori.
|
|
10
|
-
*
|
|
11
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
12
|
-
* you may not use this file except in compliance with the License.
|
|
13
|
-
* You may obtain a copy of the License at
|
|
14
|
-
*
|
|
15
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
-
*
|
|
17
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
18
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
19
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
-
* See the License for the specific language governing permissions and
|
|
21
|
-
* limitations under the License. *
|
|
22
|
-
*/
|
|
23
1
|
import { plg } from './matterbridgeTypes.js';
|
|
24
2
|
import { db, nt, wr } from './logger/export.js';
|
|
25
|
-
/**
|
|
26
|
-
* Checks for updates for Matterbridge and its plugins.
|
|
27
|
-
* If the 'shelly' parameter is present, also checks for Shelly updates.
|
|
28
|
-
*
|
|
29
|
-
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
30
|
-
* @returns {Promise<void>} A promise that resolves when the update checks are complete.
|
|
31
|
-
*/
|
|
32
3
|
export async function checkUpdates(matterbridge) {
|
|
33
|
-
const { hasParameter } = await import('./utils/
|
|
4
|
+
const { hasParameter } = await import('./utils/commandLine.js');
|
|
34
5
|
getMatterbridgeLatestVersion(matterbridge);
|
|
35
6
|
getMatterbridgeDevVersion(matterbridge);
|
|
36
7
|
for (const plugin of matterbridge.plugins) {
|
|
@@ -42,13 +13,6 @@ export async function checkUpdates(matterbridge) {
|
|
|
42
13
|
getShellyMainUpdate(matterbridge);
|
|
43
14
|
}
|
|
44
15
|
}
|
|
45
|
-
/**
|
|
46
|
-
* Retrieves the latest version of Matterbridge and updates the matterbridgeLatestVersion property.
|
|
47
|
-
* If there is an error retrieving the latest version, logs an error message.
|
|
48
|
-
*
|
|
49
|
-
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
50
|
-
* @returns {Promise<void>} A promise that resolves when the latest version is retrieved.
|
|
51
|
-
*/
|
|
52
16
|
async function getMatterbridgeLatestVersion(matterbridge) {
|
|
53
17
|
const { getNpmPackageVersion } = await import('./utils/network.js');
|
|
54
18
|
getNpmPackageVersion('matterbridge')
|
|
@@ -69,13 +33,6 @@ async function getMatterbridgeLatestVersion(matterbridge) {
|
|
|
69
33
|
matterbridge.log.warn(`Error getting Matterbridge latest version: ${error.message}`);
|
|
70
34
|
});
|
|
71
35
|
}
|
|
72
|
-
/**
|
|
73
|
-
* Retrieves the latest dev version of Matterbridge and updates the matterbridgeDevVersion property.
|
|
74
|
-
* If there is an error retrieving the latest version, logs an error message.
|
|
75
|
-
*
|
|
76
|
-
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
77
|
-
* @returns {Promise<void>} A promise that resolves when the latest dev version is retrieved.
|
|
78
|
-
*/
|
|
79
36
|
async function getMatterbridgeDevVersion(matterbridge) {
|
|
80
37
|
const { getNpmPackageVersion } = await import('./utils/network.js');
|
|
81
38
|
getNpmPackageVersion('matterbridge', 'dev')
|
|
@@ -93,14 +50,6 @@ async function getMatterbridgeDevVersion(matterbridge) {
|
|
|
93
50
|
matterbridge.log.warn(`Error getting Matterbridge latest dev version: ${error.message}`);
|
|
94
51
|
});
|
|
95
52
|
}
|
|
96
|
-
/**
|
|
97
|
-
* Retrieves the latest version of a plugin and updates the plugin's latestVersion property.
|
|
98
|
-
* If there is an error retrieving the latest version, logs an error message.
|
|
99
|
-
*
|
|
100
|
-
* @param {Matterbridge} matterbridge - The Matterbridge instance.
|
|
101
|
-
* @param {RegisteredPlugin} plugin - The plugin for which to retrieve the latest version.
|
|
102
|
-
* @returns {Promise<void>} A promise that resolves when the latest version is retrieved.
|
|
103
|
-
*/
|
|
104
53
|
async function getPluginLatestVersion(matterbridge, plugin) {
|
|
105
54
|
const { getNpmPackageVersion } = await import('./utils/network.js');
|
|
106
55
|
getNpmPackageVersion(plugin.name)
|
|
@@ -118,4 +67,3 @@ async function getPluginLatestVersion(matterbridge, plugin) {
|
|
|
118
67
|
matterbridge.log.warn(`Error getting ${plg}${plugin.name}${wr} latest version: ${error.message}`);
|
|
119
68
|
});
|
|
120
69
|
}
|
|
121
|
-
//# sourceMappingURL=update.js.map
|
package/dist/utils/colorUtils.js
CHANGED
|
@@ -1,25 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file contains the color utilities.
|
|
3
|
-
*
|
|
4
|
-
* @file colorUtils.ts
|
|
5
|
-
* @author Luca Liguori
|
|
6
|
-
* @date 2023-10-05
|
|
7
|
-
* @version 1.3.0
|
|
8
|
-
*
|
|
9
|
-
* Copyright 2023, 2024, 2025 Luca Liguori.
|
|
10
|
-
*
|
|
11
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
12
|
-
* you may not use this file except in compliance with the License.
|
|
13
|
-
* You may obtain a copy of the License at
|
|
14
|
-
*
|
|
15
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
-
*
|
|
17
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
18
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
19
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
-
* See the License for the specific language governing permissions and
|
|
21
|
-
* limitations under the License. *
|
|
22
|
-
*/
|
|
23
1
|
import { assert } from 'node:console';
|
|
24
2
|
export function hslColorToRgbColor(hue, saturation, luminance) {
|
|
25
3
|
if (hue === 360) {
|
|
@@ -32,7 +10,7 @@ export function hslColorToRgbColor(hue, saturation, luminance) {
|
|
|
32
10
|
luminance /= 100;
|
|
33
11
|
let r, g, b;
|
|
34
12
|
if (saturation === 0) {
|
|
35
|
-
r = g = b = luminance;
|
|
13
|
+
r = g = b = luminance;
|
|
36
14
|
}
|
|
37
15
|
else {
|
|
38
16
|
const hue2rgb = (p, q, t) => {
|
|
@@ -65,27 +43,21 @@ export function hslColorToRgbColor(hue, saturation, luminance) {
|
|
|
65
43
|
b: Math.ceil(b * 255),
|
|
66
44
|
};
|
|
67
45
|
}
|
|
68
|
-
// Converts from RGB to the XY color (CIE 1931 color space)
|
|
69
46
|
export function rgbColorToXYColor(rgb) {
|
|
70
47
|
let r = rgb.r / 255;
|
|
71
48
|
let g = rgb.g / 255;
|
|
72
49
|
let b = rgb.b / 255;
|
|
73
|
-
// Apply gamma correction
|
|
74
50
|
r = r > 0.04045 ? Math.pow((r + 0.055) / 1.055, 2.4) : r / 12.92;
|
|
75
51
|
g = g > 0.04045 ? Math.pow((g + 0.055) / 1.055, 2.4) : g / 12.92;
|
|
76
52
|
b = b > 0.04045 ? Math.pow((b + 0.055) / 1.055, 2.4) : b / 12.92;
|
|
77
|
-
// Scale the values to the D65 illuminant
|
|
78
53
|
r = r * 100;
|
|
79
54
|
g = g * 100;
|
|
80
55
|
b = b * 100;
|
|
81
|
-
// Convert RGB to XYZ
|
|
82
56
|
const X = r * 0.664511 + g * 0.154324 + b * 0.162028;
|
|
83
57
|
const Y = r * 0.283881 + g * 0.668433 + b * 0.047685;
|
|
84
58
|
const Z = r * 0.000088 + g * 0.07231 + b * 0.986039;
|
|
85
|
-
// Normalization
|
|
86
59
|
let x = X / (X + Y + Z);
|
|
87
60
|
let y = Y / (X + Y + Z);
|
|
88
|
-
// Round to 4 digits
|
|
89
61
|
x = Math.round(x * 10000) / 10000;
|
|
90
62
|
y = Math.round(y * 10000) / 10000;
|
|
91
63
|
return { x, y };
|
|
@@ -95,11 +67,9 @@ export function xyColorToRgbColor(x, y, brightness = 254) {
|
|
|
95
67
|
const Y = (brightness / 254).toFixed(2);
|
|
96
68
|
const X = (Number(Y) / y) * x;
|
|
97
69
|
const Z = (Number(Y) / y) * z;
|
|
98
|
-
// Convert to RGB using Wide RGB D65 conversion
|
|
99
70
|
let red = X * 1.656492 - Number(Y) * 0.354851 - Z * 0.255038;
|
|
100
71
|
let green = -X * 0.707196 + Number(Y) * 1.655397 + Z * 0.036152;
|
|
101
72
|
let blue = X * 0.051713 - Number(Y) * 0.121364 + Z * 1.01153;
|
|
102
|
-
// If red, green or blue is larger than 1.0 set it back to the maximum of 1.0
|
|
103
73
|
if (red > blue && red > green && red > 1.0) {
|
|
104
74
|
green = green / red;
|
|
105
75
|
blue = blue / red;
|
|
@@ -115,15 +85,12 @@ export function xyColorToRgbColor(x, y, brightness = 254) {
|
|
|
115
85
|
green = green / blue;
|
|
116
86
|
blue = 1.0;
|
|
117
87
|
}
|
|
118
|
-
// Reverse gamma correction
|
|
119
88
|
red = red <= 0.0031308 ? 12.92 * red : (1.0 + 0.055) * Math.pow(red, 1.0 / 2.4) - 0.055;
|
|
120
89
|
green = green <= 0.0031308 ? 12.92 * green : (1.0 + 0.055) * Math.pow(green, 1.0 / 2.4) - 0.055;
|
|
121
90
|
blue = blue <= 0.0031308 ? 12.92 * blue : (1.0 + 0.055) * Math.pow(blue, 1.0 / 2.4) - 0.055;
|
|
122
|
-
// Convert normalized decimal to decimal
|
|
123
91
|
red = Math.round(red * 255);
|
|
124
92
|
green = Math.round(green * 255);
|
|
125
93
|
blue = Math.round(blue * 255);
|
|
126
|
-
// Normalize
|
|
127
94
|
if (isNaN(red) || red < 0) {
|
|
128
95
|
red = 0;
|
|
129
96
|
}
|
|
@@ -144,7 +111,7 @@ export function rgbColorToHslColor(rgb) {
|
|
|
144
111
|
let h = 0, s = 0;
|
|
145
112
|
const l = (max + min) / 2;
|
|
146
113
|
if (max === min) {
|
|
147
|
-
h = s = 0;
|
|
114
|
+
h = s = 0;
|
|
148
115
|
}
|
|
149
116
|
else {
|
|
150
117
|
const d = max - min;
|
|
@@ -179,10 +146,8 @@ export function kelvinToMired(kelvin) {
|
|
|
179
146
|
return Math.round(1000000 / kelvin);
|
|
180
147
|
}
|
|
181
148
|
export function kelvinToRGB(kelvin) {
|
|
182
|
-
// Clamp the temperature to the range 1000K to 40000K
|
|
183
149
|
kelvin = Math.max(1000, Math.min(40000, kelvin)) / 100;
|
|
184
150
|
let r, g, b;
|
|
185
|
-
// Calculate red
|
|
186
151
|
if (kelvin <= 66) {
|
|
187
152
|
r = 255;
|
|
188
153
|
}
|
|
@@ -191,7 +156,6 @@ export function kelvinToRGB(kelvin) {
|
|
|
191
156
|
r = 329.698727446 * Math.pow(r, -0.1332047592);
|
|
192
157
|
r = Math.max(0, Math.min(255, r));
|
|
193
158
|
}
|
|
194
|
-
// Calculate green
|
|
195
159
|
if (kelvin <= 66) {
|
|
196
160
|
g = kelvin;
|
|
197
161
|
g = 99.4708025861 * Math.log(g) - 161.1195681661;
|
|
@@ -202,7 +166,6 @@ export function kelvinToRGB(kelvin) {
|
|
|
202
166
|
g = 288.1221695283 * Math.pow(g, -0.0755148492);
|
|
203
167
|
g = Math.max(0, Math.min(255, g));
|
|
204
168
|
}
|
|
205
|
-
// Calculate blue
|
|
206
169
|
if (kelvin >= 66) {
|
|
207
170
|
b = 255;
|
|
208
171
|
}
|
|
@@ -216,169 +179,3 @@ export function kelvinToRGB(kelvin) {
|
|
|
216
179
|
}
|
|
217
180
|
return { r: Math.round(r), g: Math.round(g), b: Math.round(b) };
|
|
218
181
|
}
|
|
219
|
-
/*
|
|
220
|
-
export function testColors(): void {
|
|
221
|
-
// this table has been checked with different apps and sites and is correct 100%
|
|
222
|
-
const colors = [
|
|
223
|
-
{ name: 'Pure Red 0', hsl: { h: 0, s: 100, l: 50 }, rgb: { r: 255, g: 0, b: 0 }, xy: { x: 0.7006, y: 0.2993 } },
|
|
224
|
-
{ name: 'Bright Orange 30', hsl: { h: 30, s: 100, l: 50 }, rgb: { r: 255, g: 128, b: 0 }, xy: { x: 0.6112, y: 0.375 } },
|
|
225
|
-
{ name: 'Pure Yellow 60', hsl: { h: 60, s: 100, l: 50 }, rgb: { r: 255, g: 255, b: 0 }, xy: { x: 0.4442, y: 0.5166 } },
|
|
226
|
-
{ name: 'Lime Green 90', hsl: { h: 90, s: 100, l: 50 }, rgb: { r: 128, g: 255, b: 0 }, xy: { x: 0.2707, y: 0.6635 } },
|
|
227
|
-
{ name: 'Pure Green 120', hsl: { h: 120, s: 100, l: 50 }, rgb: { r: 0, g: 255, b: 0 }, xy: { x: 0.1724, y: 0.7468 } },
|
|
228
|
-
{ name: 'Light Sea Green 150', hsl: { h: 150, s: 100, l: 50 }, rgb: { r: 0, g: 255, b: 128 }, xy: { x: 0.1642, y: 0.5886 } },
|
|
229
|
-
{ name: 'Pure Cyan 180', hsl: { h: 180, s: 100, l: 50 }, rgb: { r: 0, g: 255, b: 255 }, xy: { x: 0.1513, y: 0.3425 } },
|
|
230
|
-
{ name: 'Deep Sky Blue 210', hsl: { h: 210, s: 100, l: 50 }, rgb: { r: 0, g: 128, b: 255 }, xy: { x: 0.1406, y: 0.1382 } },
|
|
231
|
-
{ name: 'Pure Blue 240', hsl: { h: 240, s: 100, l: 50 }, rgb: { r: 0, g: 0, b: 255 }, xy: { x: 0.1355, y: 0.0399 } },
|
|
232
|
-
{ name: 'Blue Violet 270', hsl: { h: 270, s: 100, l: 50 }, rgb: { r: 128, g: 0, b: 255 }, xy: { x: 0.2181, y: 0.0778 } },
|
|
233
|
-
{ name: 'Pure Magenta 300', hsl: { h: 300, s: 100, l: 50 }, rgb: { r: 255, g: 0, b: 255 }, xy: { x: 0.3855, y: 0.1546 } },
|
|
234
|
-
{ name: 'Deep Pink 330', hsl: { h: 330, s: 100, l: 50 }, rgb: { r: 255, g: 0, b: 128 }, xy: { x: 0.5797, y: 0.2438 } },
|
|
235
|
-
|
|
236
|
-
{ name: 'Pure Red 50% 0', hsl: { h: 0, s: 50, l: 50 }, rgb: { r: 192, g: 64, b: 64 }, xy: { x: 0.6036, y: 0.3069 } },
|
|
237
|
-
{ name: 'Bright Orange 50% 30', hsl: { h: 30, s: 50, l: 50 }, rgb: { r: 192, g: 128, b: 64 }, xy: { x: 0.5194, y: 0.3928 } },
|
|
238
|
-
{ name: 'Pure Yellow 50% 60', hsl: { h: 60, s: 50, l: 50 }, rgb: { r: 192, g: 192, b: 64 }, xy: { x: 0.4258, y: 0.4883 } },
|
|
239
|
-
{ name: 'Lime Green 50% 90', hsl: { h: 90, s: 50, l: 50 }, rgb: { r: 128, g: 192, b: 64 }, xy: { x: 0.3159, y: 0.5639 } },
|
|
240
|
-
{ name: 'Pure Green 50% 120', hsl: { h: 120, s: 50, l: 50 }, rgb: { r: 64, g: 192, b: 64 }, xy: { x: 0.2127, y: 0.6349 } },
|
|
241
|
-
{ name: 'Light Sea Green 50% 150', hsl: { h: 150, s: 50, l: 50 }, rgb: { r: 64, g: 192, b: 128 }, xy: { x: 0.1932, y: 0.4845 } },
|
|
242
|
-
{ name: 'Pure Cyan 50% 180', hsl: { h: 180, s: 50, l: 50 }, rgb: { r: 64, g: 192, b: 192 }, xy: { x: 0.1745, y: 0.3407 } },
|
|
243
|
-
{ name: 'Deep Sky Blue 50% 210', hsl: { h: 210, s: 50, l: 50 }, rgb: { r: 64, g: 128, b: 192 }, xy: { x: 0.1752, y: 0.211 } },
|
|
244
|
-
{ name: 'Pure Blue 50% 240', hsl: { h: 240, s: 50, l: 50 }, rgb: { r: 64, g: 64, b: 192 }, xy: { x: 0.1758, y: 0.102 } },
|
|
245
|
-
{ name: 'Blue Violet 50% 270', hsl: { h: 270, s: 50, l: 50 }, rgb: { r: 128, g: 64, b: 192 }, xy: { x: 0.2688, y: 0.137 } },
|
|
246
|
-
{ name: 'Pure Magenta 50% 300', hsl: { h: 300, s: 50, l: 50 }, rgb: { r: 192, g: 64, b: 192 }, xy: { x: 0.3772, y: 0.1777 } },
|
|
247
|
-
{ name: 'Deep Pink 50% 330', hsl: { h: 330, s: 50, l: 50 }, rgb: { r: 192, g: 64, b: 128 }, xy: { x: 0.489, y: 0.2416 } },
|
|
248
|
-
];
|
|
249
|
-
|
|
250
|
-
colors.forEach((color) => {
|
|
251
|
-
// eslint-disable-next-line no-console
|
|
252
|
-
console.log(
|
|
253
|
-
`\x1b[48;2;${color.rgb.r};${color.rgb.g};${color.rgb.b}mColor: ${color.name}\x1b[0m, hsl: { h: ${color.hsl.h}, s: ${color.hsl.s}, l: ${color.hsl.l} }, rgb: { r: ${color.rgb.r}, g: ${color.rgb.g}, b: ${color.rgb.b} }, xy: { x: ${color.xy.x}, y: ${color.xy.y} }`,
|
|
254
|
-
);
|
|
255
|
-
|
|
256
|
-
const rgb = hslColorToRgbColor(color.hsl.h, color.hsl.s, color.hsl.l);
|
|
257
|
-
assert(rgb.r === color.rgb.r && rgb.g === color.rgb.g && rgb.b === color.rgb.b, `\x1b[48;2;${rgb.r};${rgb.g};${rgb.b}mColor: ${color.name}\x1b[0m hslColorToRgbColor { r: ${rgb.r}, g: ${rgb.g}, b: ${rgb.b} } conversion error`);
|
|
258
|
-
|
|
259
|
-
const hsl = rgbColorToHslColor({ r: color.rgb.r, g: color.rgb.g, b: color.rgb.b });
|
|
260
|
-
assert(hsl.h === color.hsl.h && hsl.s === color.hsl.s && hsl.l === color.hsl.l, `Color: ${color.name} rgbColorToHslColor conversion error`);
|
|
261
|
-
|
|
262
|
-
const xy = rgbColorToXYColor({ r: color.rgb.r, g: color.rgb.g, b: color.rgb.b });
|
|
263
|
-
assert(xy.x === color.xy.x && xy.y === color.xy.y, `Color: ${color.name} rgbColorToXYColor conversion error got x ${xy.x} y ${xy.y}`);
|
|
264
|
-
|
|
265
|
-
const rgb2 = xyColorToRgbColor(color.xy.x, color.xy.y);
|
|
266
|
-
assert(
|
|
267
|
-
rgb2.r === color.rgb.r && rgb2.g === color.rgb.g && rgb2.b === color.rgb.b,
|
|
268
|
-
`\x1b[48;2;${rgb2.r};${rgb2.g};${rgb2.b}mColor: ${color.name}\x1b[0m xyColorToRgbColor(${color.xy.x}, ${color.xy.y}) conversion error -> r: ${rgb2.r} g: ${rgb2.g} b: ${rgb2.g}`,
|
|
269
|
-
);
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
*/
|
|
273
|
-
/**
|
|
274
|
-
* Converts CIE color space to RGB color space
|
|
275
|
-
* @param {Number} x
|
|
276
|
-
* @param {Number} y
|
|
277
|
-
* @param {Number} brightness - Ranges from 1 to 254
|
|
278
|
-
* @return {Array} Array that contains the color values for red, green and blue
|
|
279
|
-
* From: https://github.com/usolved/cie-rgb-converter/blob/master/cie_rgb_converter.js
|
|
280
|
-
*/
|
|
281
|
-
/*
|
|
282
|
-
export function cie_to_rgb(x: number, y: number, brightness = 254): RGB {
|
|
283
|
-
// Set to maximum brightness if no custom value was given (Not the slick ECMAScript 6 way for compatibility reasons)
|
|
284
|
-
|
|
285
|
-
const z = 1.0 - x - y;
|
|
286
|
-
const Y = (brightness / 254).toFixed(2);
|
|
287
|
-
const X = (Number(Y) / y) * x;
|
|
288
|
-
const Z = (Number(Y) / y) * z;
|
|
289
|
-
|
|
290
|
-
// Convert to RGB using Wide RGB D65 conversion
|
|
291
|
-
let red = X * 1.656492 - Number(Y) * 0.354851 - Z * 0.255038;
|
|
292
|
-
let green = -X * 0.707196 + Number(Y) * 1.655397 + Z * 0.036152;
|
|
293
|
-
let blue = X * 0.051713 - Number(Y) * 0.121364 + Z * 1.01153;
|
|
294
|
-
|
|
295
|
-
// If red, green or blue is larger than 1.0 set it back to the maximum of 1.0
|
|
296
|
-
if (red > blue && red > green && red > 1.0) {
|
|
297
|
-
green = green / red;
|
|
298
|
-
blue = blue / red;
|
|
299
|
-
red = 1.0;
|
|
300
|
-
} else if (green > blue && green > red && green > 1.0) {
|
|
301
|
-
red = red / green;
|
|
302
|
-
blue = blue / green;
|
|
303
|
-
green = 1.0;
|
|
304
|
-
} else if (blue > red && blue > green && blue > 1.0) {
|
|
305
|
-
red = red / blue;
|
|
306
|
-
green = green / blue;
|
|
307
|
-
blue = 1.0;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
// Reverse gamma correction
|
|
311
|
-
red = red <= 0.0031308 ? 12.92 * red : (1.0 + 0.055) * Math.pow(red, 1.0 / 2.4) - 0.055;
|
|
312
|
-
green = green <= 0.0031308 ? 12.92 * green : (1.0 + 0.055) * Math.pow(green, 1.0 / 2.4) - 0.055;
|
|
313
|
-
blue = blue <= 0.0031308 ? 12.92 * blue : (1.0 + 0.055) * Math.pow(blue, 1.0 / 2.4) - 0.055;
|
|
314
|
-
|
|
315
|
-
// Convert normalized decimal to decimal
|
|
316
|
-
red = Math.round(red * 255);
|
|
317
|
-
green = Math.round(green * 255);
|
|
318
|
-
blue = Math.round(blue * 255);
|
|
319
|
-
|
|
320
|
-
// Normalize
|
|
321
|
-
if (isNaN(red) || red < 0) {
|
|
322
|
-
red = 0;
|
|
323
|
-
}
|
|
324
|
-
if (isNaN(green) || green < 0) {
|
|
325
|
-
green = 0;
|
|
326
|
-
}
|
|
327
|
-
if (isNaN(blue) || blue < 0) {
|
|
328
|
-
blue = 0;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
return { r: red, g: green, b: blue };
|
|
332
|
-
}
|
|
333
|
-
*/
|
|
334
|
-
/**
|
|
335
|
-
* Converts RGB color space to CIE color space
|
|
336
|
-
* @param {Number} red
|
|
337
|
-
* @param {Number} green
|
|
338
|
-
* @param {Number} blue
|
|
339
|
-
* @return {Array} Array that contains the CIE color values for x and y
|
|
340
|
-
* From: https://github.com/usolved/cie-rgb-converter/blob/master/cie_rgb_converter.js
|
|
341
|
-
*/
|
|
342
|
-
/*
|
|
343
|
-
export function rgb_to_cie(red: number, green: number, blue: number): XY {
|
|
344
|
-
// Apply a gamma correction to the RGB values, which makes the color more vivid and more the like the color displayed on the screen of your device
|
|
345
|
-
red = red > 0.04045 ? Math.pow((red + 0.055) / (1.0 + 0.055), 2.4) : red / 12.92;
|
|
346
|
-
green = green > 0.04045 ? Math.pow((green + 0.055) / (1.0 + 0.055), 2.4) : green / 12.92;
|
|
347
|
-
blue = blue > 0.04045 ? Math.pow((blue + 0.055) / (1.0 + 0.055), 2.4) : blue / 12.92;
|
|
348
|
-
|
|
349
|
-
// RGB values to XYZ using the Wide RGB D65 conversion formula
|
|
350
|
-
const X = red * 0.664511 + green * 0.154324 + blue * 0.162028;
|
|
351
|
-
const Y = red * 0.283881 + green * 0.668433 + blue * 0.047685;
|
|
352
|
-
const Z = red * 0.000088 + green * 0.07231 + blue * 0.986039;
|
|
353
|
-
|
|
354
|
-
// Calculate the xy values from the XYZ values
|
|
355
|
-
let x = (X / (X + Y + Z)).toFixed(4);
|
|
356
|
-
let y = (Y / (X + Y + Z)).toFixed(4);
|
|
357
|
-
|
|
358
|
-
if (isNaN(Number(x))) {
|
|
359
|
-
x = '0';
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
if (isNaN(Number(y))) {
|
|
363
|
-
y = '0';
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
return { x: Number(x), y: Number(y) };
|
|
367
|
-
}
|
|
368
|
-
*/
|
|
369
|
-
/*
|
|
370
|
-
testColors();
|
|
371
|
-
console.log('rgb_to_cie(0, 128, 255)', rgb_to_cie(0, 128, 255));
|
|
372
|
-
console.log('cie_to_rgb(0.1401, 0.1284, 254)', cie_to_rgb(0.1401, 0.1284, 254));
|
|
373
|
-
console.log('cie_to_rgb(0.1406, 0.1382, 254)', cie_to_rgb(0.1406, 0.1382, 254));
|
|
374
|
-
for (let h = 0; h < 360; h++) {
|
|
375
|
-
const rgb = hslColorToRgbColor(h, 100, 50);
|
|
376
|
-
const xy = rgbColorToXYColor({ r: rgb.r, g: rgb.g, b: rgb.b });
|
|
377
|
-
const rgb2 = cie_to_rgb(xy.x, xy.y, 254);
|
|
378
|
-
const hsl = rgbColorToHslColor({ r: rgb2.r, g: rgb2.g, b: rgb2.b });
|
|
379
|
-
assert(rgb.r === rgb2.r && rgb.g === rgb2.g && rgb.b === rgb2.b, 'Color rgb conversion error');
|
|
380
|
-
assert(h === hsl.h, 'Color hsl conversion error');
|
|
381
|
-
console.log(`\x1b[48;2;${rgb.r};${rgb.g};${rgb.b}mColor: r:${rgb.r} g:${rgb.g} b:${rgb.b}\x1b[0m => x:${xy.x} y:${xy.y} => \x1b[48;2;${rgb2.r};${rgb2.g};${rgb2.b}mColor: r:${rgb2.r} g:${rgb2.g} b:${rgb2.b} h:${hsl.h} s:${hsl.s}\x1b[0m\x1b[K`);
|
|
382
|
-
}
|
|
383
|
-
*/
|
|
384
|
-
//# sourceMappingURL=colorUtils.js.map
|