Skip to content

Commit

Permalink
add gps parse
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-du-car committed Nov 8, 2023
1 parent 1013355 commit 25cf9fc
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/v2i-hub/RSUHealthMonitorPlugin/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
{
"key":"RSUOIDConfigMap",
"default":"{\"RSUOIDConfig\":[{\"RsuField\":\"rsuGpsOutpuString\",\"OID\":\"iso.3.6.1.2.1.1.9.1.3.3\"},{\"RsuField\":\"rsuID\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuMibVersion\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuFirmwareVersion\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuManufacturer\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuIFMIndex\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuIFMPsid\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuIFMDsrcMsgId\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuIFMTxMode\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuIFMTxChannel\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuIFMEnable\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuIFMStatus\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuMode\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"}]}",
"default":"{\"RSUOIDConfig\":[{\"RsuField\":\"rsuGpsOutputString\",\"OID\":\"iso.0.15628.4.1.8.5.0\"},{\"RsuField\":\"rsuID\",\"OID\":\"iso.0.15628.4.1.17.4.0\"},{\"RsuField\":\"rsuMibVersion\",\"OID\":\"iso.0.15628.4.1.17.1.0\"},{\"RsuField\":\"rsuFirmwareVersion\",\"OID\":\"iso.0.15628.4.1.17.2.0 \"},{\"RsuField\":\"rsuManufacturer\",\"OID\":\"iso.0.15628.4.1.17.5.0\"},{\"RsuField\":\"rsuIFMIndex\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuIFMPsid\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuIFMDsrcMsgId\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuIFMTxMode\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuIFMTxChannel\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuIFMEnable\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuIFMStatus\",\"OID\":\"iso.3.6.1.2.1.1.7.0\"},{\"RsuField\":\"rsuMode\",\"OID\":\"iso.0.15628.4.1.99.0\"}]}",
"description":"OID (Object Identifier) uniquely identify managed objects in a MIB database."
}
]
Expand Down
112 changes: 79 additions & 33 deletions src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace RSUHealthMonitor
UpdateConfigSettings();
// Send SNMP call to RSU status at configurable interval.
std::thread rsuStatus_t(&RSUHealthMonitorPlugin::PeriodicRSUStatusReq, this);
rsuStatus_t.detach();
rsuStatus_t.join();
}

