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
|
/*
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/>.
*/
#include <map>
#include <sstream>
#include <string>
#include "DataModel/Relation.h"
#include "Manager.h"
#include "Utils/Utils.h"
using std::map;
using std::stringstream;
using std::string;
namespace DataModel
{
std::string Relation::Serialize() const
{
stringstream ss;
ss << LockableItem::Serialize()
<< ";type=" << static_cast<int>(type)
<< ";objectType1=" << static_cast<int>(ObjectType1())
<< ";objectID1=" << object1.GetObjectID()
<< ";objectType2=" << static_cast<int>(ObjectType2())
<< ";objectID2=" << object2.GetObjectID()
<< ";priority=" << static_cast<int>(priority)
<< ";data=" << static_cast<int>(data);
return ss.str();
}
void Relation::Deserialize(const std::string& serialized)
{
map<string,string> arguments;
ParseArguments(serialized, arguments);
LockableItem::Deserialize(arguments);
object1.SetObjectType(static_cast<ObjectType>(Utils::Utils::GetIntegerMapEntry(arguments, "objectType1")));
type = static_cast<RelationType>(Utils::Utils::GetIntegerMapEntry(arguments, "type"));
object1.SetObjectID(static_cast<ObjectID>(Utils::Utils::GetIntegerMapEntry(arguments, "objectID1")));
object2.SetObjectType(static_cast<ObjectType>(Utils::Utils::GetIntegerMapEntry(arguments, "objectType2")));
object2.SetObjectID(static_cast<ObjectID>(Utils::Utils::GetIntegerMapEntry(arguments, "objectID2")));
priority = Utils::Utils::GetIntegerMapEntry(arguments, "priority");
data = Utils::Utils::GetIntegerMapEntry(arguments, "data");
}
bool Relation::Execute(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier, const Delay delay)
{
switch (ObjectType2())
{
case ObjectTypeAccessory:
{
bool ret = manager->AccessoryState(ControlTypeInternal, ObjectID2(), static_cast<AccessoryState>(data), true);
if (!ret)
{
return false;
}
break;
}
case ObjectTypeSwitch:
{
bool ret = manager->SwitchState(ControlTypeInternal, ObjectID2(), static_cast<AccessoryState>(data), true);
if (!ret)
{
return false;
}
break;
}
case ObjectTypeSignal:
{
bool ret = manager->SignalState(ControlTypeInternal, ObjectID2(), static_cast<AccessoryState>(data), true);
if (!ret)
{
return false;
}
break;
}
case ObjectTypeTrack:
manager->TrackSetLocoOrientation(ObjectID2(), static_cast<Orientation>(data));
return true;
case ObjectTypeRoute:
return manager->RouteExecute(logger, locoBaseIdentifier, ObjectID2());
case ObjectTypeLoco:
{
const DataModel::LocoFunctionNr nr = static_cast<DataModel::LocoFunctionNr>(ObjectID2());
if (data > DataModel::LocoFunctionStateOn)
{
manager->LocoBaseFunctionState(ControlTypeInternal, locoBaseIdentifier, nr, DataModel::LocoFunctionStateOn);
Utils::Utils::SleepForMilliseconds(static_cast<unsigned int>(data) * 100);
manager->LocoBaseFunctionState(ControlTypeInternal, locoBaseIdentifier, nr, DataModel::LocoFunctionStateOff);
}
else
{
manager->LocoBaseFunctionState(ControlTypeInternal, locoBaseIdentifier, nr, static_cast<DataModel::LocoFunctionState>(data));
}
return true;
}
case ObjectTypePause:
Utils::Utils::SleepForMilliseconds(static_cast<unsigned int>(data) * 100);
return true;
case ObjectTypeMultipleUnit: // abused for loco orientation
manager->LocoBaseOrientation(ControlTypeInternal, locoBaseIdentifier, static_cast<Orientation>(data));
return true;
case ObjectTypeBooster:
manager->Booster(ControlTypeInternal, static_cast<BoosterState>(data));
return true;
case ObjectTypeCounter:
return manager->Count(ObjectID2(), static_cast<CounterType>(data));
default:
return false;
}
Utils::Utils::SleepForMilliseconds(delay);
return true;
}
Object* Relation::GetObject2()
{
switch (ObjectType2())
{
case ObjectTypeAccessory:
return manager->GetAccessory(ObjectID2());
case ObjectTypeSwitch:
return manager->GetSwitch(ObjectID2());
case ObjectTypeTrack:
return manager->GetTrack(ObjectID2());
case ObjectTypeSignal:
return manager->GetSignal(ObjectID2());
case ObjectTypeRoute:
return manager->GetRoute(ObjectID2());
case ObjectTypeCounter:
return manager->GetCounter(ObjectID2());
default:
return nullptr;
}
}
bool Relation::Reserve(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
{
bool ret = LockableItem::Reserve(logger, locoBaseIdentifier);
if (!ret)
{
logger->Debug(Languages::TextUnableToReserve);
return false;
}
const ObjectType objectType2 = ObjectType2();
switch(objectType2)
{
case ObjectTypeLoco:
case ObjectTypePause:
case ObjectTypeMultipleUnit: // abused for loco orientation
case ObjectTypeBooster:
return true;
case ObjectTypeCounter:
{
Counter* counter = manager->GetCounter(ObjectID2());
if (counter)
{
return counter->Check(static_cast<CounterType>(data));
}
else
{
logger->Debug(Languages::TextRelationTargetNotFound);
LockableItem::Release(logger, locoBaseIdentifier);
return false;
}
}
default:
break;
}
LockableItem* lockable = dynamic_cast<LockableItem*>(GetObject2());
if (!lockable)
{
logger->Debug(Languages::TextRelationTargetNotFound);
LockableItem::Release(logger, locoBaseIdentifier);
return false;
}
Route* route = dynamic_cast<Route*>(lockable);
if (route)
{
return route->Reserve(logger, locoBaseIdentifier);
}
return lockable->Reserve(logger, locoBaseIdentifier);
}
bool Relation::Lock(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
{
bool ret = LockableItem::Lock(logger, locoBaseIdentifier);
if (!ret)
{
return false;
}
const ObjectType objectType2 = ObjectType2();
switch(objectType2)
{
case ObjectTypeLoco:
case ObjectTypePause:
case ObjectTypeMultipleUnit: // abused for loco orientation
case ObjectTypeBooster:
return true;
case ObjectTypeCounter:
{
Counter* counter = manager->GetCounter(ObjectID2());
if (counter)
{
return counter->Check(static_cast<CounterType>(data));
}
else
{
logger->Debug(Languages::TextRelationTargetNotFound);
LockableItem::Release(logger, locoBaseIdentifier);
return false;
}
}
default:
break;
}
LockableItem* lockable = dynamic_cast<LockableItem*>(GetObject2());
if (!lockable)
{
LockableItem::Release(logger, locoBaseIdentifier);
return false;
}
Route* route = dynamic_cast<Route*>(lockable);
if (route)
{
return route->Lock(logger, locoBaseIdentifier);
}
bool retLockable = lockable->Lock(logger, locoBaseIdentifier);
if (retLockable)
{
return true;
}
Object* object = dynamic_cast<Object*>(lockable);
if (!object)
{
return false;
}
logger->Debug(Languages::TextUnableToLock, object->GetName());
return false;
}
bool Relation::Release(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
{
LockableItem* lockable = dynamic_cast<LockableItem*>(GetObject2());
if (lockable)
{
lockable->Release(logger, locoBaseIdentifier);
}
return LockableItem::Release(logger, locoBaseIdentifier);
}
} // namespace DataModel
|