Skip to content

Commit

Permalink
feat: some adjustements for testing locally
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLzq committed Dec 25, 2023
1 parent 580208e commit f3d8896
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/network/mqtt/routes/photo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { z } from 'zod'

import { socketConnection } from 'network/socket'
import { MAIN_TOPIC } from 'utils'
// import { writeFile } from 'fs/promises'

const TOPIC = 'receivePhoto'
const SUB_TOPIC = `${MAIN_TOPIC}/${TOPIC}`
Expand All @@ -29,6 +30,8 @@ const sub = (client: MqttClient) => {
const messageString = message.toString()
subDebug(`Topic: ${topic} - Message received: ${message.toString()}`)

if (!messageString) return

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

Expand All @@ -39,6 +42,15 @@ const sub = (client: MqttClient) => {
}

const { data } = messageValidation
// const base64 = data.base64.split(';base64,').pop()

// if (!base64) return

// const buffer = Buffer.from(base64, 'base64')
// // save the base64 as a file
// writeFile(`./${data.meta.timestamp}.jpeg`, buffer).then(() => {
// subDebug('File saved')
// })
const {
meta: { sensorId }
} = data
Expand Down
10 changes: 6 additions & 4 deletions src/network/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ const start = async () => {
await dbConnection(serverDebug).connect()
})

const jobs = await import('../jobs')
if (process.env.NODE_ENV !== 'local') {
const jobs = await import('../jobs')

;(Object.keys(jobs) as (keyof typeof jobs)[]).forEach(job => {
jobs[job](getClient(serverDebug))
})
;(Object.keys(jobs) as (keyof typeof jobs)[]).forEach(job => {
jobs[job](getClient(serverDebug))
})
}
}

export { start }
10 changes: 7 additions & 3 deletions src/pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ client.on('error', error => {
pubDebug('Error: ', error)
})

client.publish(`${MAIN_TOPIC}/electricConductivity`, 'wea', () => {
pubDebug('Message send')
})
client.publish(
`${MAIN_TOPIC}/takePhoto`,
'Wp82DjwYQVhhLzm3eYomosZTWSq1/1/1',
() => {
pubDebug('Message send')
}
)

0 comments on commit f3d8896

Please sign in to comment.