mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 07:07:21 +00:00
feat: enhance device status DTO and service with optional properties and environment checks
This commit is contained in:
@ -13,6 +13,7 @@ class StatusDto {
|
||||
|
||||
@IsNotEmpty()
|
||||
value: any;
|
||||
t?: string | number | Date;
|
||||
}
|
||||
|
||||
export class AddDeviceStatusDto {
|
||||
|
@ -28,6 +28,8 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||
export class DeviceStatusFirebaseService {
|
||||
private tuya: TuyaContext;
|
||||
private firebaseDb: Database;
|
||||
private readonly isDevEnv: boolean;
|
||||
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
@ -47,6 +49,8 @@ export class DeviceStatusFirebaseService {
|
||||
|
||||
// Initialize firebaseDb using firebaseDataBase function
|
||||
this.firebaseDb = firebaseDataBase(this.configService);
|
||||
this.isDevEnv =
|
||||
this.configService.get<string>('NODE_ENV') === 'development';
|
||||
}
|
||||
async addDeviceStatusByDeviceUuid(
|
||||
deviceTuyaUuid: string,
|
||||
@ -61,7 +65,7 @@ export class DeviceStatusFirebaseService {
|
||||
const deviceStatusSaved = await this.createDeviceStatusFirebase({
|
||||
deviceUuid: device.uuid,
|
||||
deviceTuyaUuid: deviceTuyaUuid,
|
||||
status: deviceStatus.status,
|
||||
status: deviceStatus?.status,
|
||||
productUuid: deviceStatus.productUuid,
|
||||
productType: deviceStatus.productType,
|
||||
});
|
||||
@ -122,7 +126,7 @@ export class DeviceStatusFirebaseService {
|
||||
return {
|
||||
productUuid: deviceDetails.productDevice.uuid,
|
||||
productType: deviceDetails.productDevice.prodType,
|
||||
status: deviceStatus.result[0].status,
|
||||
status: deviceStatus.result[0]?.status,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
@ -187,18 +191,18 @@ export class DeviceStatusFirebaseService {
|
||||
if (!existingData.productType) {
|
||||
existingData.productType = addDeviceStatusDto.productType;
|
||||
}
|
||||
if (!existingData.status) {
|
||||
if (!existingData?.status) {
|
||||
existingData.status = [];
|
||||
}
|
||||
|
||||
// Create a map to track existing status codes
|
||||
const statusMap = new Map(
|
||||
existingData.status.map((item) => [item.code, item.value]),
|
||||
existingData?.status.map((item) => [item.code, item.value]),
|
||||
);
|
||||
|
||||
// Update or add status codes
|
||||
|
||||
for (const statusItem of addDeviceStatusDto.status) {
|
||||
for (const statusItem of addDeviceStatusDto?.status) {
|
||||
statusMap.set(statusItem.code, statusItem.value);
|
||||
}
|
||||
|
||||
@ -211,6 +215,7 @@ export class DeviceStatusFirebaseService {
|
||||
return existingData;
|
||||
});
|
||||
|
||||
if (this.isDevEnv) {
|
||||
// Save logs to your repository
|
||||
const newLogs = addDeviceStatusDto.log.properties.map((property) => {
|
||||
return this.deviceStatusLogRepository.create({
|
||||
@ -234,8 +239,8 @@ export class DeviceStatusFirebaseService {
|
||||
PowerClampEnergyEnum.ENERGY_CONSUMED_C,
|
||||
]);
|
||||
|
||||
const energyStatus = addDeviceStatusDto?.log?.properties?.find((status) =>
|
||||
energyCodes.has(status.code),
|
||||
const energyStatus = addDeviceStatusDto?.log?.properties?.find(
|
||||
(status) => energyCodes.has(status.code),
|
||||
);
|
||||
|
||||
if (energyStatus) {
|
||||
@ -269,6 +274,67 @@ export class DeviceStatusFirebaseService {
|
||||
addDeviceStatusDto.deviceUuid,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// Save logs to your repository
|
||||
const newLogs = addDeviceStatusDto?.status.map((property) => {
|
||||
return this.deviceStatusLogRepository.create({
|
||||
deviceId: addDeviceStatusDto.deviceUuid,
|
||||
deviceTuyaId: addDeviceStatusDto.deviceTuyaUuid,
|
||||
productId: addDeviceStatusDto.log.productKey,
|
||||
log: addDeviceStatusDto.log,
|
||||
code: property.code,
|
||||
value: property.value,
|
||||
eventId: addDeviceStatusDto.log.dataId,
|
||||
eventTime: new Date(property.t).toISOString(),
|
||||
});
|
||||
});
|
||||
await this.deviceStatusLogRepository.save(newLogs);
|
||||
|
||||
if (addDeviceStatusDto.productType === ProductType.PC) {
|
||||
const energyCodes = new Set([
|
||||
PowerClampEnergyEnum.ENERGY_CONSUMED,
|
||||
PowerClampEnergyEnum.ENERGY_CONSUMED_A,
|
||||
PowerClampEnergyEnum.ENERGY_CONSUMED_B,
|
||||
PowerClampEnergyEnum.ENERGY_CONSUMED_C,
|
||||
]);
|
||||
|
||||
const energyStatus = addDeviceStatusDto?.status?.find((status) => {
|
||||
return energyCodes.has(status.code as PowerClampEnergyEnum);
|
||||
});
|
||||
|
||||
if (energyStatus) {
|
||||
await this.powerClampService.updateEnergyConsumedHistoricalData(
|
||||
addDeviceStatusDto.deviceUuid,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
addDeviceStatusDto.productType === ProductType.CPS ||
|
||||
addDeviceStatusDto.productType === ProductType.WPS
|
||||
) {
|
||||
const occupancyCodes = new Set([PresenceSensorEnum.PRESENCE_STATE]);
|
||||
|
||||
const occupancyStatus = addDeviceStatusDto?.status?.find((status) => {
|
||||
return occupancyCodes.has(status.code as PresenceSensorEnum);
|
||||
});
|
||||
|
||||
if (occupancyStatus) {
|
||||
await this.occupancyService.updateOccupancySensorHistoricalData(
|
||||
addDeviceStatusDto.deviceUuid,
|
||||
);
|
||||
await this.occupancyService.updateOccupancySensorHistoricalDurationData(
|
||||
addDeviceStatusDto.deviceUuid,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (addDeviceStatusDto.productType === ProductType.AQI) {
|
||||
// await this.aqiDataService.updateAQISensorHistoricalData(
|
||||
// addDeviceStatusDto.deviceUuid,
|
||||
// );
|
||||
}
|
||||
}
|
||||
// Return the updated data
|
||||
const snapshot: DataSnapshot = await get(dataRef);
|
||||
return snapshot.val();
|
||||
|
Reference in New Issue
Block a user