void RSUHealthMonitorPlugin::UpdateConfigSettings()
Expand Down Expand Up @@ -49,15 +49,16 @@ namespace RSUHealthMonitor
if (json_str.length() == 0)
{
PLOG(logERROR) << "Error updating RSU OID config due to JSON is empty.";
return;
}
try
{
// Example:"{\"RSUOIDConfig\": [{\"RsuField\": \"rsuGpsOutpuString\", \"OID\": \"1.0.15628.4.1.8.5.0\"}, {\"RsuField\": \"rsuID\", \"OID\": \"1.0.15628.4.1.8.5.0\"}] }"
ptree pt;
istringstream iss(json_str);
read_json(iss, pt);
//Clear the RSU OID mapping variable

// Clear the RSU OID mapping variable
_rsuOIDConfigMap.clear();
BOOST_FOREACH (ptree::value_type &child, pt.get_child("RSUOIDConfig"))
{
Expand Down Expand Up @@ -88,37 +89,11 @@ namespace RSUHealthMonitor
while (true)
{
PLOG(logDEBUG) << "RSU status update call at every " << _interval << "seconds!";
Json::Value rsuStatuJson;
//Sending RSU SNMP call for each field as each field has its own OID.
for_each(_rsuOIDConfigMap.begin(), _rsuOIDConfigMap.end(), [this, &rsuStatuJson](RSUOIDConfig &config)
{
try
{
PLOG(logINFO) << "SNMP RSU status call for field:"<< config.field << ", OID: " << config.oid;
snmp_response_obj responseVal;
if(_snmpClientPtr != nullptr)
{
_snmpClientPtr->process_snmp_request(config.oid, request_type::GET, responseVal);
if(responseVal.type == snmp_response_obj::response_type::INTEGER)
{
rsuStatuJson[config.field] = responseVal.val_int;
}
else if(responseVal.type == snmp_response_obj::response_type::STRING)
{
string response_str(responseVal.val_string.begin(), responseVal.val_string.end());
rsuStatuJson[config.field] = response_str;
}
}
}
catch (std::exception &ex)
{
PLOG(logERROR) << "SNMP call failure due to: " << ex.what();
} });
//Broadcast the RSU status info when there are any RSU responses.
if (!rsuStatuJson.empty())

// Broadcast the RSU status info when there are any RSU responses.
string json_str = getRSUstatus();
if (json_str.length() > 0)
{
Json::FastWriter fasterWirter;
string json_str = fasterWirter.write(rsuStatuJson);
tmx::messages::RSUStatusMessage sendRsuStatusMsg;
sendRsuStatusMsg.set_contents(json_str);
BroadcastMessage(sendRsuStatusMsg);
Expand All @@ -127,8 +102,79 @@ namespace RSUHealthMonitor
}
}

string RSUHealthMonitorPlugin::getRSUstatus()
{
Json::Value rsuStatuJson;
// Sending RSU SNMP call for each field as each field has its own OID.
for_each(_rsuOIDConfigMap.begin(), _rsuOIDConfigMap.end(), [this, &rsuStatuJson](RSUOIDConfig &config)
{
try
{
PLOG(logINFO) << "SNMP RSU status call for field:"<< config.field << ", OID: " << config.oid;
snmp_response_obj responseVal;
if(_snmpClientPtr != nullptr)
{
_snmpClientPtr->process_snmp_request(config.oid, request_type::GET, responseVal);
if(responseVal.type == snmp_response_obj::response_type::INTEGER)
{
rsuStatuJson[config.field] = responseVal.val_int;
}
else if(responseVal.type == snmp_response_obj::response_type::STRING)
{
string response_str(responseVal.val_string.begin(), responseVal.val_string.end());
if(boost::iequals("rsuGpsOutputString", config.field))
{
auto gps = ParseGPS(response_str);
rsuStatuJson["rsuGpsOutputStringLatitude"] = gps[0];
rsuStatuJson["rsuGpsOutputStringLongitude"] = gps[1];
}else{
rsuStatuJson[config.field] = response_str;
}
}
}
}
catch (std::exception &ex)
{
PLOG(logERROR) << "SNMP call failure due to: " << ex.what();
} });

if (!rsuStatuJson.empty())
{
Json::FastWriter fasterWirter;
string json_str = fasterWirter.write(rsuStatuJson);
return json_str;
}
return "";
}

std::map<long, long> RSUHealthMonitorPlugin::ParseGPS(const std::string &gps_nmea_data)
{
std::map<long, long> result;
nmea::NMEAParser parser;
nmea::GPSService gps(parser);
try
{
parser.readLine(gps_nmea_data);
std::stringstream ss;
ss << std::setprecision(8) << std::fixed << gps.fix.latitude << std::endl;
auto latitude_str = ss.str();
std::stringstream sss;
sss << std::setprecision(8) << std::fixed << gps.fix.longitude << std::endl;
auto longitude_str = sss.str();
boost::erase_all(longitude_str, ".");
boost::erase_all(latitude_str, ".");
result.insert({std::stol(latitude_str), std::stol(longitude_str)});
}
catch (nmea::NMEAParseError &e)
{
fprintf(stderr, "Error:%s\n", e.message.c_str());
}
return result;
}

RSUHealthMonitorPlugin::~RSUHealthMonitorPlugin()
{
_rsuOIDConfigMap.clear();
}

} // namespace RSUHealthMonitor
Expand Down
14 changes: 14 additions & 0 deletions src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "SNMPClient.h"
#include <jsoncpp/json/json.h>
#include "RSUStatusMessage.h"
#include <nmeaparse/nmea.h>

using namespace tmx::utils;
using namespace std;
Expand Down Expand Up @@ -40,7 +41,20 @@ namespace RSUHealthMonitor
* @param JSON string with RSU OID configuration.
*/
void UpdateRSUOIDConfig(string &json_str);
/**
* @brief Periodically sending SNMP requests to get RSU status info.
*/
void PeriodicRSUStatusReq();
/**
* @brief Sending SNMP requests to get info for each field in the _rsuOIDConfigMap, and return the RSU status in JSON string
*/
string getRSUstatus();
/**
* @brief Parse NMEA GPS sentense and return GPS related data
* @param gps_nmea_data NMEA GPS sentense
* @return map<long, long> A map of latitude and longitude
*/
std::map<long, long> ParseGPS(const std::string &gps_nmea_data);

public:
RSUHealthMonitorPlugin(std::string name);
Expand Down

0 comments on commit 25cf9fc

Please sign in to comment.