mirror of
https://github.com/sergeyyarkov/modbus-logger.git
synced 2026-10-11 02:18:51 +03:00
102 lines
3.8 KiB
JavaScript
102 lines
3.8 KiB
JavaScript
import db from "#root/config/database.config.js";
|
|
|
|
/**
|
|
* Services
|
|
*/
|
|
import { appService } from "#root/services/index.js";
|
|
|
|
export const appController = {
|
|
/**
|
|
* Get application config from database
|
|
* @param {import('express').Request} req
|
|
* @param {import('express').Response} res
|
|
* @param {import('express').NextFunction} next
|
|
*/
|
|
async getConfig(req, res, next) {
|
|
try {
|
|
const data = await appService.getConfig(db);
|
|
if (!data) return res.status(200).json({ config: null });
|
|
return res.status(200).json({ config: data });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Update application config
|
|
* @param {import('express').Request} req
|
|
* @param {import('express').Response} res
|
|
* @param {import('express').NextFunction} next
|
|
*/
|
|
async updateConfig(req, res, next) {
|
|
try {
|
|
const config = await appService.getConfig(db);
|
|
const payload = req.body;
|
|
|
|
if (!config) await db.run(`INSERT INTO "app_config" (id) VALUES (?)`, [0]);
|
|
|
|
/* Update TCP configuration */
|
|
if (payload.mb_connection_type === "TCP") {
|
|
const { mb_tcp_ip, mb_tcp_port } = payload;
|
|
await db.run(
|
|
`
|
|
UPDATE "app_config" SET id = 0, mb_connection_type = "TCP", mb_tcp_ip = ?, mb_tcp_port = ?, log_interval_ms = ?
|
|
`,
|
|
[mb_tcp_ip, mb_tcp_port, payload.log_interval_ms],
|
|
);
|
|
|
|
return res.status(200).json({ message: "Config Updated." });
|
|
}
|
|
|
|
/* Update RTU configuration */
|
|
if (payload.mb_connection_type === "RTU") {
|
|
const { mb_rtu_path, mb_rtu_baud, mb_rtu_parity, mb_rtu_data_bits, mb_rtu_stop_bits } = payload;
|
|
await db.run(
|
|
`
|
|
UPDATE "app_config" SET id = 0,
|
|
mb_connection_type = "RTU",
|
|
mb_rtu_path = ?,
|
|
mb_rtu_baud = ?,
|
|
mb_rtu_parity = ?,
|
|
mb_rtu_data_bits = ?,
|
|
mb_rtu_stop_bits = ?,
|
|
log_interval_ms = ?
|
|
`,
|
|
[mb_rtu_path, mb_rtu_baud, mb_rtu_parity, mb_rtu_data_bits, mb_rtu_stop_bits, payload.log_interval_ms],
|
|
);
|
|
return res.status(200).json({ message: "Config updated." });
|
|
}
|
|
|
|
return res.status(400).json({ message: "Undefined Modbus connection type." });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Check whether the application is configured or not
|
|
* @param {import('express').Request} req
|
|
* @param {import('express').Response} res
|
|
* @param {import('express').NextFunction} next
|
|
*/
|
|
async isConfigured(req, res, next) {
|
|
try {
|
|
const config = await appService.getConfig(db);
|
|
if (!config) return res.status(200).json({ is_configured: false });
|
|
|
|
for (const colPrefix of ['mb_tcp', 'mb_rtu']) {
|
|
if (config.mb_connection_type.toLocaleLowerCase() !== colPrefix.substring(3))
|
|
continue;
|
|
const keys = Object.keys(config).filter((k) => k.includes(colPrefix));
|
|
for (const k of keys) {
|
|
if (!config[k])
|
|
return res.status(200).json({ is_configured: false });
|
|
}
|
|
}
|
|
|
|
return res.status(200).json({ is_configured: true });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
}
|
|
} |