mirror of
https://github.com/sergeyyarkov/modbus-logger.git
synced 2026-10-11 02:18:51 +03:00
progress
This commit is contained in:
parent
e2ad549ed1
commit
7779ce96e5
@ -4,134 +4,133 @@ import cp from 'child_process'
|
|||||||
/**
|
/**
|
||||||
* Services
|
* Services
|
||||||
*/
|
*/
|
||||||
import { appService } from "#root/services/index.js";
|
import { appService } from "#root/services/index.js";
|
||||||
|
|
||||||
export const appController = {
|
export const appController = {
|
||||||
/**
|
/**
|
||||||
* Get application config from database
|
* Get application config from database
|
||||||
* @param {import('express').Request} req
|
* @param {import('express').Request} req
|
||||||
* @param {import('express').Response} res
|
* @param {import('express').Response} res
|
||||||
* @param {import('express').NextFunction} next
|
* @param {import('express').NextFunction} next
|
||||||
*/
|
*/
|
||||||
async getConfig(req, res, next) {
|
async getConfig(req, res, next) {
|
||||||
try {
|
try {
|
||||||
const data = await appService.getConfig();
|
const data = await appService.getConfig();
|
||||||
if (!data) return res.status(200).json({ config: null });
|
if (!data) return res.status(200).json({ config: null });
|
||||||
return res.status(200).json({ config: data });
|
return res.status(200).json({ config: data });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(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();
|
|
||||||
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();
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Application memory usage
|
|
||||||
* @param {import('express').Request} req
|
|
||||||
* @param {import('express').Response} res
|
|
||||||
* @param {import('express').NextFunction} next
|
|
||||||
*/
|
|
||||||
memoryUsage(req, res, next) {
|
|
||||||
const memoryUsage = process.memoryUsage();
|
|
||||||
const bytesToMB = (bytes) => bytes / 10**6;
|
|
||||||
Object.keys(memoryUsage).forEach(k => (memoryUsage[k] = `${bytesToMB(memoryUsage[k])}MB`));
|
|
||||||
return res.status(200).json({ data: memoryUsage });
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ping
|
|
||||||
* @param {import('express').Request} req
|
|
||||||
* @param {import('express').Response} res
|
|
||||||
* @param {import('express').NextFunction} next
|
|
||||||
*/
|
|
||||||
ping(req, res, next) {
|
|
||||||
return res.status(200).send('pong');
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send info about latest commit hash
|
|
||||||
* @param {import('express').Request} req
|
|
||||||
* @param {import('express').Response} res
|
|
||||||
* @param {import('express').NextFunction} next
|
|
||||||
*/
|
|
||||||
commitHash(req, res, next) {
|
|
||||||
const hash = cp.execSync('git rev-parse HEAD').toString().trim();
|
|
||||||
return res.status(200).json({ hash })
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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();
|
||||||
|
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();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Application memory usage
|
||||||
|
* @param {import('express').Request} req
|
||||||
|
* @param {import('express').Response} res
|
||||||
|
* @param {import('express').NextFunction} next
|
||||||
|
*/
|
||||||
|
memoryUsage(req, res, next) {
|
||||||
|
const memoryUsage = process.memoryUsage();
|
||||||
|
const bytesToMB = (bytes) => bytes / 10**6;
|
||||||
|
Object.keys(memoryUsage).forEach(k => (memoryUsage[k] = `${bytesToMB(memoryUsage[k])}MB`));
|
||||||
|
return res.status(200).json({ data: memoryUsage });
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ping
|
||||||
|
* @param {import('express').Request} req
|
||||||
|
* @param {import('express').Response} res
|
||||||
|
* @param {import('express').NextFunction} next
|
||||||
|
*/
|
||||||
|
ping(req, res, next) {
|
||||||
|
return res.status(200).send('pong');
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send info about latest commit hash
|
||||||
|
* @param {import('express').Request} req
|
||||||
|
* @param {import('express').Response} res
|
||||||
|
* @param {import('express').NextFunction} next
|
||||||
|
*/
|
||||||
|
commitHash(req, res, next) {
|
||||||
|
const hash = cp.execSync('git rev-parse HEAD').toString().trim();
|
||||||
|
return res.status(200).json({ hash })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -70,7 +70,7 @@
|
|||||||
class="flex flex-col space-y-2"
|
class="flex flex-col space-y-2"
|
||||||
>
|
>
|
||||||
<div>
|
<div>
|
||||||
<label :for="$id('id')"><span class="text-red-500">*</span>Slave ID:</label>
|
<label :for="$id('id')"><span class="text-red-500">*</span>ID устройства:</label>
|
||||||
<input
|
<input
|
||||||
x-model.number="data.id"
|
x-model.number="data.id"
|
||||||
:id="$id('id')"
|
:id="$id('id')"
|
||||||
@ -305,8 +305,8 @@
|
|||||||
<!-- Remove display value -->
|
<!-- Remove display value -->
|
||||||
<button
|
<button
|
||||||
@click="$dispatch('dialog', {
|
@click="$dispatch('dialog', {
|
||||||
title: 'Remove display value',
|
title: 'Удалить отображаемое значение',
|
||||||
body: `Remove display value ${value.name}?`,
|
body: `Удалить ${value.name}?`,
|
||||||
isOpen: true,
|
isOpen: true,
|
||||||
onConfirm: async () => await removeDisplayValue(value.id)
|
onConfirm: async () => await removeDisplayValue(value.id)
|
||||||
});"
|
});"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user