Skip to content

Commit

Permalink
feat: new sensor data: co2, pm2.5 and pressure. H2S was deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLzq committed Oct 8, 2023
1 parent a0e1c5e commit 562cb75
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 76 deletions.
14 changes: 14 additions & 0 deletions prisma/migrations/20231008184149_new_data/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Warnings:
- You are about to drop the column `h2s` on the `sensorData` table. All the data in the column will be lost.
- Added the required column `co2` to the `sensorData` table without a default value. This is not possible if the table is not empty.
- Added the required column `pm2_5` to the `sensorData` table without a default value. This is not possible if the table is not empty.
- Added the required column `pressure` to the `sensorData` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "sensorData" DROP COLUMN "h2s",
ADD COLUMN "co2" DOUBLE PRECISION NOT NULL,
ADD COLUMN "pm2_5" DOUBLE PRECISION NOT NULL,
ADD COLUMN "pressure" DOUBLE PRECISION NOT NULL;
4 changes: 3 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ model SensorData {
id Int @id @default(autoincrement())
sensorId Int
aq Float
h2s Float
co2 Float
humidity Float
pm2_5 Float
pressure Float
temperature Float
createdAt DateTime? @default(now()) @db.Timestamptz(6)
Expand Down
32 changes: 26 additions & 6 deletions src/database/firebase/queries/realTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ const realTimeDebug = debug(`${MAIN_TOPIC}:Mqtt:FirebaseRealTime`)
const clientData = z.object({
date: z.string(),
aq: z.number(),
h2s: z.number(),
co2: z.number(),
humidity: z.number(),
pm2_5: z.number(),
pressure: z.number(),
temperature: z.number(),
demo: z.boolean().optional()
demo: z.boolean().optional().default(false)
})

declare global {
Expand Down Expand Up @@ -71,10 +73,10 @@ const updateAQ = ({ db, id, moduleId, sensorId, value }: Update) => {
})
}

