#include <config.h>
#include <microsim/MSNet.h>
#include <microsim/MSEdge.h>
#include <microsim/MSRoute.h>
#include <microsim/output/MSDetectorControl.h>
#include <microsim/output/MSRouteProbe.h>
#include <libsumo/TraCIConstants.h>
#include "Helper.h"
#include "RouteProbe.h"
namespace libsumo {
SubscriptionResults RouteProbe::mySubscriptionResults;
ContextSubscriptionResults RouteProbe::myContextSubscriptionResults;
std::vector<std::string>
RouteProbe::getIDList() {
std::vector<std::string> ids;
MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_ROUTEPROBE).insertIDs(ids);
return ids;
}
int
RouteProbe::getIDCount() {
return (int)getIDList().size();
}
std::string
RouteProbe::getEdgeID(const std::string& probeID) {
MSRouteProbe* rp = getRouteProbe(probeID);
return rp->getEdge()->getID();
}
std::string
RouteProbe::sampleLastRouteID(const std::string& probeID) {
MSRouteProbe* rp = getRouteProbe(probeID);
ConstMSRoutePtr route = rp->sampleRoute(true);
if (route == nullptr) {
throw TraCIException("RouteProbe '" + probeID + "' did not collect any routes yet");
}
return route->getID();
}
std::string
RouteProbe::sampleCurrentRouteID(const std::string& probeID) {
MSRouteProbe* rp = getRouteProbe(probeID);
ConstMSRoutePtr route = rp->sampleRoute(false);
if (route == nullptr) {
throw TraCIException("RouteProbe '" + probeID + "' did not collect any routes yet");
}
return route->getID();
}
std::string
RouteProbe::getParameter(const std::string& , const std::string& ) {
return "";
}
LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(RouteProbe)
void
RouteProbe::setParameter(const std::string& , const std::string& , const std::string& ) {
}
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(RouteProbe, ROUTEPROBE)
MSRouteProbe*
RouteProbe::getRouteProbe(const std::string& id) {
MSRouteProbe* rp = dynamic_cast<MSRouteProbe*>(MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_ROUTEPROBE).get(id));
if (rp == nullptr) {
throw TraCIException("Lane area detector '" + id + "' is not known");
}
return rp;
}
std::shared_ptr<VariableWrapper>
RouteProbe::makeWrapper() {
return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
}
bool
RouteProbe::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
switch (variable) {
case TRACI_ID_LIST:
return wrapper->wrapStringList(objID, variable, getIDList());
case ID_COUNT:
return wrapper->wrapInt(objID, variable, getIDCount());
case VAR_ROAD_ID:
return wrapper->wrapString(objID, variable, getEdgeID(objID));
case VAR_SAMPLE_LAST:
return wrapper->wrapString(objID, variable, sampleLastRouteID(objID));
case VAR_SAMPLE_CURRENT:
return wrapper->wrapString(objID, variable, sampleCurrentRouteID(objID));
case libsumo::VAR_PARAMETER:
paramData->readUnsignedByte();
return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
case libsumo::VAR_PARAMETER_WITH_KEY:
paramData->readUnsignedByte();
return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
default:
return false;
}
}
}