mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
Merge pull request #429 from SyncrowIOT/add-queue-event-handler
Add queue event handler
This commit is contained in:
@ -76,6 +76,28 @@ export class DeviceStatusFirebaseService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async addDeviceStatusToOurDb(
|
||||||
|
addDeviceStatusDto: AddDeviceStatusDto,
|
||||||
|
): Promise<AddDeviceStatusDto | null> {
|
||||||
|
try {
|
||||||
|
const device = await this.getDeviceByDeviceTuyaUuid(
|
||||||
|
addDeviceStatusDto.deviceTuyaUuid,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (device?.uuid) {
|
||||||
|
return await this.createDeviceStatusInOurDb({
|
||||||
|
deviceUuid: device.uuid,
|
||||||
|
...addDeviceStatusDto,
|
||||||
|
productType: device.productDevice.prodType,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Return null if device not found or no UUID
|
||||||
|
return null;
|
||||||
|
} catch (error) {
|
||||||
|
// Handle the error silently, perhaps log it internally or ignore it
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
async addDeviceStatusToFirebase(
|
async addDeviceStatusToFirebase(
|
||||||
addDeviceStatusDto: AddDeviceStatusDto,
|
addDeviceStatusDto: AddDeviceStatusDto,
|
||||||
): Promise<AddDeviceStatusDto | null> {
|
): Promise<AddDeviceStatusDto | null> {
|
||||||
@ -211,6 +233,13 @@ export class DeviceStatusFirebaseService {
|
|||||||
return existingData;
|
return existingData;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Return the updated data
|
||||||
|
const snapshot: DataSnapshot = await get(dataRef);
|
||||||
|
return snapshot.val();
|
||||||
|
}
|
||||||
|
async createDeviceStatusInOurDb(
|
||||||
|
addDeviceStatusDto: AddDeviceStatusDto,
|
||||||
|
): Promise<any> {
|
||||||
// Save logs to your repository
|
// Save logs to your repository
|
||||||
const newLogs = addDeviceStatusDto.log.properties.map((property) => {
|
const newLogs = addDeviceStatusDto.log.properties.map((property) => {
|
||||||
return this.deviceStatusLogRepository.create({
|
return this.deviceStatusLogRepository.create({
|
||||||
@ -269,8 +298,5 @@ export class DeviceStatusFirebaseService {
|
|||||||
addDeviceStatusDto.deviceUuid,
|
addDeviceStatusDto.deviceUuid,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// Return the updated data
|
|
||||||
const snapshot: DataSnapshot = await get(dataRef);
|
|
||||||
return snapshot.val();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ export class SosHandlerService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleSosEvent(devId: string, logData: any): Promise<void> {
|
async handleSosEventFirebase(devId: string, logData: any): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
||||||
deviceTuyaUuid: devId,
|
deviceTuyaUuid: devId,
|
||||||
@ -39,4 +39,28 @@ export class SosHandlerService {
|
|||||||
this.logger.error('Failed to send SOS true value', err);
|
this.logger.error('Failed to send SOS true value', err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async handleSosEventOurDb(devId: string, logData: any): Promise<void> {
|
||||||
|
try {
|
||||||
|
await this.deviceStatusFirebaseService.addDeviceStatusToOurDb({
|
||||||
|
deviceTuyaUuid: devId,
|
||||||
|
status: [{ code: 'sos', value: true }],
|
||||||
|
log: logData,
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
await this.deviceStatusFirebaseService.addDeviceStatusToOurDb({
|
||||||
|
deviceTuyaUuid: devId,
|
||||||
|
status: [{ code: 'sos', value: false }],
|
||||||
|
log: logData,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error('Failed to send SOS false value', err);
|
||||||
|
}
|
||||||
|
}, 2000);
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error('Failed to send SOS true value', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,16 @@ export class TuyaWebSocketService {
|
|||||||
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 (this.sosHandlerService.isSosTriggered(status)) {
|
||||||
|
await this.sosHandlerService.handleSosEventFirebase(devId, logData);
|
||||||
|
} else {
|
||||||
|
// Firebase real-time update
|
||||||
|
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
||||||
|
deviceTuyaUuid: devId,
|
||||||
|
status: status,
|
||||||
|
log: logData,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Push to internal queue
|
// Push to internal queue
|
||||||
this.messageQueue.push({ devId, status, logData });
|
this.messageQueue.push({ devId, status, logData });
|
||||||
@ -93,9 +103,12 @@ export class TuyaWebSocketService {
|
|||||||
try {
|
try {
|
||||||
for (const item of batch) {
|
for (const item of batch) {
|
||||||
if (this.sosHandlerService.isSosTriggered(item.status)) {
|
if (this.sosHandlerService.isSosTriggered(item.status)) {
|
||||||
await this.sosHandlerService.handleSosEvent(item.devId, item.logData);
|
await this.sosHandlerService.handleSosEventOurDb(
|
||||||
|
item.devId,
|
||||||
|
item.logData,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
await this.deviceStatusFirebaseService.addDeviceStatusToOurDb({
|
||||||
deviceTuyaUuid: item.devId,
|
deviceTuyaUuid: item.devId,
|
||||||
status: item.status,
|
status: item.status,
|
||||||
log: item.logData,
|
log: item.logData,
|
||||||
|
Reference in New Issue
Block a user