| 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 |
#pragma once |
| 22 |
|
| 23 |
#include <mutex> |
| 24 |
#include <string> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
#include "DataTypes.h" |
| 28 |
#include "DataModel/LayoutItem.h" |
| 29 |
#include "DataModel/LockableItem.h" |
| 30 |
#include "DataModel/ObjectIdentifier.h" |
| 31 |
#include "Logger/Logger.h" |
| 32 |
|
| 33 |
class Manager; |
| 34 |
|
| 35 |
namespace DataModel |
| 36 |
{ |
| 37 |
class LocoBase; |
| 38 |
class Relation; |
| 39 |
|
| 40 |
class Route : public LayoutItem, public LockableItem |
| 41 |
{ |
| 42 |
public: |
| 43 |
static const Delay DefaultDelay = 250; |
| 44 |
|
| 45 |
enum PushpullType : unsigned char |
| 46 |
{ |
| 47 |
PushpullTypeNo = 0, |
| 48 |
PushpullTypeOnly = 1, |
| 49 |
PushpullTypeBoth = 2 |
| 50 |
}; |
| 51 |
|
| 52 |
enum Speed : unsigned char |
| 53 |
{ |
| 54 |
SpeedMax = 3, |
| 55 |
SpeedTravel = 2, |
| 56 |
SpeedReduced = 1, |
| 57 |
SpeedCreeping = 0 |
| 58 |
}; |
| 59 |
|
| 60 |
Route() = delete; |
| 61 |
|
| 62 |
Route(Manager* manager, const RouteID routeID) |
| 63 |
: LayoutItem(routeID), |
| 64 |
LockableItem(), |
| 65 |
manager(manager), |
| 66 |
executeAtUnlock(false), |
| 67 |
delay(0), |
| 68 |
pushpull(PushpullTypeBoth), |
| 69 |
propulsion(PropulsionUnknown), |
| 70 |
trainType(TrainTypeAll), |
| 71 |
minTrainLength(0), |
| 72 |
maxTrainLength(0), |
| 73 |
automode(AutomodeNo), |
| 74 |
fromTrack(), |
| 75 |
fromOrientation(OrientationRight), |
| 76 |
toTrack(), |
| 77 |
toOrientation(OrientationRight), |
| 78 |
speed(SpeedTravel), |
| 79 |
feedbackIdReduced(FeedbackNone), |
| 80 |
reducedDelay(0), |
| 81 |
feedbackIdCreep(FeedbackNone), |
| 82 |
creepDelay(0), |
| 83 |
feedbackIdStop(FeedbackNone), |
| 84 |
stopDelay(0), |
| 85 |
feedbackIdOver(FeedbackNone), |
| 86 |
waitAfterRelease(0), |
| 87 |
lastUsed(0), |
| 88 |
counter(0) |
| 89 |
{ |
| 90 |
} |
| 91 |
|
| 92 |
Route(Manager* manager, const std::string& serialized); |
| 93 |
|
| 94 |
inline ~Route() |
| 95 |
{ |
| 96 |
DeleteRelations(relationsAtLock); |
| 97 |
DeleteRelations(relationsAtUnlock); |
| 98 |
} |
| 99 |
|
| 100 |
inline ObjectType GetObjectType() const override |
| 101 |
{ |
| 102 |
return ObjectTypeRoute; |
| 103 |
} |
| 104 |
|
| 105 |
std::string Serialize() const override; |
| 106 |
bool Deserialize(const std::string& serialized) override; |
| 107 |
|
| 108 |
inline std::string GetLayoutType() const override |
| 109 |
{ |
| 110 |
return Languages::GetText(Languages::TextRoute); |
| 111 |
} |
| 112 |
|
| 113 |
inline void DeleteRelationsAtLock() |
| 114 |
{ |
| 115 |
DeleteRelations(relationsAtLock); |
| 116 |
} |
| 117 |
|
| 118 |
inline void DeleteRelationsAtUnlock() |
| 119 |
{ |
| 120 |
DeleteRelations(relationsAtUnlock); |
| 121 |
} |
| 122 |
|
| 123 |
inline bool AssignRelationsAtLock(const std::vector<DataModel::Relation*>& newRelations) |
| 124 |
{ |
| 125 |
return AssignRelations(relationsAtLock, newRelations); |
| 126 |
} |
| 127 |
|
| 128 |
inline bool AssignRelationsAtUnlock(const std::vector<DataModel::Relation*>& newRelations) |
| 129 |
{ |
| 130 |
return AssignRelations(relationsAtUnlock, newRelations); |
| 131 |
} |
| 132 |
|
| 133 |
inline const std::vector<DataModel::Relation*>& GetRelationsAtLock() const |
| 134 |
{ |
| 135 |
return relationsAtLock; |
| 136 |
} |
| 137 |
|
| 138 |
inline const std::vector<DataModel::Relation*>& GetRelationsAtUnlock() const |
| 139 |
{ |
| 140 |
return relationsAtUnlock; |
| 141 |
} |
| 142 |
|
| 143 |
bool FromTrackOrientation(Logger::Logger* logger, |
| 144 |
const TrackID trackID, |
| 145 |
const Orientation trackOrientation, |
| 146 |
const DataModel::LocoBase* loco, |
| 147 |
const bool allowLocoTurn); |
| 148 |
|
| 149 |
bool Execute(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier); |
| 150 |
|
| 151 |
inline static bool ExecuteStatic(Logger::Logger* logger, Route* route) |
| 152 |
{ |
| 153 |
return route->Execute(logger, ObjectIdentifier()); |
| 154 |
} |
| 155 |
|
| 156 |
bool Reserve(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier) override; |
| 157 |
|
| 158 |
bool Lock(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier) override; |
| 159 |
|
| 160 |
bool Release(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier) override |
| 161 |
{ |
| 162 |
std::lock_guard<std::mutex> Guard(updateMutex); |
| 163 |
return ReleaseInternal(logger, locoBaseIdentifier); |
| 164 |
} |
| 165 |
|
| 166 |
inline Delay GetDelay() const |
| 167 |
{ |
| 168 |
return delay; |
| 169 |
} |
| 170 |
|
| 171 |
inline void SetDelay(Delay delay) |
| 172 |
{ |
| 173 |
this->delay = delay; |
| 174 |
} |
| 175 |
|
| 176 |
inline PushpullType GetPushpull() const |
| 177 |
{ |
| 178 |
return pushpull; |
| 179 |
} |
| 180 |
|
| 181 |
inline void SetPushpull(const PushpullType pushpull) |
| 182 |
{ |
| 183 |
this->pushpull = pushpull; |
| 184 |
} |
| 185 |
|
| 186 |
inline Propulsion GetPropulsion() const |
| 187 |
{ |
| 188 |
return propulsion; |
| 189 |
} |
| 190 |
|
| 191 |
inline void SetPropulsion(const Propulsion propulsion) |
| 192 |
{ |
| 193 |
this->propulsion = propulsion; |
| 194 |
} |
| 195 |
|
| 196 |
inline TrainType GetTrainType() const |
| 197 |
{ |
| 198 |
return trainType; |
| 199 |
} |
| 200 |
|
| 201 |
inline void SetTrainType(const TrainType trainType) |
| 202 |
{ |
| 203 |
this->trainType = trainType; |
| 204 |
} |
| 205 |
|
| 206 |
inline Length GetMinTrainLength() const |
| 207 |
{ |
| 208 |
return minTrainLength; |
| 209 |
} |
| 210 |
|
| 211 |
inline void SetMinTrainLength(const Length length) |
| 212 |
{ |
| 213 |
this->minTrainLength = length; |
| 214 |
} |
| 215 |
|
| 216 |
inline Length GetMaxTrainLength() const |
| 217 |
{ |
| 218 |
return maxTrainLength; |
| 219 |
} |
| 220 |
|
| 221 |
inline void SetMaxTrainLength(const Length length) |
| 222 |
{ |
| 223 |
this->maxTrainLength = length; |
| 224 |
} |
| 225 |
|
| 226 |
inline time_t GetLastUsed() const |
| 227 |
{ |
| 228 |
return lastUsed; |
| 229 |
} |
| 230 |
|
| 231 |
inline void SetAutomode(const Automode automode) |
| 232 |
{ |
| 233 |
this->automode = automode; |
| 234 |
} |
| 235 |
|
| 236 |
inline Automode GetAutomode() const |
| 237 |
{ |
| 238 |
return automode; |
| 239 |
} |
| 240 |
|
| 241 |
inline void SetFromTrack(const TrackID fromTrack) |
| 242 |
{ |
| 243 |
this->fromTrack = fromTrack; |
| 244 |
} |
| 245 |
|
| 246 |
inline TrackID GetFromTrack() const |
| 247 |
{ |
| 248 |
return fromTrack; |
| 249 |
} |
| 250 |
|
| 251 |
inline void SetFromOrientation(const Orientation fromOrientation) |
| 252 |
{ |
| 253 |
this->fromOrientation = fromOrientation; |
| 254 |
} |
| 255 |
|
| 256 |
inline Orientation GetFromOrientation() const |
| 257 |
{ |
| 258 |
return fromOrientation; |
| 259 |
} |
| 260 |
|
| 261 |
inline void SetToTrack(const TrackID toTrack) |
| 262 |
{ |
| 263 |
this->toTrack = toTrack; |
| 264 |
} |
| 265 |
|
| 266 |
inline TrackID GetToTrack() const |
| 267 |
{ |
| 268 |
return toTrack; |
| 269 |
} |
| 270 |
|
| 271 |
inline void SetToOrientation(const Orientation toOrientation) |
| 272 |
{ |
| 273 |
this->toOrientation = toOrientation; |
| 274 |
} |
| 275 |
|
| 276 |
inline Orientation GetToOrientation() const |
| 277 |
{ |
| 278 |
return toOrientation; |
| 279 |
} |
| 280 |
|
| 281 |
inline void SetSpeed(Speed startSpeed) |
| 282 |
{ |
| 283 |
this->speed = startSpeed; |
| 284 |
} |
| 285 |
|
| 286 |
inline Speed GetSpeed() const |
| 287 |
{ |
| 288 |
return speed; |
| 289 |
} |
| 290 |
|
| 291 |
inline void SetFeedbackIdReduced(const FeedbackID feedbackIdReduced) |
| 292 |
{ |
| 293 |
this->feedbackIdReduced = feedbackIdReduced; |
| 294 |
} |
| 295 |
|
| 296 |
inline FeedbackID GetFeedbackIdReduced() const |
| 297 |
{ |
| 298 |
return feedbackIdReduced; |
| 299 |
} |
| 300 |
|
| 301 |
inline void SetReducedDelay(const Delay reducedDelay) |
| 302 |
{ |
| 303 |
this->reducedDelay = reducedDelay; |
| 304 |
} |
| 305 |
|
| 306 |
inline Delay GetReducedDelay() const |
| 307 |
{ |
| 308 |
return reducedDelay; |
| 309 |
} |
| 310 |
|
| 311 |
inline void SetFeedbackIdCreep(const FeedbackID feedbackIdCreep) |
| 312 |
{ |
| 313 |
this->feedbackIdCreep = feedbackIdCreep; |
| 314 |
} |
| 315 |
|
| 316 |
inline FeedbackID GetFeedbackIdCreep() const |
| 317 |
{ |
| 318 |
return feedbackIdCreep; |
| 319 |
} |
| 320 |
|
| 321 |
inline void SetCreepDelay(const Delay creepDelay) |
| 322 |
{ |
| 323 |
this->creepDelay = creepDelay; |
| 324 |
} |
| 325 |
|
| 326 |
inline Delay GetCreepDelay() const |
| 327 |
{ |
| 328 |
return creepDelay; |
| 329 |
} |
| 330 |
|
| 331 |
inline void SetFeedbackIdStop(const FeedbackID feedbackIdStop) |
| 332 |
{ |
| 333 |
this->feedbackIdStop = feedbackIdStop; |
| 334 |
} |
| 335 |
|
| 336 |
inline FeedbackID GetFeedbackIdStop() const |
| 337 |
{ |
| 338 |
return feedbackIdStop; |
| 339 |
} |
| 340 |
|
| 341 |
inline void SetStopDelay(const Delay stopDelay) |
| 342 |
{ |
| 343 |
this->stopDelay = stopDelay; |
| 344 |
} |
| 345 |
|
| 346 |
inline Delay GetStopDelay() const |
| 347 |
{ |
| 348 |
return stopDelay; |
| 349 |
} |
| 350 |
|
| 351 |
inline void SetFeedbackIdOver(const FeedbackID feedbackIdOver) |
| 352 |
{ |
| 353 |
this->feedbackIdOver = feedbackIdOver; |
| 354 |
} |
| 355 |
|
| 356 |
inline FeedbackID GetFeedbackIdOver() const |
| 357 |
{ |
| 358 |
return feedbackIdOver; |
| 359 |
} |
| 360 |
|
| 361 |
inline void SetWaitAfterRelease(const Pause wait) |
| 362 |
{ |
| 363 |
this->waitAfterRelease = wait; |
| 364 |
} |
| 365 |
|
| 366 |
inline Pause GetWaitAfterRelease() const |
| 367 |
{ |
| 368 |
return waitAfterRelease; |
| 369 |
} |
| 370 |
|
| 371 |
inline void SetFollowUpRoute(const RouteID followUpRoute) |
| 372 |
{ |
| 373 |
this->followUpRoute = followUpRoute; |
| 374 |
} |
| 375 |
|
| 376 |
inline RouteID GetFollowUpRoute() const |
| 377 |
{ |
| 378 |
return followUpRoute; |
| 379 |
} |
| 380 |
|
| 381 |
static inline bool CompareShortest(const Route* s1, const Route* s2) |
| 382 |
{ |
| 383 |
return s1->GetMinTrainLength() < s2->GetMinTrainLength(); |
| 384 |
} |
| 385 |
static inline bool CompareLastUsed(const Route* s1, const Route* s2) |
| 386 |
{ |
| 387 |
return s1->GetLastUsed() < s2->GetLastUsed(); |
| 388 |
} |
| 389 |
|
| 390 |
bool ObjectIsPartOfRoute(const ObjectIdentifier& identifier) const; |
| 391 |
|
| 392 |
private: |
| 393 |
bool ReleaseInternal(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier); |
| 394 |
void ReleaseInternalWithToTrack(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier); |
| 395 |
static void DeleteRelations(std::vector<DataModel::Relation*>& relations); |
| 396 |
bool AssignRelations(std::vector<DataModel::Relation*>& relations, const std::vector<DataModel::Relation*>& newRelations); |
| 397 |
|
| 398 |
Manager* manager; |
| 399 |
std::mutex updateMutex; |
| 400 |
bool executeAtUnlock; |
| 401 |
|
| 402 |
Delay delay; |
| 403 |
std::vector<DataModel::Relation*> relationsAtLock; |
| 404 |
std::vector<DataModel::Relation*> relationsAtUnlock; |
| 405 |
PushpullType pushpull; |
| 406 |
Propulsion propulsion; |
| 407 |
TrainType trainType; |
| 408 |
Length minTrainLength; |
| 409 |
Length maxTrainLength; |
| 410 |
Automode automode; |
| 411 |
TrackID fromTrack; |
| 412 |
Orientation fromOrientation; |
| 413 |
TrackID toTrack; |
| 414 |
Orientation toOrientation; |
| 415 |
|
| 416 |
Speed speed; |
| 417 |
FeedbackID feedbackIdReduced; |
| 418 |
Delay reducedDelay; |
| 419 |
FeedbackID feedbackIdCreep; |
| 420 |
Delay creepDelay; |
| 421 |
FeedbackID feedbackIdStop; |
| 422 |
Delay stopDelay; |
| 423 |
FeedbackID feedbackIdOver; |
| 424 |
Pause waitAfterRelease; |
| 425 |
RouteID followUpRoute; |
| 426 |
|
| 427 |
time_t lastUsed; |
| 428 |
unsigned int counter; |
| 429 |
}; |
| 430 |
} // namespace DataModel |
| 431 |
|