forked from PLCnext/CppExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpcPlcManagerComponent.cpp
126 lines (99 loc) · 3.74 KB
/
OpcPlcManagerComponent.cpp
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
#include "OpcPlcManagerComponent.hpp"
#include "Arp/Plc/Commons/Domain/PlcDomainProxy.hpp"
#include "OpcPlcManagerLibrary.hpp"
#include "Arp/System/Rsc/ServiceManager.hpp"
namespace OpcPlcManager
{
using namespace Arp::Plc::Commons::Domain;
using Arp::System::Rsc::ServiceManager;
OpcPlcManagerComponent::OpcPlcManagerComponent(IApplication& application, const String& name)
: ComponentBase(application, ::OpcPlcManager::OpcPlcManagerLibrary::GetInstance(), name, ComponentCategory::Custom)
, MetaComponentBase(::OpcPlcManager::OpcPlcManagerLibrary::GetInstance().GetNamespace())
, workerThreadInstance(make_delegate(this, &OpcPlcManagerComponent::workerThreadBody) , 1000, "WorkerThreadName")
{
}
void OpcPlcManagerComponent::Initialize()
{
// never remove next line
PlcDomainProxy::GetInstance().RegisterComponent(*this, true);
// subscribe events from the event system (Nm) here
}
void OpcPlcManagerComponent::SubscribeServices()
{
// gets the IDataAccessService pointer
this->plcManagerService2Ptr = ServiceManager::GetService<IPlcManagerService2>();}
void OpcPlcManagerComponent::LoadSettings(const String& /*settingsPath*/)
{
// load firmware settings here
}
void OpcPlcManagerComponent::SetupSettings()
{
// never remove next line
MetaComponentBase::SetupSettings();
// setup firmware settings here
}
void OpcPlcManagerComponent::PublishServices()
{
// publish the services of this component here
}
void OpcPlcManagerComponent::LoadConfig()
{
// load project config here
}
void OpcPlcManagerComponent::SetupConfig()
{
// setup project config here
Log::Info("OpcPlcManagerComponent: Starting worker thread.");
workerThreadInstance.Start();
}
void OpcPlcManagerComponent::ResetConfig()
{
// implement this inverse to SetupConfig() and LoadConfig()
Log::Info("OpcPlcManagerComponent: Stopping worker thread.");
workerThreadInstance.Stop();
}
void OpcPlcManagerComponent::Dispose()
{
// never remove next line
MetaComponentBase::Dispose();
// implement this inverse to SetupSettings(), LoadSettings() and Initialize()
}
void OpcPlcManagerComponent::PowerDown()
{
// implement this only if data must be retained even on power down event
}
// Thread Body
void OpcPlcManagerComponent::workerThreadBody(void)
{
// Check if each method has been called from the UA Client.
// If so, call the corresponding method on the RSC service,
// and return the result.
if (this->GetPlcState.UA_MethodState == 1)
{
Log::Info("OpcPlcManagerComponent: GetPlcState.");
// Get the PLC state from the RSC service
this->GetPlcState.state = static_cast<Arp::uint32>(this->plcManagerService2Ptr->GetPlcState());
// Signal that the method has completed successfully
this->GetPlcState.UA_StatusCode = 0;
this->GetPlcState.UA_MethodState = 0;
}
if (this->Start.UA_MethodState == 1)
{
Log::Info("OpcPlcManagerComponent: Start: {0:d} {1}", this->Start.startKind, (this->Start.async ? "asynchronous" : "synchronous"));
// Start the PLC via the RSC service
this->plcManagerService2Ptr->Start((PlcStartKind)this->Start.startKind, this->Start.async);
// Signal that the method has completed successfully
this->Start.UA_StatusCode = 0;
this->Start.UA_MethodState = 0;
}
if (this->Stop.UA_MethodState == 1)
{
Log::Info("OpcPlcManagerComponent: Stop {0}.", (this->Stop.async ? "asynchronous" : "synchronous"));
// Stop the PLC via the RSC service
this->plcManagerService2Ptr->Stop(this->Stop.async);
// Signal that the method has completed successfully
this->Stop.UA_StatusCode = 0;
this->Stop.UA_MethodState = 0;
}
}
} // end of namespace OpcPlcManager