| 1 |
/* |
| 2 |
RailControl - Model Railway Control Software |
| 3 |
|
| 4 |
Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org |
| 5 |
|
| 6 |
RailControl is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published by the |
| 8 |
Free Software Foundation; either version 3, or (at your option) any |
| 9 |
later version. |
| 10 |
|
| 11 |
RailControl is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 |
See the GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with RailControl; see the file LICENCE. If not see |
| 18 |
<http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <ctime> |
| 22 |
#include <map> |
| 23 |
#include <string> |
| 24 |
|
| 25 |
#include "DataModel/LocoBase.h" |
| 26 |
#include "DataModel/Relation.h" |
| 27 |
#include "DataModel/Route.h" |
| 28 |
#include "Manager.h" |
| 29 |
#include "Utils/Utils.h" |
| 30 |
|
| 31 |
using std::map; |
| 32 |
using std::string; |
| 33 |
using std::to_string; |
| 34 |
|
| 35 |
namespace DataModel |
| 36 |
{ |
| 37 |
Route::Route(Manager* manager, const std::string& serialized) |
| 38 |
: LockableItem(), |
| 39 |
manager(manager), |
| 40 |
executeAtUnlock(false) |
| 41 |
{ |
| 42 |
Deserialize(serialized); |
| 43 |
Track* track = manager->GetTrack(fromTrack); |
| 44 |
if (!track) |
| 45 |
{ |
| 46 |
return; |
| 47 |
} |
| 48 |
track->AddRoute(this); |
| 49 |
} |
| 50 |
|
| 51 |
std::string Route::Serialize() const |
| 52 |
{ |
| 53 |
std::string str; |
| 54 |
str = "objectType=Route;"; |
| 55 |
str += LayoutItem::Serialize(); |
| 56 |
str += ";" + LockableItem::Serialize(); |
| 57 |
str += ";delay=" + to_string(delay); |
| 58 |
str += ";lastused=" + to_string(lastUsed); |
| 59 |
str += ";counter=" + to_string(counter); |
| 60 |
str += ";automode=" + to_string(automode); |
| 61 |
if (automode == AutomodeNo) |
| 62 |
{ |
| 63 |
return str; |
| 64 |
} |
| 65 |
str += ";fromTrack=" + to_string(fromTrack); |
| 66 |
str += ";fromorientation=" + to_string(fromOrientation); |
| 67 |
str += ";toTrack=" + to_string(toTrack); |
| 68 |
str += ";toorientation=" + to_string(toOrientation); |
| 69 |
str += ";speed=" + to_string(speed); |
| 70 |
str += ";feedbackIdReduced=" + to_string(feedbackIdReduced); |
| 71 |
str += ";reduceddelay=" + to_string(reducedDelay); |
| 72 |
str += ";feedbackIdCreep=" + to_string(feedbackIdCreep); |
| 73 |
str += ";creepdelay=" + to_string(creepDelay); |
| 74 |
str += ";feedbackIdStop=" + to_string(feedbackIdStop); |
| 75 |
str += ";stopdelay=" + to_string(stopDelay); |
| 76 |
str += ";feedbackIdOver=" + to_string(feedbackIdOver); |
| 77 |
str += ";pushpull=" + to_string(pushpull); |
| 78 |
str += ";propulsion=" + to_string(propulsion); |
| 79 |
str += ";traintype=" + to_string(trainType); |
| 80 |
str += ";mintrainlength=" + to_string(minTrainLength); |
| 81 |
str += ";maxtrainlength=" + to_string(maxTrainLength); |
| 82 |
str += ";waitafterrelease=" + to_string(waitAfterRelease); |
| 83 |
str += ";followuproute=" + to_string(followUpRoute); |
| 84 |
return str; |
| 85 |
} |
| 86 |
|
| 87 |
bool Route::Deserialize(const std::string& serialized) |
| 88 |
{ |
| 89 |
map<string,string> arguments; |
| 90 |
ParseArguments(serialized, arguments); |
| 91 |
string objectType = Utils::Utils::GetStringMapEntry(arguments, "objectType"); |
| 92 |
if (objectType.compare("Route") != 0) |
| 93 |
{ |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
LayoutItem::Deserialize(arguments); |
| 98 |
LockableItem::Deserialize(arguments); |
| 99 |
|
| 100 |
delay = static_cast<Delay>(Utils::Utils::GetIntegerMapEntry(arguments, "delay", DefaultDelay)); |
| 101 |
lastUsed = Utils::Utils::GetIntegerMapEntry(arguments, "lastused", 0); |
| 102 |
counter = Utils::Utils::GetIntegerMapEntry(arguments, "counter", 0); |
| 103 |
automode = static_cast<Automode>(Utils::Utils::GetBoolMapEntry(arguments, "automode", AutomodeNo)); |
| 104 |
if (automode == AutomodeNo) |
| 105 |
{ |
| 106 |
fromTrack = TrackNone; |
| 107 |
fromOrientation = OrientationRight; |
| 108 |
toTrack = TrackNone; |
| 109 |
toOrientation = OrientationRight; |
| 110 |
speed = SpeedTravel; |
| 111 |
feedbackIdReduced = FeedbackNone; |
| 112 |
reducedDelay = 0; |
| 113 |
feedbackIdCreep = FeedbackNone; |
| 114 |
creepDelay = 0; |
| 115 |
feedbackIdStop = FeedbackNone; |
| 116 |
stopDelay = 0; |
| 117 |
feedbackIdOver = FeedbackNone; |
| 118 |
pushpull = PushpullTypeBoth; |
| 119 |
propulsion = PropulsionAll; |
| 120 |
trainType = TrainTypeAll; |
| 121 |
minTrainLength = 0; |
| 122 |
maxTrainLength = 0; |
| 123 |
waitAfterRelease = 0; |
| 124 |
followUpRoute = RouteNone; |
| 125 |
return true; |
| 126 |
} |
| 127 |
fromTrack = static_cast<Length>(Utils::Utils::GetIntegerMapEntry(arguments, "fromTrack", TrackNone)); |
| 128 |
fromOrientation = static_cast<Orientation>(Utils::Utils::GetBoolMapEntry(arguments, "fromorientation", OrientationRight)); |
| 129 |
toTrack = static_cast<Length>(Utils::Utils::GetIntegerMapEntry(arguments, "toTrack", TrackNone)); |
| 130 |
toOrientation = static_cast<Orientation>(Utils::Utils::GetBoolMapEntry(arguments, "toorientation", OrientationRight)); |
| 131 |
speed = static_cast<Speed>(Utils::Utils::GetIntegerMapEntry(arguments, "speed", SpeedTravel)); |
| 132 |
feedbackIdReduced = Utils::Utils::GetIntegerMapEntry(arguments, "feedbackIdReduced", FeedbackNone); |
| 133 |
reducedDelay = Utils::Utils::GetIntegerMapEntry(arguments, "reduceddelay", 0); |
| 134 |
feedbackIdCreep = Utils::Utils::GetIntegerMapEntry(arguments, "feedbackIdCreep", FeedbackNone); |
| 135 |
creepDelay = Utils::Utils::GetIntegerMapEntry(arguments, "creepdelay", 0); |
| 136 |
feedbackIdStop = Utils::Utils::GetIntegerMapEntry(arguments, "feedbackIdStop", FeedbackNone); |
| 137 |
stopDelay = Utils::Utils::GetIntegerMapEntry(arguments, "stopdelay", 0); |
| 138 |
feedbackIdOver = Utils::Utils::GetIntegerMapEntry(arguments, "feedbackIdOver", FeedbackNone); |
| 139 |
pushpull = static_cast<PushpullType>(Utils::Utils::GetIntegerMapEntry(arguments, "pushpull", PushpullTypeBoth)); |
| 140 |
propulsion = static_cast<Propulsion>(Utils::Utils::GetIntegerMapEntry(arguments, "propulsion", propulsion)); |
| 141 |
trainType = static_cast<TrainType>(Utils::Utils::GetIntegerMapEntry(arguments, "traintype", trainType)); |
| 142 |
minTrainLength = static_cast<Length>(Utils::Utils::GetIntegerMapEntry(arguments, "mintrainlength", 0)); |
| 143 |
maxTrainLength = static_cast<Length>(Utils::Utils::GetIntegerMapEntry(arguments, "maxtrainlength", 0)); |
| 144 |
waitAfterRelease = Utils::Utils::GetIntegerMapEntry(arguments, "waitafterrelease", 0); |
| 145 |
followUpRoute = Utils::Utils::GetIntegerMapEntry(arguments, "followuproute", RouteNone); |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
void Route::DeleteRelations(std::vector<DataModel::Relation*>& relations) |
| 150 |
{ |
| 151 |
while (!relations.empty()) |
| 152 |
{ |
| 153 |
delete relations.back(); |
| 154 |
relations.pop_back(); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
bool Route::AssignRelations(std::vector<DataModel::Relation*>& relations, const std::vector<DataModel::Relation*>& newRelations) |
| 159 |
{ |
| 160 |
std::lock_guard<std::mutex> Guard(updateMutex); |
| 161 |
if (GetLockState() != LockStateFree) |
| 162 |
{ |
| 163 |
return false; |
| 164 |
} |
| 165 |
DeleteRelations(relations); |
| 166 |
relations = newRelations; |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
bool Route::FromTrackOrientation(Logger::Logger* logger, |
| 171 |
const TrackID trackID, |
| 172 |
const Orientation trackOrientation, |
| 173 |
const LocoBase* locoBase, |
| 174 |
const bool allowLocoTurn) |
| 175 |
{ |
| 176 |
if (!automode) |
| 177 |
{ |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
if (fromTrack != trackID) |
| 182 |
{ |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
const Length locoLength = locoBase->GetLength(); |
| 187 |
if (locoLength < minTrainLength) |
| 188 |
{ |
| 189 |
logger->Debug(Languages::TextTrainIsToShort, GetName()); |
| 190 |
return false; |
| 191 |
} |
| 192 |
if (maxTrainLength > 0 && locoLength > maxTrainLength) |
| 193 |
{ |
| 194 |
logger->Debug(Languages::TextTrainIsToLong, GetName()); |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
const bool locoPushpull = locoBase->GetPushpull(); |
| 199 |
if (pushpull != locoPushpull && pushpull != PushpullTypeBoth) |
| 200 |
{ |
| 201 |
logger->Debug(Languages::TextDifferentPushpullTypes, GetName()); |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
const Propulsion locoPropulsion = locoBase->GetPropulsion(); |
| 206 |
if ((propulsion & locoPropulsion) != locoPropulsion) |
| 207 |
{ |
| 208 |
logger->Debug(Languages::TextDifferentPropulsions, GetName()); |
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
const TrainType locoTrainType = locoBase->GetTrainType(); |
| 213 |
if (!(trainType & locoTrainType)) |
| 214 |
{ |
| 215 |
logger->Debug(Languages::TextDifferentTrainTypes, GetName()); |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
if (fromOrientation == trackOrientation) |
| 220 |
{ |
| 221 |
return true; |
| 222 |
} |
| 223 |
|
| 224 |
if (allowLocoTurn && locoPushpull) |
| 225 |
{ |
| 226 |
Track* trackBase = manager->GetTrack(fromTrack); |
| 227 |
if (trackBase != nullptr) |
| 228 |
{ |
| 229 |
bool allowTrackTurn = trackBase->GetAllowLocoTurn(); |
| 230 |
if (allowTrackTurn == true) |
| 231 |
{ |
| 232 |
return true; |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
logger->Debug(Languages::TextDifferentOrientations, GetName()); |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
bool Route::Execute(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier) |
| 243 |
{ |
| 244 |
const bool isInUse = IsInUse(); |
| 245 |
if (isInUse && (locoBaseIdentifier != GetLocoBase())) |
| 246 |
{ |
| 247 |
logger->Info(Languages::TextRouteIsLocked, GetName()); |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
std::lock_guard<std::mutex> Guard(updateMutex); |
| 252 |
for (auto relation : relationsAtLock) |
| 253 |
{ |
| 254 |
bool retRelation = relation->Execute(logger, locoBaseIdentifier, delay); |
| 255 |
if (!retRelation) |
| 256 |
{ |
| 257 |
return false; |
| 258 |
} |
| 259 |
} |
| 260 |
lastUsed = std::time(nullptr); |
| 261 |
++counter; |
| 262 |
if (isInUse) |
| 263 |
{ |
| 264 |
executeAtUnlock = true; |
| 265 |
} |
| 266 |
|
| 267 |
return true; |
| 268 |
} |
| 269 |
|
| 270 |
bool Route::Reserve(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier) |
| 271 |
{ |
| 272 |
if (manager->Booster() == BoosterStateStop) |
| 273 |
{ |
| 274 |
logger->Debug(Languages::TextBoosterIsTurnedOff); |
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
std::lock_guard<std::mutex> Guard(updateMutex); |
| 279 |
bool ret = LockableItem::Reserve(logger, locoBaseIdentifier); |
| 280 |
if (!ret) |
| 281 |
{ |
| 282 |
return false; |
| 283 |
} |
| 284 |
|
| 285 |
if (automode == AutomodeYes) |
| 286 |
{ |
| 287 |
Track* track = manager->GetTrack(toTrack); |
| 288 |
if (!track |
| 289 |
|| !track->CanSetLocoBaseOrientation(toOrientation) |
| 290 |
|| !track->Reserve(logger, locoBaseIdentifier) |
| 291 |
|| !track->SetLocoBaseOrientation(toOrientation)) |
| 292 |
{ |
| 293 |
logger->Debug(Languages::TextUnableToReserveRouteToTrack, GetName(), track->GetName()); |
| 294 |
ReleaseInternal(logger, locoBaseIdentifier); |
| 295 |
return false; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
for (auto relation : relationsAtLock) |
| 300 |
{ |
| 301 |
bool retRelation = relation->Reserve(logger, locoBaseIdentifier); |
| 302 |
if (!retRelation) |
| 303 |
{ |
| 304 |
const LockableItem* item = relation->GetObject2(); |
| 305 |
const Object* object = dynamic_cast<const Object*>(item); |
| 306 |
const string objectName = object ? object->GetName() : Languages::GetText(Languages::TextUnknownElement); |
| 307 |
logger->Debug(Languages::TextUnableToReserveRouteElement, GetName(), objectName); |
| 308 |
ReleaseInternalWithToTrack(logger, locoBaseIdentifier); |
| 309 |
return false; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
return true; |
| 314 |
} |
| 315 |
|
| 316 |
bool Route::Lock(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier) |
| 317 |
{ |
| 318 |
if (manager->Booster() == BoosterStateStop) |
| 319 |
{ |
| 320 |
logger->Debug(Languages::TextBoosterIsTurnedOff); |
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
std::lock_guard<std::mutex> Guard(updateMutex); |
| 325 |
bool ret = LockableItem::Lock(logger, locoBaseIdentifier); |
| 326 |
if (!ret) |
| 327 |
{ |
| 328 |
return false; |
| 329 |
} |
| 330 |
|
| 331 |
if (automode == AutomodeYes) |
| 332 |
{ |
| 333 |
Track* track = manager->GetTrack(toTrack); |
| 334 |
if (!track || !track->Lock(logger, locoBaseIdentifier)) |
| 335 |
{ |
| 336 |
ReleaseInternal(logger, locoBaseIdentifier); |
| 337 |
return false; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
for (auto relation : relationsAtLock) |
| 342 |
{ |
| 343 |
bool retRelation = relation->Lock(logger, locoBaseIdentifier); |
| 344 |
if (!retRelation) |
| 345 |
{ |
| 346 |
ReleaseInternalWithToTrack(logger, locoBaseIdentifier); |
| 347 |
return false; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
return true; |
| 352 |
} |
| 353 |
|
| 354 |
bool Route::ReleaseInternal(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier) |
| 355 |
{ |
| 356 |
if (executeAtUnlock) |
| 357 |
{ |
| 358 |
for (auto relation : relationsAtUnlock) |
| 359 |
{ |
| 360 |
relation->Execute(logger, locoBaseIdentifier, delay); |
| 361 |
} |
| 362 |
executeAtUnlock = false; |
| 363 |
} |
| 364 |
|
| 365 |
for (auto relation : relationsAtLock) |
| 366 |
{ |
| 367 |
relation->Release(logger, locoBaseIdentifier); |
| 368 |
} |
| 369 |
|
| 370 |
return LockableItem::Release(logger, locoBaseIdentifier); |
| 371 |
} |
| 372 |
|
| 373 |
void Route::ReleaseInternalWithToTrack(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier) |
| 374 |
{ |
| 375 |
Track* track = manager->GetTrack(toTrack); |
| 376 |
if (track) |
| 377 |
{ |
| 378 |
track->Release(logger, locoBaseIdentifier); |
| 379 |
} |
| 380 |
ReleaseInternal(logger, locoBaseIdentifier); |
| 381 |
} |
| 382 |
|
| 383 |
bool Route::ObjectIsPartOfRoute(const ObjectIdentifier& identifier) const |
| 384 |
{ |
| 385 |
for (auto relation : relationsAtLock) |
| 386 |
{ |
| 387 |
if (relation->CompareObject2(identifier)) |
| 388 |
{ |
| 389 |
return true; |
| 390 |
} |
| 391 |
} |
| 392 |
for (auto relation : relationsAtUnlock) |
| 393 |
{ |
| 394 |
if (relation->CompareObject2(identifier)) |
| 395 |
{ |
| 396 |
return true; |
| 397 |
} |
| 398 |
} |
| 399 |
return false; |
| 400 |
} |
| 401 |
} // namespace DataModel |
| 402 |
|