迁移分支
This commit is contained in:
154
src/pStateManagement/StateManagement.cpp
Normal file
154
src/pStateManagement/StateManagement.cpp
Normal file
@@ -0,0 +1,154 @@
|
||||
/************************************************************/
|
||||
/* NAME: chenlizhi */
|
||||
/* ORGN: MIT */
|
||||
/* FILE: StateManagement.cpp */
|
||||
/* DATE: */
|
||||
/************************************************************/
|
||||
|
||||
#include <iterator>
|
||||
#include "MBUtils.h"
|
||||
#include "StateManagement.h"
|
||||
#include <json/json.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//---------------------------------------------------------
|
||||
// Constructor
|
||||
|
||||
StateManagement::StateManagement()
|
||||
{
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// Destructor
|
||||
|
||||
StateManagement::~StateManagement()
|
||||
{
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// Procedure: OnNewMail
|
||||
|
||||
bool StateManagement::OnNewMail(MOOSMSG_LIST &NewMail)
|
||||
{
|
||||
MOOSMSG_LIST::iterator p;
|
||||
|
||||
for(p=NewMail.begin(); p!=NewMail.end(); p++) {
|
||||
CMOOSMsg &msg = *p;
|
||||
|
||||
#if 1 // Keep these around just for template
|
||||
string key = msg.GetKey();
|
||||
string comm = msg.GetCommunity();
|
||||
double dval = msg.GetDouble();
|
||||
string sval = msg.GetString();
|
||||
string msrc = msg.GetSource();
|
||||
double mtime = msg.GetTime();
|
||||
bool mdbl = msg.IsDouble();
|
||||
bool mstr = msg.IsString();
|
||||
#endif
|
||||
|
||||
Json::Value deviceState;
|
||||
double manualState;
|
||||
double missionState;
|
||||
|
||||
if(key == "uManual_enable_cmd")
|
||||
{
|
||||
manualState = msg.GetDouble();
|
||||
}
|
||||
if(key == "uMission_task_fb")
|
||||
{
|
||||
std::string missionStateString = msg.GetString();
|
||||
std::string errMission;
|
||||
Json::Value missionStateData;
|
||||
std::istringstream issm(missionStateString);
|
||||
Json::CharReaderBuilder builderMission;
|
||||
bool parsingResultMission = Json::parseFromStream(builderMission, issm, &missionStateData, &errMission);
|
||||
if (!parsingResultMission) {
|
||||
std::cerr << "Failed to parse JSON string." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
missionState = missionStateData["state"].asInt();
|
||||
}
|
||||
|
||||
if(fabs(manualState - 1) < 1e-6) //manualState=1
|
||||
{
|
||||
deviceState["opMode"] = opModeLists.external;
|
||||
}
|
||||
else if (fabs(manualState - 0) < 1e-6) //manualState=0
|
||||
{
|
||||
if(missionState == 0)
|
||||
{
|
||||
deviceState["opMode"] = opModeLists.error;
|
||||
}
|
||||
if(missionState == 1)
|
||||
{
|
||||
deviceState["opMode"] = opModeLists.service;
|
||||
}
|
||||
else if((missionState == 3) )
|
||||
{
|
||||
deviceState["opMode"] = opModeLists.maneuver;
|
||||
}
|
||||
}
|
||||
|
||||
Json::StreamWriterBuilder builder;
|
||||
std::string deviceStateString = Json::writeString(builder, deviceState);
|
||||
Notify("uDevice_state_fb", deviceStateString);
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// Procedure: OnConnectToServer
|
||||
|
||||
bool StateManagement::OnConnectToServer()
|
||||
{
|
||||
RegisterVariables();
|
||||
return(true);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// Procedure: Iterate()
|
||||
// happens AppTick times per second
|
||||
|
||||
bool StateManagement::Iterate()
|
||||
{
|
||||
return(true);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// Procedure: OnStartUp()
|
||||
// happens before connection is open
|
||||
|
||||
bool StateManagement::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 StateManagement::RegisterVariables()
|
||||
{
|
||||
// Register("FOOBAR", 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user