| 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 <future> |
| 22 |
#include <iostream> |
| 23 |
#include <sstream> |
| 24 |
#include <unistd.h> |
| 25 |
|
| 26 |
#include "DataModel/LayoutItem.h" |
| 27 |
#include "Languages.h" |
| 28 |
#include "Hardware/HardwareHandler.h" |
| 29 |
#include "Hardware/HardwareParams.h" |
| 30 |
#include "Hardware/Protocols/MaerklinCANCommon.h" |
| 31 |
#include "Manager.h" |
| 32 |
#include "RailControl.h" |
| 33 |
#include "Storage/TransactionGuard.h" |
| 34 |
#include "Utils/Integer.h" |
| 35 |
#include "Utils/Utils.h" |
| 36 |
#include "Server/CS2/CS2Server.h" |
| 37 |
#include "Server/Web/WebServer.h" |
| 38 |
#include "Server/Z21/Z21Server.h" |
| 39 |
|
| 40 |
using namespace DataModel; |
| 41 |
using LayoutPosition = DataModel::LayoutItem::LayoutPosition; |
| 42 |
using LayoutItemSize = DataModel::LayoutItem::LayoutItemSize; |
| 43 |
using LayoutRotation = DataModel::LayoutItem::LayoutRotation; |
| 44 |
using Visible = DataModel::LayoutItem::Visible; |
| 45 |
using Hardware::HardwareHandler; |
| 46 |
using Hardware::HardwareParams; |
| 47 |
using std::map; |
| 48 |
using std::to_string; |
| 49 |
using std::string; |
| 50 |
using std::stringstream; |
| 51 |
using std::vector; |
| 52 |
using Storage::TransactionGuard; |
| 53 |
using Storage::StorageHandler; |
| 54 |
using Storage::StorageParams; |
| 55 |
|
| 56 |
Manager::Manager(Config& config) |
| 57 |
: logger(Logger::Logger::GetLogger(Languages::GetText(Languages::TextManager))), |
| 58 |
boosterState(BoosterStateStop), |
| 59 |
storage(nullptr), |
| 60 |
startupInitLocos(StartupInitLocosAll), |
| 61 |
defaultAccessoryDuration(DataModel::DefaultAccessoryPulseDuration), |
| 62 |
autoAddFeedback(false), |
| 63 |
stopOnFeedbackInFreeTrack(true), |
| 64 |
executeAccessory(true), |
| 65 |
selectRouteApproach(DataModel::SelectRouteRandom), |
| 66 |
nrOfTracksToReserve(DataModel::Loco::ReserveOne), |
| 67 |
run(false), |
| 68 |
debounceRun(false), |
| 69 |
initLocosDone(false), |
| 70 |
serverEnabled(false), |
| 71 |
unknownControl(Languages::GetText(Languages::TextControlDoesNotExist)), |
| 72 |
unknownLoco(Languages::GetText(Languages::TextLocoDoesNotExist)), |
| 73 |
unknownMultipleUnit(Languages::GetText(Languages::TextMultipleUnitDoesNotExist)), |
| 74 |
unknownAccessory(Languages::GetText(Languages::TextAccessoryDoesNotExist)), |
| 75 |
unknownFeedback(Languages::GetText(Languages::TextFeedbackDoesNotExist)), |
| 76 |
unknownTrack(Languages::GetText(Languages::TextTrackDoesNotExist)), |
| 77 |
unknownSwitch(Languages::GetText(Languages::TextSwitchDoesNotExist)), |
| 78 |
unknownRoute(Languages::GetText(Languages::TextRouteDoesNotExist)), |
| 79 |
unknownSignal(Languages::GetText(Languages::TextSignalDoesNotExist)) |
| 80 |
{ |
| 81 |
StorageParams storageParams; |
| 82 |
storageParams.module = "Sqlite"; |
| 83 |
storageParams.filename = config.getStringValue("dbfilename", "railcontrol.sqlite"); |
| 84 |
storageParams.keepBackups = config.getIntValue("dbkeepbackups", 10); |
| 85 |
storage = new StorageHandler(this, &storageParams); |
| 86 |
if (!storage) |
| 87 |
{ |
| 88 |
logger->Info(Languages::TextUnableToCreateStorageHandler); |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
Logger::Logger::SetLogLevel(static_cast<Logger::Logger::Level>(Utils::Integer::StringToInteger(storage->GetSetting("LogLevel"), Logger::Logger::LevelInfo))); |
| 93 |
Languages::SetDefaultLanguage(static_cast<Languages::Language>(Utils::Integer::StringToInteger(storage->GetSetting("Language"), Languages::EN))); |
| 94 |
startupInitLocos = static_cast<StartupInitLocos>(Utils::Integer::StringToInteger(storage->GetSetting("StartupInitLocos"), StartupInitLocosAll)); |
| 95 |
defaultAccessoryDuration = Utils::Integer::StringToInteger(storage->GetSetting("DefaultAccessoryDuration"), 250); |
| 96 |
autoAddFeedback = Utils::Utils::StringToBool(storage->GetSetting("AutoAddFeedback")); |
| 97 |
stopOnFeedbackInFreeTrack = Utils::Utils::StringToBool(storage->GetSetting("StopOnFeedbackInFreeTrack"), true); |
| 98 |
executeAccessory = Utils::Utils::StringToBool(storage->GetSetting("ExecuteAccessory"), true); |
| 99 |
selectRouteApproach = static_cast<DataModel::SelectRouteApproach>(Utils::Integer::StringToInteger(storage->GetSetting("SelectRouteApproach"))); |
| 100 |
nrOfTracksToReserve = static_cast<DataModel::Loco::NrOfTracksToReserve>(Utils::Integer::StringToInteger(storage->GetSetting("NrOfTracksToReserve"), 2)); |
| 101 |
|
| 102 |
controls[ControlIdWebServer] = new Server::Web::WebServer(*this, config.getStringValue("webserveraddress", "any"), config.getIntValue("webserverport", 8082)); |
| 103 |
|
| 104 |
if (config.getBoolValue("z21server", false)) |
| 105 |
{ |
| 106 |
controls[ControlIdZ21Server] = new Server::Z21::Z21Server(*this, Server::Z21::Z21Server::Z21Port); |
| 107 |
serverEnabled = true; |
| 108 |
} |
| 109 |
|
| 110 |
if (config.getBoolValue("cs2server", false)) |
| 111 |
{ |
| 112 |
controls[ControlIdCS2Server] = new Server::CS2::CS2Server(*this); |
| 113 |
serverEnabled = true; |
| 114 |
} |
| 115 |
|
| 116 |
storage->AllHardwareParams(hardwareParams); |
| 117 |
for (auto& hardwareParam : hardwareParams) |
| 118 |
{ |
| 119 |
hardwareParam.second->SetManager(this); |
| 120 |
controls[hardwareParam.second->GetControlID()] = new HardwareHandler(hardwareParam.second); |
| 121 |
logger->Info(Languages::TextLoadedControl, hardwareParam.first, hardwareParam.second->GetName()); |
| 122 |
} |
| 123 |
|
| 124 |
storage->AllLayers(layers); |
| 125 |
for (auto& layer : layers) |
| 126 |
{ |
| 127 |
logger->Info(Languages::TextLoadedLayer, layer.second->GetID(), layer.second->GetName()); |
| 128 |
} |
| 129 |
if (layers.count(LayerUndeletable) != 1) |
| 130 |
{ |
| 131 |
string result; |
| 132 |
const bool initLayer1 = LayerSave(0, Languages::GetText(Languages::TextLayer1), result); |
| 133 |
if (!initLayer1) |
| 134 |
{ |
| 135 |
logger->Error(Languages::TextUnableToAddLayer1); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
storage->AllTexts(texts); |
| 140 |
for (auto& text : texts) |
| 141 |
{ |
| 142 |
logger->Info(Languages::TextLoadedText, text.second->GetID(), text.second->GetName()); |
| 143 |
} |
| 144 |
|
| 145 |
storage->AllAccessories(accessories); |
| 146 |
for (auto& accessory : accessories) |
| 147 |
{ |
| 148 |
logger->Info(Languages::TextLoadedAccessory, accessory.second->GetID(), accessory.second->GetName()); |
| 149 |
} |
| 150 |
|
| 151 |
storage->AllFeedbacks(feedbacks); |
| 152 |
for (auto& feedback : feedbacks) |
| 153 |
{ |
| 154 |
logger->Info(Languages::TextLoadedFeedback, feedback.second->GetID(), feedback.second->GetName()); |
| 155 |
} |
| 156 |
|
| 157 |
storage->AllSignals(signals); |
| 158 |
for (auto& signal : signals) |
| 159 |
{ |
| 160 |
logger->Info(Languages::TextLoadedSignal, signal.second->GetID(), signal.second->GetName()); |
| 161 |
} |
| 162 |
|
| 163 |
storage->AllTracks(tracks); |
| 164 |
for (auto& t : tracks) |
| 165 |
{ |
| 166 |
Track* track = t.second; |
| 167 |
track->UpdateMain(); |
| 168 |
Track* main = track->GetMain(); |
| 169 |
if (main) |
| 170 |
{ |
| 171 |
main->AddExtension(track); |
| 172 |
} |
| 173 |
logger->Info(Languages::TextLoadedTrack, track->GetID(), track->GetName()); |
| 174 |
} |
| 175 |
|
| 176 |
storage->AllSwitches(switches); |
| 177 |
for (auto& mySwitch : switches) |
| 178 |
{ |
| 179 |
logger->Info(Languages::TextLoadedSwitch, mySwitch.second->GetID(), mySwitch.second->GetName()); |
| 180 |
} |
| 181 |
|
| 182 |
storage->AllClusters(clusters); |
| 183 |
for (auto& cluster : clusters) |
| 184 |
{ |
| 185 |
logger->Info(Languages::TextLoadedCluster, cluster.second->GetID(), cluster.second->GetName()); |
| 186 |
} |
| 187 |
|
| 188 |
storage->AllRoutes(routes); |
| 189 |
for (auto& route : routes) |
| 190 |
{ |
| 191 |
logger->Info(Languages::TextLoadedRoute, route.second->GetID(), route.second->GetName()); |
| 192 |
} |
| 193 |
|
| 194 |
storage->AllLocos(locos); |
| 195 |
for (auto& loco : locos) |
| 196 |
{ |
| 197 |
logger->Info(Languages::TextLoadedLoco, loco.second->GetID(), loco.second->GetName()); |
| 198 |
} |
| 199 |
|
| 200 |
storage->AllMultipleUnits(multipleUnits); |
| 201 |
for (auto& multipleUnit : multipleUnits) |
| 202 |
{ |
| 203 |
logger->Info(Languages::TextLoadedMultipleUnit, multipleUnit.second->GetID(), multipleUnit.second->GetName()); |
| 204 |
} |
| 205 |
|
| 206 |
debounceRun = true; |
| 207 |
debounceThread = std::thread(&Manager::DebounceWorker, this); |
| 208 |
|
| 209 |
{ |
| 210 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 211 |
for (auto& control : controls) |
| 212 |
{ |
| 213 |
control.second->Start(); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
run = true; |
| 218 |
InitLocos(); |
| 219 |
} |
| 220 |
|
| 221 |
Manager::~Manager() |
| 222 |
{ |
| 223 |
while (!LocoBaseStopAll()) |
| 224 |
{ |
| 225 |
Utils::Utils::SleepForSeconds(1); |
| 226 |
} |
| 227 |
|
| 228 |
debounceRun = false; |
| 229 |
debounceThread.join(); |
| 230 |
|
| 231 |
Booster(ControlTypeInternal, BoosterStateStop); |
| 232 |
|
| 233 |
run = false; |
| 234 |
{ |
| 235 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 236 |
for (auto& control : controls) |
| 237 |
{ |
| 238 |
control.second->Stop(); |
| 239 |
} |
| 240 |
} |
| 241 |
Utils::Utils::SleepForSeconds(1); |
| 242 |
{ |
| 243 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 244 |
for (auto& control : controls) |
| 245 |
{ |
| 246 |
ControlID controlID = control.first; |
| 247 |
if (controlID < ControlIdFirstHardware) |
| 248 |
{ |
| 249 |
delete control.second; |
| 250 |
continue; |
| 251 |
} |
| 252 |
if (hardwareParams.count(controlID) != 1) |
| 253 |
{ |
| 254 |
continue; |
| 255 |
} |
| 256 |
HardwareParams* params = hardwareParams.at(controlID); |
| 257 |
if (!params) |
| 258 |
{ |
| 259 |
continue; |
| 260 |
} |
| 261 |
logger->Info(Languages::TextUnloadingControl, controlID, params->GetName()); |
| 262 |
delete control.second; |
| 263 |
hardwareParams.erase(controlID); |
| 264 |
if (storage) |
| 265 |
{ |
| 266 |
logger->Info(Languages::TextSaving, params->GetName()); |
| 267 |
TransactionGuard guard(storage); |
| 268 |
storage->Save(*params); |
| 269 |
} |
| 270 |
delete params; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
{ |
| 275 |
Storage::TransactionGuard guard(storage); |
| 276 |
DeleteAllMapEntries(multipleUnits, multipleUnitMutex); |
| 277 |
DeleteAllMapEntries(locos, locoMutex); |
| 278 |
DeleteAllMapEntries(routes, routeMutex); |
| 279 |
DeleteAllMapEntries(clusters, clusterMutex); |
| 280 |
DeleteAllMapEntries(switches, switchMutex); |
| 281 |
DeleteAllMapEntries(tracks, trackMutex); |
| 282 |
DeleteAllMapEntries(signals, signalMutex); |
| 283 |
DeleteAllMapEntries(feedbacks, feedbackMutex); |
| 284 |
DeleteAllMapEntries(accessories, accessoryMutex); |
| 285 |
DeleteAllMapEntries(texts, textMutex); |
| 286 |
DeleteAllMapEntries(layers, layerMutex); |
| 287 |
} |
| 288 |
|
| 289 |
if (!storage) |
| 290 |
{ |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
delete storage; |
| 295 |
storage = nullptr; |
| 296 |
} |
| 297 |
|
| 298 |
/*************************** |
| 299 |
* Booster * |
| 300 |
***************************/ |
| 301 |
|
| 302 |
void Manager::Booster(const ControlType controlType, const BoosterState state) |
| 303 |
{ |
| 304 |
if (!run) |
| 305 |
{ |
| 306 |
return; |
| 307 |
} |
| 308 |
boosterState = state; |
| 309 |
{ |
| 310 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 311 |
for (auto& control : controls) |
| 312 |
{ |
| 313 |
control.second->Booster(controlType, state); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
if (boosterState != BoosterStateGo || initLocosDone) |
| 318 |
{ |
| 319 |
return; |
| 320 |
} |
| 321 |
|
| 322 |
__attribute__((unused)) auto r = std::async(std::launch::async, InitLocosStatic, this); |
| 323 |
initLocosDone = true; |
| 324 |
} |
| 325 |
|
| 326 |
void Manager::InitLocos() |
| 327 |
{ |
| 328 |
if (GetStartupInitLocos() == StartupInitLocosNone) |
| 329 |
{ |
| 330 |
return; |
| 331 |
} |
| 332 |
Utils::Utils::SleepForSeconds(1); |
| 333 |
map<string,LocoID> locoIds = LocoIdsByName(); |
| 334 |
for (auto locoId : locoIds) |
| 335 |
{ |
| 336 |
if (!run) |
| 337 |
{ |
| 338 |
return; |
| 339 |
} |
| 340 |
Loco* loco = GetLoco(locoId.second); |
| 341 |
if (!loco) |
| 342 |
{ |
| 343 |
continue; |
| 344 |
} |
| 345 |
|
| 346 |
if (GetStartupInitLocos() == StartupInitLocosSpeed) |
| 347 |
{ |
| 348 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 349 |
for (auto& control : controls) |
| 350 |
{ |
| 351 |
control.second->LocoBaseSpeed(ControlTypeInternal, loco, loco->GetSpeed()); |
| 352 |
control.second->LocoBaseOrientation(ControlTypeInternal, loco, loco->GetOrientation()); |
| 353 |
} |
| 354 |
} |
| 355 |
else |
| 356 |
{ |
| 357 |
std::vector<DataModel::LocoFunctionEntry> functions = loco->GetFunctionStates(); |
| 358 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 359 |
for (auto& control : controls) |
| 360 |
{ |
| 361 |
control.second->LocoBaseSpeedOrientationFunctions(loco, loco->GetSpeed(), loco->GetOrientation(), functions); |
| 362 |
} |
| 363 |
} |
| 364 |
Utils::Utils::SleepForMilliseconds(10); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
/*************************** |
| 369 |
* Control * |
| 370 |
***************************/ |
| 371 |
|
| 372 |
bool Manager::ControlSave(ControlID controlID, |
| 373 |
const HardwareType& hardwareType, |
| 374 |
const std::string& name, |
| 375 |
const std::string& arg1, |
| 376 |
const std::string& arg2, |
| 377 |
const std::string& arg3, |
| 378 |
const std::string& arg4, |
| 379 |
const std::string& arg5, |
| 380 |
string& result) |
| 381 |
{ |
| 382 |
if (controlID != ControlIdNone && controlID < ControlIdFirstHardware) |
| 383 |
{ |
| 384 |
result = Languages::GetText(Languages::TextInvalidControlID); |
| 385 |
return false; |
| 386 |
} |
| 387 |
|
| 388 |
HardwareParams* params = GetHardware(controlID); |
| 389 |
bool newControl = false; |
| 390 |
if (!params) |
| 391 |
{ |
| 392 |
params = CreateAndAddControl(); |
| 393 |
newControl = true; |
| 394 |
} |
| 395 |
|
| 396 |
if (!params) |
| 397 |
{ |
| 398 |
result = Languages::GetText(Languages::TextUnableToAddControl); |
| 399 |
return false; |
| 400 |
} |
| 401 |
|
| 402 |
// if we have a new object we have to update controlID |
| 403 |
controlID = params->GetControlID(); |
| 404 |
|
| 405 |
params->SetName(CheckObjectName(hardwareParams, hardwareMutex, controlID, name.size() == 0 ? "C" : name)); |
| 406 |
params->SetHardwareType(hardwareType); |
| 407 |
params->SetArg1(arg1); |
| 408 |
params->SetArg2(arg2); |
| 409 |
params->SetArg3(arg3); |
| 410 |
params->SetArg4(arg4); |
| 411 |
params->SetArg5(arg5); |
| 412 |
|
| 413 |
if (storage) |
| 414 |
{ |
| 415 |
TransactionGuard guard(storage); |
| 416 |
storage->Save(*params); |
| 417 |
} |
| 418 |
|
| 419 |
if (newControl) |
| 420 |
{ |
| 421 |
std::lock_guard<std::mutex> Guard(controlMutex); |
| 422 |
ControlInterface* control = new HardwareHandler(params); |
| 423 |
if (!control) |
| 424 |
{ |
| 425 |
return false; |
| 426 |
} |
| 427 |
controls[controlID] = control; |
| 428 |
return true; |
| 429 |
} |
| 430 |
|
| 431 |
ControlInterface* control = GetControl(controlID); |
| 432 |
if (!control) |
| 433 |
{ |
| 434 |
return false; |
| 435 |
} |
| 436 |
|
| 437 |
control->ReInit(params); |
| 438 |
return true; |
| 439 |
} |
| 440 |
|
| 441 |
bool Manager::ControlDelete(ControlID controlID) |
| 442 |
{ |
| 443 |
{ |
| 444 |
std::lock_guard<std::mutex> guard(hardwareMutex); |
| 445 |
if (controlID < ControlIdFirstHardware || hardwareParams.count(controlID) != 1) |
| 446 |
{ |
| 447 |
return false; |
| 448 |
} |
| 449 |
|
| 450 |
HardwareParams* params = hardwareParams.at(controlID); |
| 451 |
if (!params) |
| 452 |
{ |
| 453 |
return false; |
| 454 |
} |
| 455 |
hardwareParams.erase(controlID); |
| 456 |
delete params; |
| 457 |
} |
| 458 |
{ |
| 459 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 460 |
if (controls.count(controlID) != 1) |
| 461 |
{ |
| 462 |
return false; |
| 463 |
} |
| 464 |
ControlInterface* control = controls.at(controlID); |
| 465 |
if (!control) |
| 466 |
{ |
| 467 |
return false; |
| 468 |
} |
| 469 |
controls.erase(controlID); |
| 470 |
delete control; |
| 471 |
} |
| 472 |
|
| 473 |
if (storage) |
| 474 |
{ |
| 475 |
TransactionGuard guard(storage); |
| 476 |
storage->DeleteHardwareParams(controlID); |
| 477 |
} |
| 478 |
return true; |
| 479 |
} |
| 480 |
|
| 481 |
HardwareParams* Manager::GetHardware(const ControlID controlID) |
| 482 |
{ |
| 483 |
std::lock_guard<std::mutex> guard(hardwareMutex); |
| 484 |
if (hardwareParams.count(controlID) != 1) |
| 485 |
{ |
| 486 |
return nullptr; |
| 487 |
} |
| 488 |
return hardwareParams.at(controlID); |
| 489 |
} |
| 490 |
|
| 491 |
unsigned int Manager::ControlsOfHardwareType(const HardwareType hardwareType) |
| 492 |
{ |
| 493 |
std::lock_guard<std::mutex> guard(hardwareMutex); |
| 494 |
unsigned int counter = 0; |
| 495 |
for (auto& hardwareParam : hardwareParams) |
| 496 |
{ |
| 497 |
if (hardwareParam.second->GetHardwareType() == hardwareType) |
| 498 |
{ |
| 499 |
++counter; |
| 500 |
} |
| 501 |
} |
| 502 |
return counter; |
| 503 |
} |
| 504 |
|
| 505 |
bool Manager::ControlIsOfHardwareType(const ControlID controlID, const HardwareType hardwareType) |
| 506 |
{ |
| 507 |
std::lock_guard<std::mutex> guard(hardwareMutex); |
| 508 |
for (auto& hardwareParam : hardwareParams) |
| 509 |
{ |
| 510 |
if (hardwareParam.second->GetControlID() != controlID) |
| 511 |
{ |
| 512 |
continue; |
| 513 |
} |
| 514 |
return hardwareParam.second->GetHardwareType() == hardwareType; |
| 515 |
} |
| 516 |
return false; |
| 517 |
} |
| 518 |
|
| 519 |
ControlInterface* Manager::GetControl(const ControlID controlID) const |
| 520 |
{ |
| 521 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 522 |
if (controls.count(controlID) != 1) |
| 523 |
{ |
| 524 |
return nullptr; |
| 525 |
} |
| 526 |
return controls.at(controlID); |
| 527 |
} |
| 528 |
|
| 529 |
const std::string Manager::GetControlName(const ControlID controlID) |
| 530 |
{ |
| 531 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 532 |
if (controls.count(controlID) != 1) |
| 533 |
{ |
| 534 |
return unknownControl; |
| 535 |
} |
| 536 |
ControlInterface* control = controls.at(controlID); |
| 537 |
return control->GetName(); |
| 538 |
} |
| 539 |
|
| 540 |
const std::map<ControlID,std::string> Manager::ControlListNames(const Hardware::Capabilities capability) const |
| 541 |
{ |
| 542 |
std::map<ControlID,std::string> ret; |
| 543 |
std::lock_guard<std::mutex> guard(hardwareMutex); |
| 544 |
for (auto& hardware : hardwareParams) |
| 545 |
{ |
| 546 |
std::lock_guard<std::mutex> guard2(controlMutex); |
| 547 |
if (controls.count(hardware.second->GetControlID()) != 1) |
| 548 |
{ |
| 549 |
continue; |
| 550 |
} |
| 551 |
ControlInterface* c = controls.at(hardware.second->GetControlID()); |
| 552 |
if (!c->CanHandle(capability)) |
| 553 |
{ |
| 554 |
continue; |
| 555 |
} |
| 556 |
ret[hardware.first] = hardware.second->GetName(); |
| 557 |
} |
| 558 |
return ret; |
| 559 |
} |
| 560 |
|
| 561 |
const map<string,Hardware::HardwareParams*> Manager::ControlListByName() const |
| 562 |
{ |
| 563 |
map<string,Hardware::HardwareParams*> out; |
| 564 |
std::lock_guard<std::mutex> guard(hardwareMutex); |
| 565 |
for (auto& hardware : hardwareParams) |
| 566 |
{ |
| 567 |
out[hardware.second->GetName()] = hardware.second; |
| 568 |
} |
| 569 |
return out; |
| 570 |
} |
| 571 |
|
| 572 |
const std::map<std::string, Protocol> Manager::ProtocolsOfControl(const AddressType type, const ControlID controlID) const |
| 573 |
{ |
| 574 |
std::map<std::string,Protocol> ret; |
| 575 |
{ |
| 576 |
const ControlInterface* control = GetControl(controlID); |
| 577 |
if (!control || control->GetControlType() != ControlTypeHardware) |
| 578 |
{ |
| 579 |
ret[ProtocolSymbols[ProtocolNone]] = ProtocolNone; |
| 580 |
return ret; |
| 581 |
} |
| 582 |
|
| 583 |
const HardwareHandler* hardware = static_cast<const HardwareHandler*>(control); |
| 584 |
if (hardware->GetControlID() != controlID) |
| 585 |
{ |
| 586 |
ret[ProtocolSymbols[ProtocolNone]] = ProtocolNone; |
| 587 |
return ret; |
| 588 |
} |
| 589 |
|
| 590 |
std::vector<Protocol> protocols; |
| 591 |
if (type == AddressTypeLoco) |
| 592 |
{ |
| 593 |
hardware->LocoProtocols(protocols); |
| 594 |
} |
| 595 |
else |
| 596 |
{ |
| 597 |
hardware->AccessoryProtocols(protocols); |
| 598 |
} |
| 599 |
for (auto protocol : protocols) |
| 600 |
{ |
| 601 |
ret[ProtocolSymbols[protocol]] = protocol; |
| 602 |
} |
| 603 |
} |
| 604 |
return ret; |
| 605 |
} |
| 606 |
|
| 607 |
/*************************** |
| 608 |
* Loco * |
| 609 |
***************************/ |
| 610 |
|
| 611 |
string Manager::GetLocoList() const |
| 612 |
{ |
| 613 |
string out; |
| 614 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 615 |
for (auto& loco : locos) |
| 616 |
{ |
| 617 |
Loco* l = loco.second; |
| 618 |
out += to_string(l->GetID()) + ";"; |
| 619 |
out += to_string(l->GetSpeed()) + ";"; |
| 620 |
out += to_string(l->GetOrientation()) + ";"; |
| 621 |
out += to_string(l->GetTrackId()) + ";"; |
| 622 |
out += l->GetName() + "\n"; |
| 623 |
} |
| 624 |
return out; |
| 625 |
} |
| 626 |
|
| 627 |
Loco* Manager::GetLoco(const LocoID locoID) const |
| 628 |
{ |
| 629 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 630 |
if (locos.count(locoID) != 1) |
| 631 |
{ |
| 632 |
return nullptr; |
| 633 |
} |
| 634 |
return locos.at(locoID); |
| 635 |
} |
| 636 |
|
| 637 |
LocoConfig Manager::GetLocoOfConfigByMatchKey(const ControlID controlId, const string& matchKey) const |
| 638 |
{ |
| 639 |
ControlInterface* control = GetControl(controlId); |
| 640 |
if (!control) |
| 641 |
{ |
| 642 |
return LocoConfig(LocoTypeLoco); |
| 643 |
} |
| 644 |
return control->GetLocoByMatchKey(matchKey); |
| 645 |
} |
| 646 |
|
| 647 |
Loco* Manager::GetLocoByMatchKey(const ControlID controlId, const string& matchKey) const |
| 648 |
{ |
| 649 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 650 |
for (auto& loco : locos) |
| 651 |
{ |
| 652 |
Loco* locoConfig = loco.second; |
| 653 |
if (locoConfig->GetControlID() == controlId && locoConfig->GetMatchKey().compare(matchKey) == 0) |
| 654 |
{ |
| 655 |
return loco.second; |
| 656 |
} |
| 657 |
} |
| 658 |
return nullptr; |
| 659 |
} |
| 660 |
|
| 661 |
void Manager::LocoRemoveMatchKey(const LocoID locoId) |
| 662 |
{ |
| 663 |
Loco* loco = GetLoco(locoId); |
| 664 |
if (!loco) |
| 665 |
{ |
| 666 |
return; |
| 667 |
} |
| 668 |
loco->ClearMatchKey(); |
| 669 |
} |
| 670 |
|
| 671 |
void Manager::LocoReplaceMatchKey(const LocoID locoId, const std::string& newMatchKey) |
| 672 |
{ |
| 673 |
Loco* loco = GetLoco(locoId); |
| 674 |
if (!loco) |
| 675 |
{ |
| 676 |
return; |
| 677 |
} |
| 678 |
loco->SetMatchKey(newMatchKey); |
| 679 |
} |
| 680 |
|
| 681 |
const map<string,LocoConfig> Manager::GetUnmatchedLocosOfControl(const ControlID controlId, |
| 682 |
const std::string& matchKey) const |
| 683 |
{ |
| 684 |
ControlInterface* control = GetControl(controlId); |
| 685 |
map<string,LocoConfig> out; |
| 686 |
if (!control || !control->CanHandle(Hardware::CapabilityLocoDatabase)) |
| 687 |
{ |
| 688 |
return out; |
| 689 |
} |
| 690 |
out = control->GetUnmatchedLocos(matchKey); |
| 691 |
out[""].SetName(""); |
| 692 |
return out; |
| 693 |
} |
| 694 |
|
| 695 |
Loco* Manager::GetLoco(const ControlID controlID, const Protocol protocol, const Address address) const |
| 696 |
{ |
| 697 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 698 |
for (auto& loco : locos) |
| 699 |
{ |
| 700 |
if (loco.second->GetControlID() == controlID |
| 701 |
&& loco.second->GetProtocol() == protocol |
| 702 |
&& loco.second->GetAddress() == address) |
| 703 |
{ |
| 704 |
return loco.second; |
| 705 |
} |
| 706 |
} |
| 707 |
return nullptr; |
| 708 |
} |
| 709 |
|
| 710 |
const map<string,LocoID> Manager::LocoBaseListFree() const |
| 711 |
{ |
| 712 |
map<string,LocoID> out; |
| 713 |
{ |
| 714 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 715 |
for (auto& loco : locos) |
| 716 |
{ |
| 717 |
if (!loco.second->IsInUse()) |
| 718 |
{ |
| 719 |
out[loco.second->GetName()] = loco.second->GetID(); |
| 720 |
} |
| 721 |
} |
| 722 |
} |
| 723 |
{ |
| 724 |
std::lock_guard<std::mutex> guard(multipleUnitMutex); |
| 725 |
for (auto& multipleUnit : multipleUnits) |
| 726 |
{ |
| 727 |
if (!multipleUnit.second->IsInUse()) |
| 728 |
{ |
| 729 |
out[multipleUnit.second->GetName()] = multipleUnit.second->GetID() + MultipleUnitIdPrefix; |
| 730 |
} |
| 731 |
} |
| 732 |
} |
| 733 |
return out; |
| 734 |
} |
| 735 |
|
| 736 |
const map<string,DataModel::LocoConfig> Manager::LocoConfigByName() const |
| 737 |
{ |
| 738 |
map<string,DataModel::LocoConfig> out; |
| 739 |
{ |
| 740 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 741 |
for (auto& loco : locos) |
| 742 |
{ |
| 743 |
out[loco.second->GetName()] = *(loco.second); |
| 744 |
} |
| 745 |
} |
| 746 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 747 |
for (auto& control : controls) |
| 748 |
{ |
| 749 |
control.second->AddUnmatchedLocos(out); |
| 750 |
} |
| 751 |
|
| 752 |
return out; |
| 753 |
} |
| 754 |
|
| 755 |
const map<string,LocoID> Manager::LocoIdsByName() const |
| 756 |
{ |
| 757 |
map<string,LocoID> out; |
| 758 |
{ |
| 759 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 760 |
for (auto& loco : locos) |
| 761 |
{ |
| 762 |
Loco* locoEntry = loco.second; |
| 763 |
out[locoEntry->GetName()] = locoEntry->GetID(); |
| 764 |
} |
| 765 |
} |
| 766 |
return out; |
| 767 |
} |
| 768 |
|
| 769 |
bool Manager::LocoSave(LocoID locoID, |
| 770 |
const string& name, |
| 771 |
const ControlID controlID, |
| 772 |
const std::string& matchKey, |
| 773 |
const Protocol protocol, |
| 774 |
const Address address, |
| 775 |
const Address serverAddress, |
| 776 |
const Length length, |
| 777 |
const bool pushpull, |
| 778 |
const Speed maxSpeed, |
| 779 |
const Speed travelSpeed, |
| 780 |
const Speed reducedSpeed, |
| 781 |
const Speed creepingSpeed, |
| 782 |
const Propulsion propulsion, |
| 783 |
const TrainType type, |
| 784 |
const std::vector<DataModel::LocoFunctionEntry>& locoFunctions, |
| 785 |
string& result) |
| 786 |
{ |
| 787 |
if (!CheckControlLocoProtocolAddress(controlID, protocol, address, result)) |
| 788 |
{ |
| 789 |
return false; |
| 790 |
} |
| 791 |
|
| 792 |
Loco* loco = GetLoco(locoID); |
| 793 |
if (!loco) |
| 794 |
{ |
| 795 |
loco = CreateAndAddObject(locos, locoMutex); |
| 796 |
} |
| 797 |
|
| 798 |
if (!loco) |
| 799 |
{ |
| 800 |
result = Languages::GetText(Languages::TextUnableToAddLoco); |
| 801 |
return false; |
| 802 |
} |
| 803 |
|
| 804 |
// if we have a new object we have to update locoID |
| 805 |
locoID = loco->GetID(); |
| 806 |
|
| 807 |
loco->SetName(CheckObjectName(multipleUnits, multipleUnitMutex, MultipleUnitNone, CheckObjectName(locos, locoMutex, locoID, name.size() == 0 ? "L" : name))); |
| 808 |
loco->SetControlID(controlID); |
| 809 |
loco->SetMatchKey(matchKey); |
| 810 |
loco->SetProtocol(protocol); |
| 811 |
loco->SetAddress(address); |
| 812 |
loco->SetServerAddress(serverAddress); |
| 813 |
loco->SetLength(length); |
| 814 |
loco->SetPushpull(pushpull); |
| 815 |
loco->SetMaxSpeed(maxSpeed); |
| 816 |
loco->SetTravelSpeed(travelSpeed); |
| 817 |
loco->SetReducedSpeed(reducedSpeed); |
| 818 |
loco->SetCreepingSpeed(creepingSpeed); |
| 819 |
loco->SetPropulsion(propulsion); |
| 820 |
loco->SetTrainType(type); |
| 821 |
loco->ConfigureFunctions(locoFunctions); |
| 822 |
|
| 823 |
// save in db |
| 824 |
LocoSave(loco); |
| 825 |
|
| 826 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 827 |
for (auto& control : controls) |
| 828 |
{ |
| 829 |
control.second->LocoSettings(locoID, name, matchKey); |
| 830 |
} |
| 831 |
return true; |
| 832 |
} |
| 833 |
|
| 834 |
bool Manager::LocoDelete(const LocoID locoID, string& result) |
| 835 |
{ |
| 836 |
Loco* loco = nullptr; |
| 837 |
{ |
| 838 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 839 |
if (locoID == LocoNone || locos.count(locoID) != 1) |
| 840 |
{ |
| 841 |
result = Languages::GetText(Languages::TextLocoDoesNotExist); |
| 842 |
return false; |
| 843 |
} |
| 844 |
|
| 845 |
loco = locos.at(locoID); |
| 846 |
if (!loco) |
| 847 |
{ |
| 848 |
result = Languages::GetText(Languages::TextLocoDoesNotExist); |
| 849 |
return false; |
| 850 |
} |
| 851 |
|
| 852 |
if (loco->IsInUse()) |
| 853 |
{ |
| 854 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextLocoIsInUse), loco->GetName()); |
| 855 |
return false; |
| 856 |
} |
| 857 |
|
| 858 |
locos.erase(locoID); |
| 859 |
} |
| 860 |
|
| 861 |
if (storage) |
| 862 |
{ |
| 863 |
TransactionGuard guard(storage); |
| 864 |
storage->DeleteLoco(locoID); |
| 865 |
} |
| 866 |
const string& name = loco->GetName(); |
| 867 |
const string& matchKey = loco->GetMatchKey(); |
| 868 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 869 |
for (auto& control : controls) |
| 870 |
{ |
| 871 |
control.second->LocoDelete(locoID, name, matchKey); |
| 872 |
} |
| 873 |
delete loco; |
| 874 |
return true; |
| 875 |
} |
| 876 |
|
| 877 |
bool Manager::LocoProtocolAddress(const LocoID locoID, ControlID& controlID, Protocol& protocol, Address& address) const |
| 878 |
{ |
| 879 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 880 |
if (locos.count(locoID) != 1) |
| 881 |
{ |
| 882 |
controlID = 0; |
| 883 |
protocol = ProtocolNone; |
| 884 |
address = 0; |
| 885 |
return false; |
| 886 |
} |
| 887 |
Loco* loco = locos.at(locoID); |
| 888 |
if (!loco) |
| 889 |
{ |
| 890 |
return false; |
| 891 |
} |
| 892 |
controlID = loco->GetControlID(); |
| 893 |
protocol = loco->GetProtocol(); |
| 894 |
address = loco->GetAddress(); |
| 895 |
return true; |
| 896 |
} |
| 897 |
|
| 898 |
bool Manager::LocoBaseSpeed(const ControlType controlType, |
| 899 |
LocoBase* loco, |
| 900 |
const Speed speed) |
| 901 |
{ |
| 902 |
if (!loco) |
| 903 |
{ |
| 904 |
return false; |
| 905 |
} |
| 906 |
Speed newSpeed = speed; |
| 907 |
if (speed > MaxSpeed) |
| 908 |
{ |
| 909 |
newSpeed = MaxSpeed; |
| 910 |
} |
| 911 |
const string& locoName = loco->GetName(); |
| 912 |
logger->Info(Languages::TextLocoSpeedIs, locoName, newSpeed); |
| 913 |
const Speed oldSpeed = loco->GetSpeed(); |
| 914 |
if (oldSpeed == newSpeed) |
| 915 |
{ |
| 916 |
return true; |
| 917 |
} |
| 918 |
loco->SetSpeed(newSpeed); |
| 919 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 920 |
for (auto& control : controls) |
| 921 |
{ |
| 922 |
control.second->LocoBaseSpeed(controlType, loco, newSpeed); |
| 923 |
} |
| 924 |
return true; |
| 925 |
} |
| 926 |
|
| 927 |
Speed Manager::LocoSpeed(const LocoID locoID) const |
| 928 |
{ |
| 929 |
Loco* loco = GetLoco(locoID); |
| 930 |
if (!loco) |
| 931 |
{ |
| 932 |
return MinSpeed; |
| 933 |
} |
| 934 |
return loco->GetSpeed(); |
| 935 |
} |
| 936 |
|
| 937 |
void Manager::LocoBaseOrientation(const ControlType controlType, |
| 938 |
LocoBase* loco, |
| 939 |
Orientation orientation) |
| 940 |
{ |
| 941 |
if (!loco) |
| 942 |
{ |
| 943 |
return; |
| 944 |
} |
| 945 |
|
| 946 |
if (orientation == OrientationChange) |
| 947 |
{ |
| 948 |
const Orientation oldOrientation = loco->GetOrientation(); |
| 949 |
orientation = (oldOrientation == OrientationLeft ? OrientationRight : OrientationLeft); |
| 950 |
} |
| 951 |
|
| 952 |
logger->Info(orientation ? Languages::TextLocoDirectionOfTravelIsRight : Languages::TextLocoDirectionOfTravelIsLeft, loco->GetName()); |
| 953 |
const Orientation oldOrientation = loco->GetOrientation(); |
| 954 |
if (oldOrientation == orientation) |
| 955 |
{ |
| 956 |
return; |
| 957 |
} |
| 958 |
loco->SetOrientation(orientation); |
| 959 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 960 |
for (auto& control : controls) |
| 961 |
{ |
| 962 |
control.second->LocoBaseOrientation(controlType, loco, orientation); |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
void Manager::LocoBaseFunctionState(const ControlType controlType, |
| 967 |
const ObjectIdentifier& locoBaseIdentifier, |
| 968 |
const DataModel::LocoFunctionNr function, |
| 969 |
const DataModel::LocoFunctionState on) |
| 970 |
{ |
| 971 |
LocoBase* loco = GetLocoBase(locoBaseIdentifier); |
| 972 |
if (!loco) |
| 973 |
{ |
| 974 |
return; |
| 975 |
} |
| 976 |
|
| 977 |
DataModel::LocoFunctionNr functionInternal = function; |
| 978 |
if (function >= NumberOfLocoFunctions) |
| 979 |
{ |
| 980 |
functionInternal = loco->GetFunctionNumberFromFunctionIcon(static_cast<DataModel::LocoFunctionIcon>(function - 256)); |
| 981 |
} |
| 982 |
LocoBaseFunctionState(controlType, loco, functionInternal, on); |
| 983 |
} |
| 984 |
|
| 985 |
void Manager::LocoBaseFunctionState(const ControlType controlType, |
| 986 |
LocoBase* loco, |
| 987 |
const DataModel::LocoFunctionNr function, |
| 988 |
const DataModel::LocoFunctionState on) |
| 989 |
{ |
| 990 |
if (!loco) |
| 991 |
{ |
| 992 |
return; |
| 993 |
} |
| 994 |
|
| 995 |
logger->Info(on ? Languages::TextLocoFunctionIsOn : Languages::TextLocoFunctionIsOff, loco->GetName(), function); |
| 996 |
const DataModel::LocoFunctionState oldOn = loco->GetFunctionState(function); |
| 997 |
if (oldOn == on) |
| 998 |
{ |
| 999 |
return; |
| 1000 |
} |
| 1001 |
loco->SetFunctionState(function, on); |
| 1002 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 1003 |
for (auto& control : controls) |
| 1004 |
{ |
| 1005 |
control.second->LocoBaseFunction(controlType, loco, function, on); |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
/*************************** |
| 1010 |
* MultipleUnit * |
| 1011 |
***************************/ |
| 1012 |
|
| 1013 |
MultipleUnit* Manager::GetMultipleUnit(const MultipleUnitID multipleUnitId) const |
| 1014 |
{ |
| 1015 |
std::lock_guard<std::mutex> guard(multipleUnitMutex); |
| 1016 |
if (multipleUnits.count(multipleUnitId) != 1) |
| 1017 |
{ |
| 1018 |
return nullptr; |
| 1019 |
} |
| 1020 |
return multipleUnits.at(multipleUnitId); |
| 1021 |
} |
| 1022 |
|
| 1023 |
LocoConfig Manager::GetMultipleUnitOfConfigByMatchKey(const ControlID controlId, const string& matchKey) const |
| 1024 |
{ |
| 1025 |
ControlInterface* control = GetControl(controlId); |
| 1026 |
if (!control) |
| 1027 |
{ |
| 1028 |
return LocoConfig(LocoTypeMultipleUnit); |
| 1029 |
} |
| 1030 |
return control->GetMultipleUnitByMatchKey(matchKey); |
| 1031 |
} |
| 1032 |
|
| 1033 |
const map<string,DataModel::LocoConfig> Manager::MultipleUnitConfigByName() const |
| 1034 |
{ |
| 1035 |
map<string,DataModel::LocoConfig> out; |
| 1036 |
{ |
| 1037 |
std::lock_guard<std::mutex> guard(multipleUnitMutex); |
| 1038 |
for (auto& multipleUnit : multipleUnits) |
| 1039 |
{ |
| 1040 |
out[multipleUnit.second->GetName()] = *(multipleUnit.second); |
| 1041 |
} |
| 1042 |
} |
| 1043 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 1044 |
for (auto& control : controls) |
| 1045 |
{ |
| 1046 |
control.second->AddUnmatchedMultipleUnits(out); |
| 1047 |
} |
| 1048 |
|
| 1049 |
return out; |
| 1050 |
} |
| 1051 |
|
| 1052 |
bool Manager::MultipleUnitSave(MultipleUnitID multipleUnitID, |
| 1053 |
const string& name, |
| 1054 |
const ControlID controlID, |
| 1055 |
const std::string& matchKey, |
| 1056 |
const Address address, |
| 1057 |
const Address serverAddress, |
| 1058 |
const Length length, |
| 1059 |
const bool pushpull, |
| 1060 |
const Speed maxSpeed, |
| 1061 |
const Speed travelSpeed, |
| 1062 |
const Speed reducedSpeed, |
| 1063 |
const Speed creepingSpeed, |
| 1064 |
const TrainType type, |
| 1065 |
const std::vector<DataModel::LocoFunctionEntry>& locoFunctions, |
| 1066 |
const std::vector<DataModel::Relation*>& slaves, |
| 1067 |
string& result) |
| 1068 |
{ |
| 1069 |
if (!CheckControlMultipleUnitProtocolAddress(controlID, address, result)) |
| 1070 |
{ |
| 1071 |
return false; |
| 1072 |
} |
| 1073 |
|
| 1074 |
MultipleUnit* multipleUnit = GetMultipleUnit(multipleUnitID); |
| 1075 |
if (!multipleUnit) |
| 1076 |
{ |
| 1077 |
multipleUnit = CreateAndAddObject(multipleUnits, multipleUnitMutex); |
| 1078 |
} |
| 1079 |
|
| 1080 |
if (!multipleUnit) |
| 1081 |
{ |
| 1082 |
result = Languages::GetText(Languages::TextUnableToAddMultipleUnit); |
| 1083 |
return false; |
| 1084 |
} |
| 1085 |
|
| 1086 |
// if we have a new object we have to update multipleUnitID |
| 1087 |
if (multipleUnitID == 0) |
| 1088 |
{ |
| 1089 |
multipleUnitID = multipleUnit->GetID(); |
| 1090 |
for (auto slave : slaves) |
| 1091 |
{ |
| 1092 |
slave->ObjectID1(multipleUnitID); |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
// FIXME: replace "M" with language dependent word |
| 1097 |
multipleUnit->SetName(CheckObjectName(locos, locoMutex, LocoNone, CheckObjectName(multipleUnits, multipleUnitMutex, multipleUnitID, name.size() == 0 ? "M" : name))); |
| 1098 |
multipleUnit->SetControlID(controlID); |
| 1099 |
multipleUnit->SetMatchKey(matchKey); |
| 1100 |
multipleUnit->SetProtocol(ProtocolNone); |
| 1101 |
multipleUnit->SetAddress(address); |
| 1102 |
multipleUnit->SetServerAddress(serverAddress); |
| 1103 |
multipleUnit->SetLength(length); |
| 1104 |
multipleUnit->SetPushpull(pushpull); |
| 1105 |
multipleUnit->SetMaxSpeed(maxSpeed); |
| 1106 |
multipleUnit->SetTravelSpeed(travelSpeed); |
| 1107 |
multipleUnit->SetReducedSpeed(reducedSpeed); |
| 1108 |
multipleUnit->SetCreepingSpeed(creepingSpeed); |
| 1109 |
multipleUnit->SetTrainType(type); |
| 1110 |
multipleUnit->ConfigureFunctions(locoFunctions); |
| 1111 |
multipleUnit->AssignSlaves(slaves); |
| 1112 |
|
| 1113 |
// save in db |
| 1114 |
MultipleUnitSave(multipleUnit); |
| 1115 |
|
| 1116 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 1117 |
for (auto& control : controls) |
| 1118 |
{ |
| 1119 |
control.second->MultipleUnitSettings(multipleUnitID, name, matchKey); |
| 1120 |
} |
| 1121 |
return true; |
| 1122 |
} |
| 1123 |
|
| 1124 |
bool Manager::MultipleUnitDelete(const MultipleUnitID multipleUnitID, string& result) |
| 1125 |
{ |
| 1126 |
MultipleUnit* multipleUnit = nullptr; |
| 1127 |
{ |
| 1128 |
std::lock_guard<std::mutex> guard(multipleUnitMutex); |
| 1129 |
if (multipleUnitID == MultipleUnitNone || multipleUnits.count(multipleUnitID) != 1) |
| 1130 |
{ |
| 1131 |
result = Languages::GetText(Languages::TextMultipleUnitDoesNotExist); |
| 1132 |
return false; |
| 1133 |
} |
| 1134 |
|
| 1135 |
multipleUnit = multipleUnits.at(multipleUnitID); |
| 1136 |
if (!multipleUnit) |
| 1137 |
{ |
| 1138 |
result = Languages::GetText(Languages::TextMultipleUnitDoesNotExist); |
| 1139 |
return false; |
| 1140 |
} |
| 1141 |
|
| 1142 |
if (multipleUnit->IsInUse()) |
| 1143 |
{ |
| 1144 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextMultipleUnitIsInUse), multipleUnit->GetName()); |
| 1145 |
return false; |
| 1146 |
} |
| 1147 |
|
| 1148 |
multipleUnits.erase(multipleUnitID); |
| 1149 |
} |
| 1150 |
|
| 1151 |
if (storage) |
| 1152 |
{ |
| 1153 |
TransactionGuard guard(storage); |
| 1154 |
storage->DeleteMultipleUnit(multipleUnitID); |
| 1155 |
} |
| 1156 |
const string& name = multipleUnit->GetName(); |
| 1157 |
const string& matchKey = multipleUnit->GetMatchKey(); |
| 1158 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 1159 |
for (auto& control : controls) |
| 1160 |
{ |
| 1161 |
control.second->MultipleUnitDelete(multipleUnitID, name, matchKey); |
| 1162 |
} |
| 1163 |
delete multipleUnit; |
| 1164 |
return true; |
| 1165 |
} |
| 1166 |
|
| 1167 |
const map<string,LocoID> Manager::LocoBaseIdsByName() const |
| 1168 |
{ |
| 1169 |
map<string,LocoID> out = LocoIdsByName(); |
| 1170 |
{ |
| 1171 |
std::lock_guard<std::mutex> guard(multipleUnitMutex); |
| 1172 |
for (auto& multipleUnit : multipleUnits) |
| 1173 |
{ |
| 1174 |
MultipleUnit* multipleUnitEntry = multipleUnit.second; |
| 1175 |
out[multipleUnitEntry->GetName()] = multipleUnitEntry->GetID() + MultipleUnitIdPrefix; |
| 1176 |
} |
| 1177 |
} |
| 1178 |
return out; |
| 1179 |
} |
| 1180 |
|
| 1181 |
const std::string& Manager::GetLocoBaseName(const ObjectIdentifier& locoBaseIdentifier) const |
| 1182 |
{ |
| 1183 |
const LocoBase* locoBase = GetLocoBase(locoBaseIdentifier); |
| 1184 |
if (!locoBase) |
| 1185 |
{ |
| 1186 |
return unknownLoco; |
| 1187 |
} |
| 1188 |
return locoBase->GetName(); |
| 1189 |
} |
| 1190 |
|
| 1191 |
/*************************** |
| 1192 |
* Accessory Base * |
| 1193 |
***************************/ |
| 1194 |
|
| 1195 |
void Manager::AccessoryBaseState(const ControlType controlType, |
| 1196 |
const ControlID controlID, |
| 1197 |
const Protocol protocol, |
| 1198 |
const Address address, |
| 1199 |
const DataModel::AccessoryState state) |
| 1200 |
{ |
| 1201 |
Accessory* accessory = GetAccessory(controlID, protocol, address, static_cast<AddressPort>(state)); |
| 1202 |
if (accessory) |
| 1203 |
{ |
| 1204 |
AccessoryState(controlType, accessory, accessory->CalculateInvertedAccessoryState(state), true); |
| 1205 |
return; |
| 1206 |
} |
| 1207 |
|
| 1208 |
Switch* mySwitch = GetSwitch(controlID, protocol, address); |
| 1209 |
if (mySwitch) |
| 1210 |
{ |
| 1211 |
SwitchState(controlType, mySwitch, mySwitch->CalculateInvertedSwitchState(address, state), true); |
| 1212 |
return; |
| 1213 |
} |
| 1214 |
|
| 1215 |
Signal* signal = GetSignal(controlID, protocol, address); |
| 1216 |
if (signal) |
| 1217 |
{ |
| 1218 |
SignalState(controlType, signal, signal->CalculateMappedSignalState(address, state), true); |
| 1219 |
return; |
| 1220 |
} |
| 1221 |
|
| 1222 |
logger->Warning(Languages::TextAccessoryControlProtocolAddressDoesNotExist, controlID, Utils::Utils::ProtocolToString(protocol), address); |
| 1223 |
} |
| 1224 |
|
| 1225 |
void Manager::AccessoryBaseState(const ControlType controlType, |
| 1226 |
const ObjectIdentifier& identifier, |
| 1227 |
const DataModel::AccessoryState state) |
| 1228 |
{ |
| 1229 |
switch (identifier.GetObjectType()) |
| 1230 |
{ |
| 1231 |
case ObjectTypeAccessory: |
| 1232 |
AccessoryState(controlType, identifier.GetObjectID(), state); |
| 1233 |
return; |
| 1234 |
|
| 1235 |
case ObjectTypeSwitch: |
| 1236 |
SwitchState(controlType, identifier.GetObjectID(), state); |
| 1237 |
return; |
| 1238 |
|
| 1239 |
case ObjectTypeSignal: |
| 1240 |
SignalState(controlType, identifier.GetObjectID(), state); |
| 1241 |
return; |
| 1242 |
|
| 1243 |
default: |
| 1244 |
return; |
| 1245 |
} |
| 1246 |
} |
| 1247 |
|
| 1248 |
/*************************** |
| 1249 |
* Accessory * |
| 1250 |
***************************/ |
| 1251 |
|
| 1252 |
bool Manager::AccessoryState(const ControlType controlType, const AccessoryID accessoryID, const DataModel::AccessoryState state, const bool force) |
| 1253 |
{ |
| 1254 |
if (boosterState == BoosterStateStop) |
| 1255 |
{ |
| 1256 |
return false; |
| 1257 |
} |
| 1258 |
Accessory* accessory = GetAccessory(accessoryID); |
| 1259 |
AccessoryState(controlType, accessory, state, force); |
| 1260 |
return true; |
| 1261 |
} |
| 1262 |
|
| 1263 |
void Manager::AccessoryState(const ControlType controlType, Accessory* accessory, const DataModel::AccessoryState state, const bool force) |
| 1264 |
{ |
| 1265 |
if (!accessory) |
| 1266 |
{ |
| 1267 |
return; |
| 1268 |
} |
| 1269 |
|
| 1270 |
if (!force && accessory->IsInUse()) |
| 1271 |
{ |
| 1272 |
logger->Warning(Languages::TextAccessoryIsLocked, accessory->GetName()); |
| 1273 |
return; |
| 1274 |
} |
| 1275 |
|
| 1276 |
if (!GetExecuteAccessory()) |
| 1277 |
{ |
| 1278 |
const DataModel::AccessoryState oldState = accessory->GetAccessoryState(); |
| 1279 |
if (oldState == state) |
| 1280 |
{ |
| 1281 |
return; |
| 1282 |
} |
| 1283 |
} |
| 1284 |
accessory->SetAccessoryState(state); |
| 1285 |
|
| 1286 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 1287 |
for (auto& control : controls) |
| 1288 |
{ |
| 1289 |
control.second->AccessoryState(controlType, accessory); |
| 1290 |
} |
| 1291 |
} |
| 1292 |
|
| 1293 |
const map<string,AccessoryConfig> Manager::GetUnmatchedAccessoriesOfControl(const ControlID controlId, |
| 1294 |
const std::string& matchKey) const |
| 1295 |
{ |
| 1296 |
ControlInterface* control = GetControl(controlId); |
| 1297 |
map<string,AccessoryConfig> out; |
| 1298 |
if (!control || !control->CanHandle(Hardware::CapabilityAccessoryDatabase)) |
| 1299 |
{ |
| 1300 |
return out; |
| 1301 |
} |
| 1302 |
out = control->GetUnmatchedAccessories(matchKey); |
| 1303 |
out[""].SetName(""); |
| 1304 |
return out; |
| 1305 |
} |
| 1306 |
|
| 1307 |
Accessory* Manager::GetAccessory(const AccessoryID accessoryID) const |
| 1308 |
{ |
| 1309 |
std::lock_guard<std::mutex> guard(accessoryMutex); |
| 1310 |
if (accessories.count(accessoryID) != 1) |
| 1311 |
{ |
| 1312 |
return nullptr; |
| 1313 |
} |
| 1314 |
return accessories.at(accessoryID); |
| 1315 |
} |
| 1316 |
|
| 1317 |
Accessory* Manager::GetAccessory(const ControlID controlID, |
| 1318 |
const Protocol protocol, |
| 1319 |
const Address address, |
| 1320 |
const AddressPort port) const |
| 1321 |
{ |
| 1322 |
std::lock_guard<std::mutex> guard(accessoryMutex); |
| 1323 |
for (auto& a : accessories) |
| 1324 |
{ |
| 1325 |
Accessory* accessory = a.second; |
| 1326 |
if (accessory->GetControlID() == controlID |
| 1327 |
&& accessory->GetProtocol() == protocol |
| 1328 |
&& accessory->GetAddress() == address) |
| 1329 |
{ |
| 1330 |
switch (accessory->GetAccessoryType() & DataModel::AccessoryTypeConnectionMask) |
| 1331 |
{ |
| 1332 |
case AccessoryTypeOnOn: |
| 1333 |
return accessory; |
| 1334 |
|
| 1335 |
case AccessoryTypeOnPush: |
| 1336 |
case AccessoryTypeOnOff: |
| 1337 |
if (accessory->GetPort() == port) |
| 1338 |
{ |
| 1339 |
return accessory; |
| 1340 |
} |
| 1341 |
break; |
| 1342 |
|
| 1343 |
default: |
| 1344 |
break; |
| 1345 |
} |
| 1346 |
} |
| 1347 |
} |
| 1348 |
return nullptr; |
| 1349 |
} |
| 1350 |
|
| 1351 |
const std::string& Manager::GetAccessoryName(const AccessoryID accessoryID) const |
| 1352 |
{ |
| 1353 |
std::lock_guard<std::mutex> guard(accessoryMutex); |
| 1354 |
if (accessories.count(accessoryID) != 1) |
| 1355 |
{ |
| 1356 |
return unknownAccessory; |
| 1357 |
} |
| 1358 |
return accessories.at(accessoryID)->GetName(); |
| 1359 |
} |
| 1360 |
|
| 1361 |
bool Manager::AccessorySave(AccessoryID accessoryID, |
| 1362 |
const string& name, |
| 1363 |
const LayoutPosition posX, |
| 1364 |
const LayoutPosition posY, |
| 1365 |
const LayoutPosition posZ, |
| 1366 |
const LayoutRotation rotation, |
| 1367 |
const ControlID controlID, |
| 1368 |
const std::string& matchKey, |
| 1369 |
const Protocol protocol, |
| 1370 |
const Address address, |
| 1371 |
const AddressPort port, |
| 1372 |
const Address serverAddress, |
| 1373 |
const DataModel::AccessoryType type, |
| 1374 |
const DataModel::AccessoryPulseDuration duration, |
| 1375 |
const bool inverted, |
| 1376 |
string& result) |
| 1377 |
{ |
| 1378 |
if (!CheckControlAccessoryProtocolAddress(controlID, protocol, address, result)) |
| 1379 |
{ |
| 1380 |
return false; |
| 1381 |
} |
| 1382 |
|
| 1383 |
Accessory* accessory = GetAccessory(accessoryID); |
| 1384 |
if (!CheckLayoutItemPosition(accessory, posX, posY, posZ, result)) |
| 1385 |
{ |
| 1386 |
return false; |
| 1387 |
} |
| 1388 |
|
| 1389 |
if (!accessory) |
| 1390 |
{ |
| 1391 |
accessory = CreateAndAddObject(accessories, accessoryMutex); |
| 1392 |
} |
| 1393 |
|
| 1394 |
if (!accessory) |
| 1395 |
{ |
| 1396 |
result = Languages::GetText(Languages::TextUnableToAddAccessory); |
| 1397 |
return false; |
| 1398 |
} |
| 1399 |
|
| 1400 |
// if we have a new object we have to update accessoryID |
| 1401 |
accessoryID = accessory->GetID(); |
| 1402 |
|
| 1403 |
// update existing accessory |
| 1404 |
accessory->SetName(CheckObjectName(accessories, accessoryMutex, accessoryID, name.size() == 0 ? "A" : name)); |
| 1405 |
accessory->SetPosX(posX); |
| 1406 |
accessory->SetPosY(posY); |
| 1407 |
accessory->SetPosZ(posZ); |
| 1408 |
accessory->SetRotation(rotation); |
| 1409 |
accessory->SetControlID(controlID); |
| 1410 |
accessory->SetMatchKey(matchKey); |
| 1411 |
accessory->SetProtocol(protocol); |
| 1412 |
accessory->SetAddress(address); |
| 1413 |
accessory->SetPort(port); |
| 1414 |
accessory->SetServerAddress(serverAddress); |
| 1415 |
accessory->SetAccessoryType(type); |
| 1416 |
accessory->SetAccessoryPulseDuration(duration); |
| 1417 |
accessory->SetInverted(inverted); |
| 1418 |
|
| 1419 |
AccessorySaveAndPublishSettings(accessory); |
| 1420 |
return true; |
| 1421 |
} |
| 1422 |
|
| 1423 |
bool Manager::AccessoryPosition(const AccessoryID accessoryID, |
| 1424 |
const LayoutPosition posX, |
| 1425 |
const LayoutPosition posY, |
| 1426 |
string& result) |
| 1427 |
{ |
| 1428 |
Accessory* accessory = GetAccessory(accessoryID); |
| 1429 |
if (!accessory) |
| 1430 |
{ |
| 1431 |
result = Languages::GetText(Languages::TextAccessoryDoesNotExist); |
| 1432 |
return false; |
| 1433 |
} |
| 1434 |
|
| 1435 |
if (!CheckLayoutItemPosition(accessory, posX, posY, accessory->GetPosZ(), result)) |
| 1436 |
{ |
| 1437 |
return false; |
| 1438 |
} |
| 1439 |
|
| 1440 |
accessory->SetPosX(posX); |
| 1441 |
accessory->SetPosY(posY); |
| 1442 |
|
| 1443 |
AccessorySaveAndPublishSettings(accessory); |
| 1444 |
return true; |
| 1445 |
} |
| 1446 |
|
| 1447 |
bool Manager::AccessoryRotate(const AccessoryID accessoryID, |
| 1448 |
string& result) |
| 1449 |
{ |
| 1450 |
Accessory* accessory = GetAccessory(accessoryID); |
| 1451 |
if (!accessory) |
| 1452 |
{ |
| 1453 |
result = Languages::GetText(Languages::TextAccessoryDoesNotExist); |
| 1454 |
return false; |
| 1455 |
} |
| 1456 |
|
| 1457 |
LayoutRotation newRotation = accessory->GetRotation(); |
| 1458 |
++newRotation; |
| 1459 |
if (!CheckLayoutItemPosition(accessory, accessory->GetPosX(), accessory->GetPosY(), accessory->GetPosZ(), LayoutItem::Width1, LayoutItem::Height1, newRotation, result)) |
| 1460 |
{ |
| 1461 |
return false; |
| 1462 |
} |
| 1463 |
|
| 1464 |
accessory->SetRotation(newRotation); |
| 1465 |
|
| 1466 |
AccessorySaveAndPublishSettings(accessory); |
| 1467 |
return true; |
| 1468 |
} |
| 1469 |
|
| 1470 |
void Manager::AccessorySaveAndPublishSettings(const Accessory* const accessory) |
| 1471 |
{ |
| 1472 |
// save in db |
| 1473 |
if (storage) |
| 1474 |
{ |
| 1475 |
TransactionGuard guard(storage); |
| 1476 |
storage->Save(*accessory); |
| 1477 |
} |
| 1478 |
|
| 1479 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 1480 |
for (auto& control : controls) |
| 1481 |
{ |
| 1482 |
control.second->AccessorySettings(accessory->GetID(), accessory->GetName(), accessory->GetMatchKey()); |
| 1483 |
} |
| 1484 |
} |
| 1485 |
|
| 1486 |
const map<string,DataModel::AccessoryConfig> Manager::AccessoryConfigByName() const |
| 1487 |
{ |
| 1488 |
map<string,DataModel::AccessoryConfig> out; |
| 1489 |
{ |
| 1490 |
std::lock_guard<std::mutex> guard(accessoryMutex); |
| 1491 |
for (auto& accessory : accessories) |
| 1492 |
{ |
| 1493 |
out[accessory.second->GetName()] = *(accessory.second); |
| 1494 |
} |
| 1495 |
} |
| 1496 |
|
| 1497 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 1498 |
for (auto& control : controls) |
| 1499 |
{ |
| 1500 |
control.second->AddUnmatchedAccessories(out); |
| 1501 |
} |
| 1502 |
return out; |
| 1503 |
} |
| 1504 |
|
| 1505 |
bool Manager::AccessoryDelete(const AccessoryID accessoryID, |
| 1506 |
string& result) |
| 1507 |
{ |
| 1508 |
Accessory* accessory = nullptr; |
| 1509 |
{ |
| 1510 |
std::lock_guard<std::mutex> guard(accessoryMutex); |
| 1511 |
if (accessoryID == AccessoryNone || accessories.count(accessoryID) != 1) |
| 1512 |
{ |
| 1513 |
result = Languages::GetText(Languages::TextAccessoryDoesNotExist); |
| 1514 |
return false; |
| 1515 |
} |
| 1516 |
|
| 1517 |
accessory = accessories.at(accessoryID); |
| 1518 |
if (!accessory) |
| 1519 |
{ |
| 1520 |
result = Languages::GetText(Languages::TextAccessoryDoesNotExist); |
| 1521 |
return false; |
| 1522 |
} |
| 1523 |
|
| 1524 |
ObjectIdentifier accessoryIdentifier(ObjectTypeAccessory, accessoryID); |
| 1525 |
if (ObjectIsPartOfRoute(accessoryIdentifier, accessory, result)) |
| 1526 |
{ |
| 1527 |
return false; |
| 1528 |
} |
| 1529 |
|
| 1530 |
accessories.erase(accessoryID); |
| 1531 |
} |
| 1532 |
|
| 1533 |
if (storage) |
| 1534 |
{ |
| 1535 |
TransactionGuard guard(storage); |
| 1536 |
storage->DeleteAccessory(accessoryID); |
| 1537 |
} |
| 1538 |
const string& name = accessory->GetName(); |
| 1539 |
const string& matchKey = accessory->GetMatchKey(); |
| 1540 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 1541 |
for (auto& control : controls) |
| 1542 |
{ |
| 1543 |
control.second->AccessoryDelete(accessoryID, name, matchKey); |
| 1544 |
} |
| 1545 |
delete accessory; |
| 1546 |
return true; |
| 1547 |
} |
| 1548 |
|
| 1549 |
bool Manager::AccessoryRelease(const AccessoryID accessoryID) |
| 1550 |
{ |
| 1551 |
Accessory* accessory = GetAccessory(accessoryID); |
| 1552 |
if (!accessory) |
| 1553 |
{ |
| 1554 |
return false; |
| 1555 |
} |
| 1556 |
return accessory->Release(logger, accessory->GetLocoBase()); |
| 1557 |
} |
| 1558 |
|
| 1559 |
AccessoryConfig Manager::GetAccessoryOfConfigByMatchKey(const ControlID controlId, const string& matchKey) const |
| 1560 |
{ |
| 1561 |
ControlInterface* control = GetControl(controlId); |
| 1562 |
if (!control) |
| 1563 |
{ |
| 1564 |
return AccessoryConfig(); |
| 1565 |
} |
| 1566 |
return control->GetAccessoryByMatchKey(matchKey); |
| 1567 |
} |
| 1568 |
|
| 1569 |
Accessory* Manager::GetAccessoryByMatchKey(const ControlID controlId, const string& matchKey) const |
| 1570 |
{ |
| 1571 |
std::lock_guard<std::mutex> guard(accessoryMutex); |
| 1572 |
for (auto& accessory : accessories) |
| 1573 |
{ |
| 1574 |
Accessory* accessoryConfig = accessory.second; |
| 1575 |
if (accessoryConfig->GetControlID() == controlId && accessoryConfig->GetMatchKey().compare(matchKey) == 0) |
| 1576 |
{ |
| 1577 |
return accessoryConfig; |
| 1578 |
} |
| 1579 |
} |
| 1580 |
return nullptr; |
| 1581 |
} |
| 1582 |
|
| 1583 |
void Manager::AccessoryRemoveMatchKey(const AccessoryID accessoryId) |
| 1584 |
{ |
| 1585 |
Accessory* accessory = GetAccessory(accessoryId); |
| 1586 |
if (!accessory) |
| 1587 |
{ |
| 1588 |
return; |
| 1589 |
} |
| 1590 |
accessory->ClearMatchKey(); |
| 1591 |
} |
| 1592 |
|
| 1593 |
/*************************** |
| 1594 |
* Feedback * |
| 1595 |
***************************/ |
| 1596 |
|
| 1597 |
void Manager::FeedbackState(const ControlID controlID, const FeedbackPin pin, const DataModel::Feedback::FeedbackState state) |
| 1598 |
{ |
| 1599 |
Feedback* feedback = GetFeedback(controlID, pin); |
| 1600 |
if (feedback) |
| 1601 |
{ |
| 1602 |
FeedbackState(feedback, state); |
| 1603 |
return; |
| 1604 |
} |
| 1605 |
|
| 1606 |
if (!GetAutoAddFeedback()) |
| 1607 |
{ |
| 1608 |
return; |
| 1609 |
} |
| 1610 |
|
| 1611 |
string name = "Feedback auto added " + std::to_string(controlID) + "/" + std::to_string(pin); |
| 1612 |
logger->Info(Languages::TextAddingFeedback, name); |
| 1613 |
string result; |
| 1614 |
|
| 1615 |
FeedbackSave(FeedbackNone, name, DataModel::LayoutItem::VisibleNo, 0, 0, 0, DataModel::LayoutItem::Rotation0, controlID, "", pin, false, FeedbackTypeDefault, RouteNone, result); |
| 1616 |
} |
| 1617 |
|
| 1618 |
void Manager::FeedbackState(const FeedbackID feedbackID, const DataModel::Feedback::FeedbackState state) |
| 1619 |
{ |
| 1620 |
Feedback* feedback = GetFeedback(feedbackID); |
| 1621 |
if (!feedback) |
| 1622 |
{ |
| 1623 |
return; |
| 1624 |
} |
| 1625 |
FeedbackState(feedback, state); |
| 1626 |
} |
| 1627 |
|
| 1628 |
void Manager::FeedbackPublishState(const Feedback* feedback) |
| 1629 |
{ |
| 1630 |
if (!feedback) |
| 1631 |
{ |
| 1632 |
return; |
| 1633 |
} |
| 1634 |
DataModel::Feedback::FeedbackState state = feedback->GetState(); |
| 1635 |
const string& feedbackName = feedback->GetName(); |
| 1636 |
logger->Info(state ? Languages::TextFeedbackStateIsOn : Languages::TextFeedbackStateIsOff, feedbackName); |
| 1637 |
const FeedbackID feedbackID = feedback->GetID(); |
| 1638 |
{ |
| 1639 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 1640 |
for (auto& control : controls) |
| 1641 |
{ |
| 1642 |
control.second->FeedbackState(feedbackName, feedbackID, state); |
| 1643 |
} |
| 1644 |
} |
| 1645 |
} |
| 1646 |
|
| 1647 |
Feedback* Manager::GetFeedback(const FeedbackID feedbackID) const |
| 1648 |
{ |
| 1649 |
std::lock_guard<std::mutex> guard(feedbackMutex); |
| 1650 |
return GetFeedbackUnlocked(feedbackID); |
| 1651 |
} |
| 1652 |
|
| 1653 |
Feedback* Manager::GetFeedbackUnlocked(const FeedbackID feedbackID) const |
| 1654 |
{ |
| 1655 |
if (feedbacks.count(feedbackID) != 1) |
| 1656 |
{ |
| 1657 |
return nullptr; |
| 1658 |
} |
| 1659 |
return feedbacks.at(feedbackID); |
| 1660 |
} |
| 1661 |
|
| 1662 |
Feedback* Manager::GetFeedback(const ControlID controlID, const FeedbackPin pin) const |
| 1663 |
{ |
| 1664 |
std::lock_guard<std::mutex> guard(feedbackMutex); |
| 1665 |
for (auto& feedback : feedbacks) |
| 1666 |
{ |
| 1667 |
if (feedback.second->GetControlID() == controlID |
| 1668 |
&& feedback.second->GetPin() == pin) |
| 1669 |
{ |
| 1670 |
return feedback.second; |
| 1671 |
} |
| 1672 |
} |
| 1673 |
return nullptr; |
| 1674 |
} |
| 1675 |
|
| 1676 |
const std::string& Manager::GetFeedbackName(const FeedbackID feedbackID) const |
| 1677 |
{ |
| 1678 |
std::lock_guard<std::mutex> guard(feedbackMutex); |
| 1679 |
if (feedbacks.count(feedbackID) != 1) |
| 1680 |
{ |
| 1681 |
return unknownFeedback; |
| 1682 |
} |
| 1683 |
return feedbacks.at(feedbackID)->GetName(); |
| 1684 |
} |
| 1685 |
|
| 1686 |
const map<string,DataModel::FeedbackConfig> Manager::FeedbackConfigByName() const |
| 1687 |
{ |
| 1688 |
map<string,DataModel::FeedbackConfig> out; |
| 1689 |
{ |
| 1690 |
std::lock_guard<std::mutex> guard(feedbackMutex); |
| 1691 |
for (auto& feedback : feedbacks) |
| 1692 |
{ |
| 1693 |
out[feedback.second->GetName()] = *(feedback.second); |
| 1694 |
} |
| 1695 |
} |
| 1696 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 1697 |
for (auto& control : controls) |
| 1698 |
{ |
| 1699 |
control.second->AddUnmatchedFeedbacks(out); |
| 1700 |
} |
| 1701 |
|
| 1702 |
return out; |
| 1703 |
} |
| 1704 |
|
| 1705 |
bool Manager::FeedbackSave(FeedbackID feedbackID, |
| 1706 |
const std::string& name, |
| 1707 |
const Visible visible, |
| 1708 |
const LayoutPosition posX, |
| 1709 |
const LayoutPosition posY, |
| 1710 |
const LayoutPosition posZ, |
| 1711 |
const LayoutRotation rotation, |
| 1712 |
const ControlID controlID, |
| 1713 |
const string& matchKey, |
| 1714 |
const FeedbackPin pin, |
| 1715 |
const bool inverted, |
| 1716 |
const FeedbackType feedbackType, |
| 1717 |
const RouteID routeId, |
| 1718 |
string& result) |
| 1719 |
{ |
| 1720 |
Feedback* feedback = GetFeedback(feedbackID); |
| 1721 |
if (visible && !CheckLayoutItemPosition(feedback, posX, posY, posZ, result)) |
| 1722 |
{ |
| 1723 |
return false; |
| 1724 |
} |
| 1725 |
|
| 1726 |
if (!feedback) |
| 1727 |
{ |
| 1728 |
feedback = CreateAndAddObject(feedbacks, feedbackMutex); |
| 1729 |
} |
| 1730 |
|
| 1731 |
if (!feedback) |
| 1732 |
{ |
| 1733 |
result = Languages::GetText(Languages::TextUnableToAddFeedback); |
| 1734 |
return false; |
| 1735 |
} |
| 1736 |
|
| 1737 |
// if we have a new object we have to update feedbackID |
| 1738 |
feedbackID = feedback->GetID(); |
| 1739 |
|
| 1740 |
feedback->SetName(CheckObjectName(feedbacks, feedbackMutex, feedbackID, name.size() == 0 ? "F" : name)); |
| 1741 |
feedback->SetVisible(visible); |
| 1742 |
feedback->SetPosX(posX); |
| 1743 |
feedback->SetPosY(posY); |
| 1744 |
feedback->SetPosZ(posZ); |
| 1745 |
feedback->SetRotation(rotation); |
| 1746 |
feedback->SetControlID(controlID); |
| 1747 |
feedback->SetMatchKey(matchKey); |
| 1748 |
feedback->SetPin(pin); |
| 1749 |
feedback->SetInverted(inverted); |
| 1750 |
feedback->SetFeedbackType(feedbackType); |
| 1751 |
feedback->SetRouteId(routeId); |
| 1752 |
|
| 1753 |
FeedbackSaveAndPublishSettings(feedback); |
| 1754 |
return true; |
| 1755 |
} |
| 1756 |
|
| 1757 |
bool Manager::FeedbackPosition(const FeedbackID feedbackID, |
| 1758 |
const LayoutPosition posX, |
| 1759 |
const LayoutPosition posY, |
| 1760 |
string& result) |
| 1761 |
{ |
| 1762 |
Feedback* feedback = GetFeedback(feedbackID); |
| 1763 |
if (!feedback) |
| 1764 |
{ |
| 1765 |
result = Languages::GetText(Languages::TextFeedbackDoesNotExist); |
| 1766 |
return false; |
| 1767 |
} |
| 1768 |
|
| 1769 |
if (!feedback->GetVisible()) |
| 1770 |
{ |
| 1771 |
return false; |
| 1772 |
} |
| 1773 |
|
| 1774 |
if (!CheckLayoutItemPosition(feedback, posX, posY, feedback->GetPosZ(), result)) |
| 1775 |
{ |
| 1776 |
return false; |
| 1777 |
} |
| 1778 |
|
| 1779 |
feedback->SetPosX(posX); |
| 1780 |
feedback->SetPosY(posY); |
| 1781 |
|
| 1782 |
FeedbackSaveAndPublishSettings(feedback); |
| 1783 |
return true; |
| 1784 |
} |
| 1785 |
|
| 1786 |
bool Manager::FeedbackRotate(const FeedbackID feedbackID, |
| 1787 |
string& result) |
| 1788 |
{ |
| 1789 |
Feedback* feedback = GetFeedback(feedbackID); |
| 1790 |
if (!feedback) |
| 1791 |
{ |
| 1792 |
result = Languages::GetText(Languages::TextFeedbackDoesNotExist); |
| 1793 |
return false; |
| 1794 |
} |
| 1795 |
|
| 1796 |
LayoutRotation newRotation = feedback->GetRotation(); |
| 1797 |
++newRotation; |
| 1798 |
if (!CheckLayoutItemPosition(feedback, feedback->GetPosX(), feedback->GetPosY(), feedback->GetPosZ(), LayoutItem::Width1, LayoutItem::Height1, newRotation, result)) |
| 1799 |
{ |
| 1800 |
return false; |
| 1801 |
} |
| 1802 |
|
| 1803 |
feedback->SetRotation(newRotation); |
| 1804 |
|
| 1805 |
FeedbackSaveAndPublishSettings(feedback); |
| 1806 |
return true; |
| 1807 |
} |
| 1808 |
|
| 1809 |
void Manager::FeedbackSaveAndPublishSettings(const Feedback* const feedback) |
| 1810 |
{ |
| 1811 |
// save in db |
| 1812 |
if (storage) |
| 1813 |
{ |
| 1814 |
TransactionGuard guard(storage); |
| 1815 |
storage->Save(*feedback); |
| 1816 |
} |
| 1817 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 1818 |
for (auto& control : controls) |
| 1819 |
{ |
| 1820 |
control.second->FeedbackSettings(feedback->GetID(), feedback->GetName()); |
| 1821 |
} |
| 1822 |
} |
| 1823 |
|
| 1824 |
const map<string,DataModel::Feedback*> Manager::FeedbackListByName() const |
| 1825 |
{ |
| 1826 |
map<string,DataModel::Feedback*> out; |
| 1827 |
std::lock_guard<std::mutex> guard(feedbackMutex); |
| 1828 |
for (auto& feedback : feedbacks) |
| 1829 |
{ |
| 1830 |
out[feedback.second->GetName()] = feedback.second; |
| 1831 |
} |
| 1832 |
return out; |
| 1833 |
} |
| 1834 |
|
| 1835 |
const map<RouteID,string> Manager::RoutesOfTrack(const TrackID trackID) const |
| 1836 |
{ |
| 1837 |
map<RouteID,string> out; |
| 1838 |
const Track* track = GetTrack(trackID); |
| 1839 |
if (!track) |
| 1840 |
{ |
| 1841 |
return out; |
| 1842 |
} |
| 1843 |
const vector<const Route*> routesOfTrack = track->GetRoutes(); |
| 1844 |
for (auto route : routesOfTrack) |
| 1845 |
{ |
| 1846 |
out[route->GetID()] = route->GetName(); |
| 1847 |
} |
| 1848 |
return out; |
| 1849 |
} |
| 1850 |
|
| 1851 |
const map<string,FeedbackID> Manager::FeedbacksOfTrack(const TrackID trackID) const |
| 1852 |
{ |
| 1853 |
map<string,FeedbackID> out; |
| 1854 |
const Track* track = GetTrack(trackID); |
| 1855 |
if (!track) |
| 1856 |
{ |
| 1857 |
return out; |
| 1858 |
} |
| 1859 |
const vector<Relation*> feedbacksOfTrack = track->GetFeedbacks(); |
| 1860 |
for (auto feedbackRelation: feedbacksOfTrack) |
| 1861 |
{ |
| 1862 |
const FeedbackID feedbackID = feedbackRelation->ObjectID2(); |
| 1863 |
Feedback* feedback = GetFeedback(feedbackID); |
| 1864 |
if (!feedback) |
| 1865 |
{ |
| 1866 |
continue; |
| 1867 |
} |
| 1868 |
out[feedback->GetName()] = feedbackID; |
| 1869 |
} |
| 1870 |
return out; |
| 1871 |
} |
| 1872 |
|
| 1873 |
bool Manager::FeedbackDelete(const FeedbackID feedbackID, |
| 1874 |
string& result) |
| 1875 |
{ |
| 1876 |
Feedback* feedback = nullptr; |
| 1877 |
{ |
| 1878 |
std::lock_guard<std::mutex> guard(feedbackMutex); |
| 1879 |
if (feedbackID == FeedbackNone || feedbacks.count(feedbackID) != 1) |
| 1880 |
{ |
| 1881 |
result = Languages::GetText(Languages::TextFeedbackDoesNotExist); |
| 1882 |
return false; |
| 1883 |
} |
| 1884 |
|
| 1885 |
feedback = feedbacks.at(feedbackID); |
| 1886 |
Track* track = feedback->GetTrack(); |
| 1887 |
if (track) |
| 1888 |
{ |
| 1889 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextFeedbackIsUsedByTrack), feedback->GetName(), track->GetName()); |
| 1890 |
return false; |
| 1891 |
} |
| 1892 |
|
| 1893 |
feedbacks.erase(feedbackID); |
| 1894 |
} |
| 1895 |
|
| 1896 |
if (storage) |
| 1897 |
{ |
| 1898 |
TransactionGuard guard(storage); |
| 1899 |
storage->DeleteFeedback(feedbackID); |
| 1900 |
} |
| 1901 |
const string& name = feedback->GetName(); |
| 1902 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 1903 |
for (auto& control : controls) |
| 1904 |
{ |
| 1905 |
control.second->FeedbackDelete(feedbackID, name); |
| 1906 |
} |
| 1907 |
delete feedback; |
| 1908 |
return true; |
| 1909 |
} |
| 1910 |
|
| 1911 |
FeedbackConfig Manager::GetFeedbackOfConfigByMatchKey(const ControlID controlId, const string& matchKey) const |
| 1912 |
{ |
| 1913 |
ControlInterface* control = GetControl(controlId); |
| 1914 |
if (!control) |
| 1915 |
{ |
| 1916 |
return FeedbackConfig(); |
| 1917 |
} |
| 1918 |
return control->GetFeedbackByMatchKey(matchKey); |
| 1919 |
} |
| 1920 |
|
| 1921 |
Feedback* Manager::GetFeedbackByMatchKey(const ControlID controlId, const string& matchKey) const |
| 1922 |
{ |
| 1923 |
std::lock_guard<std::mutex> guard(feedbackMutex); |
| 1924 |
for (auto& feedback : feedbacks) |
| 1925 |
{ |
| 1926 |
Feedback* feedbackConfig = feedback.second; |
| 1927 |
if (feedbackConfig->GetControlID() == controlId && feedbackConfig->GetMatchKey().compare(matchKey) == 0) |
| 1928 |
{ |
| 1929 |
return feedback.second; |
| 1930 |
} |
| 1931 |
} |
| 1932 |
return nullptr; |
| 1933 |
} |
| 1934 |
|
| 1935 |
void Manager::FeedbackRemoveMatchKey(const FeedbackID feedbackId) |
| 1936 |
{ |
| 1937 |
Feedback* feedback = GetFeedback(feedbackId); |
| 1938 |
if (!feedback) |
| 1939 |
{ |
| 1940 |
return; |
| 1941 |
} |
| 1942 |
feedback->ClearMatchKey(); |
| 1943 |
} |
| 1944 |
|
| 1945 |
void Manager::FeedbackReplaceMatchKey(const FeedbackID feedbackId, const std::string& newMatchKey) |
| 1946 |
{ |
| 1947 |
Feedback* feedback = GetFeedback(feedbackId); |
| 1948 |
if (!feedback) |
| 1949 |
{ |
| 1950 |
return; |
| 1951 |
} |
| 1952 |
feedback->SetMatchKey(newMatchKey); |
| 1953 |
} |
| 1954 |
|
| 1955 |
const map<string,FeedbackConfig> Manager::GetUnmatchedFeedbacksOfControl(const ControlID controlId, |
| 1956 |
const std::string& matchKey) const |
| 1957 |
{ |
| 1958 |
ControlInterface* control = GetControl(controlId); |
| 1959 |
map<string,FeedbackConfig> out; |
| 1960 |
if (!control || !control->CanHandle(Hardware::CapabilityFeedbackDatabase)) |
| 1961 |
{ |
| 1962 |
return out; |
| 1963 |
} |
| 1964 |
out = control->GetUnmatchedFeedbacks(matchKey); |
| 1965 |
out[""].SetName(""); |
| 1966 |
return out; |
| 1967 |
} |
| 1968 |
|
| 1969 |
/*************************** |
| 1970 |
* Track * |
| 1971 |
***************************/ |
| 1972 |
|
| 1973 |
Track* Manager::GetTrack(const TrackID trackID) const |
| 1974 |
{ |
| 1975 |
std::lock_guard<std::mutex> guard(trackMutex); |
| 1976 |
if (tracks.count(trackID) != 1) |
| 1977 |
{ |
| 1978 |
return nullptr; |
| 1979 |
} |
| 1980 |
return tracks.at(trackID); |
| 1981 |
} |
| 1982 |
|
| 1983 |
const std::string& Manager::GetTrackName(const TrackID trackID) const |
| 1984 |
{ |
| 1985 |
if (tracks.count(trackID) != 1) |
| 1986 |
{ |
| 1987 |
return unknownTrack; |
| 1988 |
} |
| 1989 |
return tracks.at(trackID)->GetName(); |
| 1990 |
} |
| 1991 |
|
| 1992 |
const map<string,DataModel::Track*> Manager::TrackListByName() const |
| 1993 |
{ |
| 1994 |
map<string,DataModel::Track*> out; |
| 1995 |
std::lock_guard<std::mutex> guard(trackMutex); |
| 1996 |
for (auto& track : tracks) |
| 1997 |
{ |
| 1998 |
out[track.second->GetName()] = track.second; |
| 1999 |
} |
| 2000 |
return out; |
| 2001 |
} |
| 2002 |
|
| 2003 |
const map<string,DataModel::Track*> Manager::TrackListMasterByName() const |
| 2004 |
{ |
| 2005 |
map<string,DataModel::Track*> out; |
| 2006 |
std::lock_guard<std::mutex> guard(trackMutex); |
| 2007 |
for (auto& t : tracks) |
| 2008 |
{ |
| 2009 |
Track* track = t.second; |
| 2010 |
if (track && !track->GetMain()) |
| 2011 |
{ |
| 2012 |
out[track->GetName()] = track; |
| 2013 |
} |
| 2014 |
} |
| 2015 |
return out; |
| 2016 |
} |
| 2017 |
|
| 2018 |
const map<string,TrackID> Manager::TrackListIdByName(const TrackID excludeTrackID) const |
| 2019 |
{ |
| 2020 |
map<string,TrackID> out; |
| 2021 |
std::lock_guard<std::mutex> guard(trackMutex); |
| 2022 |
for (auto& t : tracks) |
| 2023 |
{ |
| 2024 |
Track* track = t.second; |
| 2025 |
const TrackID trackID = track->GetID(); |
| 2026 |
if (trackID == excludeTrackID) |
| 2027 |
{ |
| 2028 |
continue; |
| 2029 |
} |
| 2030 |
|
| 2031 |
// exclude extension tracks |
| 2032 |
if (track->GetMain()) |
| 2033 |
{ |
| 2034 |
continue; |
| 2035 |
} |
| 2036 |
out[track->GetName()] = trackID; |
| 2037 |
} |
| 2038 |
return out; |
| 2039 |
} |
| 2040 |
|
| 2041 |
bool Manager::TrackSave(TrackID trackID, |
| 2042 |
const std::string& name, |
| 2043 |
const bool showName, |
| 2044 |
const std::string& displayName, |
| 2045 |
const LayoutPosition posX, |
| 2046 |
const LayoutPosition posY, |
| 2047 |
const LayoutPosition posZ, |
| 2048 |
const LayoutItemSize height, |
| 2049 |
const LayoutRotation rotation, |
| 2050 |
const DataModel::TrackType trackType, |
| 2051 |
const TrackID main, |
| 2052 |
const vector<Relation*>& newFeedbacks, |
| 2053 |
const vector<Relation*>& newSignals, |
| 2054 |
const DataModel::SelectRouteApproach selectRouteApproach, |
| 2055 |
const bool allowLocoTurn, |
| 2056 |
const bool releaseWhenFree, |
| 2057 |
string& result) |
| 2058 |
{ |
| 2059 |
Track* track = GetTrack(trackID); |
| 2060 |
if (!CheckLayoutItemPosition(track, posX, posY, posZ, LayoutItem::Width1, height, rotation, result)) |
| 2061 |
{ |
| 2062 |
return false; |
| 2063 |
} |
| 2064 |
|
| 2065 |
if (!track) |
| 2066 |
{ |
| 2067 |
track = CreateAndAddObject(tracks, trackMutex); |
| 2068 |
} |
| 2069 |
|
| 2070 |
if (!track) |
| 2071 |
{ |
| 2072 |
result = Languages::GetText(Languages::TextUnableToAddTrack); |
| 2073 |
return false; |
| 2074 |
} |
| 2075 |
|
| 2076 |
// if we have a new object we have to update trackID |
| 2077 |
if (trackID == TrackNone) |
| 2078 |
{ |
| 2079 |
trackID = track->GetID(); |
| 2080 |
for (auto feedback : newFeedbacks) |
| 2081 |
{ |
| 2082 |
feedback->ObjectID1(trackID); |
| 2083 |
} |
| 2084 |
for (auto signal : newSignals) |
| 2085 |
{ |
| 2086 |
signal->ObjectID1(trackID); |
| 2087 |
} |
| 2088 |
} |
| 2089 |
|
| 2090 |
// update existing track |
| 2091 |
track->SetName(CheckObjectName(tracks, trackMutex, trackID, name.size() == 0 ? "T" : name)); |
| 2092 |
track->SetShowName(showName); |
| 2093 |
track->SetDisplayName(displayName); |
| 2094 |
track->SetHeight(height); |
| 2095 |
track->SetRotation(rotation); |
| 2096 |
track->SetPosX(posX); |
| 2097 |
track->SetPosY(posY); |
| 2098 |
track->SetPosZ(posZ); |
| 2099 |
track->SetTrackType(trackType); |
| 2100 |
Track* oldMain = track->GetMain(); |
| 2101 |
if (oldMain) |
| 2102 |
{ |
| 2103 |
oldMain->DeleteExtension(track); |
| 2104 |
} |
| 2105 |
Track* newMain = GetTrack(main); |
| 2106 |
if (newMain && !newMain->GetMain() && newMain->AddExtension(track)) |
| 2107 |
{ |
| 2108 |
track->SetMain(newMain); |
| 2109 |
track->SetName(newMain->GetName() + "_ext_" + to_string(trackID)); |
| 2110 |
} |
| 2111 |
track->AssignFeedbacks(newFeedbacks); |
| 2112 |
track->AssignSignals(newSignals); |
| 2113 |
track->SetSelectRouteApproach(selectRouteApproach); |
| 2114 |
track->SetAllowLocoTurn(allowLocoTurn); |
| 2115 |
track->SetReleaseWhenFree(releaseWhenFree); |
| 2116 |
|
| 2117 |
TrackSaveAndPublishSettings(track); |
| 2118 |
return true; |
| 2119 |
} |
| 2120 |
|
| 2121 |
bool Manager::TrackPosition(const TrackID trackID, |
| 2122 |
const LayoutPosition posX, |
| 2123 |
const LayoutPosition posY, |
| 2124 |
string& result) |
| 2125 |
{ |
| 2126 |
Track* track = GetTrack(trackID); |
| 2127 |
if (!track) |
| 2128 |
{ |
| 2129 |
result = Languages::GetText(Languages::TextTrackDoesNotExist); |
| 2130 |
return false; |
| 2131 |
} |
| 2132 |
|
| 2133 |
if (!CheckLayoutItemPosition(track, posX, posY, track->GetPosZ(), LayoutItem::Width1, track->GetHeight(), track->GetRotation(), result)) |
| 2134 |
{ |
| 2135 |
return false; |
| 2136 |
} |
| 2137 |
|
| 2138 |
track->SetPosX(posX); |
| 2139 |
track->SetPosY(posY); |
| 2140 |
|
| 2141 |
TrackSaveAndPublishSettings(track); |
| 2142 |
return true; |
| 2143 |
} |
| 2144 |
|
| 2145 |
bool Manager::TrackRotate(const TrackID trackID, |
| 2146 |
string& result) |
| 2147 |
{ |
| 2148 |
Track* track = GetTrack(trackID); |
| 2149 |
if (!track) |
| 2150 |
{ |
| 2151 |
result = Languages::GetText(Languages::TextTrackDoesNotExist); |
| 2152 |
return false; |
| 2153 |
} |
| 2154 |
|
| 2155 |
LayoutRotation newRotation = track->GetRotation(); |
| 2156 |
++newRotation; |
| 2157 |
if (!CheckLayoutItemPosition(track, track->GetPosX(), track->GetPosY(), track->GetPosZ(), LayoutItem::Width1, track->GetHeight(), newRotation, result)) |
| 2158 |
{ |
| 2159 |
return false; |
| 2160 |
} |
| 2161 |
|
| 2162 |
track->SetRotation(newRotation); |
| 2163 |
|
| 2164 |
TrackSaveAndPublishSettings(track); |
| 2165 |
return true; |
| 2166 |
} |
| 2167 |
|
| 2168 |
void Manager::TrackSaveAndPublishSettings(const Track* const track) |
| 2169 |
{ |
| 2170 |
TrackSave(track); |
| 2171 |
|
| 2172 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 2173 |
for (auto& control : controls) |
| 2174 |
{ |
| 2175 |
control.second->TrackSettings(track->GetID(), track->GetName()); |
| 2176 |
} |
| 2177 |
} |
| 2178 |
|
| 2179 |
bool Manager::TrackDelete(const TrackID trackID, |
| 2180 |
string& result) |
| 2181 |
{ |
| 2182 |
Track* track = nullptr; |
| 2183 |
{ |
| 2184 |
std::lock_guard<std::mutex> guard(trackMutex); |
| 2185 |
if (trackID == TrackNone || tracks.count(trackID) != 1) |
| 2186 |
{ |
| 2187 |
result = Languages::GetText(Languages::TextTrackDoesNotExist); |
| 2188 |
return false; |
| 2189 |
} |
| 2190 |
|
| 2191 |
track = tracks.at(trackID); |
| 2192 |
if (!track) |
| 2193 |
{ |
| 2194 |
result = Languages::GetText(Languages::TextTrackDoesNotExist); |
| 2195 |
return false; |
| 2196 |
} |
| 2197 |
|
| 2198 |
if (track->IsInUse()) |
| 2199 |
{ |
| 2200 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextTrackIsUsedByLoco), track->GetName(), GetLocoBaseName(track->GetLocoBase())); |
| 2201 |
return false; |
| 2202 |
} |
| 2203 |
|
| 2204 |
Route* route = GetFirstRouteFromOrToTrack(trackID); |
| 2205 |
if (route) |
| 2206 |
{ |
| 2207 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextTrackIsUsedByRoute), track->GetName(), route->GetName()); |
| 2208 |
return false; |
| 2209 |
} |
| 2210 |
|
| 2211 |
ObjectIdentifier trackIdentifier(ObjectTypeTrack, trackID); |
| 2212 |
if (ObjectIsPartOfRoute(trackIdentifier, track, result)) |
| 2213 |
{ |
| 2214 |
return false; |
| 2215 |
} |
| 2216 |
|
| 2217 |
tracks.erase(trackID); |
| 2218 |
} |
| 2219 |
|
| 2220 |
if (storage) |
| 2221 |
{ |
| 2222 |
Storage::TransactionGuard guard(storage); |
| 2223 |
storage->DeleteTrack(trackID); |
| 2224 |
} |
| 2225 |
const string& name = track->GetName(); |
| 2226 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 2227 |
for (auto& control : controls) |
| 2228 |
{ |
| 2229 |
control.second->TrackDelete(trackID, name); |
| 2230 |
} |
| 2231 |
|
| 2232 |
Cluster* cluster = track->GetCluster(); |
| 2233 |
if (cluster) |
| 2234 |
{ |
| 2235 |
cluster->DeleteTrack(track); |
| 2236 |
} |
| 2237 |
|
| 2238 |
delete track; |
| 2239 |
return true; |
| 2240 |
} |
| 2241 |
|
| 2242 |
/*************************** |
| 2243 |
* Switch * |
| 2244 |
***************************/ |
| 2245 |
|
| 2246 |
bool Manager::SwitchState(const ControlType controlType, const SwitchID switchID, const DataModel::AccessoryState state, const bool force) |
| 2247 |
{ |
| 2248 |
if (boosterState == BoosterStateStop) |
| 2249 |
{ |
| 2250 |
return false; |
| 2251 |
} |
| 2252 |
|
| 2253 |
Switch* mySwitch = GetSwitch(switchID); |
| 2254 |
SwitchState(controlType, mySwitch, state, force); |
| 2255 |
return true; |
| 2256 |
} |
| 2257 |
|
| 2258 |
void Manager::SwitchState(const ControlType controlType, Switch* mySwitch, const DataModel::AccessoryState state, const bool force) |
| 2259 |
{ |
| 2260 |
if (!mySwitch) |
| 2261 |
{ |
| 2262 |
return; |
| 2263 |
} |
| 2264 |
|
| 2265 |
if (!force && mySwitch->IsInUse()) |
| 2266 |
{ |
| 2267 |
logger->Warning(Languages::TextSwitchIsLocked, mySwitch->GetName()); |
| 2268 |
return; |
| 2269 |
} |
| 2270 |
|
| 2271 |
if (!GetExecuteAccessory()) |
| 2272 |
{ |
| 2273 |
const DataModel::AccessoryState oldState = mySwitch->GetAccessoryState(); |
| 2274 |
if (oldState == state) |
| 2275 |
{ |
| 2276 |
return; |
| 2277 |
} |
| 2278 |
} |
| 2279 |
mySwitch->SetAccessoryState(state); |
| 2280 |
|
| 2281 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 2282 |
for (auto& control : controls) |
| 2283 |
{ |
| 2284 |
control.second->SwitchState(controlType, mySwitch); |
| 2285 |
} |
| 2286 |
} |
| 2287 |
|
| 2288 |
Switch* Manager::GetSwitch(const SwitchID switchID) const |
| 2289 |
{ |
| 2290 |
std::lock_guard<std::mutex> guard(switchMutex); |
| 2291 |
if (switches.count(switchID) != 1) |
| 2292 |
{ |
| 2293 |
return nullptr; |
| 2294 |
} |
| 2295 |
return switches.at(switchID); |
| 2296 |
} |
| 2297 |
|
| 2298 |
Switch* Manager::GetSwitch(const ControlID controlID, const Protocol protocol, const Address address) const |
| 2299 |
{ |
| 2300 |
std::lock_guard<std::mutex> guard(switchMutex); |
| 2301 |
for (auto& mySwitch : switches) |
| 2302 |
{ |
| 2303 |
Switch* s = mySwitch.second; |
| 2304 |
if ((s->GetControlID() == controlID) |
| 2305 |
&& (s->GetProtocol() == protocol) |
| 2306 |
&& (s->UsesAddress(address))) |
| 2307 |
{ |
| 2308 |
return s; |
| 2309 |
} |
| 2310 |
} |
| 2311 |
return nullptr; |
| 2312 |
} |
| 2313 |
|
| 2314 |
const std::string& Manager::GetSwitchName(const SwitchID switchID) const |
| 2315 |
{ |
| 2316 |
if (switches.count(switchID) != 1) |
| 2317 |
{ |
| 2318 |
return unknownSwitch; |
| 2319 |
} |
| 2320 |
return switches.at(switchID)->GetName(); |
| 2321 |
} |
| 2322 |
|
| 2323 |
bool Manager::SwitchSave(SwitchID switchID, |
| 2324 |
const string& name, |
| 2325 |
const LayoutPosition posX, |
| 2326 |
const LayoutPosition posY, |
| 2327 |
const LayoutPosition posZ, |
| 2328 |
const LayoutRotation rotation, |
| 2329 |
const ControlID controlID, |
| 2330 |
const string& matchKey, |
| 2331 |
const Protocol protocol, |
| 2332 |
const Address address, |
| 2333 |
const Address serverAddress, |
| 2334 |
const DataModel::AccessoryType type, |
| 2335 |
const DataModel::AccessoryPulseDuration duration, |
| 2336 |
const bool inverted, |
| 2337 |
string& result) |
| 2338 |
{ |
| 2339 |
if (!CheckControlAccessoryProtocolAddress(controlID, protocol, address, result)) |
| 2340 |
{ |
| 2341 |
return false; |
| 2342 |
} |
| 2343 |
|
| 2344 |
Switch* mySwitch = GetSwitch(switchID); |
| 2345 |
if (!CheckLayoutItemPosition(mySwitch, posX, posY, posZ, LayoutItem::Width1, Switch::CalculateHeightFromType(type), rotation, result)) |
| 2346 |
{ |
| 2347 |
return false; |
| 2348 |
} |
| 2349 |
|
| 2350 |
if (!mySwitch) |
| 2351 |
{ |
| 2352 |
mySwitch = CreateAndAddObject(switches, switchMutex); |
| 2353 |
} |
| 2354 |
|
| 2355 |
if (!mySwitch) |
| 2356 |
{ |
| 2357 |
result = Languages::GetText(Languages::TextUnableToAddSwitch); |
| 2358 |
return false; |
| 2359 |
} |
| 2360 |
|
| 2361 |
// if we have a new object we have to update switchID |
| 2362 |
switchID = mySwitch->GetID(); |
| 2363 |
|
| 2364 |
// update existing switch |
| 2365 |
mySwitch->SetName(CheckObjectName(switches, switchMutex, switchID, name.size() == 0 ? "S" : name)); |
| 2366 |
mySwitch->SetPosX(posX); |
| 2367 |
mySwitch->SetPosY(posY); |
| 2368 |
mySwitch->SetPosZ(posZ); |
| 2369 |
mySwitch->SetRotation(rotation); |
| 2370 |
mySwitch->SetControlID(controlID); |
| 2371 |
mySwitch->SetMatchKey(matchKey); |
| 2372 |
mySwitch->SetProtocol(protocol); |
| 2373 |
mySwitch->SetAddress(address); |
| 2374 |
mySwitch->SetServerAddress(serverAddress); |
| 2375 |
mySwitch->SetAccessoryType(type); |
| 2376 |
mySwitch->SetAccessoryPulseDuration(duration); |
| 2377 |
mySwitch->SetInverted(inverted); |
| 2378 |
|
| 2379 |
SwitchSaveAndPublishSettings(mySwitch); |
| 2380 |
return true; |
| 2381 |
} |
| 2382 |
|
| 2383 |
bool Manager::SwitchPosition(const SwitchID switchID, |
| 2384 |
const LayoutPosition posX, |
| 2385 |
const LayoutPosition posY, |
| 2386 |
string& result) |
| 2387 |
{ |
| 2388 |
Switch* mySwitch = GetSwitch(switchID); |
| 2389 |
if (!mySwitch) |
| 2390 |
{ |
| 2391 |
result = Languages::GetText(Languages::TextSwitchDoesNotExist); |
| 2392 |
return false; |
| 2393 |
} |
| 2394 |
|
| 2395 |
if (!CheckLayoutItemPosition(mySwitch, posX, posY, mySwitch->GetPosZ(), LayoutItem::Width1, mySwitch->GetHeight(), mySwitch->GetRotation(), result)) |
| 2396 |
{ |
| 2397 |
return false; |
| 2398 |
} |
| 2399 |
|
| 2400 |
mySwitch->SetPosX(posX); |
| 2401 |
mySwitch->SetPosY(posY); |
| 2402 |
|
| 2403 |
SwitchSaveAndPublishSettings(mySwitch); |
| 2404 |
return true; |
| 2405 |
} |
| 2406 |
|
| 2407 |
bool Manager::SwitchRotate(const SwitchID switchID, |
| 2408 |
string& result) |
| 2409 |
{ |
| 2410 |
Switch* mySwitch = GetSwitch(switchID); |
| 2411 |
if (!mySwitch) |
| 2412 |
{ |
| 2413 |
result = Languages::GetText(Languages::TextSwitchDoesNotExist); |
| 2414 |
return false; |
| 2415 |
} |
| 2416 |
|
| 2417 |
LayoutRotation newRotation = mySwitch->GetRotation(); |
| 2418 |
++newRotation; |
| 2419 |
if (!CheckLayoutItemPosition(mySwitch, mySwitch->GetPosX(), mySwitch->GetPosY(), mySwitch->GetPosZ(), LayoutItem::Width1, mySwitch->GetHeight(), newRotation, result)) |
| 2420 |
{ |
| 2421 |
return false; |
| 2422 |
} |
| 2423 |
|
| 2424 |
mySwitch->Rotate(); |
| 2425 |
|
| 2426 |
SwitchSaveAndPublishSettings(mySwitch); |
| 2427 |
return true; |
| 2428 |
} |
| 2429 |
|
| 2430 |
void Manager::SwitchSaveAndPublishSettings(const Switch* const mySwitch) |
| 2431 |
{ |
| 2432 |
if (storage) |
| 2433 |
{ |
| 2434 |
TransactionGuard guard(storage); |
| 2435 |
storage->Save(*mySwitch); |
| 2436 |
} |
| 2437 |
|
| 2438 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 2439 |
for (auto& control : controls) |
| 2440 |
{ |
| 2441 |
control.second->SwitchSettings(mySwitch->GetID(), mySwitch->GetName(), mySwitch->GetMatchKey()); |
| 2442 |
} |
| 2443 |
} |
| 2444 |
|
| 2445 |
bool Manager::SwitchDelete(const SwitchID switchID, |
| 2446 |
string& result) |
| 2447 |
{ |
| 2448 |
Switch* mySwitch = nullptr; |
| 2449 |
{ |
| 2450 |
std::lock_guard<std::mutex> guard(switchMutex); |
| 2451 |
if (switchID == SwitchNone || switches.count(switchID) != 1) |
| 2452 |
{ |
| 2453 |
result = Languages::GetText(Languages::TextSwitchDoesNotExist); |
| 2454 |
return false; |
| 2455 |
} |
| 2456 |
|
| 2457 |
mySwitch = switches.at(switchID); |
| 2458 |
if (!mySwitch) |
| 2459 |
{ |
| 2460 |
result = Languages::GetText(Languages::TextSwitchDoesNotExist); |
| 2461 |
return false; |
| 2462 |
} |
| 2463 |
|
| 2464 |
ObjectIdentifier switchIdentifier(ObjectTypeSwitch, switchID); |
| 2465 |
if (ObjectIsPartOfRoute(switchIdentifier, mySwitch, result)) |
| 2466 |
{ |
| 2467 |
return false; |
| 2468 |
} |
| 2469 |
switches.erase(switchID); |
| 2470 |
} |
| 2471 |
|
| 2472 |
if (storage) |
| 2473 |
{ |
| 2474 |
TransactionGuard guard(storage); |
| 2475 |
storage->DeleteSwitch(switchID); |
| 2476 |
} |
| 2477 |
|
| 2478 |
const string& name = mySwitch->GetName(); |
| 2479 |
const string& matchKey = mySwitch->GetMatchKey(); |
| 2480 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 2481 |
for (auto& control : controls) |
| 2482 |
{ |
| 2483 |
control.second->SwitchDelete(switchID, name, matchKey); |
| 2484 |
} |
| 2485 |
delete mySwitch; |
| 2486 |
return true; |
| 2487 |
} |
| 2488 |
|
| 2489 |
const map<string,DataModel::AccessoryConfig> Manager::SwitchConfigByName() const |
| 2490 |
{ |
| 2491 |
map<string,DataModel::AccessoryConfig> out; |
| 2492 |
{ |
| 2493 |
std::lock_guard<std::mutex> guard(switchMutex); |
| 2494 |
for (auto& mySwitch : switches) |
| 2495 |
{ |
| 2496 |
out[mySwitch.second->GetName()] = *(mySwitch.second); |
| 2497 |
} |
| 2498 |
} |
| 2499 |
|
| 2500 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 2501 |
for (auto& control : controls) |
| 2502 |
{ |
| 2503 |
control.second->AddUnmatchedAccessories(out); |
| 2504 |
} |
| 2505 |
return out; |
| 2506 |
} |
| 2507 |
|
| 2508 |
bool Manager::SwitchRelease(const RouteID switchID) |
| 2509 |
{ |
| 2510 |
Switch* mySwitch = GetSwitch(switchID); |
| 2511 |
if (!mySwitch) |
| 2512 |
{ |
| 2513 |
return false; |
| 2514 |
} |
| 2515 |
return mySwitch->Release(logger, mySwitch->GetLocoBase()); |
| 2516 |
} |
| 2517 |
|
| 2518 |
Switch* Manager::GetSwitchByMatchKey(const ControlID controlId, const string& matchKey) const |
| 2519 |
{ |
| 2520 |
std::lock_guard<std::mutex> guard(switchMutex); |
| 2521 |
for (auto& mySwitch : switches) |
| 2522 |
{ |
| 2523 |
Switch* switchConfig = mySwitch.second; |
| 2524 |
if (switchConfig->GetControlID() == controlId && switchConfig->GetMatchKey().compare(matchKey) == 0) |
| 2525 |
{ |
| 2526 |
return switchConfig; |
| 2527 |
} |
| 2528 |
} |
| 2529 |
return nullptr; |
| 2530 |
} |
| 2531 |
|
| 2532 |
void Manager::SwitchRemoveMatchKey(const SwitchID switchId) |
| 2533 |
{ |
| 2534 |
Switch* mySwitch = GetSwitch(switchId); |
| 2535 |
if (!mySwitch) |
| 2536 |
{ |
| 2537 |
return; |
| 2538 |
} |
| 2539 |
mySwitch->ClearMatchKey(); |
| 2540 |
} |
| 2541 |
|
| 2542 |
/*************************** |
| 2543 |
* Route * |
| 2544 |
***************************/ |
| 2545 |
|
| 2546 |
bool Manager::RouteExecute(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier, const RouteID routeID) |
| 2547 |
{ |
| 2548 |
Route* route = GetRoute(routeID); |
| 2549 |
if (!route) |
| 2550 |
{ |
| 2551 |
return false; |
| 2552 |
} |
| 2553 |
return route->Execute(logger, locoBaseIdentifier); |
| 2554 |
} |
| 2555 |
|
| 2556 |
void Manager::RouteExecuteAsync(Logger::Logger* logger, const RouteID routeID) |
| 2557 |
{ |
| 2558 |
Route* route = GetRoute(routeID); |
| 2559 |
if (!route) |
| 2560 |
{ |
| 2561 |
return; |
| 2562 |
} |
| 2563 |
__attribute__((unused)) auto r = std::async(std::launch::async, Route::ExecuteStatic, logger, route); |
| 2564 |
} |
| 2565 |
|
| 2566 |
string Manager::GetRouteList() const |
| 2567 |
{ |
| 2568 |
string out; |
| 2569 |
std::lock_guard<std::mutex> guard(routeMutex); |
| 2570 |
for (auto& route : routes) |
| 2571 |
{ |
| 2572 |
Route* r = route.second; |
| 2573 |
out += to_string(r->GetID()) + ";"; |
| 2574 |
out += to_string(r->GetFromTrack()) + ";"; |
| 2575 |
out += to_string(r->GetFromOrientation()) + ";"; |
| 2576 |
out += to_string(r->GetToTrack()) + ";"; |
| 2577 |
out += to_string(r->GetToOrientation()) + ";"; |
| 2578 |
out += r->GetName() + "\n"; |
| 2579 |
} |
| 2580 |
return out; |
| 2581 |
} |
| 2582 |
|
| 2583 |
Route* Manager::GetRoute(const RouteID routeID) const |
| 2584 |
{ |
| 2585 |
std::lock_guard<std::mutex> guard(routeMutex); |
| 2586 |
if (routes.count(routeID) != 1) |
| 2587 |
{ |
| 2588 |
return nullptr; |
| 2589 |
} |
| 2590 |
return routes.at(routeID); |
| 2591 |
} |
| 2592 |
|
| 2593 |
const string& Manager::GetRouteName(const RouteID routeID) const |
| 2594 |
{ |
| 2595 |
std::lock_guard<std::mutex> guard(routeMutex); |
| 2596 |
if (routes.count(routeID) != 1) |
| 2597 |
{ |
| 2598 |
return unknownRoute; |
| 2599 |
} |
| 2600 |
return routes.at(routeID)->GetName(); |
| 2601 |
} |
| 2602 |
|
| 2603 |
bool Manager::RouteSave(RouteID routeID, |
| 2604 |
const std::string& name, |
| 2605 |
const Delay delay, |
| 2606 |
const Route::PushpullType pushpull, |
| 2607 |
const Propulsion propulsion, |
| 2608 |
const TrainType trainType, |
| 2609 |
const Length minTrainLength, |
| 2610 |
const Length maxTrainLength, |
| 2611 |
const std::vector<DataModel::Relation*>& relationsAtLock, |
| 2612 |
const std::vector<DataModel::Relation*>& relationsAtUnlock, |
| 2613 |
const Visible visible, |
| 2614 |
const LayoutPosition posX, |
| 2615 |
const LayoutPosition posY, |
| 2616 |
const LayoutPosition posZ, |
| 2617 |
const Automode automode, |
| 2618 |
const TrackID fromTrack, |
| 2619 |
const Orientation fromOrientation, |
| 2620 |
const TrackID toTrack, |
| 2621 |
const Orientation toOrientation, |
| 2622 |
const Route::Speed speed, |
| 2623 |
const FeedbackID feedbackIdReduced, |
| 2624 |
const Delay reducedDelay, |
| 2625 |
const FeedbackID feedbackIdCreep, |
| 2626 |
const Delay creepDelay, |
| 2627 |
const FeedbackID feedbackIdStop, |
| 2628 |
const Delay stopDelay, |
| 2629 |
const FeedbackID feedbackIdOver, |
| 2630 |
const Pause waitAfterRelease, |
| 2631 |
const RouteID followUpRoute, |
| 2632 |
string& result) |
| 2633 |
{ |
| 2634 |
|
| 2635 |
Route* route = GetRoute(routeID); |
| 2636 |
if (visible && !CheckLayoutItemPosition(route, posX, posY, posZ, result)) |
| 2637 |
{ |
| 2638 |
return false; |
| 2639 |
} |
| 2640 |
|
| 2641 |
if (!route) |
| 2642 |
{ |
| 2643 |
route = CreateAndAddObject(routes, routeMutex); |
| 2644 |
} |
| 2645 |
if (!route) |
| 2646 |
{ |
| 2647 |
result = Languages::GetText(Languages::TextUnableToAddRoute); |
| 2648 |
return false; |
| 2649 |
} |
| 2650 |
|
| 2651 |
// remove route from old track |
| 2652 |
Track* oldTrack = GetTrack(route->GetFromTrack()); |
| 2653 |
if (oldTrack) |
| 2654 |
{ |
| 2655 |
oldTrack->DeleteRoute(route); |
| 2656 |
TrackSave(oldTrack); |
| 2657 |
} |
| 2658 |
|
| 2659 |
// if we have a new object we have to update routeID |
| 2660 |
if (routeID == 0) |
| 2661 |
{ |
| 2662 |
routeID = route->GetID(); |
| 2663 |
for (auto atLock : relationsAtLock) |
| 2664 |
{ |
| 2665 |
atLock->ObjectID1(routeID); |
| 2666 |
} |
| 2667 |
for (auto atUnlock : relationsAtUnlock) |
| 2668 |
{ |
| 2669 |
atUnlock->ObjectID1(routeID); |
| 2670 |
} |
| 2671 |
} |
| 2672 |
|
| 2673 |
// update existing route |
| 2674 |
route->SetName(CheckObjectName(routes, routeMutex, routeID, name.size() == 0 ? "S" : name)); |
| 2675 |
route->SetDelay(delay); |
| 2676 |
route->AssignRelationsAtLock(relationsAtLock); |
| 2677 |
route->AssignRelationsAtUnlock(relationsAtUnlock); |
| 2678 |
route->SetVisible(visible); |
| 2679 |
route->SetPosX(posX); |
| 2680 |
route->SetPosY(posY); |
| 2681 |
route->SetPosZ(posZ); |
| 2682 |
route->SetRotation(DataModel::LayoutItem::Rotation0); |
| 2683 |
route->SetAutomode(automode); |
| 2684 |
if (automode == AutomodeYes) |
| 2685 |
{ |
| 2686 |
route->SetFromTrack(fromTrack); |
| 2687 |
route->SetFromOrientation(fromOrientation); |
| 2688 |
route->SetToTrack(toTrack); |
| 2689 |
route->SetToOrientation(toOrientation); |
| 2690 |
route->SetSpeed(speed); |
| 2691 |
route->SetFeedbackIdReduced(feedbackIdReduced); |
| 2692 |
route->SetReducedDelay(reducedDelay); |
| 2693 |
route->SetFeedbackIdCreep(feedbackIdCreep); |
| 2694 |
route->SetCreepDelay(creepDelay); |
| 2695 |
route->SetFeedbackIdStop(feedbackIdStop); |
| 2696 |
route->SetStopDelay(stopDelay); |
| 2697 |
route->SetFeedbackIdOver(feedbackIdOver); |
| 2698 |
route->SetPushpull(pushpull); |
| 2699 |
route->SetPropulsion(propulsion); |
| 2700 |
route->SetTrainType(trainType); |
| 2701 |
route->SetMinTrainLength(minTrainLength); |
| 2702 |
route->SetMaxTrainLength(maxTrainLength); |
| 2703 |
route->SetWaitAfterRelease(waitAfterRelease); |
| 2704 |
route->SetFollowUpRoute(followUpRoute); |
| 2705 |
} |
| 2706 |
else |
| 2707 |
{ |
| 2708 |
route->SetFromTrack(TrackNone); |
| 2709 |
route->SetFromOrientation(OrientationRight); |
| 2710 |
route->SetToTrack(TrackNone); |
| 2711 |
route->SetToOrientation(OrientationLeft); |
| 2712 |
route->SetSpeed(Route::SpeedTravel); |
| 2713 |
route->SetFeedbackIdReduced(FeedbackNone); |
| 2714 |
route->SetReducedDelay(0); |
| 2715 |
route->SetFeedbackIdCreep(FeedbackNone); |
| 2716 |
route->SetCreepDelay(0); |
| 2717 |
route->SetFeedbackIdStop(FeedbackNone); |
| 2718 |
route->SetStopDelay(0); |
| 2719 |
route->SetFeedbackIdOver(FeedbackNone); |
| 2720 |
route->SetPushpull(Route::PushpullTypeBoth); |
| 2721 |
route->SetPropulsion(PropulsionAll); |
| 2722 |
route->SetTrainType(TrainTypeAll); |
| 2723 |
route->SetMinTrainLength(0); |
| 2724 |
route->SetMaxTrainLength(0); |
| 2725 |
route->SetWaitAfterRelease(0); |
| 2726 |
route->SetFollowUpRoute(RouteNone); |
| 2727 |
} |
| 2728 |
|
| 2729 |
// Add new route |
| 2730 |
Track* newTrack = GetTrack(route->GetFromTrack()); |
| 2731 |
if (newTrack) |
| 2732 |
{ |
| 2733 |
newTrack->AddRoute(route); |
| 2734 |
TrackSave(newTrack); |
| 2735 |
} |
| 2736 |
|
| 2737 |
RouteSaveAndPublishSettings(route); |
| 2738 |
return true; |
| 2739 |
} |
| 2740 |
|
| 2741 |
bool Manager::RoutePosition(const RouteID routeID, |
| 2742 |
const LayoutPosition posX, |
| 2743 |
const LayoutPosition posY, |
| 2744 |
string& result) |
| 2745 |
{ |
| 2746 |
Route* route = GetRoute(routeID); |
| 2747 |
if (!route) |
| 2748 |
{ |
| 2749 |
result = Languages::GetText(Languages::TextRouteDoesNotExist); |
| 2750 |
return false; |
| 2751 |
} |
| 2752 |
|
| 2753 |
if (!route->GetVisible()) |
| 2754 |
{ |
| 2755 |
return false; |
| 2756 |
} |
| 2757 |
|
| 2758 |
if (!CheckLayoutItemPosition(route, posX, posY, route->GetPosZ(), result)) |
| 2759 |
{ |
| 2760 |
return false; |
| 2761 |
} |
| 2762 |
|
| 2763 |
route->SetPosX(posX); |
| 2764 |
route->SetPosY(posY); |
| 2765 |
|
| 2766 |
RouteSaveAndPublishSettings(route); |
| 2767 |
return true; |
| 2768 |
} |
| 2769 |
|
| 2770 |
void Manager::RouteSaveAndPublishSettings(const Route* const route) |
| 2771 |
{ |
| 2772 |
RouteSave(route); |
| 2773 |
|
| 2774 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 2775 |
for (auto& control : controls) |
| 2776 |
{ |
| 2777 |
control.second->RouteSettings(route->GetID(), route->GetName()); |
| 2778 |
} |
| 2779 |
} |
| 2780 |
|
| 2781 |
const map<string,DataModel::Route*> Manager::RouteListByName() const |
| 2782 |
{ |
| 2783 |
map<string,DataModel::Route*> out; |
| 2784 |
std::lock_guard<std::mutex> guard(routeMutex); |
| 2785 |
for (auto& route : routes) |
| 2786 |
{ |
| 2787 |
out[route.second->GetName()] = route.second; |
| 2788 |
} |
| 2789 |
return out; |
| 2790 |
} |
| 2791 |
|
| 2792 |
bool Manager::RouteDelete(const RouteID routeID, |
| 2793 |
string& result) |
| 2794 |
{ |
| 2795 |
Route* route = nullptr; |
| 2796 |
{ |
| 2797 |
{ |
| 2798 |
std::lock_guard<std::mutex> guard(routeMutex); |
| 2799 |
if (routeID == RouteNone || routes.count(routeID) != 1) |
| 2800 |
{ |
| 2801 |
result = Languages::GetText(Languages::TextRouteDoesNotExist); |
| 2802 |
return false; |
| 2803 |
} |
| 2804 |
|
| 2805 |
route = routes.at(routeID); |
| 2806 |
if (!route) |
| 2807 |
{ |
| 2808 |
result = Languages::GetText(Languages::TextRouteDoesNotExist); |
| 2809 |
return false; |
| 2810 |
} |
| 2811 |
|
| 2812 |
if (route->IsInUse()) |
| 2813 |
{ |
| 2814 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextRouteIsInUse), route->GetName()); |
| 2815 |
return false; |
| 2816 |
} |
| 2817 |
} |
| 2818 |
|
| 2819 |
// this must not be in the lock_guard, it would be a deadlock. |
| 2820 |
ObjectIdentifier routeIdentifier(ObjectTypeRoute, routeID); |
| 2821 |
if (ObjectIsPartOfRoute(routeIdentifier, route, result)) |
| 2822 |
{ |
| 2823 |
return false; |
| 2824 |
} |
| 2825 |
|
| 2826 |
{ |
| 2827 |
std::lock_guard<std::mutex> guard(routeMutex); |
| 2828 |
routes.erase(routeID); |
| 2829 |
} |
| 2830 |
} |
| 2831 |
|
| 2832 |
if (storage) |
| 2833 |
{ |
| 2834 |
TransactionGuard guard(storage); |
| 2835 |
storage->DeleteRoute(routeID); |
| 2836 |
} |
| 2837 |
|
| 2838 |
const string& routeName = route->GetName(); |
| 2839 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 2840 |
for (auto& control : controls) |
| 2841 |
{ |
| 2842 |
control.second->RouteDelete(routeID, routeName); |
| 2843 |
} |
| 2844 |
delete route; |
| 2845 |
return true; |
| 2846 |
} |
| 2847 |
|
| 2848 |
Route* Manager::GetFirstRouteFromOrToTrack(const TrackID trackID) const |
| 2849 |
{ |
| 2850 |
std::lock_guard<std::mutex> guard(routeMutex); |
| 2851 |
for (auto& route : routes) |
| 2852 |
{ |
| 2853 |
if (route.second->GetToTrack() == trackID || route.second->GetFromTrack() == trackID) |
| 2854 |
{ |
| 2855 |
return route.second; |
| 2856 |
} |
| 2857 |
} |
| 2858 |
return nullptr; |
| 2859 |
} |
| 2860 |
|
| 2861 |
Layer* Manager::GetLayer(const LayerID layerID) const |
| 2862 |
{ |
| 2863 |
std::lock_guard<std::mutex> guard(layerMutex); |
| 2864 |
if (layers.count(layerID) != 1) |
| 2865 |
{ |
| 2866 |
return nullptr; |
| 2867 |
} |
| 2868 |
return layers.at(layerID); |
| 2869 |
} |
| 2870 |
|
| 2871 |
const map<string,LayerID> Manager::LayerListByName() const |
| 2872 |
{ |
| 2873 |
map<string,LayerID> list; |
| 2874 |
std::lock_guard<std::mutex> guard(layerMutex); |
| 2875 |
for (auto& layer : layers) |
| 2876 |
{ |
| 2877 |
list[layer.second->GetName()] = layer.first; |
| 2878 |
} |
| 2879 |
return list; |
| 2880 |
} |
| 2881 |
|
| 2882 |
const map<string,LayerID> Manager::LayerListByNameWithFeedback() const |
| 2883 |
{ |
| 2884 |
map<string,LayerID> list = LayerListByName(); |
| 2885 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 2886 |
for (auto& control : controls) |
| 2887 |
{ |
| 2888 |
if (!control.second->CanHandle(Hardware::CapabilityFeedback)) |
| 2889 |
{ |
| 2890 |
continue; |
| 2891 |
} |
| 2892 |
list["| Feedbacks of " + control.second->GetShortName()] = -control.first; |
| 2893 |
} |
| 2894 |
return list; |
| 2895 |
} |
| 2896 |
|
| 2897 |
bool Manager::LayerSave(LayerID layerID, const std::string&name, std::string& result) |
| 2898 |
{ |
| 2899 |
Layer* layer = GetLayer(layerID); |
| 2900 |
if (!layer) |
| 2901 |
{ |
| 2902 |
layer = CreateAndAddObject(layers, layerMutex); |
| 2903 |
} |
| 2904 |
|
| 2905 |
if (!layer) |
| 2906 |
{ |
| 2907 |
result = Languages::GetText(Languages::TextUnableToAddLayer); |
| 2908 |
return false; |
| 2909 |
} |
| 2910 |
|
| 2911 |
// if we have a new object we have to update layerID |
| 2912 |
layerID = layer->GetID(); |
| 2913 |
|
| 2914 |
// update existing layer |
| 2915 |
layer->SetName(CheckObjectName(layers, layerMutex, layerID, name.size() == 0 ? "L" : name)); |
| 2916 |
|
| 2917 |
// save in db |
| 2918 |
if (storage) |
| 2919 |
{ |
| 2920 |
TransactionGuard guard(storage); |
| 2921 |
storage->Save(*layer); |
| 2922 |
} |
| 2923 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 2924 |
for (auto& control : controls) |
| 2925 |
{ |
| 2926 |
control.second->LayerSettings(layerID, name); |
| 2927 |
} |
| 2928 |
return true; |
| 2929 |
} |
| 2930 |
|
| 2931 |
bool Manager::LayerHasElements(const Layer* layer, |
| 2932 |
string& result) |
| 2933 |
{ |
| 2934 |
LayerID layerId = layer->GetID(); |
| 2935 |
for (auto& track : tracks) |
| 2936 |
{ |
| 2937 |
if (track.second->IsVisibleOnLayer(layerId)) |
| 2938 |
{ |
| 2939 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextLayerIsUsedByTrack), layer->GetName(), track.second->GetName()); |
| 2940 |
return true; |
| 2941 |
} |
| 2942 |
} |
| 2943 |
for (auto& mySwitch : switches) |
| 2944 |
{ |
| 2945 |
if (mySwitch.second->IsVisibleOnLayer(layerId)) |
| 2946 |
{ |
| 2947 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextLayerIsUsedBySwitch), layer->GetName(), mySwitch.second->GetName()); |
| 2948 |
return true; |
| 2949 |
} |
| 2950 |
} |
| 2951 |
for (auto& signal : signals) |
| 2952 |
{ |
| 2953 |
if (signal.second->IsVisibleOnLayer(layerId)) |
| 2954 |
{ |
| 2955 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextLayerIsUsedBySignal), layer->GetName(), signal.second->GetName()); |
| 2956 |
return true; |
| 2957 |
} |
| 2958 |
} |
| 2959 |
for (auto& accessory : accessories) |
| 2960 |
{ |
| 2961 |
if (accessory.second->IsVisibleOnLayer(layerId)) |
| 2962 |
{ |
| 2963 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextLayerIsUsedByAccessory), layer->GetName(), accessory.second->GetName()); |
| 2964 |
return true; |
| 2965 |
} |
| 2966 |
} |
| 2967 |
for (auto& route : routes) |
| 2968 |
{ |
| 2969 |
if (route.second->IsVisibleOnLayer(layerId)) |
| 2970 |
{ |
| 2971 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextLayerIsUsedByRoute), layer->GetName(), route.second->GetName()); |
| 2972 |
return true; |
| 2973 |
} |
| 2974 |
} |
| 2975 |
for (auto& feedback : feedbacks) |
| 2976 |
{ |
| 2977 |
if (feedback.second->IsVisibleOnLayer(layerId)) |
| 2978 |
{ |
| 2979 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextLayerIsUsedByFeedback), layer->GetName(), feedback.second->GetName()); |
| 2980 |
return true; |
| 2981 |
} |
| 2982 |
} |
| 2983 |
for (auto& text : texts) |
| 2984 |
{ |
| 2985 |
if (text.second->IsVisibleOnLayer(layerId)) |
| 2986 |
{ |
| 2987 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextLayerIsUsedByText), layer->GetName(), text.second->GetName()); |
| 2988 |
return true; |
| 2989 |
} |
| 2990 |
} |
| 2991 |
return false; |
| 2992 |
} |
| 2993 |
|
| 2994 |
bool Manager::LayerDelete(const LayerID layerID, |
| 2995 |
string& result) |
| 2996 |
{ |
| 2997 |
if (layerID == LayerUndeletable) |
| 2998 |
{ |
| 2999 |
result = Languages::GetText(Languages::TextLayer1IsUndeletable); |
| 3000 |
return false; |
| 3001 |
} |
| 3002 |
|
| 3003 |
if (layerID == LayerNone) |
| 3004 |
{ |
| 3005 |
result = Languages::GetText(Languages::TextLayerDoesNotExist); |
| 3006 |
return false; |
| 3007 |
} |
| 3008 |
|
| 3009 |
Layer* layer = nullptr; |
| 3010 |
{ |
| 3011 |
std::lock_guard<std::mutex> guard(layerMutex); |
| 3012 |
if (layers.count(layerID) != 1) |
| 3013 |
{ |
| 3014 |
result = Languages::GetText(Languages::TextLayerDoesNotExist); |
| 3015 |
return false; |
| 3016 |
} |
| 3017 |
|
| 3018 |
layer = layers.at(layerID); |
| 3019 |
if (!layer) |
| 3020 |
{ |
| 3021 |
result = Languages::GetText(Languages::TextLayerDoesNotExist); |
| 3022 |
return false; |
| 3023 |
} |
| 3024 |
|
| 3025 |
if (LayerHasElements(layer, result)) |
| 3026 |
{ |
| 3027 |
return false; |
| 3028 |
} |
| 3029 |
|
| 3030 |
layers.erase(layerID); |
| 3031 |
} |
| 3032 |
|
| 3033 |
if (storage) |
| 3034 |
{ |
| 3035 |
TransactionGuard guard(storage); |
| 3036 |
storage->DeleteLayer(layerID); |
| 3037 |
} |
| 3038 |
|
| 3039 |
const string& layerName = layer->GetName(); |
| 3040 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 3041 |
for (auto& control : controls) |
| 3042 |
{ |
| 3043 |
control.second->LayerDelete(layerID, layerName); |
| 3044 |
} |
| 3045 |
delete layer; |
| 3046 |
return true; |
| 3047 |
} |
| 3048 |
|
| 3049 |
/*************************** |
| 3050 |
* Signal * |
| 3051 |
***************************/ |
| 3052 |
|
| 3053 |
bool Manager::SignalState(const ControlType controlType, const SignalID signalID, const DataModel::AccessoryState state, const bool force) |
| 3054 |
{ |
| 3055 |
if (boosterState == BoosterStateStop) |
| 3056 |
{ |
| 3057 |
return false; |
| 3058 |
} |
| 3059 |
Signal* signal = GetSignal(signalID); |
| 3060 |
if (!signal) |
| 3061 |
{ |
| 3062 |
return false; |
| 3063 |
} |
| 3064 |
return SignalState(controlType, signal, state, force); |
| 3065 |
} |
| 3066 |
|
| 3067 |
bool Manager::SignalState(const ControlType controlType, Signal* signal, const DataModel::AccessoryState state, const bool force) |
| 3068 |
{ |
| 3069 |
if (!signal) |
| 3070 |
{ |
| 3071 |
return false; |
| 3072 |
} |
| 3073 |
|
| 3074 |
if (!force && signal->IsInUse()) |
| 3075 |
{ |
| 3076 |
logger->Warning(Languages::TextSignalIsLocked, signal->GetName()); |
| 3077 |
return false; |
| 3078 |
} |
| 3079 |
|
| 3080 |
const DataModel::AccessoryState oldState = signal->GetAccessoryState(); |
| 3081 |
if (oldState == state) |
| 3082 |
{ |
| 3083 |
return true; |
| 3084 |
} |
| 3085 |
signal->SetAccessoryState(state); |
| 3086 |
|
| 3087 |
SignalPublishState(controlType, signal); |
| 3088 |
return true; |
| 3089 |
} |
| 3090 |
|
| 3091 |
void Manager::SignalPublishState(const ControlType controlType, const DataModel::Signal* signal) |
| 3092 |
{ |
| 3093 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 3094 |
for (auto& control : controls) |
| 3095 |
{ |
| 3096 |
control.second->SignalState(controlType, signal); |
| 3097 |
} |
| 3098 |
} |
| 3099 |
|
| 3100 |
Signal* Manager::GetSignal(const SignalID signalID) const |
| 3101 |
{ |
| 3102 |
std::lock_guard<std::mutex> guard(signalMutex); |
| 3103 |
if (signals.count(signalID) != 1) |
| 3104 |
{ |
| 3105 |
return nullptr; |
| 3106 |
} |
| 3107 |
return signals.at(signalID); |
| 3108 |
} |
| 3109 |
|
| 3110 |
Signal* Manager::GetSignal(const ControlID controlID, const Protocol protocol, const Address address) const |
| 3111 |
{ |
| 3112 |
std::lock_guard<std::mutex> guard(signalMutex); |
| 3113 |
for (auto& signal : signals) |
| 3114 |
{ |
| 3115 |
Signal* s = signal.second; |
| 3116 |
if ((s->GetControlID() == controlID) |
| 3117 |
&& (s->GetProtocol() == protocol) |
| 3118 |
&& (s->UsesAddress(address))) |
| 3119 |
{ |
| 3120 |
return s; |
| 3121 |
} |
| 3122 |
} |
| 3123 |
return nullptr; |
| 3124 |
} |
| 3125 |
|
| 3126 |
const std::string& Manager::GetSignalName(const SignalID signalID) const |
| 3127 |
{ |
| 3128 |
if (signals.count(signalID) != 1) |
| 3129 |
{ |
| 3130 |
return unknownSignal; |
| 3131 |
} |
| 3132 |
return signals.at(signalID)->GetName(); |
| 3133 |
} |
| 3134 |
|
| 3135 |
bool Manager::SignalSave(SignalID signalID, |
| 3136 |
const string& name, |
| 3137 |
const LayoutPosition posX, |
| 3138 |
const LayoutPosition posY, |
| 3139 |
const LayoutPosition posZ, |
| 3140 |
const LayoutItemSize height, |
| 3141 |
const LayoutRotation rotation, |
| 3142 |
const ControlID controlID, |
| 3143 |
const string& matchKey, |
| 3144 |
const Protocol protocol, |
| 3145 |
const Address address, |
| 3146 |
const Address serverAddress, |
| 3147 |
const DataModel::AccessoryType type, |
| 3148 |
const std::map<DataModel::AccessoryState,DataModel::AddressOffset>& offsets, |
| 3149 |
const DataModel::AccessoryPulseDuration duration, |
| 3150 |
const bool inverted, |
| 3151 |
string& result) |
| 3152 |
{ |
| 3153 |
if (!CheckControlAccessoryProtocolAddress(controlID, protocol, address, result)) |
| 3154 |
{ |
| 3155 |
return false; |
| 3156 |
} |
| 3157 |
|
| 3158 |
Signal* signal = GetSignal(signalID); |
| 3159 |
if (!CheckLayoutItemPosition(signal, posX, posY, posZ, result)) |
| 3160 |
{ |
| 3161 |
return false; |
| 3162 |
} |
| 3163 |
|
| 3164 |
if (!signal) |
| 3165 |
{ |
| 3166 |
signal = CreateAndAddObject(signals, signalMutex); |
| 3167 |
} |
| 3168 |
|
| 3169 |
if (!signal) |
| 3170 |
{ |
| 3171 |
result = Languages::GetText(Languages::TextUnableToAddSignal); |
| 3172 |
return false; |
| 3173 |
} |
| 3174 |
|
| 3175 |
// if we have a new object we have to update locoID |
| 3176 |
signalID = signal->GetID(); |
| 3177 |
|
| 3178 |
signal->SetName(CheckObjectName(signals, signalMutex, signalID, name.size() == 0 ? "S" : name)); |
| 3179 |
signal->SetPosX(posX); |
| 3180 |
signal->SetPosY(posY); |
| 3181 |
signal->SetPosZ(posZ); |
| 3182 |
signal->SetHeight(height); |
| 3183 |
signal->SetRotation(rotation); |
| 3184 |
signal->SetControlID(controlID); |
| 3185 |
signal->SetMatchKey(matchKey); |
| 3186 |
signal->SetProtocol(protocol); |
| 3187 |
signal->SetAddress(address); |
| 3188 |
signal->SetServerAddress(serverAddress); |
| 3189 |
signal->SetAccessoryType(type); |
| 3190 |
signal->SetStateAddressOffsets(offsets); |
| 3191 |
signal->SetAccessoryPulseDuration(duration); |
| 3192 |
signal->SetInverted(inverted); |
| 3193 |
|
| 3194 |
SignalSaveAndPublishSettings(signal); |
| 3195 |
return true; |
| 3196 |
} |
| 3197 |
|
| 3198 |
bool Manager::SignalPosition(const SignalID signalID, |
| 3199 |
const LayoutPosition posX, |
| 3200 |
const LayoutPosition posY, |
| 3201 |
string& result) |
| 3202 |
{ |
| 3203 |
Signal* signal = GetSignal(signalID); |
| 3204 |
if (!signal) |
| 3205 |
{ |
| 3206 |
result = Languages::GetText(Languages::TextSignalDoesNotExist); |
| 3207 |
return false; |
| 3208 |
} |
| 3209 |
|
| 3210 |
if (!CheckLayoutItemPosition(signal, posX, posY, signal->GetPosZ(), result)) |
| 3211 |
{ |
| 3212 |
return false; |
| 3213 |
} |
| 3214 |
|
| 3215 |
signal->SetPosX(posX); |
| 3216 |
signal->SetPosY(posY); |
| 3217 |
|
| 3218 |
SignalSaveAndPublishSettings(signal); |
| 3219 |
return true; |
| 3220 |
} |
| 3221 |
|
| 3222 |
bool Manager::SignalRotate(const SignalID signalID, |
| 3223 |
string& result) |
| 3224 |
{ |
| 3225 |
Signal* signal = GetSignal(signalID); |
| 3226 |
if (!signal) |
| 3227 |
{ |
| 3228 |
result = Languages::GetText(Languages::TextSignalDoesNotExist); |
| 3229 |
return false; |
| 3230 |
} |
| 3231 |
|
| 3232 |
signal->Rotate(); |
| 3233 |
|
| 3234 |
SignalSaveAndPublishSettings(signal); |
| 3235 |
return true; |
| 3236 |
} |
| 3237 |
|
| 3238 |
void Manager::SignalSaveAndPublishSettings(const Signal* const signal) |
| 3239 |
{ |
| 3240 |
// save in db |
| 3241 |
if (storage) |
| 3242 |
{ |
| 3243 |
TransactionGuard guard(storage); |
| 3244 |
storage->Save(*signal); |
| 3245 |
} |
| 3246 |
|
| 3247 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 3248 |
for (auto& control : controls) |
| 3249 |
{ |
| 3250 |
control.second->SignalSettings(signal->GetID(), signal->GetName(), signal->GetMatchKey()); |
| 3251 |
} |
| 3252 |
} |
| 3253 |
|
| 3254 |
bool Manager::SignalDelete(const SignalID signalID, |
| 3255 |
string& result) |
| 3256 |
{ |
| 3257 |
Signal* signal = nullptr; |
| 3258 |
{ |
| 3259 |
std::lock_guard<std::mutex> guard(signalMutex); |
| 3260 |
if (signalID == SignalNone || signals.count(signalID) != 1) |
| 3261 |
{ |
| 3262 |
result = Languages::GetText(Languages::TextSignalDoesNotExist); |
| 3263 |
return false; |
| 3264 |
} |
| 3265 |
|
| 3266 |
signal = signals.at(signalID); |
| 3267 |
if (!signal) |
| 3268 |
{ |
| 3269 |
result = Languages::GetText(Languages::TextSignalDoesNotExist); |
| 3270 |
return false; |
| 3271 |
} |
| 3272 |
|
| 3273 |
if (signal->IsInUse()) |
| 3274 |
{ |
| 3275 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextSignalIsUsedByLoco), signal->GetName(), GetLocoBaseName(signal->GetLocoBase())); |
| 3276 |
return false; |
| 3277 |
} |
| 3278 |
|
| 3279 |
Track* track = signal->GetTrack(); |
| 3280 |
if (track) |
| 3281 |
{ |
| 3282 |
result = Logger::Logger::Format(Languages::GetText(Languages::TextSignalIsUsedByTrack), signal->GetName(), track->GetName()); |
| 3283 |
return false; |
| 3284 |
} |
| 3285 |
|
| 3286 |
ObjectIdentifier signalIdentifier(ObjectTypeSignal, signalID); |
| 3287 |
if (ObjectIsPartOfRoute(signalIdentifier, signal, result)) |
| 3288 |
{ |
| 3289 |
return false; |
| 3290 |
} |
| 3291 |
|
| 3292 |
signals.erase(signalID); |
| 3293 |
} |
| 3294 |
|
| 3295 |
if (storage) |
| 3296 |
{ |
| 3297 |
TransactionGuard guard(storage); |
| 3298 |
storage->DeleteSignal(signalID); |
| 3299 |
} |
| 3300 |
|
| 3301 |
const string& name = signal->GetName(); |
| 3302 |
const string& matchKey = signal->GetMatchKey(); |
| 3303 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 3304 |
for (auto& control : controls) |
| 3305 |
{ |
| 3306 |
control.second->SignalDelete(signalID, name, matchKey); |
| 3307 |
} |
| 3308 |
|
| 3309 |
delete signal; |
| 3310 |
return true; |
| 3311 |
} |
| 3312 |
|
| 3313 |
const map<string,DataModel::Signal*> Manager::SignalListByName() const |
| 3314 |
{ |
| 3315 |
map<string,DataModel::Signal*> out; |
| 3316 |
std::lock_guard<std::mutex> guard(signalMutex); |
| 3317 |
for (auto& signal : signals) |
| 3318 |
{ |
| 3319 |
out[signal.second->GetName()] = signal.second; |
| 3320 |
} |
| 3321 |
return out; |
| 3322 |
} |
| 3323 |
|
| 3324 |
const map<string,DataModel::AccessoryConfig> Manager::SignalConfigByName() const |
| 3325 |
{ |
| 3326 |
map<string,DataModel::AccessoryConfig> out; |
| 3327 |
{ |
| 3328 |
std::lock_guard<std::mutex> guard(signalMutex); |
| 3329 |
for (auto& signal : signals) |
| 3330 |
{ |
| 3331 |
out[signal.second->GetName()] = *(signal.second); |
| 3332 |
} |
| 3333 |
} |
| 3334 |
|
| 3335 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 3336 |
for (auto& control : controls) |
| 3337 |
{ |
| 3338 |
control.second->AddUnmatchedAccessories(out); |
| 3339 |
} |
| 3340 |
return out; |
| 3341 |
} |
| 3342 |
|
| 3343 |
Signal* Manager::GetSignalByMatchKey(const ControlID controlId, const string& matchKey) const |
| 3344 |
{ |
| 3345 |
std::lock_guard<std::mutex> guard(signalMutex); |
| 3346 |
for (auto& signal : signals) |
| 3347 |
{ |
| 3348 |
Signal* signalConfig = signal.second; |
| 3349 |
if (signalConfig->GetControlID() == controlId && signalConfig->GetMatchKey().compare(matchKey) == 0) |
| 3350 |
{ |
| 3351 |
return signalConfig; |
| 3352 |
} |
| 3353 |
} |
| 3354 |
return nullptr; |
| 3355 |
} |
| 3356 |
|
| 3357 |
void Manager::SignalRemoveMatchKey(const SignalID signalId) |
| 3358 |
{ |
| 3359 |
Signal* signal = GetSignal(signalId); |
| 3360 |
if (!signal) |
| 3361 |
{ |
| 3362 |
return; |
| 3363 |
} |
| 3364 |
signal->ClearMatchKey(); |
| 3365 |
} |
| 3366 |
|
| 3367 |
bool Manager::SignalRelease(const SignalID signalID) |
| 3368 |
{ |
| 3369 |
Signal* signal = GetSignal(signalID); |
| 3370 |
if (!signal) |
| 3371 |
{ |
| 3372 |
return false; |
| 3373 |
} |
| 3374 |
return signal->Release(logger, ObjectIdentifier()); |
| 3375 |
} |
| 3376 |
|
| 3377 |
/*************************** |
| 3378 |
* Cluster * |
| 3379 |
***************************/ |
| 3380 |
|
| 3381 |
Cluster* Manager::GetCluster(const ClusterID clusterID) const |
| 3382 |
{ |
| 3383 |
std::lock_guard<std::mutex> guard(clusterMutex); |
| 3384 |
if (clusters.count(clusterID) != 1) |
| 3385 |
{ |
| 3386 |
return nullptr; |
| 3387 |
} |
| 3388 |
return clusters.at(clusterID); |
| 3389 |
} |
| 3390 |
|
| 3391 |
const map<string,DataModel::Cluster*> Manager::ClusterListByName() const |
| 3392 |
{ |
| 3393 |
map<string,DataModel::Cluster*> out; |
| 3394 |
std::lock_guard<std::mutex> guard(clusterMutex); |
| 3395 |
for (auto& cluster : clusters) |
| 3396 |
{ |
| 3397 |
out[cluster.second->GetName()] = cluster.second; |
| 3398 |
} |
| 3399 |
return out; |
| 3400 |
} |
| 3401 |
|
| 3402 |
bool Manager::ClusterSave(ClusterID clusterID, |
| 3403 |
const string& name, |
| 3404 |
const vector<Relation*>& newTracks, |
| 3405 |
string& result) |
| 3406 |
{ |
| 3407 |
Cluster* cluster = GetCluster(clusterID); |
| 3408 |
if (!cluster) |
| 3409 |
{ |
| 3410 |
cluster = CreateAndAddObject(clusters, clusterMutex); |
| 3411 |
} |
| 3412 |
|
| 3413 |
if (!cluster) |
| 3414 |
{ |
| 3415 |
result = Languages::GetText(Languages::TextUnableToAddCluster); |
| 3416 |
return false; |
| 3417 |
} |
| 3418 |
|
| 3419 |
// if we have a new object we have to update clusterID |
| 3420 |
if (clusterID == 0) |
| 3421 |
{ |
| 3422 |
clusterID = cluster->GetID(); |
| 3423 |
for (auto track : newTracks) |
| 3424 |
{ |
| 3425 |
track->ObjectID1(clusterID); |
| 3426 |
} |
| 3427 |
} |
| 3428 |
|
| 3429 |
// update existing cluster |
| 3430 |
cluster->SetName(CheckObjectName(clusters, clusterMutex, clusterID, name.size() == 0 ? "C" : name)); |
| 3431 |
cluster->AssignTracks(newTracks); |
| 3432 |
|
| 3433 |
// save in db |
| 3434 |
if (storage) |
| 3435 |
{ |
| 3436 |
TransactionGuard guard(storage); |
| 3437 |
storage->Save(*cluster); |
| 3438 |
} |
| 3439 |
return true; |
| 3440 |
} |
| 3441 |
|
| 3442 |
bool Manager::ClusterDelete(const ClusterID clusterID) |
| 3443 |
{ |
| 3444 |
if (clusterID == ClusterNone) |
| 3445 |
{ |
| 3446 |
return false; |
| 3447 |
} |
| 3448 |
Cluster* cluster = nullptr; |
| 3449 |
{ |
| 3450 |
std::lock_guard<std::mutex> guard(clusterMutex); |
| 3451 |
if (clusters.count(clusterID) != 1) |
| 3452 |
{ |
| 3453 |
return false; |
| 3454 |
} |
| 3455 |
|
| 3456 |
cluster = clusters.at(clusterID); |
| 3457 |
clusters.erase(clusterID); |
| 3458 |
} |
| 3459 |
|
| 3460 |
cluster->DeleteTracks(); |
| 3461 |
|
| 3462 |
if (storage) |
| 3463 |
{ |
| 3464 |
TransactionGuard guard(storage); |
| 3465 |
storage->DeleteCluster(clusterID); |
| 3466 |
} |
| 3467 |
|
| 3468 |
const string& clusterName = cluster->GetName(); |
| 3469 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 3470 |
for (auto& control : controls) |
| 3471 |
{ |
| 3472 |
control.second->ClusterDelete(clusterID, clusterName); |
| 3473 |
} |
| 3474 |
delete cluster; |
| 3475 |
return true; |
| 3476 |
} |
| 3477 |
|
| 3478 |
/*************************** |
| 3479 |
* Text * |
| 3480 |
***************************/ |
| 3481 |
|
| 3482 |
Text* Manager::GetText(const TextID textID) const |
| 3483 |
{ |
| 3484 |
std::lock_guard<std::mutex> guard(textMutex); |
| 3485 |
if (texts.count(textID) != 1) |
| 3486 |
{ |
| 3487 |
return nullptr; |
| 3488 |
} |
| 3489 |
return texts.at(textID); |
| 3490 |
} |
| 3491 |
|
| 3492 |
const map<string,DataModel::Text*> Manager::TextListByName() const |
| 3493 |
{ |
| 3494 |
map<string,DataModel::Text*> out; |
| 3495 |
std::lock_guard<std::mutex> guard(textMutex); |
| 3496 |
for (auto& text : texts) |
| 3497 |
{ |
| 3498 |
out[text.second->GetName()] = text.second; |
| 3499 |
} |
| 3500 |
return out; |
| 3501 |
} |
| 3502 |
|
| 3503 |
bool Manager::TextSave(TextID textID, |
| 3504 |
const string& name, |
| 3505 |
const LayoutPosition posX, |
| 3506 |
const LayoutPosition posY, |
| 3507 |
const LayoutPosition posZ, |
| 3508 |
const LayoutItemSize width, |
| 3509 |
const LayoutRotation rotation, |
| 3510 |
string& result) |
| 3511 |
{ |
| 3512 |
Text* text = GetText(textID); |
| 3513 |
if (!CheckLayoutItemPosition(text, posX, posY, posZ, width, LayoutItem::Height1, rotation, result)) |
| 3514 |
{ |
| 3515 |
return false; |
| 3516 |
} |
| 3517 |
|
| 3518 |
if (!text) |
| 3519 |
{ |
| 3520 |
text = CreateAndAddObject(texts, textMutex); |
| 3521 |
} |
| 3522 |
|
| 3523 |
if (!text) |
| 3524 |
{ |
| 3525 |
result = Languages::GetText(Languages::TextUnableToAddText); |
| 3526 |
return false; |
| 3527 |
} |
| 3528 |
|
| 3529 |
// if we have a new object we have to update textID |
| 3530 |
textID = text->GetID(); |
| 3531 |
|
| 3532 |
// update existing text |
| 3533 |
text->SetName(CheckObjectName(texts, textMutex, textID, name.size() == 0 ? "T" : name)); |
| 3534 |
text->SetPosX(posX); |
| 3535 |
text->SetPosY(posY); |
| 3536 |
text->SetPosZ(posZ); |
| 3537 |
text->SetWidth(width); |
| 3538 |
text->SetHeight(LayoutItem::Height1); |
| 3539 |
text->SetRotation(rotation); |
| 3540 |
|
| 3541 |
TextSaveAndPublishSettings(text); |
| 3542 |
return true; |
| 3543 |
} |
| 3544 |
|
| 3545 |
bool Manager::TextPosition(TextID textID, |
| 3546 |
const LayoutPosition posX, |
| 3547 |
const LayoutPosition posY, |
| 3548 |
string& result) |
| 3549 |
{ |
| 3550 |
Text* text = GetText(textID); |
| 3551 |
if (!text) |
| 3552 |
{ |
| 3553 |
result = Languages::GetText(Languages::TextTextDoesNotExist); |
| 3554 |
return false; |
| 3555 |
} |
| 3556 |
|
| 3557 |
if (!CheckLayoutItemPosition(text, posX, posY, text->GetPosZ(), text->GetWidth(), LayoutItem::Height1, text->GetRotation(), result)) |
| 3558 |
{ |
| 3559 |
return false; |
| 3560 |
} |
| 3561 |
|
| 3562 |
text->SetPosX(posX); |
| 3563 |
text->SetPosY(posY); |
| 3564 |
|
| 3565 |
TextSaveAndPublishSettings(text); |
| 3566 |
return true; |
| 3567 |
} |
| 3568 |
|
| 3569 |
bool Manager::TextRotate(TextID textID, |
| 3570 |
string& result) |
| 3571 |
{ |
| 3572 |
Text* text = GetText(textID); |
| 3573 |
if (!text) |
| 3574 |
{ |
| 3575 |
result = Languages::GetText(Languages::TextTextDoesNotExist); |
| 3576 |
return false; |
| 3577 |
} |
| 3578 |
|
| 3579 |
LayoutRotation newRotation = text->GetRotation(); |
| 3580 |
++newRotation; |
| 3581 |
if (!CheckLayoutItemPosition(text, text->GetPosX(), text->GetPosY(), text->GetPosZ(), text->GetWidth(), LayoutItem::Height1, newRotation, result)) |
| 3582 |
{ |
| 3583 |
return false; |
| 3584 |
} |
| 3585 |
|
| 3586 |
text->SetRotation(newRotation); |
| 3587 |
|
| 3588 |
TextSaveAndPublishSettings(text); |
| 3589 |
return true; |
| 3590 |
} |
| 3591 |
|
| 3592 |
void Manager::TextSaveAndPublishSettings(const Text* const text) |
| 3593 |
{ |
| 3594 |
// save in db |
| 3595 |
if (storage) |
| 3596 |
{ |
| 3597 |
TransactionGuard guard(storage); |
| 3598 |
storage->Save(*text); |
| 3599 |
} |
| 3600 |
|
| 3601 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 3602 |
for (auto& control : controls) |
| 3603 |
{ |
| 3604 |
control.second->TextSettings(text->GetID(), text->GetName()); |
| 3605 |
} |
| 3606 |
} |
| 3607 |
|
| 3608 |
bool Manager::TextDelete(const TextID textID, string& result) |
| 3609 |
{ |
| 3610 |
if (textID == TextNone) |
| 3611 |
{ |
| 3612 |
result = Languages::GetText(Languages::TextTextDoesNotExist); |
| 3613 |
return false; |
| 3614 |
} |
| 3615 |
Text* text = nullptr; |
| 3616 |
{ |
| 3617 |
std::lock_guard<std::mutex> guard(textMutex); |
| 3618 |
if (texts.count(textID) != 1) |
| 3619 |
{ |
| 3620 |
result = Languages::GetText(Languages::TextTextDoesNotExist); |
| 3621 |
return false; |
| 3622 |
} |
| 3623 |
|
| 3624 |
text = texts.at(textID); |
| 3625 |
texts.erase(textID); |
| 3626 |
} |
| 3627 |
|
| 3628 |
if (storage) |
| 3629 |
{ |
| 3630 |
TransactionGuard guard(storage); |
| 3631 |
storage->DeleteText(textID); |
| 3632 |
} |
| 3633 |
|
| 3634 |
const string& textName = text->GetName(); |
| 3635 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 3636 |
for (auto& control : controls) |
| 3637 |
{ |
| 3638 |
control.second->TextDelete(textID, textName); |
| 3639 |
} |
| 3640 |
delete text; |
| 3641 |
return true; |
| 3642 |
} |
| 3643 |
|
| 3644 |
/*************************** |
| 3645 |
* Automode * |
| 3646 |
***************************/ |
| 3647 |
|
| 3648 |
bool Manager::LocoBaseIntoTrack(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier, const TrackID trackID) |
| 3649 |
{ |
| 3650 |
Track* track = GetTrack(trackID); |
| 3651 |
if (!track) |
| 3652 |
{ |
| 3653 |
return false; |
| 3654 |
} |
| 3655 |
|
| 3656 |
LocoBase* locoBase = GetLocoBase(locoBaseIdentifier); |
| 3657 |
if (!locoBase) |
| 3658 |
{ |
| 3659 |
return false; |
| 3660 |
} |
| 3661 |
|
| 3662 |
bool reserved = track->ReserveForce(logger, locoBaseIdentifier); |
| 3663 |
if (!reserved) |
| 3664 |
{ |
| 3665 |
return false; |
| 3666 |
} |
| 3667 |
|
| 3668 |
reserved = locoBase->SetTrack(trackID); |
| 3669 |
if (!reserved) |
| 3670 |
{ |
| 3671 |
track->Release(logger, locoBaseIdentifier); |
| 3672 |
return false; |
| 3673 |
} |
| 3674 |
|
| 3675 |
reserved = track->Lock(logger, locoBaseIdentifier); |
| 3676 |
if (!reserved) |
| 3677 |
{ |
| 3678 |
locoBase->Release(); |
| 3679 |
track->Release(logger, locoBaseIdentifier); |
| 3680 |
return false; |
| 3681 |
} |
| 3682 |
|
| 3683 |
const string& locoName = locoBase->GetName(); |
| 3684 |
const string& trackName = track->GetName(); |
| 3685 |
logger->Info(Languages::TextLocoIsOnTrack, locoName, trackName); |
| 3686 |
|
| 3687 |
TrackPublishState(track); |
| 3688 |
return true; |
| 3689 |
} |
| 3690 |
|
| 3691 |
bool Manager::LocoRelease(const LocoID locoID) |
| 3692 |
{ |
| 3693 |
Loco* loco = GetLoco(locoID); |
| 3694 |
if (!loco) |
| 3695 |
{ |
| 3696 |
return false; |
| 3697 |
} |
| 3698 |
return LocoBaseReleaseInternal(loco); |
| 3699 |
} |
| 3700 |
|
| 3701 |
bool Manager::MultipleUnitRelease(const MultipleUnitID multipleUnitID) |
| 3702 |
{ |
| 3703 |
MultipleUnit* multipleUnit = GetMultipleUnit(multipleUnitID); |
| 3704 |
if (!multipleUnit) |
| 3705 |
{ |
| 3706 |
return false; |
| 3707 |
} |
| 3708 |
return LocoBaseReleaseInternal(multipleUnit); |
| 3709 |
} |
| 3710 |
|
| 3711 |
bool Manager::LocoBaseReleaseInternal(LocoBase* locoBase) |
| 3712 |
{ |
| 3713 |
LocoBaseSpeed(ControlTypeInternal, locoBase, MinSpeed); |
| 3714 |
|
| 3715 |
bool ret = locoBase->Release(); |
| 3716 |
if (!ret) |
| 3717 |
{ |
| 3718 |
return false; |
| 3719 |
} |
| 3720 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 3721 |
for (auto& control : controls) |
| 3722 |
{ |
| 3723 |
control.second->LocoBaseRelease(locoBase); |
| 3724 |
} |
| 3725 |
return true; |
| 3726 |
} |
| 3727 |
|
| 3728 |
bool Manager::TrackRelease(const TrackID trackID) |
| 3729 |
{ |
| 3730 |
Track* track = GetTrack(trackID); |
| 3731 |
if (!track) |
| 3732 |
{ |
| 3733 |
return false; |
| 3734 |
} |
| 3735 |
return track->ReleaseForce(logger, ObjectIdentifier()); |
| 3736 |
} |
| 3737 |
|
| 3738 |
bool Manager::LocoBaseReleaseOnTrack(const TrackID trackID) |
| 3739 |
{ |
| 3740 |
Track* track = GetTrack(trackID); |
| 3741 |
if (!track) |
| 3742 |
{ |
| 3743 |
return false; |
| 3744 |
} |
| 3745 |
const ObjectIdentifier locoBaseIdentifier = track->GetLocoBase(); |
| 3746 |
track->ReleaseForce(logger, locoBaseIdentifier); |
| 3747 |
LocoBase* locoBase = GetLocoBase(locoBaseIdentifier); |
| 3748 |
if (!locoBase) |
| 3749 |
{ |
| 3750 |
return false; |
| 3751 |
} |
| 3752 |
return LocoBaseReleaseInternal(locoBase); |
| 3753 |
} |
| 3754 |
|
| 3755 |
bool Manager::TrackStartLocoBase(const TrackID trackID) |
| 3756 |
{ |
| 3757 |
Track* track = GetTrack(trackID); |
| 3758 |
if (!track) |
| 3759 |
{ |
| 3760 |
return false; |
| 3761 |
} |
| 3762 |
return LocoBaseStart(track->GetLocoBase()); |
| 3763 |
} |
| 3764 |
|
| 3765 |
bool Manager::TrackStopLocoBase(const TrackID trackID) |
| 3766 |
{ |
| 3767 |
Track* track = GetTrack(trackID); |
| 3768 |
if (!track) |
| 3769 |
{ |
| 3770 |
return false; |
| 3771 |
} |
| 3772 |
return LocoBaseStop(track->GetLocoBase()); |
| 3773 |
} |
| 3774 |
|
| 3775 |
void Manager::TrackBlock(const TrackID trackID, const bool blocked) |
| 3776 |
{ |
| 3777 |
Track* track = GetTrack(trackID); |
| 3778 |
if (!track) |
| 3779 |
{ |
| 3780 |
return; |
| 3781 |
} |
| 3782 |
track->SetBlocked(blocked); |
| 3783 |
TrackPublishState(track); |
| 3784 |
} |
| 3785 |
|
| 3786 |
void Manager::TrackSetLocoOrientation(const TrackID trackID, const Orientation orientation) |
| 3787 |
{ |
| 3788 |
Track* track = GetTrack(trackID); |
| 3789 |
if (!track) |
| 3790 |
{ |
| 3791 |
return; |
| 3792 |
} |
| 3793 |
track->SetLocoBaseOrientation(orientation); |
| 3794 |
TrackPublishState(track); |
| 3795 |
} |
| 3796 |
|
| 3797 |
void Manager::TrackPublishState(const DataModel::Track* track) |
| 3798 |
{ |
| 3799 |
{ |
| 3800 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 3801 |
for (auto& control : controls) |
| 3802 |
{ |
| 3803 |
control.second->TrackState(track); |
| 3804 |
} |
| 3805 |
} |
| 3806 |
vector<Track*> extensions = track->GetExtensions(); |
| 3807 |
for (Track* extension : extensions) |
| 3808 |
{ |
| 3809 |
TrackPublishState(extension); |
| 3810 |
} |
| 3811 |
} |
| 3812 |
|
| 3813 |
bool Manager::RouteRelease(const RouteID routeID) |
| 3814 |
{ |
| 3815 |
Route* route = GetRoute(routeID); |
| 3816 |
if (!route) |
| 3817 |
{ |
| 3818 |
return false; |
| 3819 |
} |
| 3820 |
return route->Release(logger, route->GetLocoBase()); |
| 3821 |
} |
| 3822 |
|
| 3823 |
bool Manager::LocoDestinationReached(const LocoBase* loco, |
| 3824 |
const Route* route, |
| 3825 |
const Track* track) |
| 3826 |
{ |
| 3827 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 3828 |
for (auto& control : controls) |
| 3829 |
{ |
| 3830 |
control.second->LocoBaseDestinationReached(loco, route, track); |
| 3831 |
} |
| 3832 |
return true; |
| 3833 |
} |
| 3834 |
|
| 3835 |
bool Manager::LocoBaseStart(const ObjectIdentifier& locoBaseIdentifier) |
| 3836 |
{ |
| 3837 |
LocoBase* locoBase = GetLocoBase(locoBaseIdentifier); |
| 3838 |
if (!locoBase) |
| 3839 |
{ |
| 3840 |
return false; |
| 3841 |
} |
| 3842 |
bool ret = locoBase->GoToAutoMode(); |
| 3843 |
if (!ret) |
| 3844 |
{ |
| 3845 |
return false; |
| 3846 |
} |
| 3847 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 3848 |
for (auto& control : controls) |
| 3849 |
{ |
| 3850 |
control.second->LocoBaseStart(locoBase); |
| 3851 |
} |
| 3852 |
return true; |
| 3853 |
} |
| 3854 |
|
| 3855 |
bool Manager::LocoBaseStartAll() |
| 3856 |
{ |
| 3857 |
{ |
| 3858 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 3859 |
for (auto& loco : locos) |
| 3860 |
{ |
| 3861 |
const bool started = loco.second->GoToAutoMode(); |
| 3862 |
if (!started) |
| 3863 |
{ |
| 3864 |
continue; |
| 3865 |
} |
| 3866 |
Utils::Utils::SleepForMilliseconds(50); |
| 3867 |
} |
| 3868 |
} |
| 3869 |
{ |
| 3870 |
std::lock_guard<std::mutex> guard(multipleUnitMutex); |
| 3871 |
for (auto& multipleUnit : multipleUnits) |
| 3872 |
{ |
| 3873 |
const bool started = multipleUnit.second->GoToAutoMode(); |
| 3874 |
if (!started) |
| 3875 |
{ |
| 3876 |
continue; |
| 3877 |
} |
| 3878 |
Utils::Utils::SleepForMilliseconds(50); |
| 3879 |
} |
| 3880 |
} |
| 3881 |
return true; |
| 3882 |
} |
| 3883 |
|
| 3884 |
bool Manager::LocoBaseStop(const ObjectIdentifier& locoBaseIdentifier) |
| 3885 |
{ |
| 3886 |
LocoBase* locoBase = GetLocoBase(locoBaseIdentifier); |
| 3887 |
if (!locoBase) |
| 3888 |
{ |
| 3889 |
return false; |
| 3890 |
} |
| 3891 |
if (locoBase->IsInManualMode()) |
| 3892 |
{ |
| 3893 |
return true; |
| 3894 |
} |
| 3895 |
locoBase->RequestManualMode(); |
| 3896 |
while (!locoBase->GoToManualMode()) |
| 3897 |
{ |
| 3898 |
Utils::Utils::SleepForSeconds(1); |
| 3899 |
} |
| 3900 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 3901 |
for (auto& control : controls) |
| 3902 |
{ |
| 3903 |
control.second->LocoBaseStop(locoBase); |
| 3904 |
} |
| 3905 |
return true; |
| 3906 |
} |
| 3907 |
|
| 3908 |
bool Manager::LocoBaseStopAll() |
| 3909 |
{ |
| 3910 |
{ |
| 3911 |
std::lock_guard<std::mutex> guard(multipleUnitMutex); |
| 3912 |
for (auto& multipleUnit : multipleUnits) |
| 3913 |
{ |
| 3914 |
if (multipleUnit.second->IsInManualMode()) |
| 3915 |
{ |
| 3916 |
continue; |
| 3917 |
} |
| 3918 |
multipleUnit.second->RequestManualMode(); |
| 3919 |
} |
| 3920 |
} |
| 3921 |
{ |
| 3922 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 3923 |
for (auto& loco : locos) |
| 3924 |
{ |
| 3925 |
if (!loco.second->IsInManualMode()) |
| 3926 |
{ |
| 3927 |
continue; |
| 3928 |
} |
| 3929 |
loco.second->RequestManualMode(); |
| 3930 |
} |
| 3931 |
} |
| 3932 |
bool anyLocosInAutoMode = true; |
| 3933 |
while (anyLocosInAutoMode && !isKillRunning()) |
| 3934 |
{ |
| 3935 |
Utils::Utils::SleepForSeconds(1); |
| 3936 |
anyLocosInAutoMode = false; |
| 3937 |
{ |
| 3938 |
std::lock_guard<std::mutex> guard(multipleUnitMutex); |
| 3939 |
for (auto& multipleUnit : multipleUnits) |
| 3940 |
{ |
| 3941 |
if (multipleUnit.second->IsInManualMode()) |
| 3942 |
{ |
| 3943 |
continue; |
| 3944 |
} |
| 3945 |
const bool multipleUnitInManualMode = multipleUnit.second->GoToManualMode(); |
| 3946 |
if (!multipleUnitInManualMode) |
| 3947 |
{ |
| 3948 |
multipleUnit.second->RequestManualMode(); |
| 3949 |
} |
| 3950 |
anyLocosInAutoMode |= !multipleUnitInManualMode; |
| 3951 |
} |
| 3952 |
} |
| 3953 |
{ |
| 3954 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 3955 |
for (auto& loco : locos) |
| 3956 |
{ |
| 3957 |
if (loco.second->IsInManualMode()) |
| 3958 |
{ |
| 3959 |
continue; |
| 3960 |
} |
| 3961 |
const bool locoInManualMode = loco.second->GoToManualMode(); |
| 3962 |
if (!locoInManualMode) |
| 3963 |
{ |
| 3964 |
loco.second->RequestManualMode(); |
| 3965 |
} |
| 3966 |
anyLocosInAutoMode |= !locoInManualMode; |
| 3967 |
} |
| 3968 |
} |
| 3969 |
} |
| 3970 |
|
| 3971 |
for (auto& multipleUnit : multipleUnits) |
| 3972 |
{ |
| 3973 |
if (multipleUnit.second->IsInManualMode()) |
| 3974 |
{ |
| 3975 |
continue; |
| 3976 |
} |
| 3977 |
multipleUnit.second->GetLogger()->Info(Languages::Languages::TextReleasingMultipleUnit); |
| 3978 |
multipleUnit.second->Release(); |
| 3979 |
} |
| 3980 |
for (auto& loco : locos) |
| 3981 |
{ |
| 3982 |
if (loco.second->IsInManualMode()) |
| 3983 |
{ |
| 3984 |
continue; |
| 3985 |
} |
| 3986 |
loco.second->GetLogger()->Info(Languages::Languages::TextReleasingLoco); |
| 3987 |
loco.second->Release(); |
| 3988 |
} |
| 3989 |
return true; |
| 3990 |
} |
| 3991 |
|
| 3992 |
void Manager::LocoBaseStopAllImmediately(const ControlType controlType) |
| 3993 |
{ |
| 3994 |
{ |
| 3995 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 3996 |
for (auto& loco : locos) |
| 3997 |
{ |
| 3998 |
LocoBaseSpeed(controlType, loco.second, MinSpeed); |
| 3999 |
} |
| 4000 |
} |
| 4001 |
{ |
| 4002 |
std::lock_guard<std::mutex> guard(multipleUnitMutex); |
| 4003 |
for (auto& multipleUnit : multipleUnits) |
| 4004 |
{ |
| 4005 |
LocoBaseSpeed(controlType, multipleUnit.second, MinSpeed); |
| 4006 |
} |
| 4007 |
} |
| 4008 |
} |
| 4009 |
|
| 4010 |
bool Manager::LocoBaseAddTimeTable(const ObjectIdentifier& locoBaseIdentifier, |
| 4011 |
const RouteID routeID, |
| 4012 |
const bool automode) |
| 4013 |
{ |
| 4014 |
LocoBase* locoBase = GetLocoBase(locoBaseIdentifier); |
| 4015 |
if (!locoBase) |
| 4016 |
{ |
| 4017 |
return false; |
| 4018 |
} |
| 4019 |
Route* route = GetRoute(routeID); |
| 4020 |
if (!route) |
| 4021 |
{ |
| 4022 |
return false; |
| 4023 |
} |
| 4024 |
locoBase->AddTimeTable(route, automode ? RouteAuto : RouteStop); |
| 4025 |
return true; |
| 4026 |
} |
| 4027 |
|
| 4028 |
string Manager::GetCs2Lokomotive() const |
| 4029 |
{ |
| 4030 |
string out("[lokomotive]\nversion\n .major=0\n .minor=3\nsession\n .id="); |
| 4031 |
out += "1"; // FIXME: replace with mfx-neuanmeldezähler |
| 4032 |
{ |
| 4033 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 4034 |
for (auto& loco : locos) |
| 4035 |
{ |
| 4036 |
out += "\nlokomotive"; |
| 4037 |
out += "\n .name=" + loco.second->GetName(); |
| 4038 |
out += "\n .typ="; |
| 4039 |
const Address address = loco.second->GetAddress(); |
| 4040 |
uint32_t uid; |
| 4041 |
switch(loco.second->GetProtocol()) |
| 4042 |
{ |
| 4043 |
case ProtocolMM: |
| 4044 |
out += "mm2_prog"; |
| 4045 |
uid = address; |
| 4046 |
break; |
| 4047 |
|
| 4048 |
case ProtocolDCC: |
| 4049 |
out += "dcc"; |
| 4050 |
uid = address + 0xC000; |
| 4051 |
break; |
| 4052 |
|
| 4053 |
case ProtocolMFX: |
| 4054 |
out += "mfx"; |
| 4055 |
uid = address + 0x4000; |
| 4056 |
break; |
| 4057 |
|
| 4058 |
default: |
| 4059 |
out += "unknown"; |
| 4060 |
uid = 0; |
| 4061 |
break; |
| 4062 |
} |
| 4063 |
out += "\n .uid=0x" + Utils::Integer::IntegerToHex(uid); |
| 4064 |
out += "\n .adresse=0x" + Utils::Integer::IntegerToHex(address); |
| 4065 |
out += "\n .icon="; |
| 4066 |
out += "\n .symbol="; |
| 4067 |
out += "\n .av=0"; |
| 4068 |
out += "\n .bv=0"; |
| 4069 |
out += "\n .velocity=" + to_string(loco.second->GetSpeed()); |
| 4070 |
out += "\n .richtung=" + to_string(loco.second->GetOrientation()); |
| 4071 |
out += "\n .tachomax=120"; |
| 4072 |
out += "\n .vmax=255"; |
| 4073 |
out += "\n .vmin=1"; |
| 4074 |
for (LocoFunctionNr nr = 0; nr < 32; ++nr) |
| 4075 |
{ |
| 4076 |
out += "\n .funktionen"; |
| 4077 |
if (nr >= 16) |
| 4078 |
{ |
| 4079 |
out += "_2"; |
| 4080 |
} |
| 4081 |
out += "\n ..nr=" + to_string(nr); |
| 4082 |
out += "\n ..typ=" + to_string(Hardware::Protocols::MaerklinCANCommon::MapLocofunctionRailControlToCs2(nr, loco.second->GetFunctionIcon(nr))); |
| 4083 |
out += "\n ..dauer=" + to_string(loco.second->GetFunctionType(nr)); |
| 4084 |
out += "\n ..wert=" + to_string(loco.second->GetFunctionState(nr));; |
| 4085 |
} |
| 4086 |
} |
| 4087 |
} |
| 4088 |
return out + "\n"; |
| 4089 |
} |
| 4090 |
|
| 4091 |
string Manager::GetCs2Magnetartikel(const AccessoryBase* accessoryBase) |
| 4092 |
{ |
| 4093 |
string out = "\nartikel"; |
| 4094 |
out += "\n .id=" + to_string(accessoryBase->GetAddress()); |
| 4095 |
out += "\n .name="; |
| 4096 |
const Object* object = dynamic_cast<const Object*>(accessoryBase); |
| 4097 |
if (object) |
| 4098 |
{ |
| 4099 |
out += object->GetName(); |
| 4100 |
} |
| 4101 |
else |
| 4102 |
{ |
| 4103 |
out += to_string(accessoryBase->GetAddress()); |
| 4104 |
} |
| 4105 |
|
| 4106 |
out += "\n .typ=std_rot_gruen"; |
| 4107 |
out += "\n .schaltzeit=" + to_string(accessoryBase->GetAccessoryPulseDuration()); |
| 4108 |
out += "\n .dectyp="; |
| 4109 |
switch(accessoryBase->GetProtocol()) |
| 4110 |
{ |
| 4111 |
case ProtocolMM: |
| 4112 |
out += "mm2"; |
| 4113 |
break; |
| 4114 |
|
| 4115 |
case ProtocolDCC: |
| 4116 |
out += "dcc"; |
| 4117 |
break; |
| 4118 |
|
| 4119 |
default: |
| 4120 |
out += "unknown"; |
| 4121 |
break; |
| 4122 |
} |
| 4123 |
return out + "\n"; |
| 4124 |
} |
| 4125 |
|
| 4126 |
string Manager::GetCs2Magnetartikel() const |
| 4127 |
{ |
| 4128 |
string out("[magnetartikel]\nversion\n. major=0\n. minor=1"); |
| 4129 |
{ |
| 4130 |
std::lock_guard<std::mutex> guard(accessoryMutex); |
| 4131 |
for (auto& accessory : accessories) |
| 4132 |
{ |
| 4133 |
out += GetCs2Magnetartikel(accessory.second); |
| 4134 |
out += "\n .typ=std_rot_gruen"; |
| 4135 |
} |
| 4136 |
} |
| 4137 |
{ |
| 4138 |
std::lock_guard<std::mutex> guard(switchMutex); |
| 4139 |
for (auto& mySwitch : switches) |
| 4140 |
{ |
| 4141 |
out += GetCs2Magnetartikel(mySwitch.second); |
| 4142 |
out += "\n .typ="; |
| 4143 |
switch (mySwitch.second->GetAccessoryType()) |
| 4144 |
{ |
| 4145 |
case SwitchTypeLeft: |
| 4146 |
out += "linksweiche"; |
| 4147 |
break; |
| 4148 |
|
| 4149 |
case SwitchTypeRight: |
| 4150 |
out += "rechtsweiche"; |
| 4151 |
break; |
| 4152 |
|
| 4153 |
case SwitchTypeThreeWay: |
| 4154 |
out += "dreiwegweiche"; |
| 4155 |
break; |
| 4156 |
|
| 4157 |
case SwitchTypeMaerklinLeft: |
| 4158 |
case SwitchTypeMaerklinRight: |
| 4159 |
out += "dkw_1antrieb"; |
| 4160 |
break; |
| 4161 |
|
| 4162 |
default: |
| 4163 |
out += "y_weiche"; |
| 4164 |
break; |
| 4165 |
} |
| 4166 |
} |
| 4167 |
} |
| 4168 |
{ |
| 4169 |
std::lock_guard<std::mutex> guard(signalMutex); |
| 4170 |
for (auto& signal : signals) |
| 4171 |
{ |
| 4172 |
out += GetCs2Magnetartikel(signal.second); |
| 4173 |
out += "\n .typ="; |
| 4174 |
switch (signal.second->GetAccessoryType()) |
| 4175 |
{ |
| 4176 |
case SignalTypeChDwarf: |
| 4177 |
case SignalTypeDeBlock: |
| 4178 |
out += "lichtsignal_SH01"; |
| 4179 |
break; |
| 4180 |
|
| 4181 |
case SignalTypeChLMain: |
| 4182 |
case SignalTypeChLCombined: |
| 4183 |
case SignalTypeChNMain: |
| 4184 |
case SignalTypeDeCombined: |
| 4185 |
case SignalTypeDeHVMain: |
| 4186 |
out += "urc_lichtsignal_HP012_SH01"; |
| 4187 |
break; |
| 4188 |
|
| 4189 |
case SignalTypeChLDistant: |
| 4190 |
case SignalTypeChNDistant: |
| 4191 |
case SignalTypeDeHVDistant: |
| 4192 |
out += "lichtvorsignal_VR012"; |
| 4193 |
break; |
| 4194 |
|
| 4195 |
case SignalTypeSimpleLeft: |
| 4196 |
case SignalTypeSimpleRight: |
| 4197 |
default: |
| 4198 |
out += "lichtsignal_HP01"; |
| 4199 |
break; |
| 4200 |
} |
| 4201 |
} |
| 4202 |
} |
| 4203 |
return out + "\n"; |
| 4204 |
} |
| 4205 |
|
| 4206 |
string Manager::GetCs2GBS() const |
| 4207 |
{ |
| 4208 |
string out("[gleisbild]\nversion\n. major=1"); |
| 4209 |
{ |
| 4210 |
std::lock_guard<std::mutex> guard(layerMutex); |
| 4211 |
for (auto& layer : layers) |
| 4212 |
{ |
| 4213 |
out += "\nseite"; |
| 4214 |
if (layer.first != LayerUndeletable) |
| 4215 |
{ |
| 4216 |
out += "\n .id=" + to_string(layer.first); |
| 4217 |
} |
| 4218 |
out += "\n .name=" + layer.second->GetName(); |
| 4219 |
} |
| 4220 |
} |
| 4221 |
return out + "\n"; |
| 4222 |
} |
| 4223 |
|
| 4224 |
string Manager::GetCs2GBS(const signed char gbs) const |
| 4225 |
{ |
| 4226 |
string out("[gleisbildseite]\nversion\n. major=1"); |
| 4227 |
unsigned int z = gbs; |
| 4228 |
if (gbs == LayerUndeletable) |
| 4229 |
{ |
| 4230 |
// default layer Z in CS2 is 0, in RailControl 1. So we map this. |
| 4231 |
--z; |
| 4232 |
} |
| 4233 |
else |
| 4234 |
{ |
| 4235 |
out += "\npage=" + to_string(gbs); |
| 4236 |
} |
| 4237 |
z <<= 16; |
| 4238 |
|
| 4239 |
{ |
| 4240 |
std::lock_guard<std::mutex> guard(trackMutex); |
| 4241 |
for (auto& track : tracks) |
| 4242 |
{ |
| 4243 |
if (gbs == track.second->GetPosZ()) |
| 4244 |
{ |
| 4245 |
out += "\nelement"; |
| 4246 |
const unsigned int id = z + (track.second->GetPosX() << 8) + track.second->GetPosY(); |
| 4247 |
out += "\n .id=0x" + Utils::Integer::IntegerToHex(id); |
| 4248 |
out += "\n .typ="; |
| 4249 |
switch(track.second->GetTrackType()) |
| 4250 |
{ |
| 4251 |
case TrackTypeTurn: |
| 4252 |
out += "bogen"; |
| 4253 |
break; |
| 4254 |
|
| 4255 |
case TrackTypeEnd: |
| 4256 |
case TrackTypeLink: |
| 4257 |
out += "prellbock"; |
| 4258 |
break; |
| 4259 |
|
| 4260 |
case TrackTypeTunnelEnd: |
| 4261 |
out += "prellbock"; |
| 4262 |
break; |
| 4263 |
|
| 4264 |
case TrackTypeStraight: |
| 4265 |
case TrackTypeBridge: |
| 4266 |
case TrackTypeTunnel: |
| 4267 |
case TrackTypeCrossingLeft: |
| 4268 |
case TrackTypeCrossingRight: |
| 4269 |
case TrackTypeCrossingSymetric: |
| 4270 |
default: |
| 4271 |
out += "gerade"; |
| 4272 |
break; |
| 4273 |
} |
| 4274 |
out += "\n .drehung=" + to_string(track.second->GetRotation() & 0x01); |
| 4275 |
out += "\n .artikel=-1"; |
| 4276 |
} |
| 4277 |
} |
| 4278 |
} |
| 4279 |
{ |
| 4280 |
std::lock_guard<std::mutex> guard(accessoryMutex); |
| 4281 |
for (auto& accessory : accessories) |
| 4282 |
{ |
| 4283 |
if (gbs == accessory.second->GetPosZ()) |
| 4284 |
{ |
| 4285 |
out += "\nelement"; |
| 4286 |
const unsigned int id = z + (accessory.second->GetPosX() << 8) + accessory.second->GetPosY(); |
| 4287 |
out += "\n .id=0x" + Utils::Integer::IntegerToHex(id); |
| 4288 |
out += "\n .typ=k84_einfach"; |
| 4289 |
out += "\n .artikel=" + to_string(accessory.second->GetAddress()); |
| 4290 |
} |
| 4291 |
} |
| 4292 |
} |
| 4293 |
{ |
| 4294 |
std::lock_guard<std::mutex> guard(switchMutex); |
| 4295 |
for (auto& mySwitch : switches) |
| 4296 |
{ |
| 4297 |
if (gbs == mySwitch.second->GetPosZ()) |
| 4298 |
{ |
| 4299 |
out += "\nelement"; |
| 4300 |
const unsigned int id = z + (mySwitch.second->GetPosX() << 8) + mySwitch.second->GetPosY(); |
| 4301 |
out += "\n .id=0x" + Utils::Integer::IntegerToHex(id); |
| 4302 |
out += "\n .typ="; |
| 4303 |
switch(mySwitch.second->GetAccessoryType()) |
| 4304 |
{ |
| 4305 |
case SwitchTypeLeft: |
| 4306 |
out += "linksweiche"; |
| 4307 |
break; |
| 4308 |
|
| 4309 |
case SwitchTypeRight: |
| 4310 |
out += "rechtsweiche"; |
| 4311 |
break; |
| 4312 |
|
| 4313 |
case SwitchTypeThreeWay: |
| 4314 |
out += "dreiwegweiche"; |
| 4315 |
break; |
| 4316 |
|
| 4317 |
case SwitchTypeMaerklinLeft: |
| 4318 |
out += "dkw3_li"; |
| 4319 |
break; |
| 4320 |
|
| 4321 |
case SwitchTypeMaerklinRight: |
| 4322 |
out += "dkw3_re"; |
| 4323 |
break; |
| 4324 |
|
| 4325 |
default: |
| 4326 |
break; |
| 4327 |
} |
| 4328 |
out += "\n .drehung=" + to_string(mySwitch.second->GetRotation()); |
| 4329 |
out += "\n .artikel=" + to_string(mySwitch.second->GetAddress()); |
| 4330 |
out += "\n .zustand=" + to_string(mySwitch.second->GetAccessoryState()); |
| 4331 |
} |
| 4332 |
} |
| 4333 |
} |
| 4334 |
return out + "\n"; |
| 4335 |
} |
| 4336 |
|
| 4337 |
/*************************** |
| 4338 |
* Layout * |
| 4339 |
***************************/ |
| 4340 |
|
| 4341 |
bool Manager::CheckPositionFree(const LayoutPosition posX, const LayoutPosition posY, const LayoutPosition posZ, string& result) const |
| 4342 |
{ |
| 4343 |
return CheckLayoutPositionFree(posX, posY, posZ, result, accessories, accessoryMutex) |
| 4344 |
&& CheckLayoutPositionFree(posX, posY, posZ, result, tracks, trackMutex) |
| 4345 |
&& CheckLayoutPositionFree(posX, posY, posZ, result, feedbacks, feedbackMutex) |
| 4346 |
&& CheckLayoutPositionFree(posX, posY, posZ, result, switches, switchMutex) |
| 4347 |
&& CheckLayoutPositionFree(posX, posY, posZ, result, routes, routeMutex) |
| 4348 |
&& CheckLayoutPositionFree(posX, posY, posZ, result, signals, signalMutex) |
| 4349 |
&& CheckLayoutPositionFree(posX, posY, posZ, result, texts, textMutex); |
| 4350 |
} |
| 4351 |
|
| 4352 |
bool Manager::CheckPositionFree(const LayoutPosition posX, |
| 4353 |
const LayoutPosition posY, |
| 4354 |
const LayoutPosition posZ, |
| 4355 |
const LayoutItemSize width, |
| 4356 |
const LayoutItemSize height, |
| 4357 |
const LayoutRotation rotation, |
| 4358 |
string& result) const |
| 4359 |
{ |
| 4360 |
if (width == 0) |
| 4361 |
{ |
| 4362 |
result.assign(Languages::GetText(Languages::TextWidthIs0)); |
| 4363 |
return false; |
| 4364 |
} |
| 4365 |
if (height == 0) |
| 4366 |
{ |
| 4367 |
result.assign(Languages::GetText(Languages::TextHeightIs0)); |
| 4368 |
return false; |
| 4369 |
} |
| 4370 |
LayoutPosition x; |
| 4371 |
LayoutPosition y; |
| 4372 |
LayoutPosition z = posZ; |
| 4373 |
LayoutItemSize w; |
| 4374 |
LayoutItemSize h; |
| 4375 |
bool ret = DataModel::LayoutItem::MapPosition(posX, posY, width, height, rotation, x, y, w, h); |
| 4376 |
if (!ret) |
| 4377 |
{ |
| 4378 |
return false; |
| 4379 |
} |
| 4380 |
for (LayoutPosition ix = x; ix < x + w; ix++) |
| 4381 |
{ |
| 4382 |
for (LayoutPosition iy = y; iy < y + h; iy++) |
| 4383 |
{ |
| 4384 |
bool ret = CheckPositionFree(ix, iy, z, result); |
| 4385 |
if (!ret) |
| 4386 |
{ |
| 4387 |
return false; |
| 4388 |
} |
| 4389 |
} |
| 4390 |
} |
| 4391 |
return true; |
| 4392 |
} |
| 4393 |
|
| 4394 |
bool Manager::CheckLayoutItemPosition(const LayoutItem* item, |
| 4395 |
const LayoutPosition posX, |
| 4396 |
const LayoutPosition posY, |
| 4397 |
const LayoutPosition posZ, |
| 4398 |
string& result) const |
| 4399 |
{ |
| 4400 |
if (item) |
| 4401 |
{ |
| 4402 |
if (item->IsVisibleOnLayer(posZ) == DataModel::LayoutItem::VisibleNo || |
| 4403 |
item->HasPosition(posX, posY, posZ)) |
| 4404 |
{ |
| 4405 |
return true; |
| 4406 |
} |
| 4407 |
} |
| 4408 |
return CheckPositionFree(posX, posY, posZ, DataModel::LayoutItem::Width1, DataModel::LayoutItem::Height1, DataModel::LayoutItem::Rotation0, result); |
| 4409 |
} |
| 4410 |
|
| 4411 |
bool Manager::CheckLayoutItemPosition(const LayoutItem* item, |
| 4412 |
const LayoutPosition posX, |
| 4413 |
const LayoutPosition posY, |
| 4414 |
const LayoutPosition posZ, |
| 4415 |
const LayoutItemSize width, |
| 4416 |
const LayoutItemSize height, |
| 4417 |
const LayoutRotation rotation, |
| 4418 |
string& result) const |
| 4419 |
{ |
| 4420 |
LayoutPosition x1; |
| 4421 |
LayoutPosition y1; |
| 4422 |
LayoutPosition z1 = posZ; |
| 4423 |
LayoutItemSize w1; |
| 4424 |
LayoutItemSize h1; |
| 4425 |
bool ret = DataModel::LayoutItem::MapPosition(posX, posY, width, height, rotation, x1, y1, w1, h1); |
| 4426 |
if (!ret) |
| 4427 |
{ |
| 4428 |
result = Languages::GetText(Languages::TextUnableToCalculatePosition); |
| 4429 |
return false; |
| 4430 |
} |
| 4431 |
|
| 4432 |
LayoutPosition x2 = 0; |
| 4433 |
LayoutPosition y2 = 0; |
| 4434 |
LayoutPosition z2 = 0; |
| 4435 |
LayoutItemSize w2 = 0; |
| 4436 |
LayoutItemSize h2 = 0; |
| 4437 |
|
| 4438 |
if (item) |
| 4439 |
{ |
| 4440 |
z2 = item->GetPosZ(); |
| 4441 |
ret = DataModel::LayoutItem::MapPosition(item->GetPosX(), item->GetPosY(), width, item->GetHeight(), item->GetRotation(), x2, y2, w2, h2); |
| 4442 |
if (!ret) |
| 4443 |
{ |
| 4444 |
result = Languages::GetText(Languages::TextUnableToCalculatePosition); |
| 4445 |
return false; |
| 4446 |
} |
| 4447 |
} |
| 4448 |
|
| 4449 |
for (LayoutPosition ix = x1; ix < x1 + w1; ++ix) |
| 4450 |
{ |
| 4451 |
for (LayoutPosition iy = y1; iy < y1 + h1; ++iy) |
| 4452 |
{ |
| 4453 |
ret = (ix >= x2 && ix < x2 + w2 && iy >= y2 && iy < y2 + h2 && z1 == z2); |
| 4454 |
if (ret) |
| 4455 |
{ |
| 4456 |
continue; |
| 4457 |
} |
| 4458 |
|
| 4459 |
ret = CheckPositionFree(ix, iy, z1, result); |
| 4460 |
if (!ret) |
| 4461 |
{ |
| 4462 |
return false; |
| 4463 |
} |
| 4464 |
} |
| 4465 |
} |
| 4466 |
return true; |
| 4467 |
} |
| 4468 |
|
| 4469 |
template<class Type> |
| 4470 |
bool Manager::CheckLayoutPositionFree(const LayoutPosition posX, const LayoutPosition posY, const LayoutPosition posZ, string& result, const map<ObjectID, Type*>& layoutVector, std::mutex& mutex) const |
| 4471 |
{ |
| 4472 |
std::lock_guard<std::mutex> guard(mutex); |
| 4473 |
for (auto& layout : layoutVector) |
| 4474 |
{ |
| 4475 |
if (layout.second->CheckPositionFree(posX, posY, posZ)) |
| 4476 |
{ |
| 4477 |
continue; |
| 4478 |
} |
| 4479 |
result.assign(Logger::Logger::Format(Languages::GetText(Languages::TextPositionAlreadyInUse), static_cast<int>(posX), static_cast<int>(posY), static_cast<int>(posZ), layout.second->GetLayoutType(), layout.second->GetName())); |
| 4480 |
return false; |
| 4481 |
} |
| 4482 |
return true; |
| 4483 |
} |
| 4484 |
|
| 4485 |
bool Manager::CheckAddressLoco(const Protocol protocol, const Address address, string& result) |
| 4486 |
{ |
| 4487 |
switch (protocol) |
| 4488 |
{ |
| 4489 |
case ProtocolDCC: |
| 4490 |
case ProtocolDCC14: |
| 4491 |
case ProtocolDCC28: |
| 4492 |
case ProtocolDCC128: |
| 4493 |
if (address > 10239) |
| 4494 |
{ |
| 4495 |
result.assign(Languages::GetText(Languages::TextLocoAddressDccTooHigh)); |
| 4496 |
return false; |
| 4497 |
} |
| 4498 |
return true; |
| 4499 |
|
| 4500 |
case ProtocolMM1: |
| 4501 |
case ProtocolMM15: |
| 4502 |
if (address > 80) |
| 4503 |
{ |
| 4504 |
result.assign(Languages::GetText(Languages::TextLocoAddressMm1TooHigh)); |
| 4505 |
return false; |
| 4506 |
} |
| 4507 |
return true; |
| 4508 |
|
| 4509 |
case ProtocolMM: |
| 4510 |
case ProtocolMM2: |
| 4511 |
if (address > 255) |
| 4512 |
{ |
| 4513 |
result.assign(Languages::GetText(Languages::TextLocoAddressMm2TooHigh)); |
| 4514 |
return false; |
| 4515 |
} |
| 4516 |
return true; |
| 4517 |
|
| 4518 |
default: |
| 4519 |
return true; |
| 4520 |
} |
| 4521 |
} |
| 4522 |
|
| 4523 |
bool Manager::CheckAddressAccessory(const Protocol protocol, const Address address, string& result) |
| 4524 |
{ |
| 4525 |
switch (protocol) |
| 4526 |
{ |
| 4527 |
case ProtocolDCC: |
| 4528 |
if (address > 2044) |
| 4529 |
{ |
| 4530 |
result.assign(Languages::GetText(Languages::TextAccessoryAddressDccTooHigh)); |
| 4531 |
return false; |
| 4532 |
} |
| 4533 |
return true; |
| 4534 |
|
| 4535 |
case ProtocolMM1: |
| 4536 |
case ProtocolMM2: |
| 4537 |
if (address > 320) { |
| 4538 |
result.assign(Languages::GetText(Languages::TextAccessoryAddressMmTooHigh)); |
| 4539 |
return false; |
| 4540 |
} |
| 4541 |
return true; |
| 4542 |
|
| 4543 |
default: |
| 4544 |
return true; |
| 4545 |
} |
| 4546 |
} |
| 4547 |
|
| 4548 |
bool Manager::CheckControlProtocolAddress(const AddressType type, const ControlID controlID, const Protocol protocol, const Address address, string& result) |
| 4549 |
{ |
| 4550 |
if (address == AddressNone) |
| 4551 |
{ |
| 4552 |
result.assign(Logger::Logger::Format(Languages::GetText(Languages::TextAddressMustBeHigherThen0))); |
| 4553 |
return false; |
| 4554 |
} |
| 4555 |
|
| 4556 |
if ((type == AddressTypeMultipleUnit) && (controlID == ControlNone)) |
| 4557 |
{ |
| 4558 |
// multiple units do not need physical control |
| 4559 |
return true; |
| 4560 |
} |
| 4561 |
|
| 4562 |
{ |
| 4563 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 4564 |
if (controlID < ControlIdFirstHardware || controls.count(controlID) != 1) |
| 4565 |
{ |
| 4566 |
result.assign(Languages::GetText(Languages::TextControlDoesNotExist)); |
| 4567 |
return false; |
| 4568 |
} |
| 4569 |
ControlInterface* control = controls.at(controlID); |
| 4570 |
if (!control) |
| 4571 |
{ |
| 4572 |
result.assign(Languages::GetText(Languages::TextControlDoesNotExist)); |
| 4573 |
return false; |
| 4574 |
} |
| 4575 |
bool protocolSupported; |
| 4576 |
switch (type) |
| 4577 |
{ |
| 4578 |
case AddressTypeLoco: |
| 4579 |
protocolSupported = control->LocoProtocolSupported(protocol); |
| 4580 |
break; |
| 4581 |
|
| 4582 |
case AddressTypeMultipleUnit: |
| 4583 |
protocolSupported = true; |
| 4584 |
break; |
| 4585 |
|
| 4586 |
case AddressTypeAccessory: |
| 4587 |
protocolSupported = control->AccessoryProtocolSupported(protocol); |
| 4588 |
break; |
| 4589 |
|
| 4590 |
default: |
| 4591 |
protocolSupported = false; |
| 4592 |
break; |
| 4593 |
} |
| 4594 |
|
| 4595 |
if (!protocolSupported) |
| 4596 |
{ |
| 4597 |
string protocolText; |
| 4598 |
if (protocol <= ProtocolEnd) |
| 4599 |
{ |
| 4600 |
protocolText = ProtocolSymbols[protocol] + " "; |
| 4601 |
} |
| 4602 |
std::vector<Protocol> protocols; |
| 4603 |
switch (type) |
| 4604 |
{ |
| 4605 |
case AddressTypeLoco: |
| 4606 |
control->LocoProtocols(protocols); |
| 4607 |
break; |
| 4608 |
|
| 4609 |
case AddressTypeAccessory: |
| 4610 |
control->AccessoryProtocols(protocols); |
| 4611 |
break; |
| 4612 |
|
| 4613 |
case AddressTypeMultipleUnit: |
| 4614 |
default: |
| 4615 |
break; |
| 4616 |
} |
| 4617 |
|
| 4618 |
string protocolsText; |
| 4619 |
for (auto p : protocols) |
| 4620 |
{ |
| 4621 |
if (protocolsText.size() > 0) |
| 4622 |
{ |
| 4623 |
protocolsText.append(", "); |
| 4624 |
} |
| 4625 |
protocolsText.append(ProtocolSymbols[p]); |
| 4626 |
} |
| 4627 |
result.assign(Logger::Logger::Format(Languages::GetText(Languages::TextProtocolNotSupported), protocolText, protocolsText)); |
| 4628 |
return false; |
| 4629 |
} |
| 4630 |
} // unlock controlMutex |
| 4631 |
|
| 4632 |
switch (type) |
| 4633 |
{ |
| 4634 |
case AddressTypeLoco: |
| 4635 |
return CheckAddressLoco(protocol, address, result); |
| 4636 |
|
| 4637 |
case AddressTypeMultipleUnit: |
| 4638 |
return true; |
| 4639 |
|
| 4640 |
case AddressTypeAccessory: |
| 4641 |
return CheckAddressAccessory(protocol, address, result); |
| 4642 |
|
| 4643 |
default: |
| 4644 |
return false; |
| 4645 |
} |
| 4646 |
} |
| 4647 |
|
| 4648 |
bool Manager::SettingsSave(const Languages::Language language, |
| 4649 |
const StartupInitLocos startupInitLocos, |
| 4650 |
const DataModel::AccessoryPulseDuration duration, |
| 4651 |
const bool autoAddFeedback, |
| 4652 |
const bool stopOnFeedbackInFreeTrack, |
| 4653 |
const bool executeAccessoryAlways, |
| 4654 |
const DataModel::SelectRouteApproach selectRouteApproach, |
| 4655 |
const DataModel::Loco::NrOfTracksToReserve nrOfTracksToReserve, |
| 4656 |
const Logger::Logger::Level logLevel |
| 4657 |
) |
| 4658 |
{ |
| 4659 |
Languages::SetDefaultLanguage(language); |
| 4660 |
this->startupInitLocos = startupInitLocos; |
| 4661 |
this->defaultAccessoryDuration = duration; |
| 4662 |
this->autoAddFeedback = autoAddFeedback; |
| 4663 |
this->stopOnFeedbackInFreeTrack = stopOnFeedbackInFreeTrack; |
| 4664 |
this->executeAccessory = executeAccessoryAlways; |
| 4665 |
this->selectRouteApproach = selectRouteApproach; |
| 4666 |
this->nrOfTracksToReserve = nrOfTracksToReserve; |
| 4667 |
Logger::Logger::SetLogLevel(logLevel); |
| 4668 |
|
| 4669 |
if (!storage) |
| 4670 |
{ |
| 4671 |
return false; |
| 4672 |
} |
| 4673 |
Storage::TransactionGuard guard(storage); |
| 4674 |
storage->SaveSetting("Language", std::to_string(static_cast<int>(language))); |
| 4675 |
storage->SaveSetting("StartupInitLocos", std::to_string(static_cast<int>(startupInitLocos))); |
| 4676 |
storage->SaveSetting("DefaultAccessoryDuration", std::to_string(duration)); |
| 4677 |
storage->SaveSetting("AutoAddFeedback", std::to_string(autoAddFeedback)); |
| 4678 |
storage->SaveSetting("StopOnFeedbackInFreeTrack", std::to_string(stopOnFeedbackInFreeTrack)); |
| 4679 |
storage->SaveSetting("ExecuteAccessory", std::to_string(executeAccessory)); |
| 4680 |
storage->SaveSetting("SelectRouteApproach", std::to_string(static_cast<int>(selectRouteApproach))); |
| 4681 |
storage->SaveSetting("NrOfTracksToReserve", std::to_string(static_cast<int>(nrOfTracksToReserve))); |
| 4682 |
storage->SaveSetting("LogLevel", std::to_string(static_cast<int>(logLevel))); |
| 4683 |
return true; |
| 4684 |
} |
| 4685 |
|
| 4686 |
void Manager::DebounceWorker() |
| 4687 |
{ |
| 4688 |
Utils::Utils::SetThreadName(Languages::GetText(Languages::TextDebouncer)); |
| 4689 |
logger->Info(Languages::TextDebounceThreadStarted); |
| 4690 |
while (debounceRun) |
| 4691 |
{ |
| 4692 |
{ |
| 4693 |
std::lock_guard<std::mutex> guard(feedbackMutex); |
| 4694 |
for (auto& feedback : feedbacks) |
| 4695 |
{ |
| 4696 |
feedback.second->Debounce(); |
| 4697 |
} |
| 4698 |
} |
| 4699 |
Utils::Utils::SleepForMilliseconds(250); |
| 4700 |
} |
| 4701 |
logger->Info(Languages::TextDebounceThreadTerminated); |
| 4702 |
} |
| 4703 |
|
| 4704 |
template<class ID, class T> |
| 4705 |
T* Manager::CreateAndAddObject(std::map<ID,T*>& objects, std::mutex& mutex) |
| 4706 |
{ |
| 4707 |
std::lock_guard<std::mutex> Guard(mutex); |
| 4708 |
ID newObjectID = 0; |
| 4709 |
for (auto& object : objects) |
| 4710 |
{ |
| 4711 |
if (object.first > newObjectID) |
| 4712 |
{ |
| 4713 |
newObjectID = object.first; |
| 4714 |
} |
| 4715 |
} |
| 4716 |
++newObjectID; |
| 4717 |
T* newObject = new T(this, newObjectID); |
| 4718 |
if (!newObject) |
| 4719 |
{ |
| 4720 |
return nullptr; |
| 4721 |
} |
| 4722 |
objects[newObjectID] = newObject; |
| 4723 |
return newObject; |
| 4724 |
} |
| 4725 |
|
| 4726 |
ControlID Manager::GetPossibleControlForLoco() const |
| 4727 |
{ |
| 4728 |
for (auto& control : controls) |
| 4729 |
{ |
| 4730 |
if (control.second->CanHandle(Hardware::CapabilityLoco)) |
| 4731 |
{ |
| 4732 |
return control.first; |
| 4733 |
} |
| 4734 |
} |
| 4735 |
return ControlIdNone; |
| 4736 |
} |
| 4737 |
|
| 4738 |
ControlID Manager::GetPossibleControlForAccessory() const |
| 4739 |
{ |
| 4740 |
for (auto& control : controls) |
| 4741 |
{ |
| 4742 |
if (control.second->CanHandle(Hardware::CapabilityAccessory)) |
| 4743 |
{ |
| 4744 |
return control.first; |
| 4745 |
} |
| 4746 |
} |
| 4747 |
return ControlIdNone; |
| 4748 |
} |
| 4749 |
|
| 4750 |
ControlID Manager::GetPossibleControlForFeedback() const |
| 4751 |
{ |
| 4752 |
for (auto& control : controls) |
| 4753 |
{ |
| 4754 |
if (control.second->CanHandle(Hardware::CapabilityFeedback)) |
| 4755 |
{ |
| 4756 |
return control.first; |
| 4757 |
} |
| 4758 |
} |
| 4759 |
return ControlIdNone; |
| 4760 |
} |
| 4761 |
|
| 4762 |
void Manager::ProgramCheckBooster(const ProgramMode mode) |
| 4763 |
{ |
| 4764 |
switch (mode) |
| 4765 |
{ |
| 4766 |
case ProgramModeDccPomLoco: |
| 4767 |
case ProgramModeDccPomAccessory: |
| 4768 |
if (boosterState == BoosterStateGo) |
| 4769 |
{ |
| 4770 |
return; |
| 4771 |
} |
| 4772 |
Booster(ControlTypeInternal, BoosterStateGo); |
| 4773 |
Utils::Utils::SleepForMilliseconds(100); |
| 4774 |
return; |
| 4775 |
|
| 4776 |
default: |
| 4777 |
return; |
| 4778 |
} |
| 4779 |
} |
| 4780 |
|
| 4781 |
void Manager::ProgramRead(const ControlID controlID, const ProgramMode mode, const Address address, const CvNumber cv) |
| 4782 |
{ |
| 4783 |
ControlInterface* control = GetControl(controlID); |
| 4784 |
if (!control) |
| 4785 |
{ |
| 4786 |
return; |
| 4787 |
} |
| 4788 |
ProgramCheckBooster(mode); |
| 4789 |
control->ProgramRead(mode, address, cv); |
| 4790 |
} |
| 4791 |
|
| 4792 |
void Manager::ProgramWrite(const ControlID controlID, const ProgramMode mode, const Address address, const CvNumber cv, const CvValue value) |
| 4793 |
{ |
| 4794 |
ControlInterface* control = GetControl(controlID); |
| 4795 |
if (!control) |
| 4796 |
{ |
| 4797 |
return; |
| 4798 |
} |
| 4799 |
ProgramCheckBooster(mode); |
| 4800 |
control->ProgramWrite(mode, address, cv, value); |
| 4801 |
} |
| 4802 |
|
| 4803 |
void Manager::ProgramValue(const CvNumber cv, const CvValue value) |
| 4804 |
{ |
| 4805 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 4806 |
for (auto& control : controls) |
| 4807 |
{ |
| 4808 |
control.second->ProgramValue(cv, value);; |
| 4809 |
} |
| 4810 |
} |
| 4811 |
|
| 4812 |
bool Manager::CanHandle(const Hardware::Capabilities capability) const |
| 4813 |
{ |
| 4814 |
std::lock_guard<std::mutex> guard(controlMutex); |
| 4815 |
for (auto& control : controls) |
| 4816 |
{ |
| 4817 |
bool ret = control.second->CanHandle(capability); |
| 4818 |
if (ret) |
| 4819 |
{ |
| 4820 |
return true; |
| 4821 |
} |
| 4822 |
} |
| 4823 |
return false; |
| 4824 |
} |
| 4825 |
|
| 4826 |
bool Manager::CanHandle(const ControlID controlId, const Hardware::Capabilities capability) const |
| 4827 |
{ |
| 4828 |
ControlInterface* control = GetControl(controlId); |
| 4829 |
if (!control) |
| 4830 |
{ |
| 4831 |
return false; |
| 4832 |
} |
| 4833 |
return control->CanHandle(capability); |
| 4834 |
} |
| 4835 |
|
| 4836 |
Hardware::Capabilities Manager::GetCapabilities(const ControlID controlID) const |
| 4837 |
{ |
| 4838 |
ControlInterface* control = GetControl(controlID); |
| 4839 |
if (!control) |
| 4840 |
{ |
| 4841 |
return Hardware::CapabilityNone; |
| 4842 |
} |
| 4843 |
return control->GetCapabilities(); |
| 4844 |
} |
| 4845 |
|
| 4846 |
bool Manager::LayoutItemRotate(const DataModel::ObjectIdentifier& identifier, |
| 4847 |
string& result) |
| 4848 |
{ |
| 4849 |
ObjectType type = identifier.GetObjectType(); |
| 4850 |
ObjectID id = identifier.GetObjectID(); |
| 4851 |
switch (type) |
| 4852 |
{ |
| 4853 |
case ObjectTypeAccessory: |
| 4854 |
return AccessoryRotate(id, result); |
| 4855 |
|
| 4856 |
case ObjectTypeFeedback: |
| 4857 |
return FeedbackRotate(id, result); |
| 4858 |
|
| 4859 |
case ObjectTypeSignal: |
| 4860 |
return SignalRotate(id, result); |
| 4861 |
|
| 4862 |
case ObjectTypeSwitch: |
| 4863 |
return SwitchRotate(id, result); |
| 4864 |
|
| 4865 |
case ObjectTypeText: |
| 4866 |
return TextRotate(id, result); |
| 4867 |
|
| 4868 |
case ObjectTypeTrack: |
| 4869 |
return TrackRotate(id, result); |
| 4870 |
|
| 4871 |
case ObjectTypeRoute: |
| 4872 |
default: |
| 4873 |
return false; |
| 4874 |
} |
| 4875 |
} |
| 4876 |
|
| 4877 |
bool Manager::LayoutItemNewPosition(const DataModel::ObjectIdentifier& identifier, |
| 4878 |
const DataModel::LayoutItem::LayoutItemSize posX, |
| 4879 |
const DataModel::LayoutItem::LayoutItemSize posY, |
| 4880 |
string& result) |
| 4881 |
{ |
| 4882 |
ObjectType type = identifier.GetObjectType(); |
| 4883 |
ObjectID id = identifier.GetObjectID(); |
| 4884 |
switch (type) |
| 4885 |
{ |
| 4886 |
case ObjectTypeAccessory: |
| 4887 |
return AccessoryPosition(id, posX, posY, result); |
| 4888 |
|
| 4889 |
case ObjectTypeFeedback: |
| 4890 |
return FeedbackPosition(id, posX, posY, result); |
| 4891 |
|
| 4892 |
case ObjectTypeRoute: |
| 4893 |
return RoutePosition(id, posX, posY, result); |
| 4894 |
|
| 4895 |
case ObjectTypeSignal: |
| 4896 |
return SignalPosition(id, posX, posY, result); |
| 4897 |
|
| 4898 |
case ObjectTypeSwitch: |
| 4899 |
return SwitchPosition(id, posX, posY, result); |
| 4900 |
|
| 4901 |
case ObjectTypeText: |
| 4902 |
return TextPosition(id, posX, posY, result); |
| 4903 |
|
| 4904 |
case ObjectTypeTrack: |
| 4905 |
return TrackPosition(id, posX, posY, result); |
| 4906 |
|
| 4907 |
default: |
| 4908 |
return false; |
| 4909 |
} |
| 4910 |
} |
| 4911 |
|
| 4912 |
ObjectIdentifier Manager::GetIdentifierOfServerLocoAddress(const Address serverAddress) const |
| 4913 |
{ |
| 4914 |
{ |
| 4915 |
std::lock_guard<std::mutex> guard(locoMutex); |
| 4916 |
for (auto& loco : locos) |
| 4917 |
{ |
| 4918 |
if (loco.second->GetServerAddress() == serverAddress) |
| 4919 |
{ |
| 4920 |
return ObjectIdentifier(ObjectTypeLoco, loco.second->GetID()); |
| 4921 |
} |
| 4922 |
} |
| 4923 |
} |
| 4924 |
|
| 4925 |
{ |
| 4926 |
std::lock_guard<std::mutex> guard(multipleUnitMutex); |
| 4927 |
for (auto& multipleUnit : multipleUnits) |
| 4928 |
{ |
| 4929 |
if (multipleUnit.second->GetServerAddress() == serverAddress) |
| 4930 |
{ |
| 4931 |
return ObjectIdentifier(ObjectTypeMultipleUnit, multipleUnit.second->GetID()); |
| 4932 |
} |
| 4933 |
} |
| 4934 |
} |
| 4935 |
|
| 4936 |
return ObjectIdentifier(ObjectTypeNone, ObjectNone); |
| 4937 |
} |
| 4938 |
|
| 4939 |
ObjectIdentifier Manager::GetIdentifierOfServerAccessoryAddress(const Address serverAddress) const |
| 4940 |
{ |
| 4941 |
{ |
| 4942 |
std::lock_guard<std::mutex> guard(accessoryMutex); |
| 4943 |
for (auto& accessory : accessories) |
| 4944 |
{ |
| 4945 |
if (accessory.second->GetServerAddress() == serverAddress) |
| 4946 |
{ |
| 4947 |
return ObjectIdentifier(ObjectTypeAccessory, accessory.second->GetID()); |
| 4948 |
} |
| 4949 |
} |
| 4950 |
} |
| 4951 |
|
| 4952 |
{ |
| 4953 |
std::lock_guard<std::mutex> guard(switchMutex); |
| 4954 |
for (auto& mySwitch : switches) |
| 4955 |
{ |
| 4956 |
if (mySwitch.second->GetServerAddress() == serverAddress) |
| 4957 |
{ |
| 4958 |
return ObjectIdentifier(ObjectTypeSwitch, mySwitch.second->GetID()); |
| 4959 |
} |
| 4960 |
} |
| 4961 |
} |
| 4962 |
|
| 4963 |
{ |
| 4964 |
std::lock_guard<std::mutex> guard(signalMutex); |
| 4965 |
for (auto& signal : signals) |
| 4966 |
{ |
| 4967 |
if (signal.second->GetServerAddress() == serverAddress) |
| 4968 |
{ |
| 4969 |
return ObjectIdentifier(ObjectTypeSignal, signal.second->GetID()); |
| 4970 |
} |
| 4971 |
} |
| 4972 |
} |
| 4973 |
|
| 4974 |
return ObjectIdentifier(ObjectTypeNone, ObjectNone); |
| 4975 |
} |
| 4976 |
|
| 4977 |
Hardware::HardwareParams* Manager::CreateAndAddControl() |
| 4978 |
{ |
| 4979 |
std::lock_guard<std::mutex> Guard(hardwareMutex); |
| 4980 |
ControlID newObjectID = ControlIdFirstHardware - 1; |
| 4981 |
for (auto& hardwareParam : hardwareParams) |
| 4982 |
{ |
| 4983 |
if (hardwareParam.first > newObjectID) |
| 4984 |
{ |
| 4985 |
newObjectID = hardwareParam.first; |
| 4986 |
} |
| 4987 |
} |
| 4988 |
++newObjectID; |
| 4989 |
Hardware::HardwareParams* newParams = new Hardware::HardwareParams(this, newObjectID); |
| 4990 |
if (!newParams) |
| 4991 |
{ |
| 4992 |
return nullptr; |
| 4993 |
} |
| 4994 |
hardwareParams[newObjectID] = newParams; |
| 4995 |
return newParams; |
| 4996 |
} |
| 4997 |
|
| 4998 |
bool Manager::ObjectIsPartOfRoute(const ObjectIdentifier& identifier, |
| 4999 |
const Object* object, |
| 5000 |
string& result) |
| 5001 |
{ |
| 5002 |
std::lock_guard<std::mutex> Guard(routeMutex); |
| 5003 |
for (auto& route : routes) |
| 5004 |
{ |
| 5005 |
if (!route.second->ObjectIsPartOfRoute(identifier)) |
| 5006 |
{ |
| 5007 |
continue; |
| 5008 |
} |
| 5009 |
|
| 5010 |
Languages::TextSelector selector; |
| 5011 |
switch (identifier.GetObjectType()) |
| 5012 |
{ |
| 5013 |
case ObjectTypeTrack: |
| 5014 |
selector = Languages::TextTrackIsUsedByRoute; |
| 5015 |
break; |
| 5016 |
|
| 5017 |
case ObjectTypeAccessory: |
| 5018 |
selector = Languages::TextAccessoryIsUsedByRoute; |
| 5019 |
break; |
| 5020 |
|
| 5021 |
case ObjectTypeSwitch: |
| 5022 |
selector = Languages::TextSwitchIsUsedByRoute; |
| 5023 |
break; |
| 5024 |
|
| 5025 |
case ObjectTypeRoute: |
| 5026 |
selector = Languages::TextRouteIsUsedByRoute; |
| 5027 |
break; |
| 5028 |
|
| 5029 |
case ObjectTypeSignal: |
| 5030 |
selector = Languages::TextSignalIsUsedByRoute; |
| 5031 |
break; |
| 5032 |
|
| 5033 |
default: |
| 5034 |
selector = Languages::TextObjectIsUsedByRoute; |
| 5035 |
break; |
| 5036 |
} |
| 5037 |
result = Logger::Logger::Format(Languages::GetText(selector), object->GetName(), route.second->GetName()); |
| 5038 |
return true; |
| 5039 |
} |
| 5040 |
return false; |
| 5041 |
} |