重构了与DB的交互逻辑,以及与任务\上位机指令的消息,使用新的loguru库进行日志管理

This commit is contained in:
zjk
2025-04-20 22:42:28 +08:00
parent c5e28cee54
commit 06226b6704
35 changed files with 5430 additions and 175 deletions
+233
View File
@@ -0,0 +1,233 @@
/************************************************************/
/* NAME: */
/* ORGN: MIT */
/* FILE: PMtest.cpp */
/* DATE: */
/************************************************************/
#include <iterator>
#include "MBUtils.h"
#include "PMtest.h"
#include <iostream>
#include <unistd.h>
#include <termios.h>
using namespace std;
//---------------------------------------------------------
// Constructor
PMtest::PMtest()
{
}
//---------------------------------------------------------
// Destructor
PMtest::~PMtest()
{
}
//---------------------------------------------------------
// Procedure: OnNewMail
#include <fstream>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <map>
std::string getCurrentTimeStamp() {
auto now = std::time(nullptr);
auto tm = *std::localtime(&now);
std::ostringstream oss;
oss << std::put_time(&tm, "%Y%m%d_%H%M%S");
return oss.str();
}
bool PMtest::OnNewMail(MOOSMSG_LIST &NewMail)
{
// 宏名到消息字符串的映射
static const std::map<std::string, std::string> msg_map = {
{PM_TO_DB_STATE, "uPower_pmState_st"},
{PM_TO_DB_CURRENTSTATE, "uPower_currentState_st"},
{PM_TO_DB_DISSTATE, "uPower_disSysState_st"},
{PM_TO_DB_LIBATSTATE, "uPower_liBatState_st"},
{PM_TO_DB_FCSSTATE, "uPower_fcsState_st"},
{PM_TO_DB_SUBDISSTATE, "uPower_secdistState_cmd"},
{PM_TO_DB_EQUIPMENTSTATE, "uPower_equState_fb"}
};
MOOSMSG_LIST::iterator p;
for(p=NewMail.begin(); p!=NewMail.end(); p++) {
CMOOSMsg &msg = *p;
string key = msg.GetKey();
// 只处理PM_TO_DB开头的消息
string sval = msg.GetString();
auto it = msg_map.find(key);
string msg_name = (it != msg_map.end()) ? it->second : key;
string filename = "/home/zjk/project/H100/H100PowerManger/data/msg/" + msg_name + ".json";
ofstream outfile(filename);
if(outfile.is_open()) {
outfile << "{\"" << msg_name << "\":" << sval << "}" << endl;
outfile.close();
}
}
return(true);
}
//---------------------------------------------------------
// Procedure: OnConnectToServer
bool PMtest::OnConnectToServer()
{
RegisterVariables();
return(true);
}
//---------------------------------------------------------
// Procedure: Iterate()
// happens AppTick times per second
// 添加非阻塞键盘检测函数
int kbhit() {
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF) {
ungetc(ch, stdin);
return 1;
}
return 0;
}
bool PMtest::Iterate()
{
handleDBToPMMsgs();
//非阻塞检测键盘输入
if(kbhit()) {
char c = getchar(); // 修改为getchar()
if(c == 'a') {
//发送DB_TO_PM_MISSION消息
std::cout << "发送DB_TO_PM_MISSION消息" << std::endl;
string filename = "/home/zjk/project/H100/H100PowerManger/data/testmsg/uPlan_taskStart_cmd.json";
string content;
if(readJsonFromFile(filename, content)) {
Notify(DB_TO_PM_MISSION, content);
}
}
else if (c == 's')
{
//发送DB_TO_PM_MISSION消息
std::cout << "发送DB_TO_PM_MISSION消息" << std::endl;
string filename = "/home/zjk/project/H100/H100PowerManger/data/testmsg/uPower_sysState_cmd.json";
string content;
if(readJsonFromFile(filename, content)) {
Notify(DB_TO_PM_MSCMD, content);
}
}
}
return(true);
}
//---------------------------------------------------------
// Procedure: readJsonFromFile
// 从文件中读取json内容
bool PMtest::readJsonFromFile(string file, string& content)
{
ifstream infile(file);
if(!infile.is_open()) {
return false;
}
content.assign((istreambuf_iterator<char>(infile)),
(istreambuf_iterator<char>()));
infile.close();
return true;
}
//---------------------------------------------------------
// Procedure: handleDBToPMMsgs
// 处理所有DB_TO_PM消息
void PMtest::handleDBToPMMsgs()
{
// DB_TO_PM消息类型映射表
static const vector<pair<string, string>> db_to_pm_msgs = {
// {DB_TO_PM_MSCMD, "uPower_sysState_cmd"},
{DB_TO_PM_EQUIPMENTCMD, "uPower_equState_cmd"},
{DB_TO_PM_SUBDISSTATE, "uDevice_secdistState_st"},
{DB_TO_PM_DEEPTH, "uDevice_depth_st"},
// {DB_TO_PM_MISSION, "uPlan_taskStart_cmd"}
};
for (const auto& msg : db_to_pm_msgs) {
string filename = "/home/zjk/project/H100/H100PowerManger/data/testmsg/" + msg.second + ".json";
string content;
if(readJsonFromFile(filename, content)) {
Notify(msg.first, content);
}
}
}
//---------------------------------------------------------
// Procedure: OnStartUp()
// happens before connection is open
bool PMtest::OnStartUp()
{
list<string> sParams;
m_MissionReader.EnableVerbatimQuoting(false);
if(m_MissionReader.GetConfiguration(GetAppName(), sParams)) {
list<string>::iterator p;
for(p=sParams.begin(); p!=sParams.end(); p++) {
string line = *p;
string param = tolower(biteStringX(line, '='));
string value = line;
if(param == "foo") {
//handled
}
else if(param == "bar") {
//handled
}
}
}
RegisterVariables();
return(true);
}
//---------------------------------------------------------
// Procedure: RegisterVariables
void PMtest::RegisterVariables()
{
Register(PM_TO_DB_STATE, 0);
Register(PM_TO_DB_CURRENTSTATE, 0);
Register(PM_TO_DB_DISSTATE, 0);
Register(PM_TO_DB_LIBATSTATE, 0);
Register(PM_TO_DB_FCSSTATE, 0);
Register(PM_TO_DB_SUBDISSTATE, 0);
Register(PM_TO_DB_EQUIPMENTSTATE, 0);
}