25#ifndef GOBY_MOOS_MOOS_TRANSLATOR_H
26#define GOBY_MOOS_MOOS_TRANSLATOR_H
38#include <MOOS/libMOOS/Comms/MOOSMsg.h>
39#include <MOOS/libMOOS/Utils/MOOSUtilityFunctions.h>
40#include <boost/lexical_cast.hpp>
41#include <boost/lexical_cast/bad_lexical_cast.hpp>
42#include <google/protobuf/descriptor.h>
45#include "dccl/dynamic_protobuf_manager.h"
66 const std::vector<moos::transitional::DCCLMessageVal>& ref_vals);
70 const std::vector<moos::transitional::DCCLMessageVal>& ref_vals);
74 const std::vector<moos::transitional::DCCLMessageVal>& ref_vals);
96 double lat_origin = std::numeric_limits<double>::quiet_NaN(),
97 double lon_origin = std::numeric_limits<double>::quiet_NaN(),
98 const std::string& modem_id_lookup_path =
"")
100 initialize(lat_origin, lon_origin, modem_id_lookup_path);
101 if (entry.IsInitialized())
107 double lat_origin = std::numeric_limits<double>::quiet_NaN(),
108 double lon_origin = std::numeric_limits<double>::quiet_NaN(),
109 const std::string& modem_id_lookup_path =
"")
111 initialize(lat_origin, lon_origin, modem_id_lookup_path);
115 void clear_entry(
const std::string& protobuf_name) { dictionary_.erase(protobuf_name); }
120 throw(std::runtime_error(
"Duplicate translator entry for " + entry.
protobuf_name()));
124 void add_entry(
const std::set<goby::moos::protobuf::TranslatorEntry>& entries)
126 for (
const auto& entry : entries) {
add_entry(entry); }
132 for (
const auto& entry : entries) {
add_entry(entry); }
136 template <
typename GoogleProtobufMessagePo
inter,
class StringCMOOSMsgMap>
137 GoogleProtobufMessagePointer
moos_to_protobuf(
const StringCMOOSMsgMap& moos_variables,
138 const std::string& protobuf_name);
140 std::multimap<std::string, CMOOSMsg>
144 std::multimap<std::string, CMOOSMsg>
147 const std::map<std::string, goby::moos::protobuf::TranslatorEntry>&
dictionary()
const
153 make_moos_msg(
const std::string& var,
const std::string& str,
bool is_binary,
155 const std::string& pb_name)
159 CMOOSMsg moos_msg(MOOS_NOTIFY, var, str.size(), str.data());
160 moos_msg.SetSourceAux(
169 auto return_double = boost::lexical_cast<double>(str);
170 return CMOOSMsg(MOOS_NOTIFY, var, return_double);
172 catch (boost::bad_lexical_cast&)
174 CMOOSMsg moos_msg(MOOS_NOTIFY, var, str);
175 moos_msg.SetSourceAux(
187 void initialize(
double lat_origin = std::numeric_limits<double>::quiet_NaN(),
188 double lon_origin = std::numeric_limits<double>::quiet_NaN(),
189 const std::string& modem_id_lookup_path =
"");
192 const std::vector<moos::transitional::DCCLMessageVal>& ref_vals);
195 const std::vector<moos::transitional::DCCLMessageVal>& ref_vals);
198 const std::vector<moos::transitional::DCCLMessageVal>& ref_vals);
201 const std::vector<moos::transitional::DCCLMessageVal>& ref_vals);
208 std::map<std::string, goby::moos::protobuf::TranslatorEntry> dictionary_;
215 os <<
"= Begin MOOSTranslator =\n";
220 os <<
"== Begin Entry " << i <<
" ==\n"
221 << it.second.DebugString() <<
"== End Entry " << i <<
" ==\n";
226 os <<
"= End MOOSTranslator =";
241inline std::multimap<std::string, CMOOSMsg>
244 std::map<std::string, goby::moos::protobuf::TranslatorEntry>::const_iterator it =
247 const std::string& pb_name = protobuf_msg.
GetDescriptor()->full_name();
249 if (it == dictionary_.end())
250 throw(std::runtime_error(
"No TranslatorEntry for Protobuf type: " + pb_name));
254 std::multimap<std::string, CMOOSMsg> moos_msgs;
256 bool is_binary =
false;
260 std::string return_string;
268 serialize(&return_string, protobuf_msg);
274 serialize(&return_string, protobuf_msg);
280 serialize(&return_string, protobuf_msg);
286 serialize(&return_string, protobuf_msg);
292 serialize(&return_string, protobuf_msg);
299 serialize(&return_string, protobuf_msg);
325 std::make_pair(moos_var,
make_moos_msg(moos_var, return_string, is_binary,
332inline std::multimap<std::string, CMOOSMsg>
335 std::map<std::string, goby::moos::protobuf::TranslatorEntry>::const_iterator it =
338 const std::string& pb_name = protobuf_msg.
GetDescriptor()->full_name();
340 if (it == dictionary_.end())
341 throw(std::runtime_error(
"No TranslatorEntry for Protobuf type: " + pb_name));
345 std::multimap<std::string, CMOOSMsg> moos_msgs;
347 bool is_binary =
false;
349 for (
int i = 0, n = entry.
create_size(); i < n; ++i)
351 std::string return_string;
359 serialize(&return_string, protobuf_msg);
364 goby::moos::protobuf::TranslatorEntry::
365 TECHNIQUE_PREFIXED_PROTOBUF_TEXT_FORMAT>::serialize(&return_string,
372 serialize(&return_string, protobuf_msg);
378 serialize(&return_string, protobuf_msg);
384 serialize(&return_string, protobuf_msg);
390 goby::moos::protobuf::TranslatorEntry::
391 TECHNIQUE_PREFIXED_PROTOBUF_NATIVE_ENCODED>::serialize(&return_string,
405 serialize(&return_string, protobuf_msg, empty_algorithms,
417 &return_string, protobuf_msg, empty_algorithms, entry.
create(i).
format(),
424 std::make_pair(moos_var, make_moos_msg(moos_var, return_string, is_binary,
433 typedef std::multimap<std::string, CMOOSMsg>::iterator It;
434 std::pair<It, It> p = moos_msgs.equal_range(entry.
trigger().
moos_var());
435 for (
auto it = p.first; it != p.second; ++it) it->second.m_dfTime = MOOSTime();
448template <
typename GoogleProtobufMessagePo
inter,
class StringCMOOSMsgMap>
449GoogleProtobufMessagePointer
451 const std::string& protobuf_name)
453 std::map<std::string, goby::moos::protobuf::TranslatorEntry>::const_iterator it =
454 dictionary_.find(protobuf_name);
456 if (it == dictionary_.end())
457 throw(std::runtime_error(
"No TranslatorEntry for Protobuf type: " + protobuf_name));
461 GoogleProtobufMessagePointer
msg;
466 msg = dccl::DynamicProtobufManager::new_protobuf_message<GoogleProtobufMessagePointer>(
471 throw(std::runtime_error(
"Unknown Protobuf type: " + protobuf_name +
472 "; be sure it is compiled in or directly loaded into the "
473 "dccl::DynamicProtobufManager."));
475 for (
int i = 0, n = entry.
create_size(); i < n; ++i)
478 std::string source_string =
479 (it == moos_variables.end())
481 : (it->second.IsString() ? it->second.GetString()
482 : goby::util::as<std::string>(it->second.GetDouble()));
495 parse(source_string, &*
msg);
507 parse(source_string, &*
msg);
513 parse(source_string, &*
msg);
519 parse(source_string, &*
msg);
static CMOOSMsg make_moos_msg(const std::string &var, const std::string &str, bool is_binary, goby::moos::protobuf::TranslatorEntry::ParserSerializerTechnique technique, const std::string &pb_name)
std::multimap< std::string, CMOOSMsg > protobuf_to_inverse_moos(const google::protobuf::Message &protobuf_msg)
void add_entry(const std::set< goby::moos::protobuf::TranslatorEntry > &entries)
MOOSTranslator(const goby::moos::protobuf::TranslatorEntry &entry=goby::moos::protobuf::TranslatorEntry(), double lat_origin=std::numeric_limits< double >::quiet_NaN(), double lon_origin=std::numeric_limits< double >::quiet_NaN(), const std::string &modem_id_lookup_path="")
std::multimap< std::string, CMOOSMsg > protobuf_to_moos(const google::protobuf::Message &protobuf_msg)
MOOSTranslator(const google::protobuf::RepeatedPtrField< goby::moos::protobuf::TranslatorEntry > &entries, double lat_origin=std::numeric_limits< double >::quiet_NaN(), double lon_origin=std::numeric_limits< double >::quiet_NaN(), const std::string &modem_id_lookup_path="")
void update_utm_datum(double lat_origin, double lon_origin)
void add_entry(const google::protobuf::RepeatedPtrField< goby::moos::protobuf::TranslatorEntry > &entries)
GoogleProtobufMessagePointer moos_to_protobuf(const StringCMOOSMsgMap &moos_variables, const std::string &protobuf_name)
const std::map< std::string, goby::moos::protobuf::TranslatorEntry > & dictionary() const
void add_entry(const goby::moos::protobuf::TranslatorEntry &entry)
void clear_entry(const std::string &protobuf_name)
::goby::moos::protobuf::TranslatorEntry_ParserSerializerTechnique technique() const
const std::string & format() const
const std::string & moos_var() const
const std::string & repeated_delimiter() const
const ::goby::moos::protobuf::TranslatorEntry_CreateParser_Algorithm & algorithm(int index) const
const std::string & moos_var() const
const ::goby::moos::protobuf::TranslatorEntry_PublishSerializer_Algorithm & algorithm(int index) const
const std::string & repeated_delimiter() const
const std::string & format() const
::goby::moos::protobuf::TranslatorEntry_ParserSerializerTechnique technique() const
const std::string & moos_var() const
::goby::moos::protobuf::TranslatorEntry_Trigger_Type type() const
static constexpr Type TRIGGER_PUBLISH
const std::string & protobuf_name() const
static constexpr ParserSerializerTechnique TECHNIQUE_PREFIXED_PROTOBUF_NATIVE_HEX
static constexpr ParserSerializerTechnique TECHNIQUE_PROTOBUF_NATIVE_ENCODED
static constexpr ParserSerializerTechnique TECHNIQUE_FORMAT
static const std::string & ParserSerializerTechnique_Name(T enum_t_value)
static constexpr ParserSerializerTechnique TECHNIQUE_COMMA_SEPARATED_KEY_EQUALS_VALUE_PAIRS
static constexpr ParserSerializerTechnique TECHNIQUE_PROTOBUF_NATIVE_HEX
const ::goby::moos::protobuf::TranslatorEntry_PublishSerializer & publish(int index) const
bool use_short_enum() const
const ::goby::moos::protobuf::TranslatorEntry_CreateParser & create(int index) const
static constexpr ParserSerializerTechnique TECHNIQUE_PREFIXED_PROTOBUF_NATIVE_ENCODED
static constexpr ParserSerializerTechnique TECHNIQUE_PROTOBUF_TEXT_FORMAT
const ::goby::moos::protobuf::TranslatorEntry_Trigger & trigger() const
static constexpr ParserSerializerTechnique TECHNIQUE_PREFIXED_PROTOBUF_TEXT_FORMAT
const Descriptor * GetDescriptor() const
Helpers for MOOS applications for serializing and parsed Google Protocol buffers messages.
TranslatorEntry_ParserSerializerTechnique
void alg_lon2hemisphere_initial(moos::transitional::DCCLMessageVal &val_to_mod)
void alg_to_upper(moos::transitional::DCCLMessageVal &val_to_mod)
std::mutex dynamic_parse_mutex
void alg_angle_0_360(moos::transitional::DCCLMessageVal &angle)
void alg_lon2nmea_lon(moos::transitional::DCCLMessageVal &val_to_mod)
void alg_lat2hemisphere_initial(moos::transitional::DCCLMessageVal &val_to_mod)
void alg_power_to_dB(moos::transitional::DCCLMessageVal &val_to_mod)
void alg_angle_n180_180(moos::transitional::DCCLMessageVal &angle)
void alg_subtract(moos::transitional::DCCLMessageVal &val, const std::vector< moos::transitional::DCCLMessageVal > &ref_vals)
void alg_TSD_to_soundspeed(moos::transitional::DCCLMessageVal &val, const std::vector< moos::transitional::DCCLMessageVal > &ref_vals)
void alg_dB_to_power(moos::transitional::DCCLMessageVal &val_to_mod)
void alg_abs(moos::transitional::DCCLMessageVal &val_to_mod)
void alg_to_lower(moos::transitional::DCCLMessageVal &val_to_mod)
void alg_lat2nmea_lat(moos::transitional::DCCLMessageVal &val_to_mod)
void alg_add(moos::transitional::DCCLMessageVal &val, const std::vector< moos::transitional::DCCLMessageVal > &ref_vals)
void alg_unix_time2nmea_time(moos::transitional::DCCLMessageVal &val_to_mod)
The global namespace for the Goby project.
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::PROTOBUF_NAMESPACE_ID::MessageOptions, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg