|
Agent SDK is a Java library that allows to implement Agent in the form of Java application running on a PC or mobile device. That application mimics Agent based on a hardware controller by connecting to AggreGate Server via secure network connection and providing a number of settings (variables), operations (functions) and events.

|
In contast to a Java application that uses AggreGate Server API, application written using Agent SDK connects to AggreGate Server in "device mode". It emulates or implements a hardware device connected to AggreGate.
|

|
Agent can be also implemented using .NET API.
|
Java-based Agents are ideal for connecting PC-based equipment to AggreGate for monitoring and management. Some examples of devices that can run PC-based Agent:
| • | Remote Data Collection Stations |

|
Device Server SDK bundle includes an open-source example of Java-based Agent. It is a runnable Java class DemoAgent located in examples.agent package.
To try the demo agent, change server address/port/login/password in the constructor of RemoteLinkServer class run DemoAgent class. It should connect to the server and auto-register a new Device account.
|
Structure of Java-based Agent
Technically, Java-based Agent is a Java application or part of application that:
| • | Creates an instance of Agent class |
| • | Retrieves the implementation of Agent's Context using Agent.getContext() method |
| • | Configures the context by adding definitions of variables, functions and events. These are settings, operations and events "provided by the hardware device" in terms of AggreGate. |
| • | Provides custom VariableGetter and VariableSetter for every Agent setting. These classes implement custom logic of reading and writing setting values. |
| • | Provides custom FunctionImplementation for every Agent operation. These classes implement the logic of operations, i.e. processing the input and generating the output. |
| • | Connects to the AggreGate Server on startup or at any desired time and starts processing server commands. These commands will trigger variable reads/writes and function execution. |
| • | Asynchronously fires events in the Agent context using Context.fireEvent() method after server connection has been established. |
Implementing Java Agent
To implement your own Agent, add the following basic code to your java application:
| 1. | Create an instance of RemoteLinkServer and specify AggreGate Server address/port, name of user account to use for registration and Agent's password. |

|
In AggreGate Server API, the password field of RemoteLinkServer object is used as a password for the server user account.
However, Agent class uses password field as the password for the Agent's server-side Device account. It doesn't match the password of Agent owner's user account.
If Agent account with the name specified in Agent class constructor already exists and passwords of Agent account and Agent application do not match, Agent won't be able to log in to the server.
|
| 2. | Create an instance of Agent and specify name of Agent's Device account in the constructor. |
| 3. | Get an instance of Context using Agent.getContext() and add definitions/implementations of variables, functions, and events using addVariableDefinition(), addFunctionDefinition() and addEventDefinition() methods. |
| 4. | Call Agent.connect() to make your Agent connect to the AggreGate Server and authenticate/authorize. This call may fail for many reasons, including server unavailability, wrong server address/port, missing or disabled server-side Agent driver, incompatible Java libraries versions, missing user account, and incorrect Agent's device account password. |
| 5. | Start periodically calling Agent.run() method. It will server a single server command and return, so it should be basically called from a separate thread. At this point, your custom VariableGetter, VariableSetter and FunctionImplementation will be called. If run() method throws a DisconnectionException, server communication has failed for some reason and connection should be re-established. |
| 6. | Once Agent.getContext().isSynchronized() method will return true, your application may start generating Agent events using Agent.getContext().fireEvent() method. |
| 7. | To disconnect from the server and stop the Agent, call Agent.disconnect(). |

|
If some events are generated when Agent is not connected to the server or not yet synchronized, these events should be stored locally (in memory or some persistent storage) and sent to AggreGate Server upon first opportunity.
|
|