134 lines
3.8 KiB
JavaScript
134 lines
3.8 KiB
JavaScript
const models = require("../models");
|
|
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) => {
|
|
try {
|
|
const intents = await Intent.findAll();
|
|
|
|
return res.status(200).send(intents);
|
|
} catch (err) {
|
|
console.error("getIntents error:", err);
|
|
|
|
return res.status(500).send({message: err.message || "Internal server error."});
|
|
}
|
|
};
|
|
|
|
// Get intent command (for now by description)
|
|
exports.getIntentCommand = async (req, res) => {
|
|
try {
|
|
const {text, device_id} = req.body;
|
|
|
|
if (!text || !device_id) {
|
|
return res.status(400).json({
|
|
error: 'Missing required fields: text, device_id',
|
|
});
|
|
}
|
|
|
|
console.log(`\n[Backend Intents] Processing intent.`);
|
|
|
|
await redis.client.connect().catch(console.error)
|
|
|
|
const intent = await Intent.findOne({ where: { description: req.body.text } });
|
|
|
|
if (intent) {
|
|
const commandId = await redis.client.xAdd(
|
|
'communication',
|
|
'*',
|
|
{
|
|
device_id: device_id,
|
|
command: intent.dataValues.command,
|
|
processed_at: new Date().toISOString(),
|
|
source: config.DEVICE_ID,
|
|
}
|
|
);
|
|
|
|
await redis.client.quit();
|
|
|
|
console.log(`Intent matched and published to stream: ${commandId}`);
|
|
|
|
return res.json({
|
|
success: true,
|
|
intent: intent.description,
|
|
command: intent.command
|
|
});
|
|
} else {
|
|
const errorId = await redis.client.xAdd(
|
|
'communication',
|
|
'*',
|
|
{
|
|
device_id: device_id,
|
|
command: 'intent_not_found',
|
|
processed_at: new Date().toISOString(),
|
|
source: config.DEVICE_ID
|
|
}
|
|
);
|
|
|
|
await redis.client.quit();
|
|
|
|
console.log(`No intent match, error published: ${errorId}`);
|
|
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'intent_not_found',
|
|
message: `Could not understand: "${text}"`,
|
|
stream_id: errorId,
|
|
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."});
|
|
}
|
|
};
|
|
|
|
// Create intent
|
|
exports.createIntent = async (req, res) => {
|
|
try {
|
|
const {command, description, type, device_id} = req.body;
|
|
|
|
console.log(`\n[Backend Intents] Creating intent.`)
|
|
|
|
await redis.client.connect().catch(console.error);
|
|
|
|
if (!command || !description || !type || !device_id) {
|
|
return res.status(400).send({message: "Missing required fields: command, description, type, device_id"});
|
|
}
|
|
|
|
const intent = await Intent.create({
|
|
command,
|
|
description,
|
|
type
|
|
});
|
|
|
|
await redis.client.connect().catch(console.error);
|
|
|
|
const commandId = await redis.client.xAdd(
|
|
'communication',
|
|
'*',
|
|
{
|
|
device_id: device_id,
|
|
command: "CREATE_INTENT",
|
|
processed_at: new Date().toISOString(),
|
|
source: config.DEVICE_ID,
|
|
}
|
|
);
|
|
|
|
await redis.client.quit();
|
|
|
|
return res.status(201).send({message: "Intent created successfully.", intent, commandId});
|
|
} catch (err) {
|
|
console.error("createIntent error:", err);
|
|
|
|
return res.status(500).send({message: err.message || "Internal server error."});
|
|
}
|
|
};
|
|
|