| 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 "Manager.h" |
| 22 |
#include "DataModel/MultipleUnit.h" |
| 23 |
|
| 24 |
#include <map> |
| 25 |
#include <string> |
| 26 |
|
| 27 |
using std::map; |
| 28 |
using std::string; |
| 29 |
|
| 30 |
namespace DataModel |
| 31 |
{ |
| 32 |
std::string MultipleUnit::Serialize() const |
| 33 |
{ |
| 34 |
string str = "objectType=MultipleUnit;"; |
| 35 |
str += LocoBase::Serialize(); |
| 36 |
return str; |
| 37 |
} |
| 38 |
|
| 39 |
bool MultipleUnit::Deserialize(const std::string& serialized) |
| 40 |
{ |
| 41 |
map<string,string> arguments; |
| 42 |
ParseArguments(serialized, arguments); |
| 43 |
if (!arguments.count("objectType") || arguments.at("objectType").compare("MultipleUnit") != 0) |
| 44 |
{ |
| 45 |
return false; |
| 46 |
} |
| 47 |
LocoBase::Deserialize(arguments); |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
void MultipleUnit::CalculatePropulsion() |
| 52 |
{ |
| 53 |
uint8_t newPropulsion = PropulsionUnknown; |
| 54 |
for (auto slave : slaves) |
| 55 |
{ |
| 56 |
Loco* loco = manager->GetLoco(slave->ObjectID2()); |
| 57 |
if (loco == nullptr) |
| 58 |
{ |
| 59 |
continue; |
| 60 |
} |
| 61 |
const uint8_t locoPropulsion = loco->GetPropulsion(); |
| 62 |
newPropulsion |= locoPropulsion; |
| 63 |
} |
| 64 |
LocoBase::SetPropulsion(static_cast<Propulsion>(newPropulsion)); |
| 65 |
} |
| 66 |
|
| 67 |
void MultipleUnit::DeleteSlaves() |
| 68 |
{ |
| 69 |
while (slaves.size() > 0) |
| 70 |
{ |
| 71 |
Relation* slave = slaves.back(); |
| 72 |
slaves.pop_back(); |
| 73 |
delete slave; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
bool MultipleUnit::AssignSlaves(const std::vector<DataModel::Relation*>& newslaves) |
| 78 |
{ |
| 79 |
DeleteSlaves(); |
| 80 |
slaves = newslaves; |
| 81 |
CalculatePropulsion(); |
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
void MultipleUnit::SetSpeed(const Speed speed) |
| 86 |
{ |
| 87 |
LocoBase::SetSpeed(speed); |
| 88 |
for (auto slave : slaves) |
| 89 |
{ |
| 90 |
manager->LocoBaseSpeed(ControlTypeInternal, slave->ObjectIdentifier2(), speed); |
| 91 |
Utils::Utils::SleepForMilliseconds(10); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void MultipleUnit::SetFunctionState(const DataModel::LocoFunctionNr nr, |
| 96 |
const DataModel::LocoFunctionState state) |
| 97 |
{ |
| 98 |
LocoBase::SetFunctionState(nr, state); |
| 99 |
for (auto slave : slaves) |
| 100 |
{ |
| 101 |
manager->LocoBaseFunctionState(ControlTypeInternal, slave->ObjectIdentifier2(), nr, state); |
| 102 |
Utils::Utils::SleepForMilliseconds(10); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
void MultipleUnit::SetOrientation(const Orientation orientation) |
| 107 |
{ |
| 108 |
LocoBase::SetOrientation(orientation); |
| 109 |
for (auto slave : slaves) |
| 110 |
{ |
| 111 |
manager->LocoBaseOrientation(ControlTypeInternal, slave->ObjectIdentifier2(), OrientationChange); |
| 112 |
Utils::Utils::SleepForMilliseconds(10); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
} // namespace DataModel |