-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathBrowseVariablesComponent.cpp
More file actions
256 lines (211 loc) · 8.76 KB
/
BrowseVariablesComponent.cpp
File metadata and controls
256 lines (211 loc) · 8.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "BrowseVariablesComponent.hpp"
#include "Arp/Plc/Commons/Domain/PlcDomainProxy.hpp"
#include "BrowseVariablesLibrary.hpp"
#include "Arp/System/Rsc/ServiceManager.hpp"
#include "Arp/Plc/Meta/Services/VariableBrowseResult.hpp"
#include "Arp/Plc/Meta/Services/VariableInformation.hpp"
// Define the delegate method that will be passed to the GetComponentNames method of the IVariableBrowseService.
// In this case, the delegate simply logs the names of all the components in the enumerator.
void LogComponentNames(Arp::Base::Rsc::Commons::IRscReadEnumerator<Arp::Base::Rsc::Commons::RscString<512>>& componentNames)
{
Arp::Base::Rsc::Commons::RscString<512> componentName;
auto elements = componentNames.BeginRead();
Arp::Base::Commons::Logging::Log::Info("*** Here are the names of all components that provide variables:");
try
{
while (componentNames.ReadNext(componentName))
{
Arp::Base::Commons::Logging::Log::Info("- {0}", componentName);
}
}
catch (std::exception& e)
{
Arp::Base::Commons::Logging::Log::Error("Error occurred in {0}:\n{1}", __FUNCTION__, e.what());
}
Arp::Base::Commons::Logging::Log::Info("*** End of component names.");
}
// Define the delegate method that will be passed to the GetRoots method of the IVariableBrowseService.
// In this case, the delegate simply logs information about each root in the enumerator.
void LogRoots(Arp::Base::Rsc::Commons::IRscReadEnumerator<Arp::Plc::Meta::Services::VariableBrowseResult>& roots)
{
Arp::Plc::Meta::Services::VariableBrowseResult root;
auto elements = roots.BeginRead();
Arp::Base::Commons::Logging::Log::Info("*** Information about the roots of the Arp.Plc.Esm component:");
try
{
while (roots.ReadNext(root))
{
Arp::Base::Commons::Logging::Log::Info("- Name: {0}, Type: {1}", root.VariableInfo.Name, root.VariableInfo.Type);
}
}
catch (std::exception& e)
{
Arp::Base::Commons::Logging::Log::Error("Error occurred in {0}:\n{1}", __FUNCTION__, e.what());
}
Arp::Base::Commons::Logging::Log::Info("*** End of Arp.Plc.Esm roots.");
}
// Define the delegate method that will be passed to the GetRoots method of the IVariableBrowseService.
// In this case, the delegate simply logs information about each root in the enumerator.
void LogChildren(Arp::Base::Rsc::Commons::IRscReadEnumerator<Arp::Plc::Meta::Services::VariableBrowseResult>& children)
{
Arp::Plc::Meta::Services::VariableBrowseResult child;
auto elements = children.BeginRead();
try
{
while (children.ReadNext(child))
{
Arp::Base::Commons::Logging::Log::Info("- Name: {0}, Type: {1}", child.VariableInfo.Name, child.VariableInfo.Type);
}
}
catch (std::exception& e)
{
Arp::Base::Commons::Logging::Log::Error("Error occurred in {0}:\n{1}", __FUNCTION__, e.what());
}
}
namespace BrowseVariables
{
using Arp::System::Rsc::ServiceManager;
using namespace Arp::Plc::Commons::Domain;
BrowseVariablesComponent::BrowseVariablesComponent(ILibrary& library, const String& name)
: ComponentBase(library, name, ComponentCategory::Custom, GetDefaultStartOrder())
, MetaComponentBase(::BrowseVariables::BrowseVariablesLibrary::GetInstance().GetNamespace())
, browseVariablesThread(this, &BrowseVariablesComponent::BrowseVariablesData, 1000, "BrowseVariablesThread")
{
}
void BrowseVariablesComponent::Initialize()
{
// never remove next line
PlcDomainProxy::GetInstance().RegisterComponent(*this, true);
// initialize singletons here, subscribe notifications here
PlcDomainProxy& plcDomainProxy = PlcDomainProxy::GetInstance();
// register all Plc event handler
plcDomainProxy.PlcLoaded += make_delegate(this, &BrowseVariablesComponent::OnPlcLoaded);
plcDomainProxy.PlcStarted += make_delegate(this, &BrowseVariablesComponent::OnPlcStarted);
plcDomainProxy.PlcStopping += make_delegate(this, &BrowseVariablesComponent::OnPlcStopping);
plcDomainProxy.PlcUnloading += make_delegate(this, &BrowseVariablesComponent::OnPlcUnloading);
plcDomainProxy.PlcChanging += make_delegate(this, &BrowseVariablesComponent::OnPlcChanging);
plcDomainProxy.PlcChanged += make_delegate(this, &BrowseVariablesComponent::OnPlcChanged);
}
void BrowseVariablesComponent::SubscribeServices()
{
// gets the IVariableBrowseService pointer
this->variableBrowseServicePtr = ServiceManager::GetService<IVariableBrowseService>();
}
void BrowseVariablesComponent::LoadSettings(const String& /*settingsPath*/)
{
// load firmware settings here
}
void BrowseVariablesComponent::SetupSettings()
{
// never remove next line
MetaComponentBase::SetupSettings();
// setup firmware settings here
}
void BrowseVariablesComponent::PublishServices()
{
// publish the services of this component here
}
void BrowseVariablesComponent::LoadConfig()
{
// load project config here
}
void BrowseVariablesComponent::SetupConfig()
{
// setup project config here
}
void BrowseVariablesComponent::ResetConfig()
{
// implement this inverse to SetupConfig() and LoadConfig()
}
void BrowseVariablesComponent::Dispose()
{
// never remove next line
MetaComponentBase::Dispose();
// implement this inverse to SetupSettings(), LoadSettings() and Initialize()
PlcDomainProxy& plcDomainProxy = PlcDomainProxy::GetInstance();
// unregister all Plc event handler
plcDomainProxy.PlcLoaded -= make_delegate(this, &BrowseVariablesComponent::OnPlcLoaded);
plcDomainProxy.PlcStarted -= make_delegate(this, &BrowseVariablesComponent::OnPlcStarted);
plcDomainProxy.PlcStopping -= make_delegate(this, &BrowseVariablesComponent::OnPlcStopping);
plcDomainProxy.PlcUnloading -= make_delegate(this, &BrowseVariablesComponent::OnPlcUnloading);
plcDomainProxy.PlcChanging -= make_delegate(this, &BrowseVariablesComponent::OnPlcChanging);
plcDomainProxy.PlcChanged -= make_delegate(this, &BrowseVariablesComponent::OnPlcChanged);
}
void BrowseVariablesComponent::PowerDown()
{
// implement this only if data shall be retained even on power down event
// will work only for PLCnext controllers with an "Integrated uninterruptible power supply (UPS)"
// Available with 2021.6 FW
}
void BrowseVariablesComponent::OnPlcLoaded()
{
}
void BrowseVariablesComponent::OnPlcStarted()
{
this->StartBrowseVariables();
}
void BrowseVariablesComponent::OnPlcStopping()
{
this->StopBrowseVariables();
}
void BrowseVariablesComponent::OnPlcUnloading(bool)
{
}
void BrowseVariablesComponent::OnPlcChanging()
{
this->StopBrowseVariables();
}
void BrowseVariablesComponent::OnPlcChanged(bool /*success*/)
{
this->StartBrowseVariables();
}
void BrowseVariablesComponent::StartBrowseVariables()
{
this->browseVariablesThread.Start();
}
void BrowseVariablesComponent::StopBrowseVariables()
{
this->browseVariablesThread.Stop();
}
void BrowseVariablesComponent::BrowseVariablesData()
{
// This is implemented as a step sequencer.
// Each step demonstrates different features of the VariableBrowse service.
switch(step)
{
case 5:
{
// Retrieve a list of all components that provide variables
this->variableBrowseServicePtr->GetComponentNames(LogComponentNames);
break;
}
case 10:
{
// Retrieve a list of root nodes on the Arp.Plc.Esm component
// In a real application the component name would not be hard-coded,
// but would be one of the strings returned by the GetComponentNames method.
this->variableBrowseServicePtr->GetRoots("Arp.Plc.Esm", LogRoots);
break;
}
case 15:
{
// Get information about a root node that we already know the name of.
// In a real application the variable name would not be hard-coded,
// but would be retrieved by iterating through component roots and children.
Arp::Base::Commons::Logging::Log::Info("*** Children of the Arp.Plc.Esm/ESM_DATA variable:");
// Get the variable info in order to retrieve the Browse Handle.
// The Browse Handle can also be obtained in the GetRoots delegate,
// but we'll do it here to demonstrate the GetVariable method.
VariableQueryResult variableInfo = this->variableBrowseServicePtr->GetVariable("Arp.Plc.Esm/ESM_DATA");
// Use the Browse Handle to get information about the variable's children.
this->variableBrowseServicePtr->GetChildren(variableInfo.BrowseHandle, LogChildren);
Arp::Base::Commons::Logging::Log::Info("*** End of ESM_DATA children.");
break;
}
default:
break;
}
// Increment the step and stop after a time
if (++step > 100) step = 100;
}
} // end of namespace BrowseVariables