matterbridge 1.2.21 → 1.3.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.
Files changed (101) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.md +48 -9
  3. package/dist/cluster/AirQualityCluster.d.ts.map +1 -0
  4. package/dist/cluster/AirQualityCluster.js.map +1 -0
  5. package/dist/cluster/BooleanStateConfigurationCluster.d.ts +2200 -0
  6. package/dist/cluster/BooleanStateConfigurationCluster.d.ts.map +1 -0
  7. package/dist/cluster/BooleanStateConfigurationCluster.js +388 -0
  8. package/dist/cluster/BooleanStateConfigurationCluster.js.map +1 -0
  9. package/dist/cluster/BridgedDeviceBasicInformationCluster.d.ts.map +1 -0
  10. package/dist/cluster/BridgedDeviceBasicInformationCluster.js.map +1 -0
  11. package/dist/cluster/CarbonMonoxideConcentrationMeasurementCluster.d.ts +396 -0
  12. package/dist/cluster/CarbonMonoxideConcentrationMeasurementCluster.d.ts.map +1 -0
  13. package/dist/cluster/CarbonMonoxideConcentrationMeasurementCluster.js +30 -0
  14. package/dist/cluster/CarbonMonoxideConcentrationMeasurementCluster.js.map +1 -0
  15. package/dist/cluster/ConcentrationMeasurementCluster.d.ts +524 -0
  16. package/dist/cluster/ConcentrationMeasurementCluster.d.ts.map +1 -0
  17. package/dist/cluster/ConcentrationMeasurementCluster.js +282 -0
  18. package/dist/cluster/ConcentrationMeasurementCluster.js.map +1 -0
  19. package/dist/cluster/DeviceEnergyManagementCluster.d.ts +7851 -0
  20. package/dist/cluster/DeviceEnergyManagementCluster.d.ts.map +1 -0
  21. package/dist/cluster/DeviceEnergyManagementCluster.js +1634 -0
  22. package/dist/cluster/DeviceEnergyManagementCluster.js.map +1 -0
  23. package/dist/cluster/DeviceEnergyManagementModeCluster.d.ts +68 -0
  24. package/dist/cluster/DeviceEnergyManagementModeCluster.d.ts.map +1 -0
  25. package/dist/cluster/DeviceEnergyManagementModeCluster.js +49 -0
  26. package/dist/cluster/DeviceEnergyManagementModeCluster.js.map +1 -0
  27. package/dist/cluster/ElectricalEnergyMeasurementCluster.d.ts +4978 -0
  28. package/dist/cluster/ElectricalEnergyMeasurementCluster.d.ts.map +1 -0
  29. package/dist/cluster/ElectricalEnergyMeasurementCluster.js +510 -0
  30. package/dist/cluster/ElectricalEnergyMeasurementCluster.js.map +1 -0
  31. package/dist/cluster/ElectricalPowerMeasurementCluster.d.ts +3250 -0
  32. package/dist/cluster/ElectricalPowerMeasurementCluster.d.ts.map +1 -0
  33. package/dist/cluster/ElectricalPowerMeasurementCluster.js +675 -0
  34. package/dist/cluster/ElectricalPowerMeasurementCluster.js.map +1 -0
  35. package/dist/cluster/FanControlCluster.d.ts +1583 -0
  36. package/dist/cluster/FanControlCluster.d.ts.map +1 -0
  37. package/dist/cluster/FanControlCluster.js +492 -0
  38. package/dist/cluster/FanControlCluster.js.map +1 -0
  39. package/dist/cluster/MeasurementAccuracy.d.ts +63 -0
  40. package/dist/cluster/MeasurementAccuracy.d.ts.map +1 -0
  41. package/dist/cluster/MeasurementAccuracy.js +47 -0
  42. package/dist/cluster/MeasurementAccuracy.js.map +1 -0
  43. package/dist/cluster/MeasurementAccuracyRange.d.ts +134 -0
  44. package/dist/cluster/MeasurementAccuracyRange.d.ts.map +1 -0
  45. package/dist/cluster/MeasurementAccuracyRange.js +103 -0
  46. package/dist/cluster/MeasurementAccuracyRange.js.map +1 -0
  47. package/dist/cluster/MeasurementType.d.ts +68 -0
  48. package/dist/cluster/MeasurementType.d.ts.map +1 -0
  49. package/dist/cluster/MeasurementType.js +69 -0
  50. package/dist/cluster/MeasurementType.js.map +1 -0
  51. package/dist/cluster/PowerTopologyCluster.d.ts +355 -0
  52. package/dist/cluster/PowerTopologyCluster.d.ts.map +1 -0
  53. package/dist/cluster/PowerTopologyCluster.js +138 -0
  54. package/dist/cluster/PowerTopologyCluster.js.map +1 -0
  55. package/dist/cluster/SmokeCoAlarmCluster.d.ts +1599 -0
  56. package/dist/cluster/SmokeCoAlarmCluster.d.ts.map +1 -0
  57. package/dist/cluster/SmokeCoAlarmCluster.js +603 -0
  58. package/dist/cluster/SmokeCoAlarmCluster.js.map +1 -0
  59. package/dist/cluster/TvocCluster.d.ts.map +1 -0
  60. package/dist/cluster/TvocCluster.js.map +1 -0
  61. package/dist/defaultConfigSchema.d.ts.map +1 -1
  62. package/dist/defaultConfigSchema.js +26 -12
  63. package/dist/defaultConfigSchema.js.map +1 -1
  64. package/dist/index.d.ts +7 -2
  65. package/dist/index.d.ts.map +1 -1
  66. package/dist/index.js +8 -2
  67. package/dist/index.js.map +1 -1
  68. package/dist/matterbridge.d.ts +63 -1
  69. package/dist/matterbridge.d.ts.map +1 -1
  70. package/dist/matterbridge.js +72 -26
  71. package/dist/matterbridge.js.map +1 -1
  72. package/dist/matterbridgeDevice.d.ts +692 -30
  73. package/dist/matterbridgeDevice.d.ts.map +1 -1
  74. package/dist/matterbridgeDevice.js +429 -73
  75. package/dist/matterbridgeDevice.js.map +1 -1
  76. package/dist/matterbridgeDeviceV8.d.ts +3 -5
  77. package/dist/matterbridgeDeviceV8.d.ts.map +1 -1
  78. package/dist/matterbridgeDeviceV8.js +62 -14
  79. package/dist/matterbridgeDeviceV8.js.map +1 -1
  80. package/dist/matterbridgeV8.d.ts +69 -5
  81. package/dist/matterbridgeV8.d.ts.map +1 -1
  82. package/dist/matterbridgeV8.js +413 -99
  83. package/dist/matterbridgeV8.js.map +1 -1
  84. package/frontend/build/asset-manifest.json +3 -3
  85. package/frontend/build/index.html +1 -1
  86. package/frontend/build/static/js/{main.23829a0f.js → main.cbfc6c9b.js} +3 -3
  87. package/frontend/build/static/js/{main.23829a0f.js.map → main.cbfc6c9b.js.map} +1 -1
  88. package/package.json +6 -6
  89. package/dist/AirQualityCluster.d.ts.map +0 -1
  90. package/dist/AirQualityCluster.js.map +0 -1
  91. package/dist/BridgedDeviceBasicInformationCluster.d.ts.map +0 -1
  92. package/dist/BridgedDeviceBasicInformationCluster.js.map +0 -1
  93. package/dist/TvocCluster.d.ts.map +0 -1
  94. package/dist/TvocCluster.js.map +0 -1
  95. /package/dist/{AirQualityCluster.d.ts → cluster/AirQualityCluster.d.ts} +0 -0
  96. /package/dist/{AirQualityCluster.js → cluster/AirQualityCluster.js} +0 -0
  97. /package/dist/{BridgedDeviceBasicInformationCluster.d.ts → cluster/BridgedDeviceBasicInformationCluster.d.ts} +0 -0
  98. /package/dist/{BridgedDeviceBasicInformationCluster.js → cluster/BridgedDeviceBasicInformationCluster.js} +0 -0
  99. /package/dist/{TvocCluster.d.ts → cluster/TvocCluster.d.ts} +0 -0
  100. /package/dist/{TvocCluster.js → cluster/TvocCluster.js} +0 -0
  101. /package/frontend/build/static/js/{main.23829a0f.js.LICENSE.txt → main.cbfc6c9b.js.LICENSE.txt} +0 -0
