| 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 <map> |
| 22 |
#include <sstream> |
| 23 |
|
| 24 |
#include "DataModel/Feedback.h" |
| 25 |
#include "Manager.h" |
| 26 |
#include "Utils/Utils.h" |
| 27 |
|
| 28 |
using std::map; |
| 29 |
using std::string; |
| 30 |
using std::to_string; |
| 31 |
|
| 32 |
namespace DataModel |
| 33 |
{ |
| 34 |
string Feedback::Serialize() const |
| 35 |
{ |
| 36 |
string str; |
| 37 |
str = "objectType=Feedback;" + LayoutItem::Serialize(); |
| 38 |
str += ";controlID=" + to_string(controlID); |
| 39 |
str += ";pin=" + to_string(pin); |
| 40 |
str += ";feedbacktype=" + to_string(feedbackType); |
| 41 |
str += ";route=" + to_string(routeId); |
| 42 |
str += ";inverted=" + to_string(inverted); |
| 43 |
str += ";state=" + to_string(stateCounter > 0); |
| 44 |
str += ";matchkey=" + matchKey; |
| 45 |
return str; |
| 46 |
} |
| 47 |
|
| 48 |
bool Feedback::Deserialize(const string& serialized) |
| 49 |
{ |
| 50 |
map<string, string> arguments; |
| 51 |
ParseArguments(serialized, arguments); |
| 52 |
string objectType = Utils::Utils::GetStringMapEntry(arguments, "objectType"); |
| 53 |
if (objectType.compare("Feedback") != 0) |
| 54 |
{ |
| 55 |
return false; |
| 56 |
} |
| 57 |
LayoutItem::Deserialize(arguments); |
| 58 |
SetHeight(Height1); |
| 59 |
SetWidth(Width1); |
| 60 |
controlID = Utils::Utils::GetIntegerMapEntry(arguments, "controlID", ControlIdNone); |
| 61 |
pin = Utils::Utils::GetIntegerMapEntry(arguments, "pin"); |
| 62 |
feedbackType = static_cast<FeedbackType>(Utils::Utils::GetIntegerMapEntry(arguments, "feedbacktype", FeedbackTypeDefault)); |
| 63 |
routeId = Utils::Utils::GetIntegerMapEntry(arguments, "route", RouteNone); |
| 64 |
inverted = Utils::Utils::GetBoolMapEntry(arguments, "inverted", false); |
| 65 |
stateCounter = Utils::Utils::GetBoolMapEntry(arguments, "state", FeedbackStateFree) ? MaxStateCounter : 0; |
| 66 |
matchKey = Utils::Utils::GetStringMapEntry(arguments, "matchkey"); |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
void Feedback::SetState(const FeedbackState newState) |
| 71 |
{ |
| 72 |
FeedbackState state = static_cast<FeedbackState>(newState != inverted); |
| 73 |
{ |
| 74 |
std::lock_guard<std::mutex> Guard(updateMutex); |
| 75 |
if (state == FeedbackStateFree) |
| 76 |
{ |
| 77 |
if (stateCounter < MaxStateCounter) |
| 78 |
{ |
| 79 |
return; |
| 80 |
} |
| 81 |
stateCounter = MaxStateCounter - 1; |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
const unsigned char oldStateCounter = stateCounter; |
| 86 |
stateCounter = MaxStateCounter; |
| 87 |
|
| 88 |
if (oldStateCounter > 0) |
| 89 |
{ |
| 90 |
return; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
manager->FeedbackPublishState(this); |
| 95 |
UpdateTrackState(FeedbackStateOccupied); |
| 96 |
|
| 97 |
Route* route = manager->GetRoute(routeId); |
| 98 |
if (route) |
| 99 |
{ |
| 100 |
static Logger::Logger* logger = Logger::Logger::GetLogger(Languages::GetText(Languages::TextManager)); |
| 101 |
route->Execute(logger, ObjectIdentifier()); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
void Feedback::UpdateTrackState(const FeedbackState state) |
| 106 |
{ |
| 107 |
if (!track) |
| 108 |
{ |
| 109 |
return; |
| 110 |
} |
| 111 |
track->SetFeedbackState(GetID(), state); |
| 112 |
} |
| 113 |
|
| 114 |
void Feedback::Debounce() |
| 115 |
{ |
| 116 |
{ |
| 117 |
std::lock_guard<std::mutex> Guard(updateMutex); |
| 118 |
if (stateCounter == MaxStateCounter || stateCounter == 0) |
| 119 |
{ |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
--stateCounter; |
| 124 |
if (stateCounter != 0) |
| 125 |
{ |
| 126 |
return; |
| 127 |
} |
| 128 |
} |
| 129 |
manager->FeedbackPublishState(this); |
| 130 |
UpdateTrackState(FeedbackStateFree); |
| 131 |
} |
| 132 |
|
| 133 |
Feedback& Feedback::operator=(const Hardware::FeedbackCacheEntry& feedback) |
| 134 |
{ |
| 135 |
SetControlID(feedback.GetControlID()); |
| 136 |
SetPin(feedback.GetPin()); |
| 137 |
SetName(feedback.GetName()); |
| 138 |
SetMatchKey(feedback.GetMatchKey()); |
| 139 |
return *this; |
| 140 |
} |
| 141 |
} // namespace DataModel |
| 142 |
|