添加一些基本类
This commit is contained in:
304
base/mycfg.cpp
Normal file
304
base/mycfg.cpp
Normal file
@@ -0,0 +1,304 @@
|
||||
#include "mycfg.h"
|
||||
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonParseError>
|
||||
#include <QJsonValue>
|
||||
#include <QJsonArray>
|
||||
#include "QDebug"
|
||||
#include "base/file.h"
|
||||
#include <QApplication>
|
||||
#include <QCryptographicHash>
|
||||
|
||||
|
||||
|
||||
// 配置路径
|
||||
#define CFG_PATH QString("/home/root/config/")
|
||||
//#define CFG_PATH qApp->applicationDirPath()+"/config/"
|
||||
|
||||
// 配置文件
|
||||
#define CFG_FILE_NAME CFG_PATH+"cfg.json"
|
||||
#define CHECK_CFG_FILE_NAME cfg_->def_check_cfg
|
||||
|
||||
// 自启动路径
|
||||
#define AUTO_START_PATH "/usr/local/QDesktop-fb"
|
||||
|
||||
// 方案配置文件关键字段,用于生成默认方案文件名
|
||||
#define CHECK_CFG_FILE_KEY "checker_ye_cfg"
|
||||
// mcu程序关键字段,用于生成默认程序文件名
|
||||
#define MCU_APP_FILE_KEY "JQChecker"
|
||||
// shell脚本关键字段,用于运行shell脚本
|
||||
#define SHELL_FILE_KEY "checker_app_pre"
|
||||
// 程序信息
|
||||
#define APP_INFO_FILE "info.json"
|
||||
// 判断脚本
|
||||
#define APP_JUDGE_FILE "judge.lua"
|
||||
|
||||
// 软件版本
|
||||
#define SOFT_VERSION "unknown"
|
||||
#define HARD_VERSION "unknown"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
QJsonArray intlist_to_jarray(QList<int> intl)
|
||||
{
|
||||
QJsonArray a;
|
||||
for(int i=0;i<intl.size();i++){
|
||||
a.append(intl[i]);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
|
||||
class app_info{
|
||||
public:
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
mycfg::mycfg()
|
||||
{
|
||||
}
|
||||
|
||||
void mycfg::to_class(mystring str)
|
||||
{
|
||||
QJsonParseError err;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(str.toUtf8(),&err);
|
||||
QJsonObject j;
|
||||
if(err.error != QJsonParseError::NoError)
|
||||
{
|
||||
qWarning()<<"parse json failed:"<<err.errorString();
|
||||
}else{
|
||||
QJsonObject j=doc.object();
|
||||
}
|
||||
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");
|
||||
def_check_cfg=j.value("def_check_cfg").toString("checker_ye_cfg.json");
|
||||
def_mcu_app=j.value("def_mcu_app").toString("slave_app.pkt");
|
||||
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);
|
||||
def_lua_judge=j.value("lua_judge").toString("judge.lua");
|
||||
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"));
|
||||
}
|
||||
|
||||
mystring mycfg::to_json()
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 初始化环境,建立工作目录,复制程序文件
|
||||
void mycfg::init_env()
|
||||
{
|
||||
// 建立工作用文件夹
|
||||
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))
|
||||
{
|
||||
qInfo()<<"release file "<<file.fileName()<<endl;
|
||||
file.open(QIODevice::ReadWrite);
|
||||
file.write(d);
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
// 设置默认方案配置文件
|
||||
if(name.contains(CHECK_CFG_FILE_KEY,Qt::CaseSensitive))
|
||||
{
|
||||
def_check_cfg=CFG_PATH+name;
|
||||
}
|
||||
else if(name.contains(MCU_APP_FILE_KEY,Qt::CaseSensitive))
|
||||
{
|
||||
def_mcu_app=CFG_PATH+name;
|
||||
}
|
||||
else if(name.contains(SHELL_FILE_KEY,Qt::CaseSensitive))
|
||||
{
|
||||
def_shell=CFG_PATH+name;
|
||||
}
|
||||
else if(name.right(4)==".lua"){
|
||||
if(name!=APP_JUDGE_FILE){
|
||||
lua_libs.append(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 复制程序文件
|
||||
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))
|
||||
{
|
||||
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)
|
||||
qWarning()<<"remove file "<<file_dst.fileName()<<"err"<<endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!file_dst.exists())
|
||||
{
|
||||
if(!QFile::copy(file_src.fileName(), file_dst.fileName()))
|
||||
{
|
||||
qWarning()<<"copy app failed."<<endl;
|
||||
}
|
||||
}
|
||||
if(file_dst.fileName()!=file_src.fileName())
|
||||
{
|
||||
system("systemctl stop atk-qtapp-start.service");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
mycfg g_syscfg;
|
||||
|
||||
mycfg *syscfg(){
|
||||
return &g_syscfg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user