@@ -32,30 +32,48 @@ import { DeviceTypeId, FabricIndex, VendorId } from '@project-chip/matter-node.j
32
32
  import { Format, Level, Logger, createFileLogger } from '@project-chip/matter-node.js/log';
33
33
  import { Environment, StorageService } from '@project-chip/matter.js/environment';
34
34
  import { ServerNode } from '@project-chip/matter.js/node';
35
- import { OnOffLightDevice } from '@project-chip/matter.js/devices/OnOffLightDevice';
36
- import { Endpoint, EndpointServer } from '@project-chip/matter.js/endpoint';
37
- import { logEndpoint } from '@project-chip/matter-node.js/device';
35
+ import { DeviceTypes } from '@project-chip/matter-node.js/device';
38
36
  import { QrCode } from '@project-chip/matter-node.js/schema';
39
37
  import { FabricAction } from '@project-chip/matter-node.js/fabric';
38
+ import { Endpoint } from '@project-chip/matter.js/endpoint';
40
39
  import { AggregatorEndpoint } from '@project-chip/matter.js/endpoints/AggregatorEndpoint';
41
40
  import { BridgedDeviceBasicInformationServer } from '@project-chip/matter.js/behavior/definitions/bridged-device-basic-information';
41
+ import { SwitchServer } from '@project-chip/matter.js/behavior/definitions/switch';
42
+ import { OnOffLightDevice } from '@project-chip/matter.js/devices/OnOffLightDevice';
43
+ import { GenericSwitchDevice } from '@project-chip/matter.js/devices/GenericSwitchDevice';
44
+ import { AnsiLogger, UNDERLINE, UNDERLINEOFF, db, er, nf } from 'node-ansi-logger';
45
+ import { NodeStorageManager } from 'node-persist-manager';
42
46
  import EventEmitter from 'events';
43
47
  import path from 'path';
44
48
  import { promises as fs } from 'fs';
49
+ import { MatterbridgeDeviceV8 } from './matterbridgeDeviceV8.js';
50
+ import { pathToFileURL } from 'url';
51
+ import { shelly_config, somfytahoma_config, zigbee2mqtt_config } from './defaultConfigSchema.js';
52
+ const plg = '\u001B[38;5;33m';
53
+ const dev = '\u001B[38;5;79m';
54
+ const typ = '\u001B[38;5;207m';
55
+ const log = Logger.get('Matterbridge');
45
56
  /**
46
57
  * Represents the Matterbridge application.
47
58
  */
