Debian Codegraph @

DataModel/LocoBase.h

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 #pragma once
22
23 #include <mutex>
24 #include <string>
25 #include <thread>
26 #include <vector>
27
28 #include "DataTypes.h"
29 #include "Logger/Logger.h"
30 #include "DataModel/HardwareHandle.h"
31 #include "DataModel/LocoFunctions.h"
32 #include "DataModel/Object.h"
33 #include "DataModel/Relation.h"
34 #include "DataModel/Route.h"
35 #include "Utils/ThreadSafeQueue.h"
36
37 class Manager;
38
39 namespace Hardware
40 {
41 class LocoCacheEntry;
42 }
43
44 namespace DataModel
45 {
46 class ObjectIdentifier;
47 class Route;
48 class Track;
49
50 class LocoBase : public Object, public HardwareHandle
51 {
52 public:
53 enum NrOfTracksToReserve : unsigned char
54 {
55 ReserveOne = 1,
56 ReserveTwo = 2
57 };
58
59 enum AutoModeType : unsigned char
60 {
61 AutoModeTypeFull = 0,
62 AutoModeTypeTimetable
63 };
64
65 LocoBase() = delete;
66 LocoBase(const LocoBase&) = delete;
67 LocoBase& operator=(const LocoBase&) = delete;
68
69 inline LocoBase(Manager* manager, const LocoID locoID)
70 : Object(locoID),
71 HardwareHandle(),
72 manager(manager),
73 length(0),
74 pushpull(false),
75 maxSpeed(0),
76 travelSpeed(0),
77 reducedSpeed(0),
78 creepingSpeed(0),
79 propulsion(PropulsionUnknown),
80 trainType(TrainTypeUnknown),
81 speed(MinSpeed),
82 orientation(OrientationRight),
83 state(LocoStateManual),
84 requestManualMode(false),
85 trackFrom(nullptr),
86 trackFirst(nullptr),
87 trackSecond(nullptr),
88 routeFirst(nullptr),
89 routeSecond(nullptr),
90 feedbackIdFirstReduced(FeedbackNone),
91 feedbackIdFirstCreep(FeedbackNone),
92 feedbackIdFirst(FeedbackNone),
93 feedbackIdReduced(FeedbackNone),
94 feedbackIdCreep(FeedbackNone),
95 feedbackIdStop(FeedbackNone),
96 feedbackIdOver(FeedbackNone),
97 feedbackIdsReached(),
98 wait(0),
99 followUpRoute(RouteAuto),
100 matchKey("")
101 {
102 logger = Logger::Logger::GetLogger(GetName());
103 }
104
105 inline LocoBase(Manager* manager, const std::string& serialized)
106 : LocoBase(manager, LocoNone)
107 {
108 LocoBase::Deserialize(serialized);
109 logger = Logger::Logger::GetLogger(GetName());
110 }
111
112 virtual ~LocoBase();
113
114 inline Logger::Logger* GetLogger() const
115 {
116 return logger;
117 }
118
119 std::string Serialize() const override;
120
•121 void Deserialize(const std::string& serialized) override;
122
•123 void Deserialize(const std::map<std::string,std::string>& arguments) override;
124
125 virtual void SetName(const std::string& name) override
126 {
127 Object::SetName(name);
128 logger = Logger::Logger::GetLogger(name);
129 }
130
131 bool GoToAutoMode();
132
133 void RequestManualMode();
134
135 bool GoToManualMode();
136
137 void AddTimeTable(const Route* route, const RouteID followUpRoute);
138
139 bool SetTrack(const TrackID trackID);
140
141 TrackID GetTrackId() const;
142
143 bool Release();
144
145 void ReleaseRouteAndTrack();
146
147 bool CheckFreeingTrack(const TrackID trackID) const;
148
149 void LocationReached(const FeedbackID feedbackID);
150
151 virtual void SetSpeed(const Speed speed);
152
153 inline Speed GetSpeed() const
154 {
155 return speed;
156 }
157
158 virtual void SetFunctionState(const DataModel::LocoFunctionNr nr,
159 const DataModel::LocoFunctionState state);
160
161 inline DataModel::LocoFunctionState GetFunctionState(const DataModel::LocoFunctionNr nr) const
162 {
163 return functions.GetFunctionState(nr);
164 }
165
166 inline std::vector<DataModel::LocoFunctionEntry> GetFunctionStates() const
167 {
168 return functions.GetFunctionStates();
169 }
170
171 inline void GetFunctions(LocoFunctionEntry* out) const
172 {
173 functions.GetFunctions(out);
174 }
175
176 inline void ConfigureFunctions(const std::vector<LocoFunctionEntry>& newEntries)
177 {
178 functions.ConfigureFunctions(newEntries);
179 }
180
181 virtual void SetOrientation(const Orientation orientation);
182
183 inline Orientation GetOrientation() const
184 {
185 return orientation;
186 }
187
188 inline bool IsInManualMode() const
189 {
190 return this->state == LocoStateManual;
191 }
192
193 inline bool IsInUse() const
194 {
195 return this->speed > 0
196 || this->state != LocoStateManual
197 || this->trackFrom != nullptr
198 || this->routeFirst != nullptr;
199 }
200
201 inline Length GetLength() const
202 {
203 return length;
204 }
205
206 inline void SetLength(const Length length)
207 {
208 this->length = length;
209 }
210
211 inline bool GetPushpull() const
212 {
213 return pushpull;
214 }
215
216 inline Speed GetMaxSpeed() const
217 {
218 return maxSpeed;
219 }
220
221 inline Speed GetTravelSpeed() const
222 {
223 return travelSpeed;
224 }
225
226 inline Speed GetReducedSpeed() const
227 {
228 return reducedSpeed;
229 }
230
231 inline Speed GetCreepingSpeed() const
232 {
233 return creepingSpeed;
234 }
235
236 inline void SetPushpull(const bool pushpull)
237 {
238 this->pushpull = pushpull;
239 }
240
241 inline void SetMaxSpeed(const Speed speed)
242 {
243 maxSpeed = speed;
244 }
245
246 inline void SetTravelSpeed(const Speed speed)
247 {
248 travelSpeed = speed;
249 }
250
251 inline void SetReducedSpeed(const Speed speed)
252 {
253 reducedSpeed = speed;
254 }
255
256 inline void SetCreepingSpeed(const Speed speed)
257 {
258 creepingSpeed = speed;
259 }
260
261 inline void SetPropulsion(const Propulsion propulsion)
262 {
263 this->propulsion = propulsion;
264 }
265
266 inline Propulsion GetPropulsion() const
267 {
268 return propulsion;
269 }
270
271 inline void SetTrainType(const TrainType trainType)
272 {
273 this->trainType = trainType;
274 }
275
276 inline TrainType GetTrainType() const
277 {
278 return trainType;
279 }
280
281 inline void SetMatchKey(const std::string& matchKey)
282 {
283 this->matchKey = matchKey;
284 }
285
286 inline void ClearMatchKey()
287 {
288 matchKey.clear();
289 }
290
291 inline std::string GetMatchKey() const
292 {
293 return matchKey;
294 }
295
296 DataModel::LocoFunctionNr GetFunctionNumberFromFunctionIcon(const DataModel::LocoFunctionIcon icon) const;
297
298 DataModel::LocoFunctionIcon GetFunctionIcon(const DataModel::LocoFunctionNr nr) const
299 {
300 return functions.GetFunctionIcon(nr);
301 }
302
303 DataModel::LocoFunctionType GetFunctionType(const DataModel::LocoFunctionNr nr) const
304 {
305 return functions.GetFunctionType(nr);
306 }
307
308 inline LocoID GetLocoIdWithPrefix() const
309 {
310 return GetID() + (GetObjectType() == ObjectTypeMultipleUnit ? MultipleUnitIdPrefix : 0);
311 }
312
313 LocoBase& operator=(const Hardware::LocoCacheEntry& loco);
314
315 protected:
316 Manager* manager;
317
318 private:
319 typedef std::pair<RouteID,RouteID> TimeTableEntry;
320
321 enum LocoState : unsigned char
322 {
323 LocoStateManual = 0,
324 LocoStateTerminated,
325 LocoStateOff,
326 LocoStateAutomodeGetFirst,
327 LocoStateAutomodeGetSecond,
328 LocoStateAutomodeRunning,
329 LocoStateStopping,
330 LocoStateError
331 };
332
333 void SetMinThreadPriorityAndThreadName();
334
335 void AutoMode();
336
337 Route* GetNextDestination(const Track* const track, const bool allowLocoTurn);
338
339 void GetTimetableDestinationFirst();
340
341 void PrepareDestinationFirst(Route* const route);
342
343 void GetTimetableDestinationSecond();
344
345 DataModel::Route* GetDestinationFromTimeTable(const Track* const track, const bool allowLocoTurn);
346
347 void PrepareDestinationSecond(Route* const route);
348
349 DataModel::Route* SearchDestination(const DataModel::Track* const oldToTrack, const bool allowLocoTurn);
350
351 bool ReserveRoute(const Track* const track, const bool allowLocoTurn, Route* const route);
352
353 bool ExecuteRoute(const Track* const track, const bool allowLocoTurn, Route* const route);
354
355 void FeedbackIdFirstReached();
356
357 void FeedbackIdStopReached();
358
359 void ShiftRoute();
360
361 void ForceManualMode();
362
363 bool GoToAutoModeInternal(const LocoState newState);
364
365 Speed GetRouteSpeed(const Route::Speed routeSpeed);
366
367 void LocationStopReached(const FeedbackID feedbackID, const Delay stopDelay);
368
369 void LocationStopReached();
370
371 static inline void LocationStopReachedStatic(LocoBase* locoBase, const FeedbackID feedbackID, const Delay stopDelay)
372 {
373 locoBase->LocationStopReached(feedbackID, stopDelay);
374 }
375
376 void LocationCreepReached(const FeedbackID feedbackID, const Delay creepDelay);
377
378 void LocationCreepReached();
379
380 static inline void LocationCreepReachedStatic(LocoBase* locoBase, const FeedbackID feedbackID, const Delay creepDelay)
381 {
382 locoBase->LocationCreepReached(feedbackID, creepDelay);
383 }
384
385 void LocationReducedReached(const FeedbackID feedbackID, const Delay reducedDelay);
386
387 void LocationReducedReached();
388
389 static inline void LocationReducedReachedStatic(LocoBase* locoBase, const FeedbackID feedbackID, const Delay reducedDelay)
390 {
391 locoBase->LocationReducedReached(feedbackID, reducedDelay);
392 }
393
394 mutable std::mutex stateMutex;
395 std::thread locoThread;
396
397 Length length;
398 bool pushpull;
399 Speed maxSpeed;
400 Speed travelSpeed;
401 Speed reducedSpeed;
402 Speed creepingSpeed;
403
404 Propulsion propulsion;
405 TrainType trainType;
406
407 Speed speed;
408 Orientation orientation;
409
410 volatile LocoState state;
411 volatile bool requestManualMode;
412 Track* trackFrom;
413 Track* trackFirst;
414 Track* trackSecond;
415 Route* routeFirst;
416 Route* routeSecond;
417 Utils::ThreadSafeQueue<Track*> releaseTrackQueue;
418 Utils::ThreadSafeQueue<Route*> releaseRouteQueue;
419 volatile FeedbackID feedbackIdFirstReduced;
420 volatile FeedbackID feedbackIdFirstCreep;
421 volatile FeedbackID feedbackIdFirst;
422 volatile FeedbackID feedbackIdReduced;
423 volatile Delay reducedDelay;
424 volatile FeedbackID feedbackIdCreep;
425 volatile Delay creepDelay;
426 volatile FeedbackID feedbackIdStop;
427 volatile Delay stopDelay;
428 volatile FeedbackID feedbackIdOver;
429 Utils::ThreadSafeQueue<FeedbackID> feedbackIdsReached;
430 Pause wait;
431 Utils::ThreadSafeQueue<TimeTableEntry> timeTableQueue;
432 RouteID followUpRoute;
433 std::string matchKey;
434
435 LocoFunctions functions;
436
437 Logger::Logger* logger;
438 };
439 } // namespace DataModel