Debian Codegraph @

DataModel/Route.cpp

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
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 void 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;
95 }
96
97 LayoutItem::Deserialize(arguments);
•98 SetRotation(LayoutItem::Rotation0);
99 LockableItem::Deserialize(arguments);
100
101 delay = static_cast<Delay>(Utils::Utils::GetIntegerMapEntry(arguments, "delay", DefaultDelay));
102 lastUsed = Utils::Utils::GetIntegerMapEntry(arguments, "lastused", 0);
103 counter = Utils::Utils::GetIntegerMapEntry(arguments, "counter", 0);
104 automode = static_cast<Automode>(Utils::Utils::GetBoolMapEntry(arguments, "automode", AutomodeNo));
105 if (automode == AutomodeNo)
106 {
107 fromTrack = TrackNone;
108 fromOrientation = OrientationRight;
109 toTrack = TrackNone;
110 toOrientation = OrientationRight;
111 speed = SpeedTravel;
112 feedbackIdReduced = FeedbackNone;
113 reducedDelay = 0;
114 feedbackIdCreep = FeedbackNone;
115 creepDelay = 0;
116 feedbackIdStop = FeedbackNone;
117 stopDelay = 0;
118 feedbackIdOver = FeedbackNone;
119 pushpull = PushpullTypeBoth;
120 propulsion = PropulsionAll;
121 trainType = TrainTypeAll;
122 minTrainLength = 0;
123 maxTrainLength = 0;
124 waitAfterRelease = 0;
125 followUpRoute = RouteNone;
•126 return;
127 }
128 fromTrack = static_cast<Length>(Utils::Utils::GetIntegerMapEntry(arguments, "fromTrack", TrackNone));
129 fromOrientation = static_cast<Orientation>(Utils::Utils::GetBoolMapEntry(arguments, "fromorientation", OrientationRight));
130 toTrack = static_cast<Length>(Utils::Utils::GetIntegerMapEntry(arguments, "toTrack", TrackNone));
131 toOrientation = static_cast<Orientation>(Utils::Utils::GetBoolMapEntry(arguments, "toorientation", OrientationRight));
132 speed = static_cast<Speed>(Utils::Utils::GetIntegerMapEntry(arguments, "speed", SpeedTravel));
133 feedbackIdReduced = Utils::Utils::GetIntegerMapEntry(arguments, "feedbackIdReduced", FeedbackNone);
134 reducedDelay = Utils::Utils::GetIntegerMapEntry(arguments, "reduceddelay", 0);
135 feedbackIdCreep = Utils::Utils::GetIntegerMapEntry(arguments, "feedbackIdCreep", FeedbackNone);
136 creepDelay = Utils::Utils::GetIntegerMapEntry(arguments, "creepdelay", 0);
137 feedbackIdStop = Utils::Utils::GetIntegerMapEntry(arguments, "feedbackIdStop", FeedbackNone);
138 stopDelay = Utils::Utils::GetIntegerMapEntry(arguments, "stopdelay", 0);
139 feedbackIdOver = Utils::Utils::GetIntegerMapEntry(arguments, "feedbackIdOver", FeedbackNone);
140 pushpull = static_cast<PushpullType>(Utils::Utils::GetIntegerMapEntry(arguments, "pushpull", PushpullTypeBoth));
141 propulsion = static_cast<Propulsion>(Utils::Utils::GetIntegerMapEntry(arguments, "propulsion", propulsion));
142 trainType = static_cast<TrainType>(Utils::Utils::GetIntegerMapEntry(arguments, "traintype", trainType));
143 minTrainLength = static_cast<Length>(Utils::Utils::GetIntegerMapEntry(arguments, "mintrainlength", 0));
144 maxTrainLength = static_cast<Length>(Utils::Utils::GetIntegerMapEntry(arguments, "maxtrainlength", 0));
145 waitAfterRelease = Utils::Utils::GetIntegerMapEntry(arguments, "waitafterrelease", 0);
146 followUpRoute = Utils::Utils::GetIntegerMapEntry(arguments, "followuproute", RouteNone);
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 result = relation->Reserve(logger, locoBaseIdentifier);
302 if (!result)
303 {
•304 const Object* object = relation->GetObject2();
305 const string objectName = object ? object->GetName() : Languages::GetText(Languages::TextUnknownElement);
306 logger->Debug(Languages::TextUnableToReserveRouteElement, GetName(), objectName);
307 ReleaseInternalWithToTrack(logger, locoBaseIdentifier);
308 return false;
309 }
310 }
311
312 return true;
313 }
314
315 bool Route::Lock(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
316 {
317 if (manager->Booster() == BoosterStateStop)
318 {
319 logger->Debug(Languages::TextBoosterIsTurnedOff);
320 return false;
321 }
322
323 std::lock_guard<std::mutex> Guard(updateMutex);
324 bool ret = LockableItem::Lock(logger, locoBaseIdentifier);
325 if (!ret)
326 {
327 return false;
328 }
329
330 if (automode == AutomodeYes)
331 {
332 Track* track = manager->GetTrack(toTrack);
333 if (!track || !track->Lock(logger, locoBaseIdentifier))
334 {
335 ReleaseInternal(logger, locoBaseIdentifier);
336 return false;
337 }
338 }
339
340 for (auto relation : relationsAtLock)
341 {
342 bool retRelation = relation->Lock(logger, locoBaseIdentifier);
343 if (!retRelation)
344 {
345 ReleaseInternalWithToTrack(logger, locoBaseIdentifier);
346 return false;
347 }
348 }
349
350 return true;
351 }
352
353 bool Route::ReleaseInternal(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
354 {
355 if (executeAtUnlock)
356 {
357 for (auto relation : relationsAtUnlock)
358 {
359 relation->Execute(logger, locoBaseIdentifier, delay);
360 }
361 executeAtUnlock = false;
362 }
363
364 for (auto relation : relationsAtLock)
365 {
366 relation->Release(logger, locoBaseIdentifier);
367 }
368
369 return LockableItem::Release(logger, locoBaseIdentifier);
370 }
371
372 void Route::ReleaseInternalWithToTrack(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
373 {
374 Track* track = manager->GetTrack(toTrack);
375 if (track)
376 {
377 track->Release(logger, locoBaseIdentifier);
378 }
379 ReleaseInternal(logger, locoBaseIdentifier);
380 }
381
382 bool Route::ObjectIsPartOfRoute(const ObjectIdentifier& identifier) const
383 {
384 for (auto relation : relationsAtLock)
385 {
386 if (relation->CompareObject2(identifier))
387 {
388 return true;
389 }
390 }
391 for (auto relation : relationsAtUnlock)
392 {
393 if (relation->CompareObject2(identifier))
394 {
395 return true;
396 }
397 }
398 return false;
399 }
400 } // namespace DataModel
401