48
59
  export class MatterbridgeV8 extends EventEmitter {
49
60
  environment = Environment.default;
50
- matterbridgeVersion = '2.0.0';
51
- osVersion = '10.0.22631';
61
+ matterbridgeVersion = '';
62
+ osVersion = '';
52
63
  matterbridgeDirectory = '';
53
64
  matterbridgePluginDirectory = '';
54
65
  globalModulesDirectory = '';
66
+ matterbridgeLogFile = '';
67
+ registeredPlugins = [];
68
+ // Node storage
69
+ nodeStorage;
70
+ nodeContext;
71
+ // Matter storage
55
72
  matterStorageService;
56
73
  matterStorageManager;
57
74
  matterStorageContext;
58
75
  matterServerNode;
76
+ matterAggregator;
59
77
  matterLogger;
60
78
  constructor() {
61
79
  super();
@@ -66,33 +84,64 @@ export class MatterbridgeV8 extends EventEmitter {
66
84
  return matterbridge;
67
85
  }
68
86
  async initialize() {
69
- this.matterLogger = Logger.get('Matterbridge');
87
+ // Set up the temporary Matterbridge environment
88
+ this.matterbridgeVersion = '2.0.0';
89
+ this.osVersion = '10.0.22631';
70
90
  this.matterbridgeDirectory = 'C:\\Users\\lligu\\.matterbridge';
71
91
  this.matterbridgePluginDirectory = 'C:\\Users\\lligu\\Matterbridge';
72
- await this.deleteMatterLogfile('matterbridge.log');
92
+ this.globalModulesDirectory = 'C:\\Users\\lligu\\AppData\\Roaming\\npm\\node_modules';
93
+ this.matterbridgeLogFile = 'matterbridge.log';
94
+ this.matterLogger = Logger.get('Matterbridge');
95
+ await this.deleteMatterLogfile(this.matterbridgeLogFile);
96
+ await this.setupMatterFileLogger(this.matterbridgeLogFile);
97
+ this.matterLogger?.notice(`Starting Matterbridge v${this.matterbridgeVersion} on Node.js ${process.version} (${process.platform} ${process.arch})`);
73
98
  this.setupMatterVars(Level.DEBUG, Format.ANSI);
74
99
  await this.setupMatterStorage();
75
- await this.setupMatterFileLogger('matterbridge.log');
100
+ await this.setupNodeStorage();
101
+ // Get the plugins from node storage
102
+ if (!this.nodeStorage)
103
+ throw new Error('No node storage initialized');
104
+ if (!this.nodeContext)
105
+ throw new Error('No node storage context initialized');
106
+ this.registeredPlugins = await this.nodeContext.get('plugins', []);
107
+ for (const plugin of this.registeredPlugins) {
108
+ plugin.nodeContext = await this.nodeStorage.createStorage(plugin.name);
109
+ await plugin.nodeContext.set('name', plugin.name);
110
+ await plugin.nodeContext.set('type', plugin.type);
111
+ await plugin.nodeContext.set('path', plugin.path);
112
+ await plugin.nodeContext.set('version', plugin.version);
113
+ await plugin.nodeContext.set('description', plugin.description);
114
+ await plugin.nodeContext.set('author', plugin.author);
115
+ this.matterLogger?.notice(`Created node storage context for plugin ${plugin.name}`);
116
+ }
76
117
  }
77
118
  setupMatterVars(level, format) {
78
119
  this.environment.vars.set('log.level', level);
79
120
  this.environment.vars.set('log.format', format);
80
121
  this.environment.vars.set('path.root', path.join(this.matterbridgeDirectory, 'matterstorage'));
122
+ this.environment.vars.set('runtime.signals', false);
123
+ this.environment.vars.set('runtime.exitcode', false);
81
124
  }
82
125
  async setupMatterStorage() {
83
126
  this.matterStorageService = this.environment.get(StorageService);
84
127
  this.matterLogger?.notice(`Storage service created: ${this.matterStorageService.location}`);
85
128
  this.matterStorageManager = await this.matterStorageService.open('Matterbridge');
86
- this.matterLogger?.notice('Storage manager created');
129
+ this.matterLogger?.notice('Storage manager "Matterbridge" created');
87
130
  this.matterStorageContext = this.matterStorageManager.createContext('persist');
88
- this.matterLogger?.notice('Storage context created');
131
+ this.matterLogger?.notice('Storage context "Matterbridge.persist" created');
132
+ }
133
+ async setupNodeStorage() {
134
+ this.nodeStorage = new NodeStorageManager({ dir: path.join(this.matterbridgeDirectory, 'storage'), logging: false });
135
+ this.matterLogger?.notice(`Created node storage manager: ${path.join(this.matterbridgeDirectory, 'storage')}`);
136
+ this.nodeContext = await this.nodeStorage.createStorage('matterbridge');
137
+ this.matterLogger?.notice('Created node storage context "matterbridge"');
89
138
  }
90
139
  async deleteMatterLogfile(filename) {
91
140
  try {
92
141
  await fs.unlink(path.join(this.matterbridgeDirectory, filename));
93
142
  }
94
143
  catch (err) {
95
- console.error(`Error deleting old log file: ${err}`);
144
+ this.matterLogger?.error(`Error deleting old log file: ${err}`);
96
145
  }
97
146
  }
98
147
  async setupMatterFileLogger(filename) {
@@ -102,11 +151,11 @@ export class MatterbridgeV8 extends EventEmitter {
102
151
  this.matterLogger?.notice('File logger created: ' + path.join(this.matterbridgeDirectory, filename));
103
152
  }
104
153
  /**
105
- * Creates a commissioning server storage context.
154
+ * Creates a server node storage context.
106
155
  *
107
156
  * @param pluginName - The name of the plugin.
108
157
  * @param deviceName - The name of the device.
109
- * @param deviceType - The type of the device.
158
+ * @param deviceType - The deviceType of the device.
110
159
  * @param vendorId - The vendor ID.
111
160
  * @param vendorName - The vendor name.
112
161
  * @param productId - The product ID.
@@ -119,17 +168,16 @@ export class MatterbridgeV8 extends EventEmitter {
119
168
  * @param hardwareVersionString - The hardware version string of the device (optional).
120
169
  * @returns The storage context for the commissioning server.
121
170
  */
122
- async createServerNodeContext(pluginName, deviceName, deviceType, vendorId, vendorName, productId, productName) {
171
+ async createServerNodeContext(pluginName, deviceName, deviceType, vendorId, vendorName, productId, productName, serialNumber) {
123
172
  if (!this.matterLogger)
124
- return;
173
+ throw new Error('No logger initialized');
125
174
  const log = this.matterLogger;
126
- if (!this.matterStorageService || !this.matterStorageManager) {
127
- log.error('No storage manager initialized');
128
- return;
129
- }
130
- log.debug(`Creating commissioning server storage context for ${pluginName}...`);
131
- const storageContext = this.matterStorageManager.createContext('persist');
132
- const random = 'AG' + CryptoNode.getRandomData(8).toHex();
175
+ if (!this.matterStorageService)
176
+ throw new Error('No storage service initialized');
177
+ log.notice(`Creating server node storage context "${pluginName}.persist" for ${pluginName}...`);
178
+ const storageManager = await this.matterStorageService.open(pluginName);
179
+ const storageContext = storageManager.createContext('persist');
180
+ const random = 'SN' + CryptoNode.getRandomData(8).toHex();
133
181
  await storageContext.set('storeId', pluginName);
134
182
  await storageContext.set('deviceName', deviceName);
135
183
  await storageContext.set('deviceType', deviceType);
@@ -139,36 +187,30 @@ export class MatterbridgeV8 extends EventEmitter {
139
187
  await storageContext.set('productName', productName.slice(0, 32));
140
188
  await storageContext.set('nodeLabel', productName.slice(0, 32));
141
189
  await storageContext.set('productLabel', productName.slice(0, 32));
142
- await storageContext.set('serialNumber', await storageContext.get('serialNumber', random));
190
+ await storageContext.set('serialNumber', await storageContext.get('serialNumber', serialNumber ? serialNumber.slice(0, 32) : random));
143
191
  await storageContext.set('uniqueId', await storageContext.get('uniqueId', random));
144
192
  await storageContext.set('softwareVersion', this.matterbridgeVersion && this.matterbridgeVersion.includes('.') ? parseInt(this.matterbridgeVersion.split('.')[0], 10) : 1);
145
193
  await storageContext.set('softwareVersionString', this.matterbridgeVersion ?? '1.0.0');
146
194
  await storageContext.set('hardwareVersion', this.osVersion && this.osVersion.includes('.') ? parseInt(this.osVersion.split('.')[0], 10) : 1);
147
195
  await storageContext.set('hardwareVersionString', this.osVersion ?? '1.0.0');
148
- log.debug(`Created commissioning server storage context for ${pluginName}:`);
196
+ log.debug(`Created server node storage context "${pluginName}.persist" for ${pluginName}:`);
149
197
  log.debug(`- deviceName: ${await storageContext.get('deviceName')} deviceType: ${await storageContext.get('deviceType')}(0x${(await storageContext.get('deviceType'))?.toString(16).padStart(4, '0')})`);
150
198
  log.debug(`- serialNumber: ${await storageContext.get('serialNumber')} uniqueId: ${await storageContext.get('uniqueId')}`);
151
199
  log.debug(`- softwareVersion: ${await storageContext.get('softwareVersion')} softwareVersionString: ${await storageContext.get('softwareVersionString')}`);
152
200
  log.debug(`- hardwareVersion: ${await storageContext.get('hardwareVersion')} hardwareVersionString: ${await storageContext.get('hardwareVersionString')}`);
153
- log.notice(`Created commissioning server storage context for ${pluginName}`);
154
201
  return storageContext;
155
202
  }
156
- async startServerNode(port = 5080, passcode = 20202021, discriminator = 3840) {
203
+ async createServerNode(storageContext, port = 5540, passcode = 20242025, discriminator = 3850) {
157
204
  if (!this.matterLogger)
158
- return;
205
+ throw new Error('No logger initialized');
159
206
  const log = this.matterLogger;
160
- log.notice('Starting Matterbridge');
161
- this.matterStorageContext = await this.createServerNodeContext('Matterbridge', 'Matterbridge', AggregatorEndpoint.deviceType, 0xfff1, 'Matterbridge', 0x8000, 'Matterbridge Aggregator');
162
- if (!this.matterStorageContext) {
163
- log.error('Error creating storage context for Matterbridge');
164
- return;
165
- }
207
+ log.notice(`Creating server node for ${await storageContext.get('storeId')}`);
166
208
  /**
167
209
  * Create a Matter ServerNode, which contains the Root Endpoint and all relevant data and configuration
168
210
  */
169
- this.matterServerNode = await ServerNode.create({
211
+ const serverNode = await ServerNode.create({
170
212
  // Required: Give the Node a unique ID which is used to store the state of this node
171
- id: await this.matterStorageContext.get('storeId'),
213
+ id: await storageContext.get('storeId'),
172
214
  // Provide Network relevant configuration like the port
173
215
  // Optional when operating only one device on a host, Default port is 5540
174
216
  network: {
@@ -183,80 +225,42 @@ export class MatterbridgeV8 extends EventEmitter {
183
225
  // Provide Node announcement settings
184
226
  // Optional: If Ommitted some development defaults are used
185
227
  productDescription: {
186
- name: await this.matterStorageContext.get('deviceName'),
187
- deviceType: DeviceTypeId(await this.matterStorageContext.get('deviceType')),
228
+ name: await storageContext.get('deviceName'),
229
+ deviceType: DeviceTypeId(await storageContext.get('deviceType')),
188
230
  },
189
231
  // Provide defaults for the BasicInformation cluster on the Root endpoint
190
232
  // Optional: If Omitted some development defaults are used
191
233
  basicInformation: {
192
- vendorId: VendorId(await this.matterStorageContext.get('vendorId')),
193
- vendorName: await this.matterStorageContext.get('vendorName'),
194
- productId: await this.matterStorageContext.get('productId'),
195
- productName: await this.matterStorageContext.get('productName'),
196
- productLabel: await this.matterStorageContext.get('productName'),
197
- nodeLabel: await this.matterStorageContext.get('productName'),
198
- serialNumber: await this.matterStorageContext.get('serialNumber'),
199
- uniqueId: await this.matterStorageContext.get('uniqueId'),
200
- softwareVersion: await this.matterStorageContext.get('softwareVersion'),
201
- softwareVersionString: await this.matterStorageContext.get('softwareVersionString'),
202
- hardwareVersion: await this.matterStorageContext.get('hardwareVersion'),
203
- hardwareVersionString: await this.matterStorageContext.get('hardwareVersionString'),
234
+ vendorId: VendorId(await storageContext.get('vendorId')),
235
+ vendorName: await storageContext.get('vendorName'),
236
+ productId: await storageContext.get('productId'),
237
+ productName: await storageContext.get('productName'),
238
+ productLabel: await storageContext.get('productName'),
239
+ nodeLabel: await storageContext.get('productName'),
240
+ serialNumber: await storageContext.get('serialNumber'),
241
+ uniqueId: await storageContext.get('uniqueId'),
242
+ softwareVersion: await storageContext.get('softwareVersion'),
243
+ softwareVersionString: await storageContext.get('softwareVersionString'),
244
+ hardwareVersion: await storageContext.get('hardwareVersion'),
245
+ hardwareVersionString: await storageContext.get('hardwareVersionString'),
204
246
  },
205
247
  });
206
- /**
207
- * Matter Nodes are a composition of endpoints. Create and add a single endpoint to the node. This example uses the
208
- * OnOffLightDevice or OnOffPlugInUnitDevice depending on the value of the type parameter. It also assigns this Part a
209
- * unique ID to store the endpoint number for it in the storage to restore the device on restart.
210
- * In this case we directly use the default command implementation from matter.js. Check out the DeviceNodeFull example
211
- * to see how to customize the command handlers.
212
- */
213
- const endpoint = new Endpoint(OnOffLightDevice.with(BridgedDeviceBasicInformationServer), {
214
- id: 'OnOffLight',
215
- bridgedDeviceBasicInformation: {
216
- vendorId: VendorId(await this.matterStorageContext.get('vendorId')),
217
- vendorName: await this.matterStorageContext.get('vendorName'),
218
- productName: 'Light',
219
- productLabel: 'Light',
220
- nodeLabel: 'Light',
221
- serialNumber: '0x123456789',
222
- uniqueId: '0x123456789',
223
- reachable: true,
224
- },
225
- });
226
- /**
227
- * Register state change handlers and events of the node for identify and onoff states to react to the commands.
228
- * If the code in these change handlers fail then the change is also rolled back and not executed and an error is
229
- * reported back to the controller.
230
- */
231
- endpoint.events.identify.startIdentifying.on(() => log.notice('Run identify logic, ideally blink a light every 0.5s ...'));
232
- endpoint.events.identify.stopIdentifying.on(() => log.notice('Stop identify logic ...'));
233
- endpoint.events.onOff.onOff$Changed.on((value) => log.notice(`OnOff is now ${value ? 'ON' : 'OFF'}`));
234
- // await this.matterServerNode.add(endpoint);
235
- // const mbV8 = new MatterbridgeDeviceV8(DeviceTypes.OnOffLight);
236
- // const endpoint = mbV8.getBridgedNodeEndpointV8();
237
- const aggregator = new Endpoint(AggregatorEndpoint, { id: 'aggregator' });
238
- await this.matterServerNode.add(aggregator);
239
- await aggregator.add(endpoint);
240
- /**
241
- * Log the endpoint structure for debugging reasons and to allow to verify anything is correct
242
- */
243
- logEndpoint(EndpointServer.forEndpoint(this.matterServerNode));
244
248
  /**
245
249
  * This event is triggered when the device is initially commissioned successfully.
246
250
  * This means: It is added to the first fabric.
247
251
  */
248
- this.matterServerNode.lifecycle.commissioned.on(() => log.notice('Server was initially commissioned successfully!'));
252
+ serverNode.lifecycle.commissioned.on(() => log.notice('Server was initially commissioned successfully!'));
249
253
  /** This event is triggered when all fabrics are removed from the device, usually it also does a factory reset then. */
250
- this.matterServerNode.lifecycle.decommissioned.on(() => log.notice('Server was fully decommissioned successfully!'));
254
+ serverNode.lifecycle.decommissioned.on(() => log.notice('Server was fully decommissioned successfully!'));
251
255
  /** This event is triggered when the device went online. This means that it is discoverable in the network. */
252
- this.matterServerNode.lifecycle.online.on(() => log.notice('Server is online'));
256
+ serverNode.lifecycle.online.on(() => log.notice('Server is online'));
253
257
  /** This event is triggered when the device went offline. it is not longer discoverable or connectable in the network. */
254
- this.matterServerNode.lifecycle.offline.on(() => log.notice('Server is offline'));
258
+ serverNode.lifecycle.offline.on(() => log.notice('Server is offline'));
255
259
  /**
256
260
  * This event is triggered when a fabric is added, removed or updated on the device. Use this if more granular
257
261
  * information is needed.
258
262
  */
259
- this.matterServerNode.events.commissioning.fabricsChanged.on((fabricIndex, fabricAction) => {
263
+ serverNode.events.commissioning.fabricsChanged.on((fabricIndex, fabricAction) => {
260
264
  let action = '';
261
265
  switch (fabricAction) {
262
266
  case FabricAction.Added:
@@ -276,17 +280,17 @@ export class MatterbridgeV8 extends EventEmitter {
276
280
  * This event is triggered when an operative new session was opened by a Controller.
277
281
  * It is not triggered for the initial commissioning process, just afterwards for real connections.
278
282
  */
279
- this.matterServerNode.events.sessions.opened.on((session) => log.notice('Session opened', session));
283
+ serverNode.events.sessions.opened.on((session) => log.notice('Session opened', session));
280
284
  /**
281
285
  * This event is triggered when an operative session is closed by a Controller or because the Device goes offline.
282
286
  */
283
- this.matterServerNode.events.sessions.closed.on((session) => log.notice('Session closed', session));
287
+ serverNode.events.sessions.closed.on((session) => log.notice('Session closed', session));
284
288
  /** This event is triggered when a subscription gets added or removed on an operative session. */
285
- this.matterServerNode.events.sessions.subscriptionsChanged.on((session) => {
289
+ serverNode.events.sessions.subscriptionsChanged.on((session) => {
286
290
  log.notice('Session subscriptions changed', session);
287
291
  log.notice('Status of all sessions', this.matterServerNode?.state.sessions.sessions);
288
292
  });
289
- await this.matterServerNode.bringOnline();
293
+ return serverNode;
290
294
  }
291
295
  showServerNodeQR() {
292
296
  if (!this.matterServerNode || !this.matterLogger)
@@ -312,16 +316,326 @@ export class MatterbridgeV8 extends EventEmitter {
312
316
  return;
313
317
  await this.matterServerNode.close();
314
318
  }
319
+ async createAggregator(storageContext) {
320
+ if (!this.matterLogger)
321
+ throw new Error('No logger initialized');
322
+ const log = this.matterLogger;
323
+ log.notice(`Creating ${await storageContext.get('storeId')} aggregator `);
324
+ const aggregator = new Endpoint(AggregatorEndpoint, { id: `${await storageContext.get('storeId')} aggregator` });
325
+ return aggregator;
326
+ }
327
+ async startBridge() {
328
+ if (!this.matterLogger)
329
+ throw new Error('No logger initialized');
330
+ const log = this.matterLogger;
331
+ const storageContext = await this.createServerNodeContext('Matterbridge', 'Matterbridge', AggregatorEndpoint.deviceType, 0xfff1, 'Matterbridge', 0x8000, 'Matterbridge Aggregator');
332
+ this.matterServerNode = await this.createServerNode(storageContext);
333
+ this.matterAggregator = await this.createAggregator(storageContext);
334
+ log.notice(`Adding ${await storageContext.get('storeId')} aggregator to ${await storageContext.get('storeId')} server node`);
335
+ await this.matterServerNode.add(this.matterAggregator);
336
+ for (const plugin of this.registeredPlugins) {
337
+ if (!plugin.enabled) {
338
+ log.info(`Plugin ${plg}${plugin.name}${nf} not enabled`);
339
+ continue;
340
+ }
341
+ plugin.error = false;
342
+ plugin.loaded = false;
343
+ plugin.started = false;
344
+ plugin.configured = false;
345
+ plugin.connected = undefined;
346
+ plugin.qrPairingCode = undefined;
347
+ plugin.manualPairingCode = undefined;
348
+ this.loadPlugin(plugin, true, 'Matterbridge is starting'); // No await do it asyncronously
349
+ }
350
+ log.notice(`Adding lightEndpoint1 to ${await storageContext.get('storeId')} aggregator`);
351
+ const lightEndpoint1 = new Endpoint(OnOffLightDevice.with(BridgedDeviceBasicInformationServer), {
352
+ id: 'OnOffLight',
353
+ bridgedDeviceBasicInformation: {
354
+ vendorId: VendorId(await storageContext.get('vendorId')),
355
+ vendorName: await storageContext.get('vendorName'),
356
+ productName: 'Light',
357
+ productLabel: 'Light',
358
+ nodeLabel: 'Light',
359
+ serialNumber: '0x123456789',
360
+ uniqueId: '0x123456789',
361
+ reachable: true,
362
+ },
363
+ });
364
+ this.matterAggregator.add(lightEndpoint1);
365
+ log.notice(`Adding switchEnpoint2 to ${await storageContext.get('storeId')} aggregator`);
366
+ const switchEnpoint2 = new Endpoint(GenericSwitchDevice.with(BridgedDeviceBasicInformationServer, SwitchServer.with('MomentarySwitch', 'MomentarySwitchLongPress', 'MomentarySwitchMultiPress', 'MomentarySwitchRelease')), {
367
+ id: 'GenericSwitch',
368
+ bridgedDeviceBasicInformation: {
369
+ vendorId: VendorId(await storageContext.get('vendorId')),
370
+ vendorName: await storageContext.get('vendorName'),
371
+ productName: 'GenericSwitch',
372
+ productLabel: 'GenericSwitch',
373
+ nodeLabel: 'GenericSwitch',
374
+ serialNumber: '0x123456739',
375
+ uniqueId: '0x123456739',
376
+ reachable: true,
377
+ },
378
+ switch: {
379
+ numberOfPositions: 2,
380
+ currentPosition: 0,
381
+ multiPressMax: 2,
382
+ },
383
+ });
384
+ this.matterAggregator.add(switchEnpoint2);
385
+ switchEnpoint2.events.identify.startIdentifying.on(() => log.notice('Run identify logic, ideally blink a light every 0.5s ...'));
386
+ switchEnpoint2.events.switch.currentPosition$Changed.on(() => log.notice('Run identify logic, ideally blink a light every 0.5s ...'));
387
+ // switchEnpoint2.events.switch.emit('initialPress', { newPosition: 1 }, ActionContext.agentFor(switchEnpoint2) );
388
+ log.notice(`Adding matterbridge device to ${await storageContext.get('storeId')} aggregator`);
389
+ const matterbridgeDevice3 = new MatterbridgeDeviceV8(DeviceTypes.TEMPERATURE_SENSOR, { uniqueStorageKey: 'TemperatureSensor' });
390
+ this.matterAggregator.add(matterbridgeDevice3);
391
+ log.notice(`Starting ${await storageContext.get('storeId')} server node`);
392
+ await this.matterServerNode.bringOnline();
393
+ /*
394
+ logEndpoint(EndpointServer.forEndpoint(this.matterServerNode));
395
+ console.log('matterbridgeDevice3\n', matterbridgeDevice3);
396
+ console.log('matterbridgeDevice3.events\n', matterbridgeDevice3.events);
397
+ console.log('matterbridgeDevice3.events.identify\n', matterbridgeDevice3.eventsOf(IdentifyServer));
398
+ console.log('matterbridgeDevice3.state\n', matterbridgeDevice3.state);
399
+ console.log('matterbridgeDevice3.state.temperatureMeasurement\n', matterbridgeDevice3.stateOf(TemperatureMeasurementServer));
400
+ // matterbridgeDevice3.eventsOf(IdentifyServer);
401
+ // matterbridgeDevice3.events.identify.startIdentifying.on(() => log.notice('Run identify logic, ideally blink a light every 0.5s ...'));
402
+ */
403
+ this.showServerNodeQR();
404
+ }
405
+ async startChildbridge() {
406
+ //
407
+ }
408
+ async startController() {
409
+ //
410
+ }
411
+ /**
412
+ * Adds a bridged device to the Matterbridge.
413
+ * @param pluginName - The name of the plugin.
414
+ * @param device - The bridged device to add.
415
+ * @returns {Promise<void>} - A promise that resolves when the storage process is started.
416
+ */
417
+ async addBridgedDevice(pluginName, device) {
418
+ log.info(`Adding bridged device ${dev}${device.deviceName}${nf} for plugin ${plg}${pluginName}${nf}`);
419
+ }
420
+ /**
421
+ * Loads a plugin and returns the corresponding MatterbridgePlatform instance.
422
+ * @param plugin - The plugin to load.
423
+ * @param start - Optional flag indicating whether to start the plugin after loading. Default is false.
424
+ * @param message - Optional message to pass to the plugin when starting.
425
+ * @returns A Promise that resolves to the loaded MatterbridgePlatform instance.
426
+ * @throws An error if the plugin is not enabled, already loaded, or fails to load.
427
+ */
428
+ async loadPlugin(plugin, start = false, message = '') {
429
+ if (!plugin.enabled) {
430
+ return Promise.reject(new Error(`Plugin ${plg}${plugin.name}${er} not enabled`));
431
+ }
432
+ if (plugin.platform) {
433
+ return Promise.resolve(plugin.platform);
434
+ }
435
+ log.info(`Loading plugin ${plg}${plugin.name}${nf} type ${typ}${plugin.type}${nf}`);
436
+ try {
437
+ // Load the package.json of the plugin
438
+ const packageJson = JSON.parse(await fs.readFile(plugin.path, 'utf8'));
439
+ // Resolve the main module path relative to package.json
440
+ const pluginEntry = path.resolve(path.dirname(plugin.path), packageJson.main);
441
+ // Dynamically import the plugin
442
+ const pluginUrl = pathToFileURL(pluginEntry);
443
+ log.debug(`Importing plugin ${plg}${plugin.name}${db} from ${pluginUrl.href}`);
444
+ const pluginInstance = await import(pluginUrl.href);
445
+ log.debug(`Imported plugin ${plg}${plugin.name}${db} from ${pluginUrl.href}`);
446
+ // Call the default export function of the plugin, passing this MatterBridge instance, the log and the config
447
+ if (pluginInstance.default) {
448
+ const config = await this.loadPluginConfig(plugin);
449
+ const log = new AnsiLogger({ logName: plugin.description, logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */, logDebug: true });
450
+ const platform = pluginInstance.default(this, log, config);
451
+ platform.name = packageJson.name;
452
+ platform.config = config;
453
+ platform.version = packageJson.version;
454
+ plugin.name = packageJson.name;
455
+ plugin.description = packageJson.description;
456
+ plugin.version = packageJson.version;
457
+ plugin.author = packageJson.author;
458
+ plugin.type = platform.type;
459
+ plugin.platform = platform;
460
+ plugin.loaded = true;
461
+ plugin.registeredDevices = 0;
462
+ plugin.addedDevices = 0;
463
+ // Save the updated plugin data in the node storage
464
+ // await this.nodeContext?.set<RegisteredPlugin[]>('plugins', await this.getBaseRegisteredPlugins());
465
+ // await this.getPluginLatestVersion(plugin);
466
+ log.info(`Loaded plugin ${plg}${plugin.name}${nf} type ${typ}${platform.type} ${db}(entrypoint ${UNDERLINE}${pluginEntry}${UNDERLINEOFF})`);
467
+ if (start)
468
+ this.startPlugin(plugin, message); // No await do it asyncronously
469
+ return Promise.resolve(platform);
470
+ }
471
+ else {
472
+ plugin.error = true;
473
+ return Promise.reject(new Error(`Plugin ${plg}${plugin.name}${er} does not provide a default export`));
474
+ }
475
+ }
476
+ catch (err) {
477
+ plugin.error = true;
478
+ return Promise.reject(new Error(`Failed to load plugin ${plg}${plugin.name}${er}: ${err}`));
479
+ }
480
+ }
481
+ /**
482
+ * Starts a plugin.
483
+ *
484
+ * @param {RegisteredPlugin} plugin - The plugin to start.
485
+ * @param {string} [message] - Optional message to pass to the plugin's onStart method.
486
+ * @param {boolean} [configure=false] - Indicates whether to configure the plugin after starting.
487
+ * @returns {Promise<void>} A promise that resolves when the plugin is started successfully, or rejects with an error if starting the plugin fails.
488
+ */
489
+ async startPlugin(plugin, message, configure = false) {
490
+ if (!plugin.loaded || !plugin.platform) {
491
+ return Promise.reject(new Error(`Plugin ${plg}${plugin.name}${er} not loaded or no platform`));
492
+ }
493
+ if (plugin.started) {
494
+ log.debug(`Plugin ${plg}${plugin.name}${db} already started`);
495
+ return Promise.resolve();
496
+ }
497
+ log.info(`Starting plugin ${plg}${plugin.name}${db} type ${typ}${plugin.type}${db}`);
498
+ try {
499
+ plugin.platform
500
+ .onStart(message)
501
+ .then(() => {
502
+ plugin.started = true;
503
+ log.info(`Started plugin ${plg}${plugin.name}${db} type ${typ}${plugin.type}${db}`);
504
+ if (configure)
505
+ this.configurePlugin(plugin); // No await do it asyncronously
506
+ return Promise.resolve();
507
+ })
508
+ .catch((err) => {
509
+ plugin.error = true;
510
+ return Promise.reject(new Error(`Failed to start plugin ${plg}${plugin.name}${er}: ${err}`));
511
+ });
512
+ }
513
+ catch (err) {
514
+ plugin.error = true;
515
+ return Promise.reject(new Error(`Failed to start plugin ${plg}${plugin.name}${er}: ${err}`));
516
+ }
517
+ }
518
+ /**
519
+ * Configures a plugin.
520
+ *
521
+ * @param {RegisteredPlugin} plugin - The plugin to configure.
522
+ * @returns {Promise<void>} A promise that resolves when the plugin is configured successfully, or rejects with an error if configuration fails.
523
+ */
524
+ async configurePlugin(plugin) {
525
+ if (!plugin.loaded || !plugin.started || !plugin.platform) {
526
+ return Promise.reject(new Error(`Plugin ${plg}${plugin.name}${er} not loaded (${plugin.loaded}) or not started (${plugin.started}) or not platform (${plugin.platform?.name})`));
527
+ }
528
+ if (plugin.configured) {
529
+ log.info(`Plugin ${plg}${plugin.name}${nf} already configured`);
530
+ return Promise.resolve();
531
+ }
532
+ log.info(`Configuring plugin ${plg}${plugin.name}${db} type ${typ}${plugin.type}${db}`);
533
+ try {
534
+ plugin.platform
535
+ .onConfigure()
536
+ .then(() => {
537
+ plugin.configured = true;
538
+ log.info(`Configured plugin ${plg}${plugin.name}${db} type ${typ}${plugin.type}${db}`);
539
+ // this.savePluginConfig(plugin);
540
+ return Promise.resolve();
541
+ })
542
+ .catch((err) => {
543
+ plugin.error = true;
544
+ return Promise.reject(new Error(`Failed to configure plugin ${plg}${plugin.name}${er}: ${err}`));
545
+ });
546
+ }
547
+ catch (err) {
548
+ plugin.error = true;
549
+ return Promise.reject(new Error(`Failed to configure plugin ${plg}${plugin.name}${er}: ${err}`));
550
+ }
551
+ }
552
+ /**
553
+ * Loads the configuration for a plugin.
554
+ * If the configuration file exists, it reads the file and returns the parsed JSON data.
555
+ * If the configuration file does not exist, it creates a new file with default configuration and returns it.
556
+ * If any error occurs during file access or creation, it logs an error and rejects the promise with the error.
557
+ *
558
+ * @param plugin - The plugin for which to load the configuration.
559
+ * @returns A promise that resolves to the loaded or created configuration.
560
+ */
561
+ async loadPluginConfig(plugin) {
562
+ const configFile = path.join(this.matterbridgeDirectory, `${plugin.name}.config.json`);
563
+ try {
564
+ await fs.access(configFile);
565
+ const data = await fs.readFile(configFile, 'utf8');
566
+ const config = JSON.parse(data);
567
+ // this.log.debug(`Config file found: ${configFile}.\nConfig:${rs}\n`, config);
568
+ log.debug(`Config file found: ${configFile}.`);
569
+ /* The first time a plugin is added to the system, the config file is created with the plugin name and type "".*/
570
+ config.name = plugin.name;
571
+ config.type = plugin.type;
572
+ return config;
573
+ }
574
+ catch (err) {
575
+ if (err instanceof Error) {
576
+ const nodeErr = err;
577
+ if (nodeErr.code === 'ENOENT') {
578
+ let config;
579
+ if (plugin.name === 'matterbridge-zigbee2mqtt')
580
+ config = zigbee2mqtt_config;
581
+ else if (plugin.name === 'matterbridge-somfy-tahoma')
582
+ config = somfytahoma_config;
583
+ else if (plugin.name === 'matterbridge-shelly')
584
+ config = shelly_config;
585
+ else
586
+ config = { name: plugin.name, type: plugin.type, unregisterOnShutdown: false };
587
+ try {
588
+ await this.writeFile(configFile, JSON.stringify(config, null, 2));
589
+ log.debug(`Created config file: ${configFile}.`);
590
+ // this.log.debug(`Created config file: ${configFile}.\nConfig:${rs}\n`, config);
591
+ return config;
592
+ }
593
+ catch (err) {
594
+ log.error(`Error creating config file ${configFile}: ${err}`);
595
+ return config;
596
+ }
597
+ }
598
+ else {
599
+ log.error(`Error accessing config file ${configFile}: ${err}`);
600
+ return {};
601
+ }
602
+ }
603
+ log.error(`Error loading config file ${configFile}: ${err}`);
604
+ return {};
605
+ }
606
+ }
607
+ /**
608
+ * Writes data to a file.
609
+ *
610
+ * @param {string} filePath - The path of the file to write to.
611
+ * @param {string} data - The data to write to the file.
612
+ * @returns {Promise<void>} - A promise that resolves when the data is successfully written to the file.
613
+ */
614
+ async writeFile(filePath, data) {
615
+ // Write the data to a file
616
+ await fs
617
+ .writeFile(`${filePath}`, data, 'utf8')
618
+ .then(() => {
619
+ log.debug(`Successfully wrote to ${filePath}`);
620
+ })
621
+ .catch((error) => {
622
+ log.error(`Error writing to ${filePath}:`, error);
623
+ });
624
+ }
315
625
  }
316
626
  // node dist/matterbridgeV8.js MatterbridgeV8
317
627
  if (process.argv.includes('MatterbridgeV8')) {
318
628
  const matterbridge = await MatterbridgeV8.create();
319
- await matterbridge.startServerNode(5070, 20242025, 3950);
320
- matterbridge.showServerNodeQR();
629
+ if (process.argv.includes('-bridge'))
630
+ await matterbridge.startBridge();
631
+ if (process.argv.includes('-childbridge'))
632
+ await matterbridge.startChildbridge();
633
+ if (process.argv.includes('-controller'))
634
+ await matterbridge.startController();
321
635
  process.on('SIGINT', async function () {
322
636
  console.log('Caught interrupt signal');
323
637
  await matterbridge.stopServerNode();
324
- // process.exit();
638
+ process.exit();
325
639
  });
326
640
  }
327
641
  //# sourceMappingURL=matterbridgeV8.js.map