Skip to content

Commit

Permalink
feat: implemented web socket route to update devices
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLzq committed Dec 26, 2023
1 parent a244af6 commit 03eac8b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
41 changes: 32 additions & 9 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions src/database/postgres/queries/device.ts
Original file line number Diff line number Diff line change
@@ -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 }
1 change: 1 addition & 0 deletions src/database/postgres/queries/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './device'
export * from './sensorData'
28 changes: 28 additions & 0 deletions src/network/socket/routes/device.ts
Original file line number Diff line number Diff line change
@@ -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 }
1 change: 1 addition & 0 deletions src/network/socket/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './device'
export * from './takePhoto'

0 comments on commit 03eac8b

Please sign in to comment.