-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathCOMP_Notifications.cpp
More file actions
155 lines (128 loc) · 5.95 KB
/
COMP_Notifications.cpp
File metadata and controls
155 lines (128 loc) · 5.95 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
/******************************************************************************
*
* 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 "COMP_Notifications.hpp"
#include "Arp/Plc/Commons/Esm/ProgramComponentBase.hpp"
#include "NotificationExampleLibrary.hpp"
namespace NotificationExample
{
COMP_Notifications::COMP_Notifications(ILibrary& library, const String& name)
: ComponentBase(library, name, ComponentCategory::Custom, GetDefaultStartOrder())
, programProvider(*this)
, ProgramComponentBase(::NotificationExample::NotificationExampleLibrary::GetInstance().GetNamespace(), programProvider)
, Custom_subscription("My.NameSpace.1")
, Custom_subscription2("My.NameSpace.2")
, NetworkConfigurationChanged_subscription("Arp.Device.Interface.NetworkConfigurationChanged")
, PlcStateChanged_subscription("Arp.Plc.Domain.Plc.StateChanged")
{
}
void COMP_Notifications::Initialize()
{
// never remove next line
ProgramComponentBase::Initialize();
log.Info("Notifications:");
for (auto &x : nm.GetAllKnownNotificationNameIds()){
log.Info("Name:{1} Value:{0}",x.GetValue(), nm.GetNotificationName(x));
}
// subscribe events from the event system (Nm) here
this->Custom_subscription.OnNotification += make_delegate(this, &COMP_Notifications::NM_Subscription1_Callback);
this->Custom_subscription2.OnNotification += make_delegate(this, &COMP_Notifications::NM_Subscription2_Callback);
this->NetworkConfigurationChanged_subscription.OnNotification += make_delegate(this, &COMP_Notifications::NetworkConfigurationChanged_Callback);
this->PlcStateChanged_subscription.OnNotification += make_delegate(this, &COMP_Notifications::PlcStateChanged_Callback);
}
void COMP_Notifications::LoadConfig()
{
// load project config here
}
void COMP_Notifications::SetupConfig()
{
// never remove next line
ProgramComponentBase::SetupConfig();
// setup project config here
}
void COMP_Notifications::ResetConfig()
{
// never remove next line
ProgramComponentBase::ResetConfig();
// implement this inverse to SetupConfig() and LoadConfig()
}
void COMP_Notifications::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
}
// Receive first self-defined notification
void COMP_Notifications::NM_Subscription1_Callback(const Arp::System::Nm::Notification& notification)
{
//
auto payload = notification.GetPayloadAs<ExamplePayload>();
OP_uiValue1 = payload.GetMyValue();
auto MyString = payload.GetMyString();
log.Info( "1 MyValue:{0} MyString{1}",OP_uiValue1,MyString );
}
// Receive second notification
void COMP_Notifications::NM_Subscription2_Callback(const Arp::System::Nm::Notification& notification)
{
// within the "ExamplePayload" string
auto payload = notification.GetPayloadAs<ExamplePayload>();
OP_uiValue2 = payload.GetMyValue();
auto MyString = payload.GetMyString();
log.Info( "2 MyValue:{0} MyString{1}",OP_uiValue2,MyString );
}
// Receive Network Configuration Changes.
void COMP_Notifications::NetworkConfigurationChanged_Callback(const Arp::System::Nm::Notification& notification)
{
auto payload = notification.GetPayloadAs<Arp::System::NmPayload::Device::NetworkConfigurationChangedPayload>();
//Configuration of network interface {number} changed: {Parameter} = {Value}
auto parameter = payload.GetChangedParameter();
auto deviceId = payload.GetDeviceId();
auto id = payload.GetId();
auto name = payload.GetName();
auto value = payload.GetValue();
log.Info("Parameter:{0},devieId:{1},id:{2},name:{3},value:{4}",parameter,deviceId,id,name,value);
}
// This notification informs you about the latest PLC Status changes.
// And allows you to react accordingly.
void COMP_Notifications::PlcStateChanged_Callback(const Arp::System::Nm::Notification& notification)
{
//Plc state changed: {"None"|"Ready"|"Stop"|"Running"|"Halt"|"Changing","Warning"|"Error"|"SuspendedBySwitch"|"DcgNotPossible"|"DcgRealTimeViolation"}
// ==> {"None"|"Ready"|"Stop"|"Running"|"Halt"|"Changing","Warning"|"Error"|"SuspendedBySwitch"|"DcgNotPossible"|"DcgRealTimeViolation"}
auto payload = notification.GetPayloadAs<Arp::System::NmPayload::Plc::PlcStateChangedPayload>();
Arp::Plc::Commons::Domain::PlcState NewState = payload.GetNewState();
Arp::Plc::Commons::Domain::PlcState LastState = payload.GetLastState();
log.Info("Component noticed a PLC State change from '{0}' to '{1}' ." ,LastState , NewState);
switch( NewState )
{
case Arp::Plc::Commons::Domain::PlcState::Stop:
log.Info( "Handling Stop State" );
break;
case Arp::Plc::Commons::Domain::PlcState::Running:
if(LastState == Arp::Plc::Commons::Domain::PlcState::Halt){
log.Info( "Continue doing Something" );
}
if(LastState == Arp::Plc::Commons::Domain::PlcState::Stop){
log.Info( "Reinitialise Something");
}
break;
case Arp::Plc::Commons::Domain::PlcState::Halt:
log.Info( "Handling Halt State" );
log.Info( "Stop execution store next free commands be prepared to continue.");
break;
case Arp::Plc::Commons::Domain::PlcState::Changing:
break;
case Arp::Plc::Commons::Domain::PlcState::Warning:
break;
case Arp::Plc::Commons::Domain::PlcState::Error:
break;
case Arp::Plc::Commons::Domain::PlcState::SuspendedBySwitch:
break;
default:
log.Info( "Handling Halt State:{0} LastState:{1}", NewState,LastState );
break;
}
}
} // end of namespace NotificationExample