-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathForceComponent.cpp
More file actions
297 lines (234 loc) · 10.1 KB
/
ForceComponent.cpp
File metadata and controls
297 lines (234 loc) · 10.1 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/******************************************************************************
*
* Copyright (c) Phoenix Contact GmbH & Co. KG. All rights reserved.
* Licensed under the MIT. See LICENSE file in the project root for full license information.
*
******************************************************************************/
#include "ForceComponent.hpp"
#include "Arp/Plc/Commons/Domain/PlcDomainProxy.hpp"
#include "ForceLibrary.hpp"
#include "Arp/System/Rsc/ServiceManager.hpp"
#include "Arp/Plc/Gds/Services/ForceItem.hpp"
namespace Force
{
using Arp::System::Rsc::ServiceManager;
using namespace Arp::Plc::Commons::Domain;
ForceComponent::ForceComponent(ILibrary& library, const String& name)
: ComponentBase(library, name, ComponentCategory::Custom, GetDefaultStartOrder())
, MetaComponentBase(::Force::ForceLibrary::GetInstance().GetNamespace())
, forceThread(this, &ForceComponent::ForceData, 1000, "ForceThread")
{
}
void ForceComponent::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, &ForceComponent::OnPlcLoaded);
plcDomainProxy.PlcStarted += make_delegate(this, &ForceComponent::OnPlcStarted);
plcDomainProxy.PlcStopping += make_delegate(this, &ForceComponent::OnPlcStopping);
plcDomainProxy.PlcUnloading += make_delegate(this, &ForceComponent::OnPlcUnloading);
plcDomainProxy.PlcChanging += make_delegate(this, &ForceComponent::OnPlcChanging);
plcDomainProxy.PlcChanged += make_delegate(this, &ForceComponent::OnPlcChanged);
}
void ForceComponent::SubscribeServices()
{
// gets the IForceService pointer
this->forceServicePtr = ServiceManager::GetService<IForceService>();
}
void ForceComponent::LoadSettings(const String& /*settingsPath*/)
{
// load firmware settings here
}
void ForceComponent::SetupSettings()
{
// never remove next line
MetaComponentBase::SetupSettings();
// setup firmware settings here
}
void ForceComponent::PublishServices()
{
// publish the services of this component here
}
void ForceComponent::LoadConfig()
{
// load project config here
}
void ForceComponent::SetupConfig()
{
// setup project config here
}
void ForceComponent::ResetConfig()
{
// implement this inverse to SetupConfig() and LoadConfig()
}
void ForceComponent::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, &ForceComponent::OnPlcLoaded);
plcDomainProxy.PlcStarted -= make_delegate(this, &ForceComponent::OnPlcStarted);
plcDomainProxy.PlcStopping -= make_delegate(this, &ForceComponent::OnPlcStopping);
plcDomainProxy.PlcUnloading -= make_delegate(this, &ForceComponent::OnPlcUnloading);
plcDomainProxy.PlcChanging -= make_delegate(this, &ForceComponent::OnPlcChanging);
plcDomainProxy.PlcChanged -= make_delegate(this, &ForceComponent::OnPlcChanged);
}
void ForceComponent::PowerDown()
{
// implement this only if data shall be retained even on power down event
// will work only for PLCnext Control devices with an "Integrated uninterruptible power supply (UPS)"
// Available with 2021.6 FW
}
void ForceComponent::OnPlcLoaded()
{
}
void ForceComponent::OnPlcStarted()
{
this->StartForce();
}
void ForceComponent::OnPlcStopping()
{
this->StopForce();
}
void ForceComponent::OnPlcUnloading(bool)
{
}
void ForceComponent::OnPlcChanging()
{
this->StopForce();
}
void ForceComponent::OnPlcChanged(bool /*success*/)
{
this->StartForce();
}
void ForceComponent::StartForce()
{
this->forceThread.Start();
}
void ForceComponent::StopForce()
{
this->forceThread.Stop();
}
void ForceComponent::ForceData()
{
// This is implemented as a step sequencer.
// Each step demonstrates different features of the Force service.
switch(step)
{
case 5:
{
// Check if certain GDS variables are forceable
RscString<512> portName;
boolean isForceable;
// Global variables not connected to process data are not forceable
portName = "Arp.Plc.Eclr/Bool_GLOBAL";
isForceable = this->forceServicePtr->IsForcable(portName);
this->log.Info("Is '{0}' forceable? {1}", portName, isForceable);
portName = "Arp.Plc.Eclr/Int_GLOBAL";
isForceable = this->forceServicePtr->IsForcable(portName);
this->log.Info("Is '{0}' forceable? {1}", portName, isForceable);
// Certain types of global variables connected to process data are forceable
portName = "Arp.Plc.Eclr/Bool_GLOBAL_IN";
isForceable = this->forceServicePtr->IsForcable(portName);
this->log.Info("Is '{0}' forceable? {1}", portName, isForceable);
portName = "Arp.Plc.Eclr/Byte_GLOBAL_IN";
isForceable = this->forceServicePtr->IsForcable(portName);
this->log.Info("Is '{0}' forceable? {1}", portName, isForceable);
portName = "Arp.Plc.Eclr/Byte_GLOBAL_OUT";
isForceable = this->forceServicePtr->IsForcable(portName);
this->log.Info("Is '{0}' forceable? {1}", portName, isForceable);
// Certain types of program port variables are forceable
portName = "Arp.Plc.Eclr/MainInstance.Bool_IN";
isForceable = this->forceServicePtr->IsForcable(portName);
this->log.Info("Is '{0}' forceable? {1}", portName, isForceable);
portName = "Arp.Plc.Eclr/MainInstance.Bool_OUT";
isForceable = this->forceServicePtr->IsForcable(portName);
this->log.Info("Is '{0}' forceable? {1}", portName, isForceable);
portName = "Arp.Plc.Eclr/MainInstance.Int_IN";
isForceable = this->forceServicePtr->IsForcable(portName);
this->log.Info("Is '{0}' forceable? {1}", portName, isForceable);
portName = "Arp.Plc.Eclr/MainInstance.Int_OUT";
isForceable = this->forceServicePtr->IsForcable(portName);
this->log.Info("Is '{0}' forceable? {1}", portName, isForceable);
// I/O port variables are not directly forceable
// These are not even recognised as valid variable names
portName = "Arp.Io.AxlC/0.IN00";
isForceable = this->forceServicePtr->IsForcable(portName);
this->log.Info("Is '{0}' forceable? {1}", portName, isForceable);
portName = "Arp.Io.AxlC/0.OUT00";
isForceable = this->forceServicePtr->IsForcable(portName);
this->log.Info("Is '{0}' forceable? {1}", portName, isForceable);
portName = "Arp.Io.AxlC/0.~DI8";
isForceable = this->forceServicePtr->IsForcable(portName);
this->log.Info("Is '{0}' forceable? {1}", portName, isForceable);
portName = "Arp.Io.AxlC/0.~DO8";
isForceable = this->forceServicePtr->IsForcable(portName);
this->log.Info("Is '{0}' forceable? {1}", portName, isForceable);
break;
}
case 6:
{
boolean isActive;
ForceItem forceVariable;
// Check that forcing is inactive
isActive = this->forceServicePtr->IsActive();
this->log.Info("Before adding variables, is forcing active? {0}", isActive);
// Start forcing by adding a variable to the force list
forceVariable.VariableName = "Arp.Plc.Eclr/Byte_GLOBAL_OUT";
forceVariable.ForceValue = true;
this->forceServicePtr->AddVariable(forceVariable);
// Check that forcing is active
isActive = this->forceServicePtr->IsActive();
this->log.Info("After adding a variable, is forcing active? {0}", isActive);
// Add another variable to the force list
forceVariable.VariableName = "Arp.Plc.Eclr/MainInstance.Int_IN";
forceVariable.ForceValue = (Arp::int16)42;
this->forceServicePtr->AddVariable(forceVariable);
break;
}
case 10:
{
ForceItem forceVariable;
// Change the value of variables already in the force list
forceVariable.VariableName = "Arp.Plc.Eclr/Byte_GLOBAL_OUT";
forceVariable.ForceValue = 0x01;
this->forceServicePtr->AddVariable(forceVariable);
forceVariable.VariableName = "Arp.Plc.Eclr/MainInstance.Int_IN";
forceVariable.ForceValue = 420;
this->forceServicePtr->AddVariable(forceVariable);
// Get all the variables currently in the force list
auto forceVariables = this->forceServicePtr->GetVariables();
this->log.Info("Current list of forced variables:");
for (auto& item : forceVariables)
{
this->log.Info("- {0}", item.VariableName);
}
break;
}
case 15:
{
boolean isActive;
// Remove a variable from the force list
this->forceServicePtr->RemoveVariable("Arp.Plc.Eclr/Byte_GLOBAL_OUT");
// Check that forcing is still active
isActive = this->forceServicePtr->IsActive();
this->log.Info("After removing a variable from the force list, is forcing active? {0}", isActive);
// Stop forcing by removing the final variable to the force list
this->forceServicePtr->RemoveVariable("Arp.Plc.Eclr/MainInstance.Int_IN");
// Check that forcing is active
isActive = this->forceServicePtr->IsActive();
this->log.Info("After removing all variables from the force list, is forcing active? {0}", isActive);
break;
}
default:
break;
}
// Increment the step and reset the sequence after a time
if (++step > 100) step = 0;
}
} // end of namespace Force