Skip to content

Commit

Permalink
modified: src/v2i-hub/MapPlugin/src/MapPlugin.cpp
Browse files Browse the repository at this point in the history
	modified:   src/v2i-hub/MapPlugin/src/MapPlugin.h
  • Loading branch information
jwillmartin committed Jun 27, 2024
1 parent bc83c6c commit 7de6bbd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 47 deletions.
70 changes: 36 additions & 34 deletions src/v2i-hub/MapPlugin/src/MapPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,23 +236,33 @@ namespace MapPlugin {
}
}

std::string MapPlugin::checkMapContent(std::ifstream &in)
std::string MapPlugin::checkMapContent(const std::string &fn)
{
PLOG(logDEBUG4) << "In MapPlugin :: checkMapContent";
try
{
std::string content((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
in.close();
// Remove any newline characters
content.erase(remove(content.begin(), content.end(), '\n'), content.end());
PLOG(logDEBUG4) << "Map without newline " << content;
std::string payload = removeMessageFrame(content);

return payload;
std::ifstream in(fn.c_str(), std::ios::binary);
if (!in)
{
PLOG(logERROR) << "Failed to open file: " << fn.c_str();
throw std::ios_base::failure("Failed to open file: " + fn);
}
else
{
std::string content((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
in.close();
// Remove any newline characters
content.erase(remove(content.begin(), content.end(), '\n'), content.end());
PLOG(logDEBUG4) << "Map without newline " << content;
std::string payload = removeMessageFrame(content);

return payload;
}
}
catch (const std::ios_base::failure& e)
{
PLOG(logERROR) << "Exception encountered while reading file: " << e.what();
return "";
throw;
}
}

Expand All @@ -279,10 +289,10 @@ namespace MapPlugin {
inType = "ISD";
else if (fn.substr(fn.size() - 4) == ".txt")
inType = "TXT";
else if (fn.substr(fn.size()- 5) == ".uper")
inType ="UPER";
else
else if (fn.substr(fn.size() - 4) == ".xml")
inType = "XML";
else
PLOG(logWARNING) << "Incorrect MapFile extension entered!";

if (inType == "ISD")
{
Expand All @@ -293,30 +303,22 @@ namespace MapPlugin {
}
else if (inType == "TXT")
{
std::ifstream in(fn, std::ios::binary);
if (!in)
{
PLOG(logERROR) << "Failed to open file: " << fn;
}
else
std::string payload = checkMapContent(fn);
byte_stream bytes;
std::istringstream streamableContent(payload);
streamableContent >> bytes;
PLOG(logINFO) << "MAP encoded bytes are " << bytes;
tmx::messages::MapDataMessage *mapMsg = tmx::messages::MapDataEncodedMessage::decode_j2735_message<tmx::messages::codec::uper<tmx::messages::MapDataMessage>>(bytes);

if (mapMsg)
{
std::string payload = checkMapContent(in);
byte_stream bytes;
std::istringstream streamableContent(payload);
streamableContent >> bytes;
PLOG(logINFO) << "MAP encoded bytes are " << bytes;
tmx::messages::MapDataMessage *mapMsg = tmx::messages::MapDataEncodedMessage::decode_j2735_message<tmx::messages::codec::uper<tmx::messages::MapDataMessage>>(bytes);

if (mapMsg)
{
PLOG(logDEBUG) << "Map is " << *mapMsg;
PLOG(logDEBUG) << "Map is " << *mapMsg;

tmx::messages::MapDataEncodedMessage mapEnc;
mapEnc.encode_j2735_message(*mapMsg);
mapFile.set_Bytes(mapEnc.get_payload_str());
tmx::messages::MapDataEncodedMessage mapEnc;
mapEnc.encode_j2735_message(*mapMsg);
mapFile.set_Bytes(mapEnc.get_payload_str());

PLOG(logINFO) << "J2735 message bytes encoded as " << mapFile.get_Bytes();
}
PLOG(logINFO) << "J2735 message bytes encoded as " << mapFile.get_Bytes();
}
}
else if (inType == "XML")
Expand Down
25 changes: 12 additions & 13 deletions src/v2i-hub/MapPlugin/src/MapPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class MapFile: public tmx::message {
std_attribute(this->msg, std::string, InputType, "", );
std_attribute(this->msg, std::string, Bytes, "", );

static tmx::message_tree_type to_tree(const MapFile& m) {
static tmx::message_tree_type to_tree(const MapFile &m) {
return tmx::message::to_tree(static_cast<const tmx::message&>(m));
}

Expand All @@ -89,27 +89,26 @@ class MapPlugin: public tmx::utils::PluginClientClockAware {
void OnMessageReceived(IvpMessage *msg) override;
void OnStateChange(IvpPluginState state) override;

bool LoadMapFiles();
void DebugPrintMapFiles();

std::string enum_to_hex_string();
std::string removeMessageFrame(const std::string &fileContent);
std::string checkMapContent(const std::string &fn);

private:
tmx::messages::J2735MessageFactory factory;
tmx::utils::FrequencyThrottle<int> errThrottle;

std::atomic<int> _mapAction {-1};
std::atomic<bool> _isMapFileNew {false};
std::atomic<bool> _cohdaR63 {false};

std::map<int, MapFile> _mapFiles;
std::mutex data_lock;

tmx::messages::J2735MessageFactory factory;

std::map<int, MapFile> _mapFiles;
int sendFrequency = 1000;
tmx::utils::FrequencyThrottle<int> errThrottle;

std::array<char, 5> mapID_buffer;

bool LoadMapFiles();
void DebugPrintMapFiles();
std::string enum_to_hex_string();
std::string removeMessageFrame(const std::string &fileContent);
std::string checkMapContent(std::ifstream &in);

};

} // namespace MapPlugin

0 comments on commit 7de6bbd

Please sign in to comment.