File: LocoBase.h

package info (click to toggle)
railcontrol 24%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,540 kB
  • sloc: cpp: 40,823; javascript: 2,509; makefile: 110; php: 97; sh: 60
file content (439 lines) | stat: -rw-r--r-- 10,358 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
RailControl - Model Railway Control Software

Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org

RailControl is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.

RailControl is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RailControl; see the file LICENCE. If not see
<http://www.gnu.org/licenses/>.
*/

#pragma once

#include <mutex>
#include <string>
#include <thread>
#include <vector>

#include "DataTypes.h"
#include "Logger/Logger.h"
#include "DataModel/HardwareHandle.h"
#include "DataModel/LocoFunctions.h"
#include "DataModel/Object.h"
#include "DataModel/Relation.h"
#include "DataModel/Route.h"
#include "Utils/ThreadSafeQueue.h"

class Manager;

namespace Hardware
{
		class LocoCacheEntry;
}

namespace DataModel
{
	class ObjectIdentifier;
	class Route;
	class Track;

	class LocoBase : public Object, public HardwareHandle
	{
		public:
			enum NrOfTracksToReserve : unsigned char
			{
				ReserveOne = 1,
				ReserveTwo = 2
			};

			enum AutoModeType : unsigned char
			{
				AutoModeTypeFull = 0,
				AutoModeTypeTimetable
			};

			LocoBase() = delete;
			LocoBase(const LocoBase&) = delete;
			LocoBase& operator=(const LocoBase&) = delete;

			inline LocoBase(Manager* manager, const LocoID locoID)
			:	Object(locoID),
				HardwareHandle(),
				manager(manager),
				length(0),
				pushpull(false),
				maxSpeed(0),
				travelSpeed(0),
				reducedSpeed(0),
				creepingSpeed(0),
				propulsion(PropulsionUnknown),
				trainType(TrainTypeUnknown),
				speed(MinSpeed),
				orientation(OrientationRight),
				state(LocoStateManual),
				requestManualMode(false),
				trackFrom(nullptr),
				trackFirst(nullptr),
				trackSecond(nullptr),
				routeFirst(nullptr),
				routeSecond(nullptr),
				feedbackIdFirstReduced(FeedbackNone),
				feedbackIdFirstCreep(FeedbackNone),
				feedbackIdFirst(FeedbackNone),
				feedbackIdReduced(FeedbackNone),
				feedbackIdCreep(FeedbackNone),
				feedbackIdStop(FeedbackNone),
				feedbackIdOver(FeedbackNone),
				feedbackIdsReached(),
				wait(0),
				followUpRoute(RouteAuto),
				matchKey("")
			{
				logger = Logger::Logger::GetLogger(GetName());
			}

			inline LocoBase(Manager* manager, const std::string& serialized)
			:	LocoBase(manager, LocoNone)
			{
				LocoBase::Deserialize(serialized);
				logger = Logger::Logger::GetLogger(GetName());
			}

			virtual ~LocoBase();

			inline Logger::Logger* GetLogger() const
			{
				return logger;
			}

			std::string Serialize() const override;

			void Deserialize(const std::string& serialized) override;

			void Deserialize(const std::map<std::string,std::string>& arguments) override;

			virtual void SetName(const std::string& name) override
			{
				Object::SetName(name);
				logger = Logger::Logger::GetLogger(name);
			}

			bool GoToAutoMode();

			void RequestManualMode();

			bool GoToManualMode();

			void AddTimeTable(const Route* route, const RouteID followUpRoute);

			bool SetTrack(const TrackID trackID);

			TrackID GetTrackId() const;

			bool Release();

			void ReleaseRouteAndTrack();

			bool CheckFreeingTrack(const TrackID trackID) const;

			void LocationReached(const FeedbackID feedbackID);

			virtual void SetSpeed(const Speed speed);

			inline Speed GetSpeed() const
			{
				return speed;
			}

			virtual void SetFunctionState(const DataModel::LocoFunctionNr nr,
				const DataModel::LocoFunctionState state);

			inline DataModel::LocoFunctionState GetFunctionState(const DataModel::LocoFunctionNr nr) const
			{
				return functions.GetFunctionState(nr);
			}

			inline std::vector<DataModel::LocoFunctionEntry> GetFunctionStates() const
			{
				return functions.GetFunctionStates();
			}

			inline void GetFunctions(LocoFunctionEntry* out) const
			{
				functions.GetFunctions(out);
			}

			inline void ConfigureFunctions(const std::vector<LocoFunctionEntry>& newEntries)
			{
				functions.ConfigureFunctions(newEntries);
			}

			virtual void SetOrientation(const Orientation orientation);

			inline Orientation GetOrientation() const
			{
				return orientation;
			}

			inline bool IsInManualMode() const
			{
				return this->state == LocoStateManual;
			}

			inline bool IsInUse() const
			{
				return this->speed > 0
					|| this->state != LocoStateManual
					|| this->trackFrom != nullptr
					|| this->routeFirst != nullptr;
			}

			inline Length GetLength() const
			{
				return length;
			}

			inline void SetLength(const Length length)
			{
				this->length = length;
			}

			inline bool GetPushpull() const
			{
				return pushpull;
			}

			inline Speed GetMaxSpeed() const
			{
				return maxSpeed;
			}

			inline Speed GetTravelSpeed() const
			{
				return travelSpeed;
			}

			inline Speed GetReducedSpeed() const
			{
				return reducedSpeed;
			}

			inline Speed GetCreepingSpeed() const
			{
				return creepingSpeed;
			}

			inline void SetPushpull(const bool pushpull)
			{
				this->pushpull = pushpull;
			}

			inline void SetMaxSpeed(const Speed speed)
			{
				maxSpeed = speed;
			}

