mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
Update Device Service to Handle Promise Settlements and Add Battery Status for Door Locks
This commit is contained in:
@ -630,8 +630,26 @@ export class DeviceService {
|
||||
'permission.permissionType',
|
||||
],
|
||||
});
|
||||
const devicesData = await Promise.all(
|
||||
const devicesData = await Promise.allSettled(
|
||||
devices.map(async (device) => {
|
||||
let battery = null;
|
||||
|
||||
// Check if the device is a door lock (DL)
|
||||
if (device.productDevice.prodType === ProductType.DL) {
|
||||
console.log('device', device.deviceTuyaUuid);
|
||||
|
||||
const doorLockInstructionsStatus =
|
||||
await this.getDevicesInstructionStatus(device.uuid);
|
||||
|
||||
const batteryStatus: any = doorLockInstructionsStatus.status.find(
|
||||
(status: any) => status.code === 'residual_electricity',
|
||||
);
|
||||
|
||||
if (batteryStatus) {
|
||||
battery = batteryStatus.value;
|
||||
}
|
||||
}
|
||||
|
||||
const spaceDevice = device?.spaceDevice;
|
||||
const parentDevice = spaceDevice?.parent;
|
||||
return {
|
||||
@ -650,11 +668,20 @@ export class DeviceService {
|
||||
device.deviceTuyaUuid,
|
||||
)),
|
||||
uuid: device.uuid,
|
||||
...(battery && { battery }),
|
||||
} as GetDeviceDetailsInterface;
|
||||
}),
|
||||
);
|
||||
|
||||
return devicesData;
|
||||
// Filter out rejected promises and extract the fulfilled values
|
||||
const fulfilledDevices = devicesData
|
||||
.filter((result) => result.status === 'fulfilled')
|
||||
.map(
|
||||
(result) =>
|
||||
(result as PromiseFulfilledResult<GetDeviceDetailsInterface>).value,
|
||||
);
|
||||
|
||||
return fulfilledDevices;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
|
Reference in New Issue
Block a user