Debian Codegraph @

DataModel/LocoBase.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch 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 <algorithm>
22 #include <map>
23 #include <string>
24
25 #include "DataModel/LocoBase.h"
26 #include "DataModel/Track.h"
27 #include "Hardware/LocoCache.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 using std::vector;
35
36 namespace DataModel
37 {
38 LocoBase::~LocoBase()
39 {
40 while (true)
41 {
42 {
43 std::lock_guard<std::mutex> Guard(stateMutex);
44 if (state == LocoStateManual)
45 {
46 break;
47 }
48 }
49 logger->Info(Languages::TextWaitingUntilHasStopped, GetName());
50 Utils::Utils::SleepForSeconds(1);
51 }
52 }
53
54 std::string LocoBase::Serialize() const
55 {
56 string str;
57 str += Object::Serialize();
58 str += ";" + HardwareHandle::Serialize();
59 str += ";functions=" + functions.Serialize();
60 str += ";orientation=" + to_string(orientation);
61 str += ";track=" + to_string(trackFrom ? trackFrom->GetID() : TrackNone);
62 str += ";length=" + to_string(length);
63 str += ";pushpull=" + to_string(pushpull);
64 str += ";maxspeed=" + to_string(maxSpeed);
65 str += ";travelspeed=" + to_string(travelSpeed);
66 str += ";reducedspeed=" + to_string(reducedSpeed);
67 str += ";creepingspeed=" + to_string(creepingSpeed);
68 str += ";propulsion=" + to_string(propulsion);
69 str += ";type=" + to_string(trainType);
70 str += ";matchkey=" + matchKey;
71 return str;
72 }
73
•74 void LocoBase::Deserialize(const std::string& serialized)
75 {
76 map<string, string> arguments;
77 ParseArguments(serialized, arguments);
•78 LocoBase::Deserialize(arguments);
79 }
80
•81 void LocoBase::Deserialize(const std::map<std::string,std::string>& arguments)
82 {
83 Object::Deserialize(arguments);
84 HardwareHandle::Deserialize(arguments);
85 trackFrom = manager->GetTrack(static_cast<Length>(Utils::Utils::GetIntegerMapEntry(arguments, "track", TrackNone)));
86 functions.Deserialize(Utils::Utils::GetStringMapEntry(arguments, "functions", "0"));
87 orientation = static_cast<Orientation>(Utils::Utils::GetBoolMapEntry(arguments, "orientation", OrientationRight));
88 length = static_cast<Length>(Utils::Utils::GetIntegerMapEntry(arguments, "length", 0));
89 pushpull = Utils::Utils::GetBoolMapEntry(arguments, "pushpull", false);
90 maxSpeed = Utils::Utils::GetIntegerMapEntry(arguments, "maxspeed", MaxSpeed);
91 travelSpeed = Utils::Utils::GetIntegerMapEntry(arguments, "travelspeed", DefaultTravelSpeed);
92 reducedSpeed = Utils::Utils::GetIntegerMapEntry(arguments, "reducedspeed", DefaultReducedSpeed);
93 creepingSpeed = Utils::Utils::GetIntegerMapEntry(arguments, "creepspeed", DefaultCreepingSpeed);
94 creepingSpeed = Utils::Utils::GetIntegerMapEntry(arguments, "creepingspeed", creepingSpeed);
95 propulsion = static_cast<Propulsion>(Utils::Utils::GetIntegerMapEntry(arguments, "propulsion", PropulsionUnknown));
96 trainType = static_cast<TrainType>(Utils::Utils::GetIntegerMapEntry(arguments, "type", TrainTypeUnknown));
97 matchKey = Utils::Utils::GetStringMapEntry(arguments, "matchkey");
98 }
99
100 bool LocoBase::SetTrack(const TrackID trackID)
101 {
102 std::lock_guard<std::mutex> Guard(stateMutex);
103 // there must not be set a track
104 if (trackFrom)
105 {
106 return false;
107 }
108 trackFrom = manager->GetTrack(trackID);
109 return true;
110 }
111
112 TrackID LocoBase::GetTrackId() const
113 {
114 return (trackFrom ? trackFrom->GetID() : TrackNone);
115 }
116
117 bool LocoBase::Release()
118 {
119 manager->LocoBaseSpeed(ControlTypeInternal, this, MinSpeed);
120 ForceManualMode();
121 {
122 std::lock_guard<std::mutex> Guard(stateMutex);
123
124 if (routeFirst)
125 {
126 Route* removeRoute = routeFirst;
127 routeFirst = nullptr;
128 releaseRouteQueue.EnqueueBack(removeRoute);
129 }
130 if (routeSecond)
131 {
132 Route* removeRoute = routeSecond;
133 routeSecond = nullptr;
134 releaseRouteQueue.EnqueueBack(removeRoute);
135 }
136 if (trackFrom)
137 {
138 Track* removeTrack = trackFrom;
139 trackFrom = nullptr;
140 releaseTrackQueue.EnqueueBack(removeTrack);
141 }
142 if (trackFirst)
143 {
144 Track* removeTrack = trackFirst;
145 trackFirst = nullptr;
146 releaseTrackQueue.EnqueueBack(removeTrack);
147 }
148 if (trackSecond)
149 {
150 Track* removeTrack = trackSecond;
151 trackSecond = nullptr;
152 releaseTrackQueue.EnqueueBack(removeTrack);
153 }
154 feedbackIdOver = FeedbackNone;
155 feedbackIdStop = FeedbackNone;
156 feedbackIdCreep = FeedbackNone;
157 feedbackIdReduced = FeedbackNone;
158 feedbackIdFirst = FeedbackNone;
159 }
160 ReleaseRouteAndTrack();
161 return true;
162 }
163
164 void LocoBase::ReleaseRouteAndTrack()
165 {
166 const ObjectIdentifier identifier = GetObjectIdentifier();
167 while(!releaseTrackQueue.IsEmpty())
168 {
169 Track* track = releaseTrackQueue.Dequeue();
170 track->Release(logger, identifier);
171 }
172 while(!releaseRouteQueue.IsEmpty())
173 {
174 Route* route = releaseRouteQueue.Dequeue();
175 route->Release(logger, identifier);
176 }
177 }
178
179 bool LocoBase::CheckFreeingTrack(const TrackID trackID) const
180 {
181 std::lock_guard<std::mutex> Guard(stateMutex);
182 if (trackFrom && (trackFrom->GetID() == trackID) && !trackFirst)
183 {
184 return false;
185 }
186 if (trackFirst && (trackFirst->GetID() == trackID))
187 {
188 return false;
189 }
190 if (trackSecond && (trackSecond->GetID() == trackID))
191 {
192 return false;
193 }
194 return true;
195 }
196
197 bool LocoBase::GoToAutoMode()
198 {
199 std::lock_guard<std::mutex> Guard(stateMutex);
200 if (!trackFrom)
201 {
202 logger->Warning(Languages::TextCanNotStartNotOnTrack);
203 return false;
204 }
205 if (state == LocoStateError)
206 {
207 logger->Warning(Languages::TextCanNotStartInErrorState);
208 return false;
209 }
210 if (state == LocoStateTerminated)
211 {
212 locoThread.join();
213 state = LocoStateManual;
214 }
215 if (state != LocoStateManual)
216 {
217 logger->Info(Languages::TextCanNotStartAlreadyRunning);
218 return false;
219 }
220
221 followUpRoute = RouteAuto;
222 state = LocoStateAutomodeGetFirst;
223 locoThread = std::thread(&DataModel::LocoBase::AutoMode, this);
224
225 return true;
226 }
227
228 void LocoBase::RequestManualMode()
229 {
230 if (state == LocoStateManual || state == LocoStateTerminated)
231 {
232 return;
233 }
234 requestManualMode = true;
235 }
236
237 bool LocoBase::GoToManualMode()
238 {
239 if (state == LocoStateManual)
240 {
241 return true;
242 }
243 if (state != LocoStateTerminated)
244 {
245 return false;
246 }
247 locoThread.join();
248 state = LocoStateManual;
249 return true;
250 }
251
252 void LocoBase::ForceManualMode()
253 {
254 {
255 std::lock_guard<std::mutex> Guard(stateMutex);
256 switch (state)
257 {
258 case LocoStateManual:
259 return;
260
261 case LocoStateTerminated:
262 break;
263
264 default:
265 state = LocoStateOff;
266 break;
267 }
268 }
269 locoThread.join();
270 state = LocoStateManual;
271 }
272
273 void LocoBase::AutoMode()
274 {
275 Utils::Utils::SetMinThreadPriority();
276 const string& name = GetName();
277 Utils::Utils::SetThreadName(name);
278 logger->Info(Languages::TextIsNowInAutoMode);
279
280 while (true)
281 {
282 unsigned char pause = 0;
283 { // sleep must be outside of locked block
284 std::lock_guard<std::mutex> Guard(stateMutex);
285
286 if (!feedbackIdsReached.IsEmpty())
287 {
288 FeedbackID feedbackId = feedbackIdsReached.Dequeue();
289 if (feedbackId == feedbackIdFirst)
290 {
291 FeedbackIdFirstReached();
292 }
293 else if (feedbackId == feedbackIdStop)
294 {
295 FeedbackIdStopReached();
296 }
297 }
298 else
299 {
300 switch (state)
301 {
302 case LocoStateOff:
303 // automode is turned off, terminate thread
304 logger->Info(Languages::TextIsNowInManualMode);
305 state = LocoStateTerminated;
306 requestManualMode = false;
307 wait = 0;
308 return;
309
310 case LocoStateAutomodeGetFirst:
311 if (requestManualMode || (followUpRoute == RouteStop))
312 {
313 state = LocoStateOff;
314 break;
315 }
316 if (wait > 0)
317 {
318 --wait;
319 break;
320 }
321 GetTimetableDestinationFirst();
322 break;
323
324 case LocoStateAutomodeGetSecond:
325 if (requestManualMode)
326 {
327 logger->Info(Languages::TextIsRunningWaitingUntilDestination);
328 state = LocoStateStopping;
329 break;
330 }
331 if (manager->GetNrOfTracksToReserve() <= 1)
332 {
333 break;
334 }
335 if (wait > 0)
336 {
337 break;
338 }
339 GetTimetableDestinationSecond();
340 break;
341
342 case LocoStateAutomodeRunning:
343 // loco is already running, waiting until destination reached
344 if (requestManualMode)
345 {
346 logger->Info(Languages::TextIsRunningWaitingUntilDestination);
347 state = LocoStateStopping;
348 }
349 break;
350
351 case LocoStateStopping:
352 if (requestManualMode)
353 {
354 logger->Info(Languages::TextHasNotReachedDestination);
355 break;
356 }
357
358 if (trackSecond)
359 {
360 state = LocoStateAutomodeRunning;
361 }
362 else if (trackFirst)
363 {
364 state = LocoStateAutomodeGetSecond;
365 }
366 else
367 {
368 state = LocoStateAutomodeGetFirst;
369 }
370 break;
371
372 case LocoStateTerminated:
373 logger->Error(Languages::TextIsInTerminatedState);
374 state = LocoStateError;
375 break;
376
377 case LocoStateManual:
378 logger->Error(Languages::TextIsInManualState);
379 state = LocoStateError;
380 #include "Fallthrough.h"
381
382 case LocoStateError:
383 logger->Error(Languages::TextIsInErrorState);
384 manager->LocoBaseSpeed(ControlTypeInternal, this, MinSpeed);
385 if (requestManualMode)
386 {
387 state = LocoStateOff;
388 }
389 break;
390 }
391 pause = 1;
392 }
393 }
394
395 ReleaseRouteAndTrack();
396
397 // FIXME: make configurable
398 Utils::Utils::SleepForSeconds(pause);
399 }
400 }
401
402 Route* LocoBase::GetNextDestination(const Track* const track, const bool allowLocoTurn)
403 {
404 if (timeTableQueue.IsEmpty())
405 {
406 if (followUpRoute == RouteStop)
407 {
408 RequestManualMode();
409 return nullptr;
410 }
411
412 const Route* route = nullptr;
413
414 if (followUpRoute != RouteAuto)
415 {
416 route = manager->GetRoute(followUpRoute);
417 }
418 else
419 {
420 route = SearchDestination(track, allowLocoTurn);
421 }
422
423 if (route)
424 {
425 AddTimeTable(route, route->GetFollowUpRoute());
426 }
427 }
428
429 return GetDestinationFromTimeTable(track, allowLocoTurn);
430 }
431
432 void LocoBase::GetTimetableDestinationFirst()
433 {
434 if (routeFirst)
435 {
436 state = LocoStateError;
437 logger->Error(Languages::TextHasAlreadyReservedRoute);
438 return;
439 }
440
441 Route* route = GetNextDestination(trackFrom, true);
442 if (!route)
443 {
444 return;
445 }
446 PrepareDestinationFirst(route);
447 }
448
449 void LocoBase::GetTimetableDestinationSecond()
450 {
451 Route* route = GetNextDestination(trackFirst, false);
452 if (!route)
453 {
454 return;
455 }
456 PrepareDestinationSecond(route);
457 }
458
459 void LocoBase::PrepareDestinationFirst(Route* const route)
460 {
461 if (!route)
462 {
463 return;
464 }
465
466 Track* newTrack = manager->GetTrack(route->GetToTrack());
467 if (!newTrack)
468 {
469 return;
470 }
471
472 bool isOrientationSet = newTrack->SetLocoBaseOrientation(route->GetToOrientation());
473 if (!isOrientationSet)
474 {
475 return;
476 }
477
478 bool turnLoco = (trackFrom->GetLocoBaseOrientation() != route->GetFromOrientation());
479 Orientation newLocoOrientation = static_cast<Orientation>(orientation != turnLoco);
480 if (turnLoco)
481 {
482 bool canTurnOrientation = trackFrom->SetLocoBaseOrientation(route->GetFromOrientation());
483 if (!canTurnOrientation)
484 {
485 return;
486 }
487 manager->TrackPublishState(trackFrom);
488 }
489 manager->LocoBaseOrientation(ControlTypeInternal, this, newLocoOrientation);
490 logger->Info(Languages::TextHeadingToVia, newTrack->GetName(), route->GetName());
491
492 trackFirst = newTrack;
493 routeFirst = route;
494 feedbackIdFirstReduced = FeedbackNone;
495 feedbackIdFirstCreep = FeedbackNone;
496 feedbackIdFirst = FeedbackNone;
497 feedbackIdReduced = routeFirst->GetFeedbackIdReduced();
498 reducedDelay = routeFirst->GetReducedDelay();
499 feedbackIdCreep = routeFirst->GetFeedbackIdCreep();
500 creepDelay = routeFirst->GetCreepDelay();
501 feedbackIdStop = routeFirst->GetFeedbackIdStop();
502 stopDelay = routeFirst->GetStopDelay();
503 feedbackIdOver = routeFirst->GetFeedbackIdOver();
504
505 wait = routeFirst->GetWaitAfterRelease();
506
507 // start loco
508 manager->TrackPublishState(newTrack);
509 Speed newSpeed;
510 switch (routeFirst->GetSpeed())
511 {
512 case Route::SpeedTravel:
513 newSpeed = travelSpeed;
514 break;
515
516 case Route::SpeedReduced:
517 newSpeed = reducedSpeed;
518 break;
519
520 case Route::SpeedCreeping:
521 default:
522 newSpeed = creepingSpeed;
523 break;
524 }
525 manager->LocoBaseSpeed(ControlTypeInternal, this, newSpeed);
526 state = LocoStateAutomodeGetSecond;
527 }
528
529 Route* LocoBase::GetDestinationFromTimeTable(const Track* const track, const bool allowLocoTurn)
530 {
531 if (timeTableQueue.IsEmpty())
532 {
533 return nullptr;
534 }
535
536 const TimeTableEntry entry = timeTableQueue.Dequeue();
537 Route* const route = manager->GetRoute(entry.first);
538 if (route->GetFromTrack() != track->GetID())
539 {
540 return nullptr;
541 }
542 logger->Debug(Languages::TextUsingRouteFromTimetable, route->GetName());
543 bool ret = ExecuteRoute(track, allowLocoTurn, route);
544 if (!ret)
545 {
546 timeTableQueue.EnqueueFront(entry);
547 return nullptr;
548 }
549 followUpRoute = entry.second;
550 return route;
551 }
552
553 void LocoBase::AddTimeTable(const Route* route, const RouteID followUpRoute)
554 {
555 logger->Debug(Languages::TextAddingRouteToTimetable, route->GetName());
556 requestManualMode = false;
557 const TimeTableEntry entry(route->GetID(), followUpRoute);
558 timeTableQueue.EnqueueBack(entry);
559 }
560
561 void LocoBase::PrepareDestinationSecond(Route* const route)
562 {
563 if (!route)
564 {
565 return;
566 }
567
568 Track* newTrack = manager->GetTrack(route->GetToTrack());
569 if (!newTrack)
570 {
571 return;
572 }
573
574 const bool isOrientationSet = newTrack->SetLocoBaseOrientation(route->GetToOrientation());
575 if (!isOrientationSet)
576 {
577 return;
578 }
579 logger->Info(Languages::TextHeadingToViaVia, newTrack->GetName(), routeFirst->GetName(), route->GetName());
580
581 trackSecond = newTrack;
582 routeSecond = route;
583 feedbackIdFirst = feedbackIdStop;
584 feedbackIdFirstCreep = feedbackIdCreep;
585 feedbackIdFirstReduced = feedbackIdReduced;
586 feedbackIdOver = routeSecond->GetFeedbackIdOver();
587 feedbackIdStop = routeSecond->GetFeedbackIdStop();
588 feedbackIdCreep = routeSecond->GetFeedbackIdCreep();
589 feedbackIdReduced = routeSecond->GetFeedbackIdReduced();
590
591 wait = routeSecond->GetWaitAfterRelease();
592
593 manager->TrackPublishState(newTrack);
594 state = LocoStateAutomodeRunning;
595 }
596
597 Route* LocoBase::SearchDestination(const Track* const track, const bool allowLocoTurn)
598 {
599 if (manager->Booster() == BoosterStateStop)
600 {
601 return nullptr;
602 }
603 logger->Debug(Languages::TextLookingForDestination, track->GetName());
604 if (routeSecond)
605 {
606 state = LocoStateError;
607 logger->Error(Languages::TextHasAlreadyReservedRoute);
608 return nullptr;
609 }
610
611 if (!track)
612 {
613 state = LocoStateOff;
614 logger->Info(Languages::TextIsNotOnTrack);
615 return nullptr;
616 }
617
618 const ObjectIdentifier& locoBaseOfTrack = track->GetLocoBase();
619 if (locoBaseOfTrack != GetObjectIdentifier())
620 {
621 state = LocoStateError;
622 logger->Error(Languages::TextIsOnOcupiedTrack, track->GetName(), manager->GetLocoBaseName(locoBaseOfTrack));
623 return nullptr;
624 }
625
626 vector<Route*> validRoutes;
627 track->GetValidRoutes(logger, this, allowLocoTurn, validRoutes);
628 for (auto route : validRoutes)
629 {
630 bool ret = ReserveRoute(track, allowLocoTurn, route);
631 if (ret)
632 {
633 return route;
634 }
635 }
636 logger->Debug(Languages::TextNoValidRouteFound, GetName());
637 return nullptr;
638 }
639
640 bool LocoBase::ReserveRoute(const Track* const track, const bool allowLocoTurn, Route* const route)
641 {
642 const ObjectIdentifier locoBaseIdentifier = GetObjectIdentifier();
643 if (!route->Reserve(logger, locoBaseIdentifier))
644 {
645 return false;
646 }
647
648 Track* newTrack = manager->GetTrack(route->GetToTrack());
649 if (!newTrack)
650 {
651 route->Release(logger, locoBaseIdentifier);
652 return false;
653 }
654
655 bool canSetOrientation = newTrack->CanSetLocoBaseOrientation(route->GetToOrientation(), locoBaseIdentifier);
656 if (!canSetOrientation)
657 {
658 route->Release(logger, locoBaseIdentifier);
659 newTrack->Release(logger, locoBaseIdentifier);
660 return false;
661 }
662
663 if (!allowLocoTurn && track->GetLocoBaseOrientation() != route->GetFromOrientation())
664 {
665 route->Release(logger, locoBaseIdentifier);
666 newTrack->Release(logger, locoBaseIdentifier);
667 return false;
668 }
669
670 logger->Debug(Languages::TextReservingRoute, route->GetName());
671 return true;
672 }
673
674 bool LocoBase::ExecuteRoute(const Track* const track, const bool allowLocoTurn, Route* const route)
675 {
676 const LockableItem::LockState lockState = route->GetLockState();
677 if (lockState == LockableItem::LockStateFree)
678 {
679 if (!ReserveRoute(track, allowLocoTurn, route))
680 {
681 return false;
682 }
683 }
684
685 if (lockState == LockableItem::LockStateReserved)
686 {
687 if (!route->Lock(logger, GetObjectIdentifier()))
688 {
689 return false;
690 }
691 }
692
693 logger->Debug(Languages::TextExecutingRoute, route->GetName());
694
695 const ObjectIdentifier& locoBaseIdentifier = GetObjectIdentifier();
696 if (!route->Execute(logger, locoBaseIdentifier))
697 {
698 route->Release(logger, locoBaseIdentifier);
699
700 Track* newTrack = manager->GetTrack(route->GetToTrack());
701 if (newTrack)
702 {
703 newTrack->Release(logger, locoBaseIdentifier);
704 }
705 return false;
706 }
707
708 return true;
709 }
710
711 Speed LocoBase::GetRouteSpeed(const Route::Speed routeSpeed)
712 {
713 switch (routeSpeed)
714 {
715 case Route::SpeedTravel:
716 return travelSpeed;
717
718 case Route::SpeedReduced:
719 return reducedSpeed;
720
721 case Route::SpeedCreeping:
722 return creepingSpeed;
723
724 default:
725 return MinSpeed;
726 }
727 }
728
729 void LocoBase::LocationStopReached(const FeedbackID feedbackID, const Delay stopDelay)
730 {
731 Utils::Utils::SleepForMilliseconds(stopDelay * 100);
732 if (feedbackID != feedbackIdStop)
733 {
734 return;
735 }
736 LocationStopReached();
737 }
738
739 void LocoBase::LocationStopReached()
740 {
741 manager->LocoBaseSpeed(ControlTypeInternal, this, MinSpeed);
742 feedbackIdsReached.EnqueueBack(feedbackIdStop);
743 }
744
745 void LocoBase::LocationCreepReached(const FeedbackID feedbackID, const Delay creepDelay)
746 {
747 Utils::Utils::SleepForMilliseconds(creepDelay * 100);
748 if (feedbackID != feedbackIdCreep)
749 {
750 return;
751 }
752 LocationCreepReached();
753 }
754
755 void LocoBase::LocationCreepReached()
756 {
757 if (speed > creepingSpeed)
758 {
759 manager->LocoBaseSpeed(ControlTypeInternal, this, creepingSpeed);
760 }
761 if (feedbackIdFirst != FeedbackNone)
762 {
763 feedbackIdsReached.EnqueueBack(feedbackIdFirst);
764 }
765 }
766
767 void LocoBase::LocationReducedReached(const FeedbackID feedbackID, const Delay reducedDelay)
768 {
769 Utils::Utils::SleepForMilliseconds(reducedDelay * 100);
770 if (feedbackID != feedbackIdReduced)
771 {
772 return;
773 }
774 LocationReducedReached();
775 }
776
777 void LocoBase::LocationReducedReached()
778 {
779 if (speed > reducedSpeed)
780 {
781 manager->LocoBaseSpeed(ControlTypeInternal, this, reducedSpeed);
782 }
783 if (feedbackIdFirst != 0)
784 {
785 feedbackIdsReached.EnqueueBack(feedbackIdFirst);
786 }
787 }
788
789 void LocoBase::LocationReached(const FeedbackID feedbackID)
790 {
791 if (feedbackID == feedbackIdFirstReduced)
792 {
793 Route::Speed routeSpeed = routeSecond->GetSpeed();
794 switch (routeSpeed)
795 {
796 case Route::SpeedReduced:
797 case Route::SpeedCreeping:
798 if (speed > reducedSpeed)
799 {
800 manager->LocoBaseSpeed(ControlTypeInternal, this, reducedSpeed);
801 }
802 break;
803
804 default:
805 break;
806 }
807 }
808
809 if (feedbackID == feedbackIdFirstCreep)
810 {
811 Route::Speed routeSpeed = routeSecond->GetSpeed();
812 switch (routeSpeed)
813 {
814 case Route::SpeedCreeping:
815 if (speed > creepingSpeed)
816 {
817 manager->LocoBaseSpeed(ControlTypeInternal, this, creepingSpeed);
818 }
819 break;
820
821 default:
822 break;
823 }
824 }
825
826 if (feedbackID == feedbackIdFirst)
827 {
828 Speed newSpeed = GetRouteSpeed(routeSecond->GetSpeed());
829 if (speed > newSpeed)
830 {
831 manager->LocoBaseSpeed(ControlTypeInternal, this, newSpeed);
832 }
833 feedbackIdsReached.EnqueueBack(feedbackIdFirst);
834 }
835
836 if (feedbackID == feedbackIdReduced)
837 {
838 if (reducedDelay)
839 {
840 __attribute((unused)) auto r = std::async(std::launch::async, LocationReducedReachedStatic, this, feedbackID, reducedDelay);
841 }
842 else
843 {
844 LocationReducedReached();
845 }
846 }
847
848 if (feedbackID == feedbackIdCreep)
849 {
850 if (creepDelay)
851 {
852 __attribute((unused)) auto r = std::async(std::launch::async, LocationCreepReachedStatic, this, feedbackID, creepDelay);
853 }
854 else
855 {
856 LocationCreepReached();
857 }
858 }
859
860 if (feedbackID == feedbackIdStop)
861 {
862 if (stopDelay)
863 {
864 __attribute((unused)) auto r = std::async(std::launch::async, LocationStopReachedStatic, this, feedbackID, stopDelay);
865 }
866 else
867 {
868 LocationStopReached();
869 }
870 }
871
872 if (feedbackID == feedbackIdOver)
873 {
874 manager->LocoBaseSpeed(ControlTypeInternal, this, MinSpeed);
875 manager->Booster(ControlTypeInternal, BoosterStateStop);
876 logger->Error(Languages::TextHitOverrun, manager->GetFeedbackName(feedbackID));
877 }
878 }
879
880 void LocoBase::SetSpeed(const Speed speed)
881 {
882 this->speed = speed;
883 }
884
885 void LocoBase::SetFunctionState(const DataModel::LocoFunctionNr nr,
886 const DataModel::LocoFunctionState state)
887 {
888 functions.SetFunctionState(nr, state);
889 }
890
891 void LocoBase::SetOrientation(const Orientation orientation)
892 {
893 this->orientation = orientation;
894 }
895
896 void LocoBase::FeedbackIdFirstReached()
897 {
898 if (!routeFirst || !trackFrom)
899 {
900 manager->LocoBaseSpeed(ControlTypeInternal, this, MinSpeed);
901 state = LocoStateError;
902 logger->Error(Languages::TextIsInAutomodeWithoutRouteTrack);
903 return;
904 }
905
906 Speed newSpeed = GetRouteSpeed(routeSecond->GetSpeed());
907 if (speed != newSpeed)
908 {
909 manager->LocoBaseSpeed(ControlTypeInternal, this, newSpeed);
910 }
911
912 ShiftRoute();
913
914 // set state
915 switch (state)
916 {
917 case LocoStateAutomodeRunning:
918 state = LocoStateAutomodeGetSecond;
919 break;
920
921 case LocoStateStopping:
922 // do nothing
923 break;
924
925 default:
926 logger->Error(Languages::TextIsInInvalidAutomodeState, state, manager->GetFeedbackName(feedbackIdFirst));
927 state = LocoStateError;
928 break;
929 }
930
931 feedbackIdFirstCreep = FeedbackNone;
932 feedbackIdFirstReduced = FeedbackNone;
933 feedbackIdFirst = FeedbackNone;
934 }
935
936 void LocoBase::FeedbackIdStopReached()
937 {
938 if (!routeFirst || !trackFrom)
939 {
940 manager->LocoBaseSpeed(ControlTypeInternal, this, MinSpeed);
941 state = LocoStateError;
942 logger->Error(Languages::TextIsInAutomodeWithoutRouteTrack);
943 return;
944 }
945
946 if (routeSecond)
947 {
948 // this happens when FeedbackIdFirst has been missed
949 manager->LocoDestinationReached(this, routeSecond, trackSecond);
950 }
951 else
952 {
953 manager->LocoDestinationReached(this, routeFirst, trackFirst);
954 }
955 logger->Info(Languages::TextReachedItsDestination);
956
957 while (routeFirst)
958 {
959 ShiftRoute();
960 }
961
962 // set state
963 switch (state)
964 {
965 case LocoStateAutomodeRunning:
966 case LocoStateAutomodeGetSecond:
967 state = LocoStateAutomodeGetFirst;
968 break;
969
970 case LocoStateStopping:
971 state = LocoStateOff;
972 break;
973
974 default:
975 logger->Error(Languages::TextIsInInvalidAutomodeState, state, manager->GetFeedbackName(feedbackIdStop));
976 state = LocoStateError;
977 break;
978 }
979
980 feedbackIdStop = FeedbackNone;
981 feedbackIdCreep = FeedbackNone;
982 feedbackIdReduced = FeedbackNone;
983 }
984
985 void LocoBase::ShiftRoute()
986 {
987 Route* removeRoute = routeFirst;
988 routeFirst = routeSecond;
989 routeSecond = nullptr;
990
991 Track* removeTrack = trackFrom;
992 trackFrom = trackFirst;
993 trackFirst = trackSecond;
994 trackSecond = nullptr;
995
996 releaseRouteQueue.EnqueueBack(removeRoute);
997 releaseTrackQueue.EnqueueBack(removeTrack);
998 }
999
1000 DataModel::LocoFunctionNr LocoBase::GetFunctionNumberFromFunctionIcon(const DataModel::LocoFunctionIcon icon) const
1001 {
1002 for (DataModel::LocoFunctionNr nr = 0; nr < NumberOfLocoFunctions; ++nr)
1003 {
1004 if (icon == functions.GetFunctionIcon(nr))
1005 {
1006 return nr;
1007 }
1008 }
1009 return NumberOfLocoFunctions;
1010 }
1011
1012 LocoBase& LocoBase::operator=(const Hardware::LocoCacheEntry& loco)
1013 {
1014 SetControlID(loco.GetControlID());
1015 SetAddress(loco.GetAddress());
1016 SetProtocol(loco.GetProtocol());
1017 SetName(loco.GetName());
1018 SetMatchKey(loco.GetMatchKey());
1019 ConfigureFunctions(loco.GetFunctionStates());
1020 return *this;
1021 }
1022 } // namespace DataModel