Debian Codegraph @

DataModel/Feedback.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 void 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;
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 }
68
69 void Feedback::SetState(const FeedbackState newState)
70 {
71 FeedbackState state = static_cast<FeedbackState>(newState != inverted);
72 {
73 std::lock_guard<std::mutex> Guard(updateMutex);
74 if (state == FeedbackStateFree)
75 {
76 if (stateCounter < MaxStateCounter)
77 {
78 return;
79 }
80 stateCounter = MaxStateCounter - 1;
81 return;
82 }
83
84 const unsigned char oldStateCounter = stateCounter;
85 stateCounter = MaxStateCounter;
86
87 if (oldStateCounter > 0)
88 {
89 return;
90 }
91 }
92
93 manager->FeedbackPublishState(this);
94 UpdateTrackState(FeedbackStateOccupied);
95
96 Route* route = manager->GetRoute(routeId);
97 if (route)
98 {
99 static Logger::Logger* logger = Logger::Logger::GetLogger(Languages::GetText(Languages::TextManager));
100 route->Execute(logger, ObjectIdentifier());
101 }
102 }
103
104 void Feedback::UpdateTrackState(const FeedbackState state)
105 {
106 if (!track)
107 {
108 return;
109 }
110 track->SetFeedbackState(GetID(), state);
111 }
112
113 void Feedback::Debounce()
114 {
115 {
116 std::lock_guard<std::mutex> Guard(updateMutex);
117 if (stateCounter == MaxStateCounter || stateCounter == 0)
118 {
119 return;
120 }
121
122 --stateCounter;
123 if (stateCounter != 0)
124 {
125 return;
126 }
127 }
128 manager->FeedbackPublishState(this);
129 UpdateTrackState(FeedbackStateFree);
130 }
131
132 Feedback& Feedback::operator=(const Hardware::FeedbackCacheEntry& feedback)
133 {
134 SetControlID(feedback.GetControlID());
135 SetPin(feedback.GetPin());
136 SetName(feedback.GetName());
137 SetMatchKey(feedback.GetMatchKey());
138 return *this;
139 }
140 } // namespace DataModel
141