// This example shows how to subscribe to changes of a single item and display the value of the item with each change.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in Object Pascal (Delphi) on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-OP .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.
type
  TSubscribeItem_ClientEventHandlers = class
    // Item changed event handler
    procedure OnItemChanged(
      ASender: TObject;
      sender: OleVariant;
      const eventArgs: _EasyDAItemChangedEventArgs);
  end;
procedure TSubscribeItem_ClientEventHandlers.OnItemChanged(
  ASender: TObject;
  sender: OleVariant;
  const eventArgs: _EasyDAItemChangedEventArgs);
begin
  if eventArgs.Succeeded then
    WriteLn(eventArgs.Vtq.ToString)
  else
    WriteLn(Format('*** Failure: %s', [eventArgs.ErrorMessageBrief]));
end;
class procedure SubscribeItem.Main;
var
  Client: TEasyDAClient;
  ClientEventHandlers: TSubscribeItem_ClientEventHandlers;
begin
  // Instantiate the client object and hook events
  Client := TEasyDAClient.Create(nil);
  ClientEventHandlers := TSubscribeItem_ClientEventHandlers.Create;
  Client.OnItemChanged := ClientEventHandlers.OnItemChanged;
  Client.SubscribeItem('', 'OPCLabs.KitServer.2', 'Simulation.Random', 1000);
  WriteLn('Processing item changed events for 1 minute...');
  PumpSleep(60*1000);
  WriteLn('Unsubscribing...');
  Client.UnsubscribeAllItems;
  WriteLn('Waiting for 5 seconds...');
  PumpSleep(5*1000);
  WriteLn('Finished.');
  FreeAndNil(Client);
  FreeAndNil(ClientEventHandlers);
end;
	 
	
		// This example shows how to subscribe to changes of a single item and display the value of the item with each change.
//
// Some related documentation: http://php.net/manual/en/function.com-event-sink.php . Pay attention to the comment that says 
// "Be careful how you use this feature; if you are doing something similar to the example below, then it doesn't really make 
// sense to run it in a web server context.". What they are trying to say is that processing a web request should be 
// a short-lived code, which does not fit well with the idea of being subscribed to events and received them over longer time. 
// It is possible to write such code, but it is only useful when processing the request is allowed to take relatively long. Or, 
// when you are using PHP from command-line, or otherwise - not to serve a web page directly.
// 
// Subscribing to QuickOPC-COM events in the context of PHP Web application, while not imposing the limitations to the request 
// processing time, has to be "worked around", e.g. using the "event pull" mechanism.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in PHP on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-PHP .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.
class DEasyDAClientEvents {
    function ItemChanged($varSender, $varE)
    {
        if ($varE->Succeeded)
        {
        print $varE->Vtq->ToString();
            print "\n";
        }
        else
        {
            printf("*** Failure: %s\n", $varE->ErrorMessageBrief);
        }
    }
}
$Client = new COM("OpcLabs.EasyOpc.DataAccess.EasyDAClient");
$Events = new DEasyDAClientEvents();
com_event_sink($Client, $Events, "DEasyDAClientEvents");
$Client->SubscribeItem("", "OPCLabs.KitServer.2", "Simulation.Random", 1000);
print "Processing item changed events for 1 minute...\n";
$startTime = time(); do { com_message_pump(1000); } while (time() < $startTime + 60);
	 
	
		REM This example shows how to subscribe to changes of a single item and display the value of the item with each change.
REM
REM Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
REM OPC client and subscriber examples in Visual Basic on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VB .
REM Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
REM a commercial license in order to use Online Forums, and we reply to every post.
' The client object, with events
'Public WithEvents Client1 As EasyDAClient
Private Sub SubscribeItem_Main_Command_Click()
    OutputText = ""
    
    ' Instantiate the client object and hook events
    Set Client1 = New EasyDAClient
    OutputText = OutputText & "Subscribing..." & vbCrLf
    Call Client1.SubscribeItem("", "OPCLabs.KitServer.2", "Simulation.Random", 1000)
    OutputText = OutputText & "Processing item changed events for 1 minute..." & vbCrLf
    Pause 60000
    OutputText = OutputText & "Unsubscribing..." & vbCrLf
    Client1.UnsubscribeAllItems
    OutputText = OutputText & "Waiting for 5 seconds..." & vbCrLf
    Pause 5000
    OutputText = OutputText & "Finished." & vbCrLf
    Set Client1 = Nothing
End Sub
Public Sub Client1_ItemChanged(ByVal sender As Variant, ByVal eventArgs As EasyDAItemChangedEventArgs)
    If eventArgs.Succeeded Then
        OutputText = OutputText & eventArgs.vtq & vbCrLf
    Else
        OutputText = OutputText & "*** Failure: " & eventArgs.ErrorMessageBrief & vbCrLf
    End If
End Sub
	 
	
		Rem This example shows how to subscribe to changes of a single item and display the value of the item with each change.
Rem
Rem Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
Rem OPC client and subscriber examples in VBScript on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VBScript .
Rem Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
Rem a commercial license in order to use Online Forums, and we reply to every post.
Option Explicit
Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient")
WScript.ConnectObject Client, "Client_"
Client.SubscribeItem "", "OPCLabs.KitServer.2", "Simulation.Random", 1000
WScript.Echo "Processing item changed events for 1 minute..."
WScript.Sleep 60*1000
Sub Client_ItemChanged(Sender, e)
    If Not (e.Succeeded) Then
        WScript.Echo "*** Failure: " & e.ErrorMessageBrief
        Exit Sub
    End If
    WScript.Echo e.Vtq.ToString
End Sub