From 4c0d5b13d18ec9cb445167c633b4f15ad6bef523 Mon Sep 17 00:00:00 2001 From: Apher Date: Mon, 29 Jun 2026 01:19:23 +0000 Subject: [PATCH] Add device id as source and redis connection management --- src/controllers/intent.controller.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/controllers/intent.controller.js b/src/controllers/intent.controller.js index 3169657..c140aaa 100644 --- a/src/controllers/intent.controller.js +++ b/src/controllers/intent.controller.js @@ -3,6 +3,7 @@ const db = models.db; const redis = models.redis; const {sequelize, Sequelize} = db; const Intent = db.intent; +const config = require("../config"); // Get all intents exports.getAllIntents = async (req, res) => { @@ -31,21 +32,25 @@ exports.getIntentCommand = async (req, res) => { console.log(`\n[FALLBACK API] Processing intent.`); + await redis.client.connect().catch(console.error) + const intents = await Intent.findAll(); const intent = intents.find(intent => intent.description === text); if (intent) { const commandId = await redis.client.xAdd( - 'commands', + 'communication', '*', { device_id: device_id, command: intent.command, processed_at: new Date().toISOString(), - source: 'fallback_api', + source: config.DEVICE_ID, } ); + await redis.client.quit(); + console.log(`Intent matched and published to stream: ${commandId}`); return res.json({ @@ -55,17 +60,19 @@ exports.getIntentCommand = async (req, res) => { }); } else { const errorId = await redis.client.xAdd( - 'commands', + 'communication', '*', { device_id: device_id, intent: 'error', command: 'intent_not_found', processed_at: new Date().toISOString(), - source: 'fallback_api' + source: config.DEVICE_ID } ); + await redis.client.quit(); + console.log(`No intent match, error published: ${errorId}`); return res.status(400).json({ @@ -73,10 +80,12 @@ exports.getIntentCommand = async (req, res) => { error: 'intent_not_found', message: `Could not understand: "${text}"`, stream_id: errorId, - source: 'fallback_api', + source: config.DEVICE_ID, }); } } catch (err) { + await redis.client.quit(); + console.error("getIntentCommand error:", err); return res.status(500).send({message: err.message || "Internal server error."});