diff --git a/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/PlatoonMessage.h b/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/PlatoonMessage.h index cbcd60a9b6a84e8f96885debed0d68eff3b263c4..dff590acd3be3fd817ea6afc10ad64e9a6300320 100644 --- a/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/PlatoonMessage.h +++ b/modules/catkin_ws/src/PlatoonProtocolLib/include/PlatoonProtocolLib/PlatoonMessage.h @@ -33,6 +33,13 @@ inline bool isStandardMessageType(MessageType messageType) return messageTypes::standardMessageTypes.find(messageType) != messageTypes::standardMessageTypes.end(); } +template<typename ... MessageTypes> +bool contains(MessageType type, MessageTypes ... types) +{ + std::unordered_set<MessageType> set{types...}; + return set.find(type) != set.end(); +} + } namespace jsonNames @@ -145,11 +152,16 @@ struct Encoder<platoonProtocol::PlatoonMessage> using json = nlohmann::json; using namespace jsonNames; - auto j = json{{SRC_VEHICLE, message.srcVehicle}, - {DST_VEHICLE, message.dstVehicle}, - {PLATOON_ID, message.platoonId}}; + auto j = json{{SRC_VEHICLE, message.srcVehicle}}; + + using namespace messageTypes; + if (messageTypes::contains(messageType, FV_HEARTBEAT, LV_ACCEPT, REJECT, FV_LEAVE)) + j[DST_VEHICLE] = message.dstVehicle; - if (messageType == messageTypes::LV_BROADCAST) + if (messageTypes::contains(messageType, LV_BROADCAST, FV_HEARTBEAT, LV_ACCEPT, FV_LEAVE)) + j[PLATOON_ID] = message.platoonId; + + if (messageTypes::contains(messageType, LV_BROADCAST)) { j[PLATOON_SPEED] = message.platoonSpeed; j[INNER_PLATOON_DISTANCE] = message.innerPlatoonDistance; @@ -184,10 +196,15 @@ struct Decoder<platoonProtocol::PlatoonMessage> using namespace jsonNames; message.srcVehicle = j.at(SRC_VEHICLE).get<VehicleId>(); - message.dstVehicle = j.at(DST_VEHICLE).get<VehicleId>(); - message.platoonId = j.at(PLATOON_ID).get<PlatoonId>(); - if (messageType == messageTypes::LV_BROADCAST) + using namespace messageTypes; + if (messageTypes::contains(messageType, FV_HEARTBEAT, LV_ACCEPT, REJECT, FV_LEAVE)) + message.dstVehicle = j.at(DST_VEHICLE).get<VehicleId>(); + + if (messageTypes::contains(messageType, LV_BROADCAST, FV_HEARTBEAT, LV_ACCEPT, FV_LEAVE)) + message.platoonId = j.at(PLATOON_ID).get<VehicleId>(); + + if (messageTypes::contains(messageType, LV_BROADCAST)) { message.platoonSpeed = j.at(PLATOON_SPEED).get<PlatoonSpeed>(); message.innerPlatoonDistance = j.at(INNER_PLATOON_DISTANCE).get<InnerPlatoonDistance>();