const updateH2S = ({ db, id, moduleId, sensorId, value }: Update) => {
db.ref(`/ids/${id}/${moduleId}/${sensorId}/h2s`).set(value, error => {
const updateCO2 = ({ db, id, moduleId, sensorId, value }: Update) => {
db.ref(`/ids/${id}/${moduleId}/${sensorId}/co2`).set(value, error => {
if (error) realTimeDebug(`Error: ${error}`)
else realTimeDebug('H2S updated.')
else realTimeDebug('CO2 updated.')
})
}

Expand All @@ -85,6 +87,21 @@ const updateHumidity = ({ db, id, moduleId, sensorId, value }: Update) => {
})
}

// eslint-disable-next-line camelcase
const updatePm2_5 = ({ db, id, moduleId, sensorId, value }: Update) => {
db.ref(`/ids/${id}/${moduleId}/${sensorId}/pm2_5`).set(value, error => {
if (error) realTimeDebug(`Error: ${error}`)
else realTimeDebug('Pm2.5 updated.')
})
}

const updatePressure = ({ db, id, moduleId, sensorId, value }: Update) => {
db.ref(`/ids/${id}/${moduleId}/${sensorId}/pressure`).set(value, error => {
if (error) realTimeDebug(`Error: ${error}`)
else realTimeDebug('Pressure updated.')
})
}

const updateTemperature = ({ db, id, moduleId, sensorId, value }: Update) => {
db.ref(`/ids/${id}/${moduleId}/${sensorId}/temperature`).set(value, error => {
if (error) realTimeDebug(`Error: ${error}`)
Expand Down Expand Up @@ -128,8 +145,11 @@ const listenChangesInDate = ({
export {
getData,
updateAQ,
updateH2S,
updateCO2,
updateHumidity,
// eslint-disable-next-line camelcase
updatePm2_5,
updatePressure,
updateTemperature,
updateDate,
listenChangesInDate
Expand Down
34 changes: 27 additions & 7 deletions src/jobs/fakePub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ const updateData = (client: MqttClient) => {
cb: () => {
clientPublish({
client,
value: parseFloat(randomInInterval(200, 450)),
value: parseFloat(randomInInterval(500, 800)),
id,
moduleId,
sensorId,
topic: 'h2s',
topic: 'co2',
cb: () => {
clientPublish({
client,
Expand All @@ -85,20 +85,40 @@ const updateData = (client: MqttClient) => {
cb: () => {
clientPublish({
client,
value: randomInInterval(23, 27, 1),
value: randomInInterval(40, 70),
id,
moduleId,
sensorId,
topic: 'temperature',
topic: 'pm2.5',
cb: () => {
clientPublish({
client,
value: currentIsoTime,
value: 998,
id,
moduleId,
sensorId,
topic: 'date',
demo: true
topic: 'pressure',
cb: () => {
clientPublish({
client,
value: randomInInterval(23, 27, 1),
id,
moduleId,
sensorId,
topic: 'temperature',
cb: () => {
clientPublish({
client,
value: currentIsoTime,
id,
moduleId,
sensorId,
topic: 'date',
demo: true
})
}
})
}
})
}
})
Expand Down
1 change: 0 additions & 1 deletion src/jobs/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './fakePub'
export * from './ping'
20 changes: 0 additions & 20 deletions src/jobs/ping.ts

This file was deleted.

21 changes: 8 additions & 13 deletions src/network/mqtt/routes/h2s.ts → src/network/mqtt/routes/co2.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import debug from 'debug'
import { MqttClient } from 'mqtt'

import { updateH2S } from 'database'
import { MAIN_TOPIC } from 'utils'
import { socketConnection } from 'network/socket'
// eslint-disable-next-line camelcase
import { updateCO2 } from 'database'
import { MAIN_TOPIC } from 'utils'

const TOPIC = 'h2s'
const TOPIC = 'co2'
const SUB_TOPIC = `${MAIN_TOPIC}/${TOPIC}`

const sub = (client: MqttClient) => {
Expand All @@ -27,21 +28,15 @@ const sub = (client: MqttClient) => {
subDebug(
`Received a ${TOPIC.toUpperCase()} update at: ${new Date().toISOString()}`
)
updateH2S({
db,
id,
moduleId,
value: floatValue,
sensorId
})
socketConnection(subDebug).connect().emit(`${sensorId}/h2s`, floatValue)
updateCO2({ db, moduleId, id, value: floatValue, sensorId })
socketConnection(subDebug).connect().emit(`${sensorId}/pm2.5`, floatValue)
}
})
}

const temperature: Route = {
const pH: Route = {
sub,
SUB_TOPIC
}

export { temperature }
export { pH }
2 changes: 0 additions & 2 deletions src/network/mqtt/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export * from './aq'
export * from './date'
export * from './humidity'
export * from './h2s'
export * from './ping'
export * from './temperature'
26 changes: 0 additions & 26 deletions src/network/mqtt/routes/ping.ts

This file was deleted.

42 changes: 42 additions & 0 deletions src/network/mqtt/routes/pm2.5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import debug from 'debug'
import { MqttClient } from 'mqtt'

import { socketConnection } from 'network/socket'
// eslint-disable-next-line camelcase
import { updatePm2_5 } from 'database'
import { MAIN_TOPIC } from 'utils'

const TOPIC = 'pm2.5'
const SUB_TOPIC = `${MAIN_TOPIC}/${TOPIC}`

const sub = (client: MqttClient) => {
const subDebug = debug(`${MAIN_TOPIC}:Mqtt:${TOPIC}:sub`)
const db = global.__firebase__.database(process.env.FIREBASE_REAL_TIME_DB)

client.subscribe(SUB_TOPIC, error => {
if (!error) subDebug(`Subscribed to Topic: ${SUB_TOPIC}`)
})

client.on('message', (topic, message) => {
if (topic.includes(TOPIC)) {
const [id, moduleId, sensorId, value] = message.toString().split('/')
const floatValue = parseFloat(value)

if (floatValue === 0) return

subDebug(`Topic: ${topic} - Message received: ${message.toString()}`)
subDebug(
`Received a ${TOPIC.toUpperCase()} update at: ${new Date().toISOString()}`
)
updatePm2_5({ db, moduleId, id, value: floatValue, sensorId })
socketConnection(subDebug).connect().emit(`${sensorId}/pm2.5`, floatValue)
}
})
}

const pH: Route = {
sub,
SUB_TOPIC
}

export { pH }
43 changes: 43 additions & 0 deletions src/network/mqtt/routes/pressure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import debug from 'debug'
import { MqttClient } from 'mqtt'

import { socketConnection } from 'network/socket'
import { updatePressure } from 'database'
import { MAIN_TOPIC } from 'utils'

const TOPIC = 'pressure'
const SUB_TOPIC = `${MAIN_TOPIC}/${TOPIC}`

const sub = (client: MqttClient) => {
const subDebug = debug(`${MAIN_TOPIC}:Mqtt:${TOPIC}:sub`)
const db = global.__firebase__.database(process.env.FIREBASE_REAL_TIME_DB)

client.subscribe(SUB_TOPIC, error => {
if (!error) subDebug(`Subscribed to Topic: ${SUB_TOPIC}`)
})

client.on('message', (topic, message) => {
if (topic.includes(TOPIC)) {
const [id, moduleId, sensorId, value] = message.toString().split('/')
const floatValue = parseFloat(value)

if (floatValue === 0) return

subDebug(`Topic: ${topic} - Message received: ${message.toString()}`)
subDebug(
`Received a ${TOPIC.toUpperCase()} update at: ${new Date().toISOString()}`
)
updatePressure({ db, moduleId, id, value: floatValue, sensorId })
socketConnection(subDebug)
.connect()
.emit(`${sensorId}/pressure`, floatValue)
}
})
}

const pH: Route = {
sub,
SUB_TOPIC
}

export { pH }

0 comments on commit 562cb75

Please sign in to comment.