From 03eac8bd0e5d3355dd47a30d8e1a52f21955cbf0 Mon Sep 17 00:00:00 2001 From: AnthonyLzq Date: Tue, 26 Dec 2023 15:02:25 -0500 Subject: [PATCH] feat: implemented web socket route to update devices --- prisma/schema.prisma | 41 +++++++++++++++++++------ src/database/postgres/queries/device.ts | 18 +++++++++++ src/database/postgres/queries/index.ts | 1 + src/network/socket/routes/device.ts | 28 +++++++++++++++++ src/network/socket/routes/index.ts | 1 + 5 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 src/database/postgres/queries/device.ts create mode 100644 src/network/socket/routes/device.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 1e4673d..6139f6f 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -45,25 +45,48 @@ model Sensor { updatedAt DateTime? @default(now()) @db.Timestamptz(6) deletedAt DateTime? @db.Timestamptz(6) - Module Module @relation(fields: [moduleId], references: [id]) + Devices Device[] SensorData SensorData[] + Module Module @relation(fields: [moduleId], references: [id]) @@map("sensors") } model SensorData { - id Int @id @default(autoincrement()) - sensorId Int - aq Float - co2 Float - humidity Float - pm2_5 Float - pressure Float - temperature Float + id Int @id @default(autoincrement()) + sensorId Int createdAt DateTime? @default(now()) @db.Timestamptz(6) + aq Float @default(0) + h2s Float @default(0) + humidity Float @default(0) + co2 Float @default(0) + pm2_5 Float @default(0) + pressure Float @default(0) + temperature Float @default(0) + Sensor Sensor @relation(fields: [sensorId], references: [id]) @@map("sensorData") } + +model Device { + id Int @id @default(autoincrement()) + sensorId Int + name String @db.VarChar(255) + status DeviceStatus @default(OFF) + + createdAt DateTime? @default(now()) @db.Timestamptz(6) + updatedAt DateTime? @default(now()) @db.Timestamptz(6) + deletedAt DateTime? @db.Timestamptz(6) + + Sensor Sensor @relation(fields: [sensorId], references: [id]) + + @@map("devices") +} + +enum DeviceStatus { + ON + OFF +} diff --git a/src/database/postgres/queries/device.ts b/src/database/postgres/queries/device.ts new file mode 100644 index 0000000..f568e71 --- /dev/null +++ b/src/database/postgres/queries/device.ts @@ -0,0 +1,18 @@ +import { Device } from '@prisma/client' + +import { dbConnection } from '../connection' + +const updateDevices = async (devices: Device[]) => { + const client = await dbConnection().connect() + + for await (const device of devices) { + const { id, ...data } = device + + await client.device.update({ + where: { id }, + data + }) + } +} + +export { updateDevices } diff --git a/src/database/postgres/queries/index.ts b/src/database/postgres/queries/index.ts index 84f4814..5932b77 100644 --- a/src/database/postgres/queries/index.ts +++ b/src/database/postgres/queries/index.ts @@ -1 +1,2 @@ +export * from './device' export * from './sensorData' diff --git a/src/network/socket/routes/device.ts b/src/network/socket/routes/device.ts new file mode 100644 index 0000000..8bf7c64 --- /dev/null +++ b/src/network/socket/routes/device.ts @@ -0,0 +1,28 @@ +import { Socket } from 'socket.io' +import debug from 'debug' +import { Device } from '@prisma/client' + +import { MAIN_TOPIC } from 'utils' +import { updateDevices as ud } from 'database' + +const realTimeDebug = debug(`${MAIN_TOPIC}:WebSockets:updateDevices`) + +const updateDevices = (sensorData: SensorData, socket: Socket) => { + const { sensorId } = sensorData + + realTimeDebug(`Creating route: "${sensorId}/updateDevices"`) + + socket.on(`${sensorId}/updateDevices`, (message: Device[]) => { + ud(message) + .then(() => { + socket.emit(`${sensorId}/updatedDevices`, true) + }) + .catch(error => { + socket.emit(`${sensorId}/updatedDevices`, false) + realTimeDebug('Error updating devices') + realTimeDebug(error) + }) + }) +} + +export { updateDevices } diff --git a/src/network/socket/routes/index.ts b/src/network/socket/routes/index.ts index d426c87..ae3e2fd 100644 --- a/src/network/socket/routes/index.ts +++ b/src/network/socket/routes/index.ts @@ -1 +1,2 @@ +export * from './device' export * from './takePhoto'