Event Conversion ChaiScripts
Event Conversion ChaiScripts
The EventsFromOPC-UA and EventsToOPC-UA blocks use ChaiScript expressions in the EventConverterScript property to convert the event from one system to another.
ChaiScript is a strongly typed declarative language with syntax similar to C++. The scripts support the same set of built-in ChaiScript operators and functions that are described in the Chai script documentation.
The EventConverterScript's' can refer to all the source event attributes in the C++ like map<string,any> variable map, with attribute name as the key pointing to the value for that attribute. The script task is to set into similar C++ like map<string,any> variable map of the target event type.
Note: When accessing the map values, the script should be prepared to handle the case when the map key does not exist or the value is not of the expected type. This can be done either by looking if the key exists using mapname.count("keyname") > 0 expression or by using try-catch blocks around the map value access statements. Look at the Example Conversion Script for different examples how these can be used.
CDP Event Attributes
The CDP Event Attributes are always in the map<string,any> named CDPEvent. In EventsToOPC-UA block this map is read-only and acts as the source for conversion, whereas in EventsFromOPC-UA block this map is writeable and its attributes should be valued by the script. The scripts can refer to all CDP event attributes via this map, including these:
Attribute | Type | Description |
---|---|---|
Handle | unsigned int | The CDP handle of the event sender |
Sender | string | The CDP path of the event sender |
Code | unsigned int | The CDP event code. See CDP Event Code Flags for details. Note, that when the EventsFromOPC-UA block calculates Code to 0, the event is not forwarded to the CDP system at all (this feature can be used to not to ignore some OPC UA events and not to send them to CDP). |
Status | unsigned int | The new status of the object that caused the CDP event. For alarms, see the Alarm Status Defines in CDPAlarm documentation. |
Level | string | The alarm level (in case of an alarm event) |
Text | string | The event message |
Description | string | The event description |
OPC UA Event attributes
The OPC UA Event Attributes are always in the map<string,any> named UAEvent. In EventsFromOPC-UA block this map is read-only and acts as the source for conversion, whereas in EventsToOPC-UA block this map is writeable and its attributes should be valued by the script. The script can refer to all OPC UA attributes via this map, including these:
Attribute | Type | Description |
---|---|---|
EventId | string | The OPC UA event identifier |
Message | string | The OPC UA event message |
Severity | unsigned int | The OPC UA event severity |
SourceName | string | The OPC UA event source name |
SourceNode | string | The OPC UA event source node id |
Comment | string | The OPC UA condition comment |
ConditionName | string | The OPC UA condition name |
EnabledState/Id | boolean | The OPC UA alarm two-state-variable EnabledState/Id value (true/false) |
ActiveState/Id | boolean | The OPC UA alarm two-state-variable ActiveState/Id value (true/false) |
AckedState/Id | boolean | The OPC UA alarm two-state-variable AckedState/Id value (true/false) |
ConfirmedState/Id | boolean | The OPC UA alarm two-state-variable ConfirmedState/Id value (true/false) |
Note: In addition to these, in EventsFromOPC-UA block scripts all the event attributes that were added to the OPC UA event by the FilterSelect are also available in the UAEvent map. On the other hand, in EventsToOPC-UA block scripts, all the OPC UA event or alarm event attributes can be set via this UAEvent map. Explore your OPC UA server event type definition for the list of available attributes.
Example Conversion Script
As an example, the following is the default ChaiScript code in EventsFromOPC-UA block that calculates the CDP event attributes from OPC UA event attributes. You can find the reasoning next to the statements as ChaiScript comments.
// First, calculate CDP Event Code: CDPEvent["Code"] = 0; // default to zero that means no event will be forwarded to CDP try { if (UAEvent["EnabledState/Id"]) { // check if the event is from OPC UA enabled alarm/condition if (UAEvent["ActiveState/Id"]) { // check if the event is from OPC UA activated alarm/condition CDPEvent["Code"] |= 0x1; // hexadecimal value for CDP alarm Set event code } else { CDPEvent["Code"] |= 0x2; // hexadecimal value for CDP alarm Cleared event code } if (UAEvent["AckedState/Id"]) { CDPEvent["Code"] |= 0x4; // hexadecimal value for CDP alarm Acknowledged event code } } } catch (e) {} // prevents the script from exiting with an error when UAEvent map values do not exist or their type is not compatible // For alarms, calculate CDP Event Status and Level: CDPEvent["Status"] = 0; try { if (UAEvent["EnabledState/Id"]) { CDPEvent["Group"] = "OPC-UA-Alarms"; // set group name for OPC UA alarms if (UAEvent["ActiveState/Id"]) { if (UAEvent["Severity"] < 100) { CDPEvent["Status"] |= 0x1; // hexadecimal value for CDP alarm status Notify+Set CDPEvent["Level"] = "NOTIFY"; } else if (UAEvent["Severity"] < 500) { CDPEvent["Status"] |= 0x10; // hexadecimal value for CDP alarm status Warning+Set CDPEvent["Level"] = "WARNING"; } else { CDPEvent["Status"] |= 0x100; // hexadecimal value for CDP alarm status Error+Set CDPEvent["Level"] = "ERROR"; } } if (!UAEvent["AckedState/Id"]) { if (UAEvent["Severity"] < 100) { CDPEvent["Status"] |= 0x10000; // hexadecimal value for CDP alarm status Notify+Unacked } else if (UAEvent["Severity"] < 500) { CDPEvent["Status"] |= 0x100000; // hexadecimal value for CDP alarm status Warning+Unacked } else { CDPEvent["Status"] |= 0x1000000; // hexadecimal value for CDP alarm status Error+Unacked } } if (UAEvent["Severity"] < 100) { CDPEvent["Level"] = "NOTIFY"; // set CDP alarm level to NOTIFY for OPC UA alarms with severity less than 100 } else if (UAEvent["Severity"] < 500) { CDPEvent["Level"] = "WARNING"; // set CDP alarm level to WARNING for OPC UA alarms with severity less than 500 } else { CDPEvent["Level"] = "ERROR"; // set CDP alarm level to ERROR for OPC UA alarms with severity greater or equal than 500 } } } catch (e) {} if (UAEvent.count("Message") > 0) { CDPEvent["Text"] = UAEvent["Message"]; // use Message as CDP event Text, when available } if (UAEvent.count("Comment") > 0) { CDPEvent["Description"] = UAEvent["Comment"]; // use Comment as CDP event Description, when available }
Note: The default script is provided as a base for further customization. The script can be modified to suit the specific requirements of the events and alarms occurring in the system.
CDP Event Code Flags
Common event codes used in the CDP framework (note multiple flags can be set at the same time)): Common event codes used in the CDP framework (note that multiple flags can be set at the same time):
Name | Code | Description |
---|---|---|
AlarmSet | 0x1 | The alarm's Set flag/state was set. The alarm changed state to "Unack-Set" (The Unack flag was set if not already set). |
AlarmClr | 0x2 | The alarm's Set flag was cleared. The Unack state is unchanged. |
AlarmAck | 0x4 | The alarm changed state from "Unacknowledged" to "Acknowledged". The Set state is unchanged. |
AlarmReprise | 0x40 | A repetition/update of an event that has been reported before. Courtesy of late subscribers. |
SourceObjectUnavailable | 0x100 | The provider of the event has become unavailable (disconnected or similar). |
NodeBoot | 0x40000000 | The provider reports that the CDPEventNode has just booted. |
See also EventsFromOPC-UA and EventsToOPC-UA.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.