Java Edge SDK Manual
October 21, 2024
Environment
-
Runtime
- Java 8 or higher (Java 8 is recommended)
-
Installation
-
Source
-
Download
- Maven Central
io.github.edgehub-repo
EdgeSync360.EdgeHub.Edge.Java.SDK
1.0.2- Gradle
dependencies {
implementation 'io.github.edgehub-repo:EdgeSync360.EdgeHub.Edge.Java.SDK:1.0.2'
}
-
EdgeAgent
Constructor (EdgeAgentOptions options)
New EdgeAgent object
EdgeAgentOptions options = new EdgeAgentOptions();
options.ConnectType = ConnectType.DCCS; // Connection type (DCCS, MQTT). The default setting is DCCS.
// If connectType is DCCS, the following options must be input:
options.DCCS = new DCCSOptions("", "");
If connectType is MQTT, the following options must be input:
options.MQTT = new MQTTOptions("127.0.0.1", 1883, "admin", "pwd", Protocol.TCP);
options.UseSecure = false;
options.AutoReconnect = true;
options.NodeID = ""; // Obtain from portal
options.Type = EdgeType.Gateway; // Configure the edge as a Gateway or Device. The default setting is Gateway.
options.DeviceID = "SmartDevice1"; // If the Type is Device, the DeviceID must be input.
options.Heartbeat = 60000; // The default is 60 seconds.
options.DataRecover = true; // Whether to recover data when disconnected
// If connectType is AzureIoTHub, the following options must be input:
// EdgeLink
options.AzureIoTHub.ConnectionString = "" ;
// SCADA
options.AzureIoTHub.HostName = "";
options.AzureIoTHub.SASToken = "";
EdgeAgent edgeAgent = new EdgeAgent(options, agentListener);
Azure IoT Hub
Due to the limitations of the Azure IoT Hub SDK, the Java SDK supports connecting to the Azure IoT Hub either via a connection string or SAS token. Therefore, when using an EdgeLink device, the connection is established through the connection string. On the other hand, when using a SCADA device, the connection is made using the hostname and SAS token
Event
EdgeAgent has three subscription events.
- Connected: When EdgeAgent is connected to the IoTHub.
- Disconnected: When EdgeAgent is disconnected from the IoTHub.
- MessageReceived: When EdgeAgent receives an MQTT message from the cloud. The message types are as follows:
- WriteValue: Change the tag value from the cloud.
- TimeSync: Returns the current time from the cloud.
- ConfigAck: Response to config uploads from the edge to the cloud.
EdgeAgentListener agentListener = new EdgeAgentListener() {
@Override
public void Connected(EdgeAgent agent, EdgeAgentConnectedEventArgs args) {
System.out.println("Connected");
}
@Override
public void Disconnected(EdgeAgent agent, DisconnectedEventArgs args) {
System.out.println("Disconnected");
}
@Override
public void MessageReceived(EdgeAgent agent, MessageReceivedEventArgs e) {
System.out.println("MessageReceived");
switch (e.Type) {
case Const.MessageType.WriteValue:
WriteValueCommand wvcMsg = (WriteValueCommand) e.Message;
for (WriteValueCommand.Device device: wvcMsg.DeviceList) {
System.out.println("DeviceID:" + device.ID);
for (WriteValueCommand.Tag tag: device.TagList) {
System.out.printf("TagName: %s, Value: %s\n", tag.Name, tag.Value.toString());
}
}
break;
case Const.MessageType.TimeSync:
TimeSyncCommand tscMsg = (TimeSyncCommand) e.Message;
System.out.println("UTC Time:" + tscMsg.UTCTime.toString());
break;
case Const.MessageType.ConfigAck:
ConfigAck cfgAckMsg = (ConfigAck) e.Message;
String result = cfgAckMsg.Result.toString();
break;
}
}
};
EdgeAgent edgeAgent = new EdgeAgent(options, agentListener);
Connect()
Connect to the IoTHub. When successfully connected, a connected event will be triggered.
edgeAgent.Connect();
Disconnect()
Disconnect from the IoTHub. When successfully disconnected, a disconnected event will be triggered.
edgeAgent.Disconnect();
UploadConfig(ActionType action, EdgeConfig edgeConfig)
Upload Node/Device/Tag Config with Action Type (Create/Update/Delete).
EdgeConfig config = new EdgeConfig();
// set node config
// set device config
// set tag config
Boolean result = agent.UploadConfig(Const.ActionType.Create, config);
Node Config:
config.Node = new EdgeConfig.NodeConfig();
Device Config:
EdgeConfig.DeviceConfig device = new EdgeConfig.DeviceConfig();
device.ID = "Device1";
device.Name = "Device1";
device.Type = "Smart Device 1";
device.Description = "Device1";