Line data Source code
1 : /****************************************************************************/ 2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3 : // Copyright (C) 2013-2024 German Aerospace Center (DLR) and others. 4 : // This program and the accompanying materials are made available under the 5 : // terms of the Eclipse Public License 2.0 which is available at 6 : // https://www.eclipse.org/legal/epl-2.0/ 7 : // This Source Code may also be made available under the following Secondary 8 : // Licenses when the conditions for such availability set forth in the Eclipse 9 : // Public License 2.0 are satisfied: GNU General Public License, version 2 10 : // or later which is available at 11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 13 : /****************************************************************************/ 14 : /// @file MSDevice_Example.cpp 15 : /// @author Daniel Krajzewicz 16 : /// @author Michael Behrisch 17 : /// @author Jakob Erdmann 18 : /// @date 11.06.2013 19 : /// 20 : // A device which stands as an implementation example and which outputs movereminder calls 21 : /****************************************************************************/ 22 : #include <config.h> 23 : 24 : #include <utils/common/StringUtils.h> 25 : #include <utils/options/OptionsCont.h> 26 : #include <utils/iodevices/OutputDevice.h> 27 : #include <utils/vehicle/SUMOVehicle.h> 28 : #include <microsim/MSNet.h> 29 : #include <microsim/MSLane.h> 30 : #include <microsim/MSEdge.h> 31 : #include <microsim/MSVehicle.h> 32 : #include "MSDevice_Tripinfo.h" 33 : #include "MSDevice_Example.h" 34 : 35 : 36 : // =========================================================================== 37 : // method definitions 38 : // =========================================================================== 39 : // --------------------------------------------------------------------------- 40 : // static initialisation methods 41 : // --------------------------------------------------------------------------- 42 : void 43 36322 : MSDevice_Example::insertOptions(OptionsCont& oc) { 44 36322 : oc.addOptionSubTopic("Example Device"); 45 72644 : insertDefaultAssignmentOptions("example", "Example Device", oc); 46 : 47 36322 : oc.doRegister("device.example.parameter", new Option_Float(0.0)); 48 72644 : oc.addDescription("device.example.parameter", "Example Device", TL("An exemplary parameter which can be used by all instances of the example device")); 49 36322 : } 50 : 51 : 52 : void 53 4655777 : MSDevice_Example::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) { 54 4655777 : OptionsCont& oc = OptionsCont::getOptions(); 55 9311554 : if (equippedByDefaultAssignmentOptions(oc, "example", v, false)) { 56 : // build the device 57 : // get custom vehicle parameter 58 : double customParameter2 = -1; 59 192 : if (v.getParameter().hasParameter("example")) { 60 : try { 61 12 : customParameter2 = StringUtils::toDouble(v.getParameter().getParameter("example", "-1")); 62 0 : } catch (...) { 63 0 : WRITE_WARNINGF(TL("Invalid value '%' for vehicle parameter 'example'"), v.getParameter().getParameter("example", "-1")); 64 0 : } 65 : 66 : } else { 67 90 : std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'example'. Using default of " << customParameter2 << "\n"; 68 : } 69 : // get custom vType parameter 70 : double customParameter3 = -1; 71 192 : if (v.getVehicleType().getParameter().hasParameter("example")) { 72 : try { 73 12 : customParameter3 = StringUtils::toDouble(v.getVehicleType().getParameter().getParameter("example", "-1")); 74 0 : } catch (...) { 75 0 : WRITE_WARNINGF(TL("Invalid value '%' for vType parameter 'example'"), v.getVehicleType().getParameter().getParameter("example", "-1")); 76 0 : } 77 : 78 : } else { 79 90 : std::cout << "vehicle '" << v.getID() << "' does not supply vType parameter 'example'. Using default of " << customParameter3 << "\n"; 80 : } 81 96 : MSDevice_Example* device = new MSDevice_Example(v, "example_" + v.getID(), 82 192 : oc.getFloat("device.example.parameter"), 83 : customParameter2, 84 96 : customParameter3); 85 96 : into.push_back(device); 86 : } 87 4655777 : } 88 : 89 : void 90 0 : MSDevice_Example::cleanup() { 91 : // cleaning up global state (if any) 92 0 : } 93 : 94 : // --------------------------------------------------------------------------- 95 : // MSDevice_Example-methods 96 : // --------------------------------------------------------------------------- 97 96 : MSDevice_Example::MSDevice_Example(SUMOVehicle& holder, const std::string& id, 98 96 : double customValue1, double customValue2, double customValue3) : 99 : MSVehicleDevice(holder, id), 100 96 : myCustomValue1(customValue1), 101 96 : myCustomValue2(customValue2), 102 96 : myCustomValue3(customValue3) { 103 288 : std::cout << "initialized device '" << id << "' with myCustomValue1=" << myCustomValue1 << ", myCustomValue2=" << myCustomValue2 << ", myCustomValue3=" << myCustomValue3 << "\n"; 104 96 : } 105 : 106 : 107 192 : MSDevice_Example::~MSDevice_Example() { 108 192 : } 109 : 110 : 111 : bool 112 220 : MSDevice_Example::notifyMove(SUMOTrafficObject& tObject, double /* oldPos */, 113 : double /* newPos */, double newSpeed) { 114 220 : std::cout << "device '" << getID() << "' notifyMove: newSpeed=" << newSpeed << "\n"; 115 220 : if (tObject.isVehicle()) { 116 : SUMOVehicle& veh = static_cast<SUMOVehicle&>(tObject); 117 : // check whether another device is present on the vehicle: 118 220 : MSDevice_Tripinfo* otherDevice = static_cast<MSDevice_Tripinfo*>(veh.getDevice(typeid(MSDevice_Tripinfo))); 119 : if (otherDevice != nullptr) { 120 96 : std::cout << " veh '" << veh.getID() << " has device '" << otherDevice->getID() << "'\n"; 121 : } 122 : } 123 220 : return true; // keep the device 124 : } 125 : 126 : 127 : bool 128 142 : MSDevice_Example::notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) { 129 426 : std::cout << "device '" << getID() << "' notifyEnter: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n"; 130 142 : return true; // keep the device 131 : } 132 : 133 : 134 : bool 135 142 : MSDevice_Example::notifyLeave(SUMOTrafficObject& veh, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) { 136 426 : std::cout << "device '" << getID() << "' notifyLeave: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n"; 137 142 : return true; // keep the device 138 : } 139 : 140 : 141 : void 142 96 : MSDevice_Example::generateOutput(OutputDevice* tripinfoOut) const { 143 96 : if (tripinfoOut != nullptr) { 144 90 : tripinfoOut->openTag("example_device"); 145 180 : tripinfoOut->writeAttr("customValue1", toString(myCustomValue1)); 146 180 : tripinfoOut->writeAttr("customValue2", toString(myCustomValue2)); 147 180 : tripinfoOut->closeTag(); 148 : } 149 96 : } 150 : 151 : std::string 152 0 : MSDevice_Example::getParameter(const std::string& key) const { 153 0 : if (key == "customValue1") { 154 0 : return toString(myCustomValue1); 155 0 : } else if (key == "customValue2") { 156 0 : return toString(myCustomValue2); 157 0 : } else if (key == "meaningOfLife") { 158 0 : return "42"; 159 : } 160 0 : throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'"); 161 : } 162 : 163 : 164 : void 165 0 : MSDevice_Example::setParameter(const std::string& key, const std::string& value) { 166 : double doubleValue; 167 : try { 168 0 : doubleValue = StringUtils::toDouble(value); 169 0 : } catch (NumberFormatException&) { 170 0 : throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'"); 171 0 : } 172 0 : if (key == "customValue1") { 173 0 : myCustomValue1 = doubleValue; 174 : } else { 175 0 : throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'"); 176 : } 177 0 : } 178 : 179 : 180 : /****************************************************************************/