Skip to content

Commit

Permalink
Improve details and error infos
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed Dec 14, 2024
1 parent 42ce325 commit 5cc0691
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 35 deletions.
8 changes: 7 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,9 @@ export class MatterAdapter extends utils.Adapter {
if (obj !== undefined) {
await matterBridge.start();
}
} else {
this.log.error(`Cannot create device for ${bridge._id}`);
this.#devices.set(bridge._id, { error: 'Cannot create bridge' });
}
} else {
const config = await this.prepareMatterBridgeConfiguration(
Expand Down Expand Up @@ -1261,6 +1264,9 @@ export class MatterAdapter extends utils.Adapter {
if (obj !== undefined) {
await matterDevice.start();
}
} else {
this.log.error(`Cannot create device for ${device._id}`);
this.#devices.set(device._id, { error: 'Cannot create device' });
}
} else {
const config = await this.prepareMatterDeviceConfiguration(
Expand Down Expand Up @@ -1356,7 +1362,7 @@ export class MatterAdapter extends utils.Adapter {
__header__error: 'Error information',
__text__info: `${type === 'bridge' ? 'Bridge' : 'Device'} is in error state. Fix the error before enabling it again`,
__text__error: error,
__text__uuid: `UUID: ${uuid}`,
uuid: `UUID: ${uuid}`,
},
};

Expand Down
83 changes: 60 additions & 23 deletions src/matter/BridgedDevicesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class BridgedDevices extends BaseServerNode {
await this.serverNode.add(this.#aggregator);

let erroredCount = 0;
for (const { device, error, options: deviceOptions } of this.#devices.values()) {
for (const [uuid, { device, error, options: deviceOptions }] of this.#devices.entries()) {
if (!device || error) {
erroredCount++;
this.adapter.log.info(`Skipping device ${deviceOptions.uuid} because could not be initialized.`);
Expand All @@ -230,6 +230,11 @@ class BridgedDevices extends BaseServerNode {
erroredCount++;
const errorText = inspect(error, { depth: 10 });
this.adapter.log.error(`Error adding device ${deviceOptions.uuid} to bridge: ${errorText}`);
const details = this.#devices.get(uuid);
if (details !== undefined) {
details.error = error.message;
this.#devices.set(uuid, details);
}
}
}
if (erroredCount === this.#devices.size) {
Expand All @@ -256,23 +261,30 @@ class BridgedDevices extends BaseServerNode {
const newDeviceList = new Set<string>();

for (const { device, error, options: deviceOptions } of options.devices.values()) {
this.adapter.log.debug(`Processing device ${deviceOptions.uuid} "${deviceOptions.name}" in bridge`);
const existingDevice = this.#devices.get(deviceOptions.uuid)?.device;
const uuid = deviceOptions.uuid;
this.adapter.log.debug(`Processing device ${uuid} "${deviceOptions.name}" in bridge`);
const existingDevice = this.#devices.get(uuid)?.device;
if (existingDevice) {
newDeviceList.add(deviceOptions.uuid);
this.adapter.log.debug(`Device ${deviceOptions.uuid} already in bridge. Sync Configuration`);
newDeviceList.add(uuid);
this.adapter.log.debug(`Device ${uuid} already in bridge. Sync Configuration`);
existingDevice.applyConfiguration(deviceOptions);
continue;
}
if (!device || error) {
this.adapter.log.info(`Skipping device ${deviceOptions.uuid} because could not be initialized.`);
this.#devices.set(deviceOptions.uuid, { device, error, options: deviceOptions });
this.adapter.log.info(`Skipping device ${uuid} because could not be initialized.`);
this.#devices.set(uuid, { device, error, options: deviceOptions });
continue;
}
newDeviceList.add(deviceOptions.uuid);
this.adapter.log.info(`Adding device ${deviceOptions.uuid} "${deviceOptions.name}" to bridge`);
await this.addBridgedIoBrokerDevice(device, deviceOptions);
this.#devices.set(deviceOptions.uuid, { device, options: deviceOptions });
newDeviceList.add(uuid);
this.adapter.log.info(`Adding device ${uuid} "${deviceOptions.name}" to bridge`);
try {
await this.addBridgedIoBrokerDevice(device, deviceOptions);
this.#devices.set(uuid, { device, options: deviceOptions });
} catch (error) {
const errorText = inspect(error, { depth: 10 });
this.adapter.log.error(`Error adding device ${uuid} to bridge: ${errorText}`);
this.#devices.set(uuid, { error: error.message, options: deviceOptions });
}
}

for (const [uuid, endpoints] of this.#deviceEndpoints) {
Expand Down Expand Up @@ -356,29 +368,54 @@ class BridgedDevices extends BaseServerNode {
}

getDeviceDetails(message: ioBroker.MessagePayload): StructuredJsonFormData {
const bridgedDeviceUuid = message.bridgedDeviceUuid as string;
const bridgedDeviceUuid = message.bridgedDeviceUuid;
const details: StructuredJsonFormData = {};

const isError = true;
if (isError) {
const { error } = this.#devices.get(bridgedDeviceUuid) ?? {};
if (error) {
details.error = {
__header__error: 'Error information',
__text__info: `Bridged Device is in error state. Fix the error before enabling it again`,
__text__uuid: `UUID: ${bridgedDeviceUuid} on ${this.uuid}`,
uuid: `UUID: ${bridgedDeviceUuid} on ${this.uuid}`,
__text__error: `Error: ${error}`,
};
}

details.info = {
__header__info: 'Device information',
__text__uuid: `UUID: ${bridgedDeviceUuid}`,
};
if (bridgedDeviceUuid !== undefined) {
const mappingDevice = this.#mappingDevices.get(bridgedDeviceUuid);

const mappingDevice = this.#mappingDevices.get(bridgedDeviceUuid);
if (mappingDevice) {
return {
...details,
...mappingDevice?.getDeviceDetails(),
};
}

return {
...details,
...mappingDevice?.getDeviceDetails(),
return {
...details,
noDevice: {
__header__error: 'Device not created',
uuid: `UUID: ${bridgedDeviceUuid} on ${this.uuid}`,
__text__error: `Error: The device does not exist on this bridge`,
},
};
}

details.overview = {
__header__info: 'Bridge Overview',
uuid: this.uuid,
port: this.port,
deviceName: this.#parameters.deviceName,
productName: this.#parameters.productName,
vendorId: this.#parameters.vendorId,
productId: this.#parameters.productId,
numberOfBridgedDevices: [...this.#devices.values()].reduce(
(count, { device }) => count + (device ? 1 : 0),
0,
),
};

return details;
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/matter/DeviceNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,17 @@ class Device extends BaseServerNode {
}

getDeviceDetails(_message: ioBroker.MessagePayload): StructuredJsonFormData {
const details: StructuredJsonFormData = {
information: {
__header__info: 'Device information',
__text__uuid: `UUID: ${this.uuid}`,
},
const details: StructuredJsonFormData = {};

details.overview = {
__header__info: 'Device Overview',
uuid: this.uuid,
port: this.port,
deviceName: this.#parameters.deviceName,
productName: this.#parameters.productName,
vendorId: this.#parameters.vendorId,
productId: this.#parameters.productId,
};

return {
...details,
...this.#mappingDevice?.getDeviceDetails(),
Expand Down
12 changes: 7 additions & 5 deletions src/matter/to-matter/GenericDeviceToMatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ export abstract class GenericDeviceToMatter {
const details: StructuredJsonFormData = {};

details.detectedStates = {
__header__device: 'Detected ioBroker Device type',
deviceType: this.ioBrokerDevice.deviceType,
__header__states: 'Detected device states',
__text__info: 'The following states were detected for this device.',
__devider__info: true,
__text__info: 'The following states were detected for this device:',
__divider__info: true,
...this.ioBrokerDevice.getStates(true, true),
};

Expand All @@ -120,11 +122,11 @@ export abstract class GenericDeviceToMatter {
details.endpoints = {
__header__endpoints: 'Device Endpoints',
__text__info: 'The following Matter endpoints are mapped for this device.',
__devider__info: true,
__divider__info: true,
};
endpoints.forEach(endpoint => {
details.endpoints.__header__endpoint = `Endpoint ${endpoint.number}`;
details.endpoints.__text__deviceType = endpoint.type.name;
details.endpoints[`__header__endpoint${endpoint.number}`] = `Endpoint ${endpoint.number}`;
details.endpoints[`dt${endpoint.number}__deviceType`] = endpoint.type.name;
// TODO expose potentially more
});
}
Expand Down

0 comments on commit 5cc0691

Please sign in to comment.