mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 10:44:55 +00:00
fix: refactor device status logging and improve property handling in batch processing
This commit is contained in:
@ -1,15 +1,13 @@
|
|||||||
|
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||||
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import {
|
import {
|
||||||
HttpException,
|
HttpException,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
Injectable,
|
Injectable,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { AddDeviceStatusDto } from '../dtos/add.devices-status.dto';
|
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
|
||||||
import { GetDeviceDetailsFunctionsStatusInterface } from 'src/device/interfaces/get.device.interface';
|
|
||||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { firebaseDataBase } from '../../firebase.config';
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
import {
|
import {
|
||||||
Database,
|
Database,
|
||||||
DataSnapshot,
|
DataSnapshot,
|
||||||
@ -17,7 +15,9 @@ import {
|
|||||||
ref,
|
ref,
|
||||||
runTransaction,
|
runTransaction,
|
||||||
} from 'firebase/database';
|
} from 'firebase/database';
|
||||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
import { GetDeviceDetailsFunctionsStatusInterface } from 'src/device/interfaces/get.device.interface';
|
||||||
|
import { firebaseDataBase } from '../../firebase.config';
|
||||||
|
import { AddDeviceStatusDto } from '../dtos/add.devices-status.dto';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DeviceStatusFirebaseService {
|
export class DeviceStatusFirebaseService {
|
||||||
private tuya: TuyaContext;
|
private tuya: TuyaContext;
|
||||||
@ -79,35 +79,53 @@ export class DeviceStatusFirebaseService {
|
|||||||
device: any;
|
device: any;
|
||||||
}[],
|
}[],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const allLogs = [];
|
|
||||||
|
|
||||||
console.log(`🔁 Preparing logs from batch of ${batch.length} items...`);
|
console.log(`🔁 Preparing logs from batch of ${batch.length} items...`);
|
||||||
|
|
||||||
|
const allLogs = [];
|
||||||
|
|
||||||
for (const item of batch) {
|
for (const item of batch) {
|
||||||
const device = item.device;
|
const device = item.device;
|
||||||
|
|
||||||
if (!device?.uuid) {
|
if (!device?.uuid) {
|
||||||
console.log(`⛔ Skipped unknown device: ${item.deviceTuyaUuid}`);
|
console.log(`⛔ Skipped unknown device: ${item.deviceTuyaUuid}`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const logs = item.log.properties.map((property) =>
|
// Determine properties based on environment
|
||||||
|
const properties =
|
||||||
|
this.isDevEnv && Array.isArray(item.log?.properties)
|
||||||
|
? item.log.properties
|
||||||
|
: Array.isArray(item.status)
|
||||||
|
? item.status
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (!properties) {
|
||||||
|
console.log(
|
||||||
|
`⛔ Skipped invalid status/properties for device: ${item.deviceTuyaUuid}`,
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const logs = properties.map((property) =>
|
||||||
this.deviceStatusLogRepository.create({
|
this.deviceStatusLogRepository.create({
|
||||||
deviceId: device.uuid,
|
deviceId: device.uuid,
|
||||||
deviceTuyaId: item.deviceTuyaUuid,
|
deviceTuyaId: item.deviceTuyaUuid,
|
||||||
productId: item.log.productId,
|
productId: device.productDevice?.uuid,
|
||||||
log: item.log,
|
log: item.log,
|
||||||
code: property.code,
|
code: property.code,
|
||||||
value: property.value,
|
value: property.value,
|
||||||
eventId: item.log.dataId,
|
eventId: item.log?.dataId,
|
||||||
eventTime: new Date(property.time).toISOString(),
|
eventTime: new Date(
|
||||||
|
this.isDevEnv ? property.time : property.t,
|
||||||
|
).toISOString(),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
allLogs.push(...logs);
|
allLogs.push(...logs);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`📝 Total logs to insert: ${allLogs.length}`);
|
console.log(`📝 Total logs to insert: ${allLogs.length}`);
|
||||||
|
|
||||||
const insertLogsPromise = (async () => {
|
|
||||||
const chunkSize = 300;
|
const chunkSize = 300;
|
||||||
let insertedCount = 0;
|
let insertedCount = 0;
|
||||||
|
|
||||||
@ -117,9 +135,9 @@ export class DeviceStatusFirebaseService {
|
|||||||
const result = await this.deviceStatusLogRepository
|
const result = await this.deviceStatusLogRepository
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.insert()
|
.insert()
|
||||||
.into('device-status-log') // or use DeviceStatusLogEntity
|
.into('device-status-log')
|
||||||
.values(chunk)
|
.values(chunk)
|
||||||
.orIgnore() // skip duplicates
|
.orIgnore()
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
insertedCount += result.identifiers.length;
|
insertedCount += result.identifiers.length;
|
||||||
@ -131,12 +149,7 @@ export class DeviceStatusFirebaseService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(
|
console.log(`✅ Total logs inserted: ${insertedCount} / ${allLogs.length}`);
|
||||||
`✅ Total logs inserted: ${insertedCount} / ${allLogs.length}`,
|
|
||||||
);
|
|
||||||
})();
|
|
||||||
|
|
||||||
await insertLogsPromise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async addDeviceStatusToFirebase(
|
async addDeviceStatusToFirebase(
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
|
||||||
import TuyaWebsocket from '../../config/tuya-web-socket-config';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||||
import { SosHandlerService } from './sos.handler.service';
|
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||||
|
import { ConfigService } from '@nestjs/config';
|
||||||
import * as NodeCache from 'node-cache';
|
import * as NodeCache from 'node-cache';
|
||||||
|
import TuyaWebsocket from '../../config/tuya-web-socket-config';
|
||||||
|
import { SosHandlerService } from './sos.handler.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TuyaWebSocketService implements OnModuleInit {
|
export class TuyaWebSocketService implements OnModuleInit {
|
||||||
@ -74,7 +74,12 @@ export class TuyaWebSocketService implements OnModuleInit {
|
|||||||
this.client.message(async (ws: WebSocket, message: any) => {
|
this.client.message(async (ws: WebSocket, message: any) => {
|
||||||
try {
|
try {
|
||||||
const { devId, status, logData } = this.extractMessageData(message);
|
const { devId, status, logData } = this.extractMessageData(message);
|
||||||
if (!Array.isArray(logData?.properties)) {
|
// console.log(
|
||||||
|
// `📬 Received message for device: ${devId}, status:`,
|
||||||
|
// status,
|
||||||
|
// logData,
|
||||||
|
// );
|
||||||
|
if (!Array.isArray(status)) {
|
||||||
this.client.ackMessage(message.messageId);
|
this.client.ackMessage(message.messageId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -162,6 +167,8 @@ export class TuyaWebSocketService implements OnModuleInit {
|
|||||||
status: any;
|
status: any;
|
||||||
logData: any;
|
logData: any;
|
||||||
} {
|
} {
|
||||||
|
// console.log('Received message:', message);
|
||||||
|
|
||||||
const payloadData = message.payload.data;
|
const payloadData = message.payload.data;
|
||||||
|
|
||||||
if (this.isDevEnv) {
|
if (this.isDevEnv) {
|
||||||
|
|||||||
Reference in New Issue
Block a user