BiFace_Server_Config_API/api/controllers/ApiController.js
2023-09-21 14:14:46 +07:00

119 lines
3.8 KiB
JavaScript

'use strict';
const util = require('util');
const fs = require('fs');
exports.ReadAPIConfig = async function (req, res) {
var path = '/root/monitor/setup/apiConfig.txt';
if (fs.existsSync(path)) {
var text = fs.readFileSync(path, 'utf8');
res.send({ status: true, data: text });
} else {
res.send({ status: false });
}
};
exports.SaveAPIConfig = async function (req, res) {
fs.writeFileSync('/root/monitor/setup/apiConfig.txt', JSON.stringify(req.body));
const { execSync } = require('child_process');
execSync('sudo systemctl enable S905X_BI');
execSync('sudo systemctl start S905X_BI');
res.send(req.body);
};
exports.ReadEngineConfig = async function (req, res) {
var path = '/root/monitor/setup/engine.json';
if (fs.existsSync(path)) {
var text = fs.readFileSync(path, 'utf8');
if (text === "") {
const { execSync } = require('child_process');
execSync('rm /root/monitor/setup/engine.json');
execSync('sudo systemctl stop S905X_BI');
execSync('sudo systemctl disable S905X_BI');
}
res.send({ status: true, data: text });
} else {
res.send({ status: false });
}
};
exports.SaveEngineConfig = async function (req, res) {
fs.writeFileSync(req.body.path + "/data/config.json", req.body.configEngine);
fs.writeFileSync('/root/monitor/setup/engine.json', JSON.stringify(req.body.config));
const { execSync } = require('child_process');
execSync('sudo systemctl restart S905X_BI');
res.send(req.body);
};
exports.ReadConfig = async function (req, res) {
var path = req.body.path + "/data/config.json";
if (fs.existsSync(path)) {
var text = fs.readFileSync(path, 'utf8');
res.send({ status: true, data: text });
} else {
res.send({ status: false });
}
};
exports.Reset = async function (req, res) {
console.log("Reset " + req.body.path);
const { execSync } = require('child_process');
execSync('sudo systemctl stop S905X_BI');
execSync('sudo systemctl disable S905X_BI');
try {
execSync('rm /root/monitor/setup/engine.json');
} catch (err) {
//console.log(err);
}
if (req.body.path !== "") {
try {
execSync('rm -rf ' + req.body.path);
} catch (err) {
//console.log(err);
}
}
res.send({ status: true });
};
exports.Reboot = async function (req, res) {
const { execSync } = require('child_process');
execSync('sudo reboot now');
res.send({ status: true });
};
exports.ControlEngine = async function (req, res) {
const { execSync } = require('child_process');
if (req.body.action == "off")
execSync('sudo systemctl stop S905X_BI');
else
execSync('sudo systemctl start S905X_BI');
res.send({ status: true });
};
exports.EngineStatus = async function (req, res) {
const { execSync } = require('child_process');
try {
execSync('pgrep FaceProArm');
res.send({ status: true });
} catch (error) {
console.log(error);
res.send({ status: false });
}
};
exports.SetupMedia = async function (req, res) {
const { execSync } = require('child_process');
var text = fs.readFileSync('/root/monitor/setup/engine.json', 'utf8');
var json = JSON.parse(text);
var folderEngine = json.data.engines[0].name;
try {
execSync('rm -rf /root/monitor/engine/' + folderEngine + '/data/audio');
execSync('ln -s /var/www/html/BiFace_Server_Lite/web/data/uploads /root/monitor/engine/' + folderEngine + '/data/bg/uploads');
execSync('ln -s /var/www/html/BiFace_Server_Lite/web/data/audio /root/monitor/engine/' + folderEngine + '/data/audio');
} catch (error) {
}
res.send({ status: true });
};