Debian Codegraph @

DataModel/LocoBase.cpp

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