From b93ee97475412a7bf2a3b8c3485072f45e2b5082 Mon Sep 17 00:00:00 2001 From: AnthonyLzq Date: Mon, 25 Dec 2023 15:42:53 -0500 Subject: [PATCH] fix: clean queue to avoid retain --- .gitignore | 1 + package.json | 2 +- {src => scripts}/pub.ts | 4 ++-- src/network/mqtt/routes/photo.ts | 21 +++++++++++++-------- tsconfig.base.json | 3 ++- tsconfig.json | 3 ++- 6 files changed, 21 insertions(+), 13 deletions(-) rename {src => scripts}/pub.ts (76%) diff --git a/.gitignore b/.gitignore index 8c20674..b471dbb 100644 --- a/.gitignore +++ b/.gitignore @@ -128,3 +128,4 @@ dist # End of https://www.toptal.com/developers/gitignore/api/node,yarn src/**/*.png +*.jpeg \ No newline at end of file diff --git a/package.json b/package.json index 8641af1..6ff1f07 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "release": "standard-version", "test:local": "jest --setupFiles dotenv/config --ci -i", "test:ci": "jest --ci -i", - "pub": "nodemon --exec \"DEBUG=WQ:* ts-node -r dotenv/config src/pub.ts\"" + "pub": "nodemon --exec \"DEBUG=WQ:* ts-node -r dotenv/config scripts/pub.ts\"" }, "author": "AnthonyLzq ", "license": "MIT", diff --git a/src/pub.ts b/scripts/pub.ts similarity index 76% rename from src/pub.ts rename to scripts/pub.ts index d1141ef..d99dee7 100644 --- a/src/pub.ts +++ b/scripts/pub.ts @@ -1,7 +1,7 @@ import debug from 'debug' -import { MAIN_TOPIC } from 'utils' -import { getClient } from './network/mqtt' +import { MAIN_TOPIC } from '../src/utils' +import { getClient } from '../src/network/mqtt' const pubDebug = debug(`${MAIN_TOPIC}:Mqtt:demo:pub`) const client = getClient() diff --git a/src/network/mqtt/routes/photo.ts b/src/network/mqtt/routes/photo.ts index 365ddc4..f390e87 100644 --- a/src/network/mqtt/routes/photo.ts +++ b/src/network/mqtt/routes/photo.ts @@ -4,7 +4,8 @@ import { z } from 'zod' import { socketConnection } from 'network/socket' import { MAIN_TOPIC } from 'utils' -// import { writeFile } from 'fs/promises' + +import { writeFile } from 'fs/promises' const TOPIC = 'receivePhoto' const SUB_TOPIC = `${MAIN_TOPIC}/${TOPIC}` @@ -42,15 +43,19 @@ const sub = (client: MqttClient) => { } const { data } = messageValidation - // const base64 = data.base64.split(';base64,').pop() - // if (!base64) return + if (process.env.NODE_ENV === 'local') { + 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 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 diff --git a/tsconfig.base.json b/tsconfig.base.json index 3aab36d..93b54a7 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -75,7 +75,8 @@ "src", "test", "tests", - "__test__" + "__test__", + "scripts" ], "exclude": [ "node_modules" diff --git a/tsconfig.json b/tsconfig.json index ae5b703..de526a7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,8 @@ "test", "tests", "__test__", - "dist" + "dist", + "scripts" ], "include": [ "src"