199 lines
5.4 KiB
C++
199 lines
5.4 KiB
C++
/*
|
|
* @Author: your name
|
|
* @Date: 2022-02-08 15:59:33
|
|
* @LastEditors: your name
|
|
* @LastEditTime: 2022-02-16 10:39:05
|
|
* @Description: file content
|
|
* @FilePath: \lirs\code\MyYaml\MyYaml.h
|
|
*
|
|
* Copyright © 2022 <Shandong Matrix Software Engineering Co., Ltd>
|
|
*/
|
|
#ifndef MYYAML_H_
|
|
#define MYYAML_H_
|
|
|
|
|
|
#include <mutex>
|
|
#include <fstream>
|
|
#include "yaml-cpp/yaml.h"
|
|
#include "Log.h"
|
|
|
|
namespace ai_matrix
|
|
{
|
|
|
|
// 基础控制参数
|
|
struct BaseConfig
|
|
{
|
|
// 股道名称
|
|
std::string strTrackName;
|
|
// 测试模式
|
|
bool bTestModel;
|
|
// 连接模式
|
|
int iConnectType;
|
|
// 是否上传识别结果
|
|
bool bUpResult;
|
|
// 是否存在磁钢
|
|
bool bHaveMagnetSteel;
|
|
// 火车进厂的磁钢顺序
|
|
std::string strMagnetSteelOrder;
|
|
// 原始数据存储目录
|
|
std::string strRfidPath;
|
|
// 日志文件目录
|
|
std::string strLogPath;
|
|
// 识别结果目录
|
|
std::string strResultPath;
|
|
// 日志存储天数
|
|
int iResultSaveDays;
|
|
};
|
|
|
|
// 日志参数
|
|
struct LogConfig{
|
|
// 日志级别[DEBUG, INFO, WARN, ERROR, FATAL]
|
|
std::string strLevel;
|
|
};
|
|
|
|
// 串口通讯参数
|
|
struct SerialConfig
|
|
{
|
|
// 串口名
|
|
std::string strComName;
|
|
// 波特率
|
|
int iBaud;
|
|
// 数据位
|
|
int iDataBits;
|
|
// 校验位
|
|
std::string strParityBits;
|
|
// 停止位
|
|
int iStopBits;
|
|
};
|
|
|
|
// 网口通讯参数
|
|
struct EthernetConfig
|
|
{
|
|
// 串口服务器IP
|
|
std::string strIp;
|
|
// 端口号
|
|
int iPort;
|
|
};
|
|
|
|
// web服务器参数
|
|
struct HttpServerConfig
|
|
{
|
|
// 使用状态
|
|
bool bIsUse;
|
|
// 获取接口授权地址
|
|
std::string strIp;
|
|
// 通讯端口
|
|
int iPort;
|
|
// 获取接口授权地址
|
|
std::string strTokenUrl;
|
|
// 识别结果上传地址
|
|
std::string strUpResultUrl;
|
|
// 接口用户名
|
|
std::string strUserName;
|
|
// 接口密码
|
|
std::string strPassword;
|
|
};
|
|
|
|
// 视频识别辅助来车状态检测
|
|
struct VideoAuxiliaryConfig
|
|
{
|
|
// 是否使用外部接口辅助开关识别
|
|
bool bUseVideoAuxiliary;
|
|
// 服务器IP
|
|
std::string strIp;
|
|
// 端口号
|
|
int iPort;
|
|
// 延迟上传的节数
|
|
int iDelayedUpload;
|
|
};
|
|
|
|
class Config final
|
|
{
|
|
public:
|
|
static Config *GetIns();
|
|
|
|
// 读yaml文件
|
|
int readYaml(std::string &strPath);
|
|
// 写yaml文件
|
|
int writeYaml();
|
|
std::string getStringValue(const char *pszKey, const YAML::Node *pConfig = nullptr) const;
|
|
int getIntValue(const char *pszKey, const YAML::Node *pConfig = nullptr) const;
|
|
bool getBoolValue(const char *pszKey, const YAML::Node *pConfig = nullptr) const;
|
|
float getFloatValue(const char *pszKey, const YAML::Node *pConfig = nullptr) const;
|
|
std::string getPathValue(const char *pszKey, const YAML::Node *pConfig =nullptr) const;
|
|
|
|
// 初始化运行数据
|
|
// RunningData initRunningData();
|
|
// RunningData setRunningData(const RunningData runningData);
|
|
// RunningData getRunningData() const;
|
|
|
|
// 获取控制参数
|
|
BaseConfig getBaseConfig() const;
|
|
void setBaseConfig(const BaseConfig baseConfig);
|
|
// 获取日志参数
|
|
LogConfig getLogConfig() const;
|
|
void setLogConfig(const LogConfig logConfig);
|
|
// 串口通讯参数
|
|
SerialConfig getSerialConfig() const;
|
|
void setSerialConfig(const SerialConfig serialConfig);
|
|
// 网口通讯参数
|
|
EthernetConfig getEthernetConfig() const;
|
|
void setEthernetConfig(const EthernetConfig ethernetConfig);
|
|
// 获取web服务器参数
|
|
HttpServerConfig getHttpServerConfig() const;
|
|
void setHttpServerConfig(const HttpServerConfig httpServerConfig);
|
|
// 获取FTP服务器参数
|
|
VideoAuxiliaryConfig getVideoAuxiliaryConfig() const;
|
|
void setVideoAuxiliaryConfig(const VideoAuxiliaryConfig videoAuxiliaryConfig);
|
|
|
|
YAML::Node config_;
|
|
|
|
private:
|
|
Config() = default;
|
|
Config(const Config &) = delete;
|
|
Config(Config &&) = delete;
|
|
Config &operator=(const Config &) = delete;
|
|
Config &operator=(Config &&) = delete;
|
|
~Config() = default;
|
|
|
|
static Config *pInstance_;
|
|
static std::mutex mx_; //锁,保证线程安全
|
|
std::string strConfigYamlPath_;
|
|
|
|
|
|
// 控制参数
|
|
BaseConfig baseConfig_;
|
|
|
|
// 日志参数
|
|
LogConfig logConfig_;
|
|
|
|
// 串口通讯参数
|
|
SerialConfig serialConfig_;
|
|
|
|
// 网口通讯参数
|
|
EthernetConfig ethernetConfig_;
|
|
|
|
// web服务器参数
|
|
HttpServerConfig httpServerConfig_;
|
|
|
|
// ftp服务器参数
|
|
VideoAuxiliaryConfig videoAuxiliaryConfig_;
|
|
|
|
//定义一个嵌套类,负责释放内存,操作系统自动完成,不用担心内存泄露
|
|
class GarbageCollector
|
|
{
|
|
public:
|
|
~GarbageCollector()
|
|
{
|
|
if (Config::pInstance_)
|
|
{
|
|
delete Config::pInstance_;
|
|
Config::pInstance_ = nullptr;
|
|
}
|
|
}
|
|
};
|
|
static GarbageCollector gc_;
|
|
};
|
|
}
|
|
|
|
#endif |