重构了与DB的交互逻辑,以及与任务\上位机指令的消息,使用新的loguru库进行日志管理
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
#--------------------------------------------------------
|
||||
# The CMakeLists.txt for: pPMtest
|
||||
# Author(s):
|
||||
#--------------------------------------------------------
|
||||
|
||||
SET(SRC
|
||||
PMtest.cpp
|
||||
PMtest_Info.cpp
|
||||
main.cpp
|
||||
)
|
||||
|
||||
ADD_EXECUTABLE(pPMtest ${SRC})
|
||||
|
||||
TARGET_LINK_LIBRARIES(pPMtest
|
||||
${MOOS_LIBRARIES}
|
||||
mbutil
|
||||
m
|
||||
pthread)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/************************************************************/
|
||||
/* NAME: */
|
||||
/* ORGN: MIT */
|
||||
/* FILE: PMtest.h */
|
||||
/* DATE: */
|
||||
/************************************************************/
|
||||
|
||||
#ifndef PMtest_HEADER
|
||||
#define PMtest_HEADER
|
||||
|
||||
#include "MOOS/libMOOS/MOOSLib.h"
|
||||
#include "json/json.h"
|
||||
#include "string"
|
||||
using namespace std;
|
||||
//能源管理反馈的消息
|
||||
#define PM_TO_DB_STATE "uPower_pmState_st"
|
||||
#define PM_TO_DB_CURRENTSTATE "uPower_currentState_st"
|
||||
#define PM_TO_DB_DISSTATE "uPower_disSysState_st"
|
||||
#define PM_TO_DB_LIBATSTATE "uPower_liBatState_st"
|
||||
#define PM_TO_DB_FCSSTATE "uPower_fcsState_st"
|
||||
|
||||
#define PM_TO_DB_SUBDISSTATE "uPower_secdistState_cmd"
|
||||
#define PM_TO_DB_EQUIPMENTSTATE "uPower_equState_fb"
|
||||
|
||||
//控制消息
|
||||
#define DB_TO_PM_MSCMD "uPower_sysState_cmd"
|
||||
#define DB_TO_PM_EQUIPMENTCMD "uPower_equState_cmd"
|
||||
#define DB_TO_PM_SUBDISSTATE "uDevice_secdistState_st"
|
||||
|
||||
//决策消息
|
||||
#define DB_TO_PM_DEEPTH "uDevice_depth_st"
|
||||
#define DB_TO_PM_MISSION "uPlan_taskStart_cmd"
|
||||
|
||||
class PMtest : public CMOOSApp
|
||||
{
|
||||
public:
|
||||
PMtest();
|
||||
~PMtest();
|
||||
|
||||
protected: // Standard MOOSApp functions to overload
|
||||
bool OnNewMail(MOOSMSG_LIST &NewMail);
|
||||
bool Iterate();
|
||||
bool OnConnectToServer();
|
||||
bool OnStartUp();
|
||||
protected:
|
||||
void RegisterVariables();
|
||||
void handleDBToPMMsgs();
|
||||
int snedFlag;
|
||||
|
||||
private: // Configuration variables
|
||||
|
||||
private: // State variables
|
||||
// 从文件中读取json内容
|
||||
bool readJsonFromFile(string file, string& content);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,115 @@
|
||||
/****************************************************************/
|
||||
/* NAME: */
|
||||
/* ORGN: MIT Cambridge MA */
|
||||
/* FILE: PMtest_Info.cpp */
|
||||
/* DATE: Dec 29th 1963 */
|
||||
/****************************************************************/
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include "PMtest_Info.h"
|
||||
#include "ColorParse.h"
|
||||
#include "ReleaseInfo.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
//----------------------------------------------------------------
|
||||
// Procedure: showSynopsis
|
||||
|
||||
void showSynopsis()
|
||||
{
|
||||
blk("SYNOPSIS: ");
|
||||
blk("------------------------------------ ");
|
||||
blk(" The pPMtest application is used for ");
|
||||
blk(" ");
|
||||
blk(" ");
|
||||
blk(" ");
|
||||
blk(" ");
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
// Procedure: showHelpAndExit
|
||||
|
||||
void showHelpAndExit()
|
||||
{
|
||||
blk(" ");
|
||||
blu("=============================================================== ");
|
||||
blu("Usage: pPMtest file.moos [OPTIONS] ");
|
||||
blu("=============================================================== ");
|
||||
blk(" ");
|
||||
showSynopsis();
|
||||
blk(" ");
|
||||
blk("Options: ");
|
||||
mag(" --alias","=<ProcessName> ");
|
||||
blk(" Launch pPMtest with the given process name ");
|
||||
blk(" rather than pPMtest. ");
|
||||
mag(" --example, -e ");
|
||||
blk(" Display example MOOS configuration block. ");
|
||||
mag(" --help, -h ");
|
||||
blk(" Display this help message. ");
|
||||
mag(" --interface, -i ");
|
||||
blk(" Display MOOS publications and subscriptions. ");
|
||||
mag(" --version,-v ");
|
||||
blk(" Display the release version of pPMtest. ");
|
||||
blk(" ");
|
||||
blk("Note: If argv[2] does not otherwise match a known option, ");
|
||||
blk(" then it will be interpreted as a run alias. This is ");
|
||||
blk(" to support pAntler launching conventions. ");
|
||||
blk(" ");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
// Procedure: showExampleConfigAndExit
|
||||
|
||||
void showExampleConfigAndExit()
|
||||
{
|
||||
blk(" ");
|
||||
blu("=============================================================== ");
|
||||
blu("pPMtest Example MOOS Configuration ");
|
||||
blu("=============================================================== ");
|
||||
blk(" ");
|
||||
blk("ProcessConfig = pPMtest ");
|
||||
blk("{ ");
|
||||
blk(" AppTick = 4 ");
|
||||
blk(" CommsTick = 4 ");
|
||||
blk(" ");
|
||||
blk("} ");
|
||||
blk(" ");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
//----------------------------------------------------------------
|
||||
// Procedure: showInterfaceAndExit
|
||||
|
||||
void showInterfaceAndExit()
|
||||
{
|
||||
blk(" ");
|
||||
blu("=============================================================== ");
|
||||
blu("pPMtest INTERFACE ");
|
||||
blu("=============================================================== ");
|
||||
blk(" ");
|
||||
showSynopsis();
|
||||
blk(" ");
|
||||
blk("SUBSCRIPTIONS: ");
|
||||
blk("------------------------------------ ");
|
||||
blk(" NODE_MESSAGE = src_node=alpha,dest_node=bravo,var_name=FOO, ");
|
||||
blk(" string_val=BAR ");
|
||||
blk(" ");
|
||||
blk("PUBLICATIONS: ");
|
||||
blk("------------------------------------ ");
|
||||
blk(" Publications are determined by the node message content. ");
|
||||
blk(" ");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
// Procedure: showReleaseInfoAndExit
|
||||
|
||||
void showReleaseInfoAndExit()
|
||||
{
|
||||
showReleaseInfo("pPMtest", "gpl");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
/****************************************************************/
|
||||
/* NAME: */
|
||||
/* ORGN: MIT Cambridge MA */
|
||||
/* FILE: PMtest_Info.h */
|
||||
/* DATE: Dec 29th 1963 */
|
||||
/****************************************************************/
|
||||
|
||||
#ifndef PMtest_INFO_HEADER
|
||||
#define PMtest_INFO_HEADER
|
||||
|
||||
void showSynopsis();
|
||||
void showHelpAndExit();
|
||||
void showExampleConfigAndExit();
|
||||
void showInterfaceAndExit();
|
||||
void showReleaseInfoAndExit();
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/************************************************************/
|
||||
/* NAME: */
|
||||
/* ORGN: MIT */
|
||||
/* FILE: main.cpp */
|
||||
/* DATE: */
|
||||
/************************************************************/
|
||||
|
||||
#include <string>
|
||||
#include "MBUtils.h"
|
||||
#include "ColorParse.h"
|
||||
#include "PMtest.h"
|
||||
#include "PMtest_Info.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
string mission_file;
|
||||
string run_command = argv[0];
|
||||
|
||||
for(int i=1; i<argc; i++) {
|
||||
string argi = argv[i];
|
||||
if((argi=="-v") || (argi=="--version") || (argi=="-version"))
|
||||
showReleaseInfoAndExit();
|
||||
else if((argi=="-e") || (argi=="--example") || (argi=="-example"))
|
||||
showExampleConfigAndExit();
|
||||
else if((argi == "-h") || (argi == "--help") || (argi=="-help"))
|
||||
showHelpAndExit();
|
||||
else if((argi == "-i") || (argi == "--interface"))
|
||||
showInterfaceAndExit();
|
||||
else if(strEnds(argi, ".moos") || strEnds(argi, ".moos++"))
|
||||
mission_file = argv[i];
|
||||
else if(strBegins(argi, "--alias="))
|
||||
run_command = argi.substr(8);
|
||||
else if(i==2)
|
||||
run_command = argi;
|
||||
}
|
||||
|
||||
if(mission_file == "")
|
||||
showHelpAndExit();
|
||||
|
||||
cout << termColor("green");
|
||||
cout << "pPMtest launching as " << run_command << endl;
|
||||
cout << termColor() << endl;
|
||||
|
||||
PMtest PMtest;
|
||||
|
||||
PMtest.Run(run_command.c_str(), mission_file.c_str());
|
||||
|
||||
return(0);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
//------------------------------------------------
|
||||
// pPMtest config block
|
||||
|
||||
ProcessConfig = pPMtest
|
||||
{
|
||||
AppTick = 4
|
||||
CommsTick = 4
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user