Skip to content

Commit

Permalink
feat: added takePhoto socket route and photo mqtt route
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLzq committed Dec 24, 2023
1 parent fdf0051 commit 580208e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/database/firebase/queries/realTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const listenChangesInDate = ({
return
}

if (data && data.demo) {
if (data?.demo) {
realTimeDebug(
`The data for the sensor ${sensorId} was not saved because it is a demo.`
)
Expand Down
1 change: 1 addition & 0 deletions src/network/mqtt/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './aq'
export * from './co2'
export * from './date'
export * from './humidity'
export * from './photo'
// eslint-disable-next-line import/extensions
export * from './pm2.5'
export * from './pressure'
Expand Down
57 changes: 57 additions & 0 deletions src/network/mqtt/routes/photo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import debug from 'debug'
import { MqttClient } from 'mqtt'
import { z } from 'zod'

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

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

const messageSchema = z.object({
base64: z.string(),
meta: z.object({
sensorId: z.string(),
timestamp: z.string(),
gps: z.object({ lat: z.number(), lng: z.number() })
})
})

const sub = (client: MqttClient) => {
const subDebug = debug(`${MAIN_TOPIC}:Mqtt:${TOPIC}:sub`)

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

client.on('message', (topic, message) => {
if (topic.includes(TOPIC)) {
const messageString = message.toString()
subDebug(`Topic: ${topic} - Message received: ${message.toString()}`)

const messageJson = JSON.parse(messageString)
const messageValidation = messageSchema.safeParse(messageJson)

if (!messageValidation.success) {
subDebug(`Message validation failed: ${messageValidation.error}`)

return
}

const { data } = messageValidation
const {
meta: { sensorId }
} = data
socketConnection(subDebug)
.connect()
.emit(`${sensorId}/receivePhoto`, data)
}
})
}

const receivePhoto: Route = {
sub,
SUB_TOPIC
}

export { receivePhoto }
6 changes: 4 additions & 2 deletions src/network/socket/routes/takePhoto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Server } from 'socket.io'

const takePhoto = ({ sensorId }: SensorData, io: Server) => {
const takePhoto = (sensorData: SensorData, io: Server) => {
const { id, moduleId, sensorId } = sensorData

io.on(`${sensorId}/takePhoto`, () => {
global.__mqttClient__.publish('takePhoto', sensorId)
global.__mqttClient__.publish('takePhoto', `${id}/${moduleId}/${sensorId}`)
})
}

Expand Down

0 comments on commit 580208e

Please sign in to comment.