			inline void SetTravelSpeed(const Speed speed)
			{
				travelSpeed = speed;
			}

			inline void SetReducedSpeed(const Speed speed)
			{
				reducedSpeed = speed;
			}

			inline void SetCreepingSpeed(const Speed speed)
			{
				creepingSpeed = speed;
			}

			inline void SetPropulsion(const Propulsion propulsion)
			{
				this->propulsion = propulsion;
			}

			inline Propulsion GetPropulsion() const
			{
				return propulsion;
			}

			inline void SetTrainType(const TrainType trainType)
			{
				this->trainType = trainType;
			}

			inline TrainType GetTrainType() const
			{
				return trainType;
			}

			inline void SetMatchKey(const std::string& matchKey)
			{
				this->matchKey = matchKey;
			}

			inline void ClearMatchKey()
			{
				matchKey.clear();
			}

			inline std::string GetMatchKey() const
			{
				return matchKey;
			}

			DataModel::LocoFunctionNr GetFunctionNumberFromFunctionIcon(const DataModel::LocoFunctionIcon icon) const;

			DataModel::LocoFunctionIcon GetFunctionIcon(const DataModel::LocoFunctionNr nr) const
			{
				return functions.GetFunctionIcon(nr);
			}

			DataModel::LocoFunctionType GetFunctionType(const DataModel::LocoFunctionNr nr) const
			{
				return functions.GetFunctionType(nr);
			}

			inline LocoID GetLocoIdWithPrefix() const
			{
				return GetID() + (GetObjectType() == ObjectTypeMultipleUnit ? MultipleUnitIdPrefix : 0);
			}

			LocoBase& operator=(const Hardware::LocoCacheEntry& loco);

		protected:
			Manager* manager;

		private:
			typedef std::pair<RouteID,RouteID> TimeTableEntry;

			enum LocoState : unsigned char
			{
				LocoStateManual = 0,
				LocoStateTerminated,
				LocoStateOff,
				LocoStateAutomodeGetFirst,
				LocoStateAutomodeGetSecond,
				LocoStateAutomodeRunning,
				LocoStateStopping,
				LocoStateError
			};

			void SetMinThreadPriorityAndThreadName();

			void AutoMode();

			Route* GetNextDestination(const Track* const track, const bool allowLocoTurn);

			void GetTimetableDestinationFirst();

			void PrepareDestinationFirst(Route* const route);

			void GetTimetableDestinationSecond();

			DataModel::Route* GetDestinationFromTimeTable(const Track* const track, const bool allowLocoTurn);

			void PrepareDestinationSecond(Route* const route);

			DataModel::Route* SearchDestination(const DataModel::Track* const oldToTrack, const bool allowLocoTurn);

			bool ReserveRoute(const Track* const track, const bool allowLocoTurn, Route* const route);

			bool ExecuteRoute(const Track* const track, const bool allowLocoTurn, Route* const route);

			void FeedbackIdFirstReached();

			void FeedbackIdStopReached();

			void ShiftRoute();

			void ForceManualMode();

			bool GoToAutoModeInternal(const LocoState newState);

			Speed GetRouteSpeed(const Route::Speed routeSpeed);

			void LocationStopReached(const FeedbackID feedbackID, const Delay stopDelay);

			void LocationStopReached();

			static inline void LocationStopReachedStatic(LocoBase* locoBase, const FeedbackID feedbackID, const Delay stopDelay)
			{
				locoBase->LocationStopReached(feedbackID, stopDelay);
			}

			void LocationCreepReached(const FeedbackID feedbackID, const Delay creepDelay);

			void LocationCreepReached();

			static inline void LocationCreepReachedStatic(LocoBase* locoBase, const FeedbackID feedbackID, const Delay creepDelay)
			{
				locoBase->LocationCreepReached(feedbackID, creepDelay);
			}

			void LocationReducedReached(const FeedbackID feedbackID, const Delay reducedDelay);

			void LocationReducedReached();

			static inline void LocationReducedReachedStatic(LocoBase* locoBase, const FeedbackID feedbackID, const Delay reducedDelay)
			{
				locoBase->LocationReducedReached(feedbackID, reducedDelay);
			}

			mutable std::mutex stateMutex;
			std::thread locoThread;

			Length length;
			bool pushpull;
			Speed maxSpeed;
			Speed travelSpeed;
			Speed reducedSpeed;
			Speed creepingSpeed;

			Propulsion propulsion;
			TrainType trainType;

			Speed speed;
			Orientation orientation;

			volatile LocoState state;
			volatile bool requestManualMode;
			Track* trackFrom;
			Track* trackFirst;
			Track* trackSecond;
			Route* routeFirst;
			Route* routeSecond;
			Utils::ThreadSafeQueue<Track*> releaseTrackQueue;
			Utils::ThreadSafeQueue<Route*> releaseRouteQueue;
			volatile FeedbackID feedbackIdFirstReduced;
			volatile FeedbackID feedbackIdFirstCreep;
			volatile FeedbackID feedbackIdFirst;
			volatile FeedbackID feedbackIdReduced;
			volatile Delay reducedDelay;
			volatile FeedbackID feedbackIdCreep;
			volatile Delay creepDelay;
			volatile FeedbackID feedbackIdStop;
			volatile Delay stopDelay;
			volatile FeedbackID feedbackIdOver;
			Utils::ThreadSafeQueue<FeedbackID> feedbackIdsReached;
			Pause wait;
			Utils::ThreadSafeQueue<TimeTableEntry> timeTableQueue;
			RouteID followUpRoute;
			std::string matchKey;

			LocoFunctions functions;

			Logger::Logger* logger;
	};
} // namespace DataModel