Subscribing to Events
Your form can respond to published events.
For example, a time event can be set up to recur every 30 seconds, and the form display the time of the most recent event. The code is as follows:
Imports System
Imports System.Collections.Generic
Imports System.Windows
Imports Proficy.Platform.Core.Cdo.Connections
Imports Proficy.Platform.Core.ClientTools.ServiceDirectory
Imports Proficy.Platform.Core.ProficySystem.MobileObject
Imports Proficy.Platform.Core.ProficySystem.Types
Imports Proficy.Platform.Samples.Forms.Base
Public Class EventVB
Inherits BaseFormVB.Proficy.Platform.Samples.Forms.Base.VB.BaseFormVB
Public Overloads Overrides Sub TransferDataIn()
' The published Time Event to subscribe to is coming from Workflow via an input parameter in this case.
SubscribeToEvent(_directoryResource.LdapAddress )
End Sub
Private Sub SubscribeToEvent(ByVal eventLdapAddress As String)
Dim userStartup As UserStartup = userStartup.[Default]
Dim serviceProvider As IServiceProvider = New BasicServicesProvider(userStartup.ServiceDirectory)
Dim subscription As New DirectoryAddressableSubscription(serviceProvider, eventLdapAddress)
' Note: DirectoryAddressableSubscription has been renamed to Subscription
AddHandler subscription.EventOccurred, AddressOf subscription_EventOccurred
subscription.Subscribe()
End Sub
Private Sub subscription_EventOccurred(ByVal sender As Object, ByVal e As EventOccurredEventArgs)
' This is called when the event occurs, and the EventArgs may be used to adjust the
' values of display objects, or to set output parameter values. In this case we have a
' recurring event,and we update a text block with the time of the latest event, changing
' color alternately between red and blue.
Dim newText As String = DateTime.Now.ToString()
EventTimeText.Text = newText
If EventTimeText.ForeColor = System.Drawing.Color.Red Then
EventTimeText.ForeColor = System.Drawing.Color.Blue
Else
EventTimeText.ForeColor = System.Drawing.Color.Red
End If
EventTimeText.Refresh()
End Sub
[InputProperty("EventToSubscribe", "Event to Subscribe to", "The time event to subscribe to")]
protected DirectoryResource _directoryResource = null;
End Class
This technique can be generalized for any item that can be subscribed to; however, the EventOccurredEventArgs will contain different information depending on the type of event. In the above example, this information is ignored, but in general the information can be extracted and displayed in the form.
Common subscribable items are:
- equipment properties
- time events
- condition events
- new work request events
- work request status changed events
- workflow task expired events
- workflow task step expired events
In each case the LDAP address of the item must be located in the DirectoryViewer, and then copied and pasted as an argument into the SubscribeToEvent call.
Display Equipment Property Changes ExamplePrivate Sub subscription1_EventOccurred(ByVal sender As Object, ByVal e As EventOccurredEventArgs)
Dim message As MobileMessage = e.EventMessage
Dim dataItemEvent As DataItemChangedEvent = TryCast(message.ObjectInstance, DataItemChangedEvent)
Dim newValue As ProcessValue = dataItemEvent.NewValue
SomeLabel.Text = newValue.ToString()
End Sub
Display Time Event Data Example
Private Sub subscription1_EventOccurred(ByVal sender As Object, ByVal e As EventOccurredEventArgs)
Dim eventInfo As TimeEvent = TryCast(e.EventData("EventData").GetObject(), TimeEvent)
SomeLabel.Text = eventInfo.Description
End Sub