UA ANSI C Server Professional  1.4.2.297
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Modules Pages
custom_provider_events.c
/******************************************************************************
**
** Copyright (C) 2011-2014 Unified Automation GmbH. All Rights Reserved.
** Web: http://www.unifiedautomation.com
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** Project: OPC Ansi C OPC Server Examples
**
******************************************************************************/
#include <uaserver_config.h>
#include <opcua_identifiers.h>
#include <opcua_memory.h>
#include <opcua_string.h>
#include <opcua_errorhandling.h>
#include <uaserver_events.h>
#include <uaserver_utilities.h>
#include "custom_provider_helper.h"
OPCUA_BEGIN_EXTERN_C
OpcUa_UInt32 g_HeaterSwitchedEventTypeId;
OpcUa_Int32 g_HeaterSwitchedEventType_IndexSwitchPosition;
OpcUa_Int32 g_HeaterSwitchedEventType_IndexCurrentTemperature;
OpcUa_StatusCode CustomProvider_RegisterEventTypes(OpcUa_NodeId *a_pStartingNodeId)
{
OpcUa_NodeId parentEventTypeId, nodeId;
UaServer_EventField eventField;
UaServer_AddressSpace *pAddressSpace = &g_pCustomProvider->AddressSpace;
UaServer_AddressSpace *pServerAddressSpace = OpcUa_Null;
OpcUa_BaseNode *pBaseNode = OpcUa_Null;
OpcUa_ObjectType *pEventType = OpcUa_Null;
OpcUa_Property *pProperty = OpcUa_Null;
OpcUa_InitializeStatus(OpcUa_Module_Server, "CustomProvider_RegisterEventTypes");
OpcUa_ReturnErrorIfArgumentNull(a_pStartingNodeId);
OpcUa_NodeId_Initialize(&parentEventTypeId);
OpcUa_NodeId_Initialize(&nodeId);
UaServer_AddressSpace_Get(0, &pServerAddressSpace);
/* HeaterSwitchedEventType */
a_pStartingNodeId->Identifier.Numeric++;
g_HeaterSwitchedEventTypeId = a_pStartingNodeId->Identifier.Numeric;
parentEventTypeId.NamespaceIndex = 0;
parentEventTypeId.Identifier.Numeric = OpcUaId_BaseEventType;
/* Register HeaterSwitchedEventType as new event type in the SDK */
uStatus = UaServer_Events_RegisterEventType(a_pStartingNodeId, &parentEventTypeId);
OpcUa_GotoErrorIfBad(uStatus);
/* Register the event fields of HeaterSwitchedEventType in the SDK */
/* SwitchPosition */
eventField.BrowsePath.NoOfElements = 1;
eventField.BrowsePath.Elements = (OpcUa_QualifiedName*)OpcUa_Alloc(sizeof(OpcUa_QualifiedName));
OpcUa_QualifiedName_Initialize(&eventField.BrowsePath.Elements[0]);
eventField.BrowsePath.Elements[0].NamespaceIndex = g_uCustomProvider_NamespaceIndex;
OpcUa_String_StrnCpy(&eventField.BrowsePath.Elements[0].Name,
OpcUa_String_FromCString("SwitchPosition"),
OPCUA_STRING_LENDONTCARE);
g_HeaterSwitchedEventType_IndexSwitchPosition = UaServer_Events_RegisterEventField(a_pStartingNodeId,
&eventField);
/* CurrentTemperature */
OpcUa_QualifiedName_Clear(&eventField.BrowsePath.Elements[0]);
eventField.BrowsePath.Elements[0].NamespaceIndex = g_uCustomProvider_NamespaceIndex;
OpcUa_String_StrnCpy(&eventField.BrowsePath.Elements[0].Name,
OpcUa_String_FromCString("CurrentTemperature"),
OPCUA_STRING_LENDONTCARE);
g_HeaterSwitchedEventType_IndexCurrentTemperature = UaServer_Events_RegisterEventField(a_pStartingNodeId,
&eventField);
/* Create nodes for HeaterSwitchedEventType in the address space */
nodeId.NamespaceIndex = 0;
nodeId.Identifier.Numeric = OpcUaId_BaseEventType;
UaServer_GetNode(pServerAddressSpace, &nodeId, &pBaseNode);
uStatus = UaServer_CreateObjectType(pAddressSpace,
&pEventType,
(OpcUa_BaseNode*)pBaseNode,
a_pStartingNodeId->Identifier.Numeric,
g_uCustomProvider_NamespaceIndex,
"HeaterSwitchedEventType");
OpcUa_GotoErrorIfBad(uStatus);
a_pStartingNodeId->Identifier.Numeric++;
uStatus = UaServer_CreateProperty(pAddressSpace,
&pProperty,
(OpcUa_BaseNode*)pEventType,
a_pStartingNodeId->Identifier.Numeric,
g_uCustomProvider_NamespaceIndex,
"SwitchPosition");
OpcUa_GotoErrorIfBad(uStatus);
OpcUa_Variable_SetDataType_Numeric(pProperty, OpcUaId_Boolean, 0);
a_pStartingNodeId->Identifier.Numeric++;
uStatus = UaServer_CreateProperty(pAddressSpace,
&pProperty,
(OpcUa_BaseNode*)pEventType,
a_pStartingNodeId->Identifier.Numeric,
g_uCustomProvider_NamespaceIndex,
"CurrentTemperature");
OpcUa_GotoErrorIfBad(uStatus);
OpcUa_Variable_SetDataType_Numeric(pProperty, OpcUaId_Double, 0);
OpcUa_ReturnStatusCode;
OpcUa_BeginErrorHandling;
OpcUa_FinishErrorHandling;
}
OpcUa_StatusCode CustomProvider_UnregisterEventTypes()
{
OpcUa_NodeId eventTypeId;
OpcUa_InitializeStatus(OpcUa_Module_Server, "CustomProvider_UnregisterEventTypes");
OpcUa_NodeId_Initialize(&eventTypeId);
/* HeaterSwitchedEventType */
eventTypeId.NamespaceIndex = g_uCustomProvider_NamespaceIndex;
eventTypeId.Identifier.Numeric = g_HeaterSwitchedEventTypeId;
uStatus = UaServer_Events_UnregisterEventType(&eventTypeId);
OpcUa_GotoErrorIfBad(uStatus);
OpcUa_ReturnStatusCode;
OpcUa_BeginErrorHandling;
OpcUa_FinishErrorHandling;
}
OpcUa_StatusCode CustomProvider_FireHeaterSwitchedEvent(Machine *a_pMachine)
{
UaServer_Event *pEvent = OpcUa_Null;
OpcUa_NodeId eventTypeId;
OpcUa_ByteString bsEventId;
OpcUa_Variant value;
OpcUa_InitializeStatus(OpcUa_Module_Server, "CustomProvider_FireHeaterSwitchedEvent");
OpcUa_ReturnErrorIfArgumentNull(a_pMachine);
OpcUa_NodeId_Initialize(&eventTypeId);
OpcUa_ByteString_Initialize(&bsEventId);
OpcUa_Variant_Initialize(&value);
eventTypeId.NamespaceIndex = g_uCustomProvider_NamespaceIndex;
eventTypeId.Identifier.Numeric = g_HeaterSwitchedEventTypeId;
pEvent = UaServer_Events_CreateEvent(&eventTypeId);
/* Set event fields of BaseEventType */
UaServer_Events_SetEventId(pEvent, &bsEventId);
UaServer_Events_SetEventType(pEvent, &eventTypeId);
UaServer_Events_SetSourceNode(pEvent, &a_pMachine->nodeId);
UaServer_Events_SetSourceName(pEvent, OpcUa_String_GetRawString(&a_pMachine->sMachineName));
UaServer_Events_SetTime(pEvent, OpcUa_DateTime_UtcNow());
UaServer_Events_SetReceiveTime(pEvent, OpcUa_DateTime_UtcNow());
UaServer_Events_SetLocalTime(pEvent, OpcUa_DateTime_UtcNow());
if (*a_pMachine->pHeaterSwitch->pValue == OpcUa_False)
{
UaServer_Events_SetMessage(pEvent, "en", "The switch was set to OFF");
}
else
{
UaServer_Events_SetMessage(pEvent, "en", "The switch was set to ON");
}
/* Set custom event fields */
value.Datatype = OpcUaType_Boolean;
value.Value.Boolean = *a_pMachine->pHeaterSwitch->pValue;
UaServer_Events_SetEventField(pEvent, g_HeaterSwitchedEventType_IndexSwitchPosition, &value);
value.Datatype = OpcUaType_Double;
value.Value.Double = *a_pMachine->pTemperatureSensor->pValue;
UaServer_Events_SetEventField(pEvent, g_HeaterSwitchedEventType_IndexCurrentTemperature, &value);
uStatus = UaServer_Events_FireEvent(pEvent);
OpcUa_ByteString_Clear(&bsEventId);
OpcUa_ReturnStatusCode;
OpcUa_BeginErrorHandling;
OpcUa_FinishErrorHandling;
}
OPCUA_END_EXTERN_C