mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-08-25 15:59:38 +00:00
Implement code changes to enhance functionality and improve performance
This commit is contained in:
@ -39,12 +39,7 @@ export class DeviceStatusFirebaseService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Initialize firebaseDb using firebaseDataBase function
|
// Initialize firebaseDb using firebaseDataBase function
|
||||||
try {
|
this.firebaseDb = firebaseDataBase(this.configService);
|
||||||
this.firebaseDb = firebaseDataBase(this.configService);
|
|
||||||
} catch (error) {
|
|
||||||
console.warn('Firebase initialization failed, continuing without Firebase:', error.message);
|
|
||||||
this.firebaseDb = null;
|
|
||||||
}
|
|
||||||
this.isDevEnv =
|
this.isDevEnv =
|
||||||
this.configService.get<string>('NODE_ENV') === 'development';
|
this.configService.get<string>('NODE_ENV') === 'development';
|
||||||
}
|
}
|
||||||
@ -257,14 +252,6 @@ export class DeviceStatusFirebaseService {
|
|||||||
async createDeviceStatusFirebase(
|
async createDeviceStatusFirebase(
|
||||||
addDeviceStatusDto: AddDeviceStatusDto,
|
addDeviceStatusDto: AddDeviceStatusDto,
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
// Check if Firebase is available
|
|
||||||
if (!this.firebaseDb) {
|
|
||||||
console.warn('Firebase not available, skipping Firebase operations');
|
|
||||||
// Still process the database logs but skip Firebase operations
|
|
||||||
await this.processDeviceStatusLogs(addDeviceStatusDto);
|
|
||||||
return { message: 'Device status processed without Firebase' };
|
|
||||||
}
|
|
||||||
|
|
||||||
const dataRef = ref(
|
const dataRef = ref(
|
||||||
this.firebaseDb,
|
this.firebaseDb,
|
||||||
`device-status/${addDeviceStatusDto.deviceUuid}`,
|
`device-status/${addDeviceStatusDto.deviceUuid}`,
|
||||||
@ -314,127 +301,4 @@ export class DeviceStatusFirebaseService {
|
|||||||
const snapshot: DataSnapshot = await get(dataRef);
|
const snapshot: DataSnapshot = await get(dataRef);
|
||||||
return snapshot.val();
|
return snapshot.val();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async processDeviceStatusLogs(addDeviceStatusDto: AddDeviceStatusDto): Promise<void> {
|
|
||||||
if (this.isDevEnv) {
|
|
||||||
// Save logs to your repository
|
|
||||||
const newLogs = addDeviceStatusDto.log.properties.map((property) => {
|
|
||||||
return this.deviceStatusLogRepository.create({
|
|
||||||
deviceId: addDeviceStatusDto.deviceUuid,
|
|
||||||
deviceTuyaId: addDeviceStatusDto.deviceTuyaUuid,
|
|
||||||
productId: addDeviceStatusDto.log.productId,
|
|
||||||
log: addDeviceStatusDto.log,
|
|
||||||
code: property.code,
|
|
||||||
value: property.value,
|
|
||||||
eventId: addDeviceStatusDto.log.dataId,
|
|
||||||
eventTime: new Date(property.time).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?.log?.properties?.find(
|
|
||||||
(status) => energyCodes.has(status.code),
|
|
||||||
);
|
|
||||||
|
|
||||||
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?.log?.properties?.find(
|
|
||||||
(status) => occupancyCodes.has(status.code),
|
|
||||||
);
|
|
||||||
|
|
||||||
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,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} 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,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
14639
package-lock.json
generated
Normal file
14639
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user