local ua = require("opcua.api") local server = ua.newServer() server:initialize() -- Define a concrete application EventType derived from BaseEventType. local editor = server.model:edit() local temperatureEventType = editor:addEventType("TemperatureEventType") temperatureEventType:addProperty("Temperature", { Type = ua.VariantType.Double, Value = 0, }) -- Keep containment and Event routing explicit and independent. local plant = editor:addObject("Plant") local temperatureSource = plant:addObject("TemperatureSource") plant.Attrs.EventNotifier = ua.EventNotifierType.SubscribeToEvents temperatureSource.Attrs.EventNotifier = ua.EventNotifierType.SubscribeToEvents plant:addReference(temperatureSource, ua.ReferenceType.HasEventSource) editor:save() server:run() -- Call this function when the corresponding application condition occurs. local function emitTemperatureEvent(temperature) local event = { EventType = temperatureEventType.Attrs.NodeId, SourceNode = temperatureSource.Attrs.NodeId, SourceName = "Temperature monitor", Message = { Text = string.format("Temperature is %.1f C", temperature), }, Severity = temperature >= 80 and 700 or 200, Fields = { ["0:Temperature"] = { Type = ua.VariantType.Double, Value = temperature, }, }, } -- Early validation allows to catch programming errors before the event is emitted to clients, -- which may be harder to debug if the event rejected on later stages of the event processing pipeline. server:validateEvent(event) -- Emit the event to all subscribed clients. -- Note that the event is emitted asynchronously, so the function returns immediately. return server:emitEvent(event) end local eventId = emitTemperatureEvent(82.5) trace("Emitted TemperatureEventType Event " .. eventId) server:shutdown()