Files
checker_host/base/mycfg.cpp

422 lines
10 KiB
C++
Raw Normal View History

2023-11-21 20:28:23 +08:00
#include "mycfg.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonValue>
#include <QJsonArray>
#include "QDebug"
#include "base/file.h"
#include <QApplication>
#include <QCryptographicHash>
2023-11-29 15:36:45 +08:00
#include "complier_info.h"
2023-11-21 20:28:23 +08:00
// 配置路径
2023-11-29 15:36:45 +08:00
#define CFG_PATH mystring("/home/root/config/")
2023-11-27 14:31:00 +08:00
// #define CFG_PATH qApp->applicationDirPath()+"/config/"
2023-11-21 20:28:23 +08:00
// 配置文件
2023-11-27 14:31:00 +08:00
#define CFG_FILE_NAME CFG_PATH + "cfg.json"
2023-11-21 20:28:23 +08:00
// 自启动路径
#define AUTO_START_PATH "/usr/local/QDesktop-fb"
// 方案配置文件关键字段,用于生成默认方案文件名
2023-11-27 14:31:00 +08:00
#define CHECK_CFG_FILE_KEY "checker_ye_cfg"
2023-11-21 20:28:23 +08:00
// mcu程序关键字段,用于生成默认程序文件名
2023-11-27 14:31:00 +08:00
#define MCU_APP_FILE_KEY "JQChecker"
2023-11-21 20:28:23 +08:00
// shell脚本关键字段用于运行shell脚本
2023-11-27 14:31:00 +08:00
#define SHELL_FILE_KEY "checker_app_pre"
2023-11-21 20:28:23 +08:00
// 程序信息
2023-11-27 14:31:00 +08:00
#define APP_INFO_FILE "info.json"
2023-11-21 20:28:23 +08:00
// 判断脚本
2023-11-27 14:31:00 +08:00
#define APP_JUDGE_FILE "judge.lua"
2023-11-21 20:28:23 +08:00
// 软件版本
#define SOFT_VERSION "unknown"
#define HARD_VERSION "unknown"
2023-11-27 14:31:00 +08:00
QList<int> jarray_to_intlist(QJsonValue j)
{
QJsonArray arr;
if (j.isArray())
{
arr = j.toArray();
}
QList<int> intl;
for (int i = 0; i < arr.size(); i++)
{
QJsonValue item = arr.at(i);
intl.append(item.toInt(0));
}
return intl;
2023-11-21 20:28:23 +08:00
}
2023-11-27 14:31:00 +08:00
QList<mystring> jarray_to_strlist(QJsonValue j)
{
QJsonArray arr;
if (j.isArray())
{
arr = j.toArray();
}
QList<mystring> strl;
for (int i = 0; i < arr.size(); i++)
{
QJsonValue item = arr.at(i);
strl.append(item.toString());
}
return strl;
2023-11-21 20:28:23 +08:00
}
QJsonArray intlist_to_jarray(QList<int> intl)
{
2023-11-27 14:31:00 +08:00
QJsonArray a;
for (int i = 0; i < intl.size(); i++)
{
a.append(intl[i]);
}
return a;
2023-11-21 20:28:23 +08:00
}
2023-11-27 14:31:00 +08:00
class app_info
{
2023-11-21 20:28:23 +08:00
public:
2023-11-27 14:31:00 +08:00
app_info(QJsonObject json)
{
build_date = json.value("build_date").toString("default");
hard_version = json.value("hard_version").toString("default");
soft_version = json.value("soft_version").toString("default");
privates = jarray_to_strlist(json.value("private"));
}
mystring build_date;
mystring hard_version;
mystring soft_version;
QList<mystring> privates;
2023-11-21 20:28:23 +08:00
};
mycfg::mycfg()
{
2023-11-29 15:36:45 +08:00
// 加載默認參數
tcp_enable = true;
server_ip = mystring("192.168.10.100");
server_port = 9527;
local_port = 0;
local_ip = mystring("192.168.10.50");
gateway_ip = mystring("192.168.10.1");
netmask = mystring("255.255.255.0");
can_bitrate = 100000;
local_id = 1;
slave_num=1;
mcu_timeout=1000;
debug_ip=mystring("192.168.10.100");
debug_port=5346;
build_date=mystring(BUILD_DATE);
cmd_port=7777;
log_redirect=mystring("console");
soft_version=mystring(SOFT_VERSION);
hard_version=mystring(HARD_VERSION);
def_lua_judge=CFG_PATH+APP_JUDGE_FILE;
use_lua_judge=true;
config_path=CFG_PATH;
device_type=mystring("checker");
moter_count=20000;
uart_bsp=115200;
auto_test=false;
coder_return_mode=1;
slave_addr_start=1;
slave_scheme_ext=0;
init_env();
if(!def_shell.isEmpty())
{
QString str="chmod 777 %1";
system(str.arg(def_shell).toLocal8Bit().data());
system((def_shell).toLocal8Bit().data());
}
reload();
// 自启动时把打印路径设置为文件
if(qApp->applicationFilePath()==AUTO_START_PATH)
{
log_redirect=mystring("file");
}
2023-11-21 20:28:23 +08:00
}
void mycfg::to_class(mystring str)
{
2023-11-27 14:31:00 +08:00
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(str.toUtf8(), &err);
QJsonObject j;
if (err.error != QJsonParseError::NoError)
{
qWarning() << "parse json failed:" << err.errorString();
}
else
{
2023-12-07 18:29:49 +08:00
j = doc.object();
2023-11-27 14:31:00 +08:00
}
tcp_enable = j.value("tcp_enable").toBool(true);
server_ip = j.value("server_ip").toString("192.168.80.80");
server_port = j.value("server_port").toInt(9527);
local_ip = j.value("local_ip").toString("192.168.80.87");
local_port = j.value("local_port").toInt(0);
gateway_ip = j.value("gateway_ip").toString("192.168.80.1");
netmask = j.value("netmask").toString("255.255.255.0");
can_bitrate = j.value("can_bitrate").toInt(200000);
local_id = j.value("local_id").toInt(7);
slave_num = j.value("slave_num").toInt(10);
mcu_timeout = j.value("mcu_timeout").toInt(500);
debug_ip = j.value("debug_ip").toString("192.168.80.80");
debug_port = j.value("debug_port").toInt(12345);
cmd_port = j.value("cmd_port").toInt(7777);
log_redirect = j.value("log_redirect").toString("console");
2023-12-02 11:27:25 +08:00
def_check_cfg = j.value("def_check_cfg").toString(CFG_PATH+"checker_ye_cfg.json");
def_mcu_app = j.value("def_mcu_app").toString(CFG_PATH+"slave_app.pkt");
2023-11-27 14:31:00 +08:00
use_lua_judge = j.value("use_lua_judge").toBool(false);
device_type = j.value("device_type").toString("checker");
moter_count = j.value("moter_count").toInt(20000);
uart_bsp = j.value("uart_bsp").toInt(57600);
2023-12-02 11:27:25 +08:00
def_lua_judge = j.value("lua_judge").toString(CFG_PATH+"judge.lua");
2023-11-27 14:31:00 +08:00
coder_return_mode = j.value("coder_return_mode").toInt(1);
slave_addr_start = j.value("slave_addr_start").toInt(0);
slave_scheme_ext = j.value("slave_scheme_ext").toInt(0);
production_info = jarray_to_intlist(j.value("production_info"));
2023-11-21 20:28:23 +08:00
}
mystring mycfg::to_json()
{
2023-11-27 14:31:00 +08:00
QJsonObject qJsonObject{
{"tcp_enable", tcp_enable},
{"server_ip", server_ip},
{"server_port", server_port},
{"local_ip", local_ip},
{"local_port", local_port},
{"gateway_ip", gateway_ip},
{"netmask", netmask},
{"can_bitrate", can_bitrate},
{"local_id", local_id},
{"slave_num", slave_num},
{"mcu_timeout", mcu_timeout},
{"debug_ip", debug_ip},
{"debug_port", debug_port},
{"cmd_port", cmd_port},
{"log_redirect", log_redirect},
{"def_check_cfg", def_check_cfg},
{"def_mcu_app", def_mcu_app},
{"use_lua_judge", use_lua_judge},
{"device_type", device_type},
{"moter_count", moter_count},
{"uart_bsp", uart_bsp},
{"lua_judge", def_lua_judge},
{"coder_return_mode", coder_return_mode},
{"slave_addr_start", slave_addr_start},
{"slave_scheme_ext", slave_scheme_ext}};
qJsonObject.insert("production_info", intlist_to_jarray(production_info));
QJsonDocument jsonDoc(qJsonObject);
// qDebug() << jsonDoc.toJson()<<endl;
return jsonDoc.toJson();
2023-11-21 20:28:23 +08:00
}
// 初始化环境,建立工作目录,复制程序文件
void mycfg::init_env()
{
2023-11-27 14:31:00 +08:00
// 建立工作用文件夹
QDir path;
if (!path.exists(CFG_PATH))
{
path.mkpath(CFG_PATH);
}
// 把附加文件释放到工作文件夹
app_file app(qApp->applicationFilePath());
QByteArray d;
d = app.get_file(APP_INFO_FILE);
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(d, &err);
if (err.error != QJsonParseError::NoError)
{
qWarning() << "parse json failed:" << err.errorString();
return;
}
QJsonObject j = doc.object();
app_info info(j);
soft_version = info.soft_version;
hard_version = info.hard_version;
QList<mystring> list_files = app.get_file_list();
for (int i = 0; i < list_files.size(); i++)
{
QFile file;
mystring name = list_files[i];
d = app.get_file(name);
file.setFileName(CFG_PATH + name);
if ((!file.exists()) || (file.size() == 0))
{
if (!info.privates.contains(name))
{
2023-12-02 11:27:25 +08:00
qInfo() << "release file " << file.fileName();
2023-11-27 14:31:00 +08:00
file.open(QIODevice::ReadWrite);
file.write(d);
file.close();
}
2023-11-21 20:28:23 +08:00
}
2023-11-27 14:31:00 +08:00
// 设置默认方案配置文件
if (name.contains(CHECK_CFG_FILE_KEY, Qt::CaseSensitive))
2023-11-21 20:28:23 +08:00
{
2023-11-27 14:31:00 +08:00
def_check_cfg = CFG_PATH + name;
2023-11-21 20:28:23 +08:00
}
2023-11-27 14:31:00 +08:00
else if (name.contains(MCU_APP_FILE_KEY, Qt::CaseSensitive))
{
def_mcu_app = CFG_PATH + name;
2023-11-21 20:28:23 +08:00
}
2023-11-27 14:31:00 +08:00
else if (name.contains(SHELL_FILE_KEY, Qt::CaseSensitive))
{
def_shell = CFG_PATH + name;
}
else if (name.right(4) == ".lua")
2023-11-21 20:28:23 +08:00
{
2023-11-27 14:31:00 +08:00
if (name != APP_JUDGE_FILE)
{
lua_libs.append(name);
}
2023-11-21 20:28:23 +08:00
}
2023-11-27 14:31:00 +08:00
}
// 复制程序文件
QFile file_dst;
file_dst.setFileName(AUTO_START_PATH);
QFile file_src;
file_src.setFileName(qApp->applicationFilePath());
if (file_dst.exists())
{
if (file_dst.open(QIODevice::ReadOnly) && file_src.open(QIODevice::ReadOnly))
2023-11-21 20:28:23 +08:00
{
2023-11-27 14:31:00 +08:00
QByteArray c1 = file_src.readAll();
QByteArray c2 = file_dst.readAll();
QByteArray md5_1 = QCryptographicHash::hash(c1, QCryptographicHash::Md5);
QByteArray md5_2 = QCryptographicHash::hash(c2, QCryptographicHash::Md5);
if (md5_1 != md5_2)
{
if (file_dst.remove() != true)
2023-12-02 11:27:25 +08:00
qWarning() << "remove file " << file_dst.fileName() << "err";
2023-11-27 14:31:00 +08:00
}
2023-11-21 20:28:23 +08:00
}
2023-11-27 14:31:00 +08:00
}
if (!file_dst.exists())
{
if (!QFile::copy(file_src.fileName(), file_dst.fileName()))
2023-11-21 20:28:23 +08:00
{
2023-12-02 11:27:25 +08:00
qWarning() << "copy app failed.";
2023-11-21 20:28:23 +08:00
}
2023-11-27 14:31:00 +08:00
}
if (file_dst.fileName() != file_src.fileName())
{
system("systemctl stop atk-qtapp-start.service");
}
2023-11-21 20:28:23 +08:00
}
2023-11-26 23:05:35 +08:00
void mycfg::restart()
{
2023-11-27 14:31:00 +08:00
system("systemctl restart atk-qtapp-start.service");
2023-11-26 23:05:35 +08:00
}
void mycfg::reload()
{
2023-11-27 14:31:00 +08:00
QFile file;
file.setFileName(CFG_FILE_NAME);
if (!file.exists())
{
2023-11-26 23:05:35 +08:00
file.open(QIODevice::ReadWrite);
QTextStream stream(&file);
stream.setCodec("utf-8");
2023-11-27 14:31:00 +08:00
stream << to_json();
file.close();
}
else
{
file.open(QIODevice::ReadOnly);
QTextStream stream(&file);
stream.setCodec("utf-8");
mystring json_str = stream.readAll();
to_class(json_str);
2023-11-26 23:05:35 +08:00
file.close();
2023-11-27 14:31:00 +08:00
}
2023-11-26 23:05:35 +08:00
}
2023-11-27 14:31:00 +08:00
// 把配置写入json文件
void mycfg::save()
{
QFile file;
file.setFileName(CFG_FILE_NAME);
if (file.exists())
{
if (file.remove() != true)
2023-12-02 11:27:25 +08:00
qWarning() << "remove file " << file.fileName() << "err";
2023-11-27 14:31:00 +08:00
}
file.open(QIODevice::ReadWrite);
QTextStream stream(&file);
stream.setCodec("utf-8");
stream << to_json();
file.flush();
file.close();
system((char *)"sync");
}
2023-11-21 20:28:23 +08:00
2023-11-26 23:05:35 +08:00
// 把文件保存到配置路径下
2023-11-27 14:31:00 +08:00
bool mycfg::save_file(mystring name, myarray data)
{
QFile file;
file.setFileName(CFG_PATH + name);
if (file.exists())
{
if (file.remove() != true)
2023-11-26 23:05:35 +08:00
{
2023-12-02 11:27:25 +08:00
qWarning() << "remove file " << file.fileName() << "err";
2023-11-27 14:31:00 +08:00
return false;
2023-11-26 23:05:35 +08:00
}
2023-11-27 14:31:00 +08:00
}
file.open(QIODevice::ReadWrite);
file.write(data);
file.flush();
file.close();
system((char *)"sync");
return true;
2023-11-26 23:05:35 +08:00
}
// 更新生产信息
void mycfg::updata_produc_info(myarray data)
{
2023-11-27 14:31:00 +08:00
production_info.clear();
for (int i = 0; i < data.size(); i++)
{
production_info.append(data[i]);
}
2023-11-26 23:05:35 +08:00
}
void mycfg::updata_produc_info(QList<int> data)
{
2023-11-27 14:31:00 +08:00
production_info.clear();
for (int i = 0; i < data.size(); i++)
{
production_info.append(data[i]);
}
2023-11-26 23:05:35 +08:00
}
2023-11-21 20:28:23 +08:00
2023-11-27 14:31:00 +08:00
QList<int> mycfg::calc_slave_addrs()
{
QList<int> r;
for(int i=0;i<slave_num;i++){
r.append(i+1);
}
return r;
}
2023-11-26 23:05:35 +08:00
mycfg *g_syscfg;
2023-11-21 20:28:23 +08:00
2023-11-27 14:31:00 +08:00
mycfg *syscfg()
{
if (g_syscfg == nullptr)
{
g_syscfg = new mycfg();
}
return g_syscfg;
2023-11-21 20:28:23 +08:00
}