Go Edge SDK Manual
October 21, 2024
Environment
Installation
- Via the command line:
go get github.com/EdgeHub-Repo/dc-edge-sdk-golang
EdgeAgent
Constructor (EdgeAgentOptions options)
New EdgeAgent object.
options := agent.NewEdgeAgentOptions() // Set default value
options.NodeID = "" // Get from portal
options.DeviceID = "" // If the type is Device, the DeviceID must be entered.
options.Type = agent.EdgeType["Gateway"] // Configure the edge as a Gateway or Device. The default setting is Gateway.
options.ReconnectInterval = 1, // The MQTT reconnect interval in seconds. The default setting is 1.
options.HeartBeatInterval = 60 // The default setting is 60 seconds.
options.DataRecover = true // Whether to recover data when disconnected. The default setting is True.
options.ConnectType = agent.ConnectType["DCCS"] // Connection type (DCCS, MQTT,AzureIoTHub). The default setting is DCCS.
options.UseSecure = false
options.MQTT = &agent.MQTTOptions { // If connectType is MQTT, the following options must be input:
HostName: "127.0.0.1",
Port: 1883,
UserName: "admin",
Password: "admin",
ProtocolType: Protocol["TCP"], // MQTT protocol (TCP, WebSocket). The default setting is TCP.
}
options.DCCS = &agent.DCCSOptions { // If connectType is DCCS, the following options must be input:
URL: "", // DCCS API URL
Key: "", // Credential key
}
options.AzureIoTHubOptions = &agent.AzureIoTHubOptions { // If connectType is AzureIoTHub, the following options must be input:
ConnectionString : "",
}
edgeAgent := agent.NewAgent(options)
Azure IoT Hub
Due to the connection limit of the Azure IoT Hub SDK, the GO SDK can only connect to the Azure IoT Hub using a connection string. Therefore, you can only connect to the Edge Hub using a EdgeLink device.
Event
EdgeAgent has three subscription events.
- Connected: When EdgeAgent is connected to the IoTHub.
- Disconnected: When EdgeAgent is disconnected to 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.
- ConfigAck: Response to a config upload from the edge to the cloud.
edgeAgent.SetOnConnectHandler(onConnectHandler)
edgeAgent.SetOnDisconnectHandler(onDisconnectHandler)
edgeAgent.SetOnMessageReceiveHandler(onMessageReceiveHandler)
func onConnectHandler (a agent.Agent) {
fmt.Println("Connect success")
}
func onDisconnectHandler (a agent.Agent) {
fmt.Println("Connect success")
}
func onMessageReceiveHandler (args agent.MessageReceivedEventArgs) {
msgType := args.Type
msg := args.Message
switch msgType {
case MessageType["WriteValue"]: // message format: WriteDataMessage
for _, device := range message.(agent.WriteDataMessage).DeviceList {
fmt.Println("DeviceID:", device.ID)
for _, tag := range device.TagList {
fmt.Println("TagName:", tag.Name, "Value:", tag.Value)
}
}
case MessageType["ConfigAck"]: // message format: ConfigAckMessage
fmt.Println(message.(agent.ConfigAckMessage).Result)
}
}
Connect ()
Connect to the IoTHub. When successfully connected, a connected event will be triggered.
edgeAgent.Connect()
Disconnect ()
Disconnect from the IoTHub. When successfully disconnected success, a disconnected event will be triggered.
edgeAgent.disconnect()
UploadConfig (ActionType action, EdgeConfig edgeConfig)
Upload Node/Device/Tag Config with Action Type (Create/Update/Delete).
config = agent.EdgeConfig()
result = edgeAgent.UploadConfig(agent.Action["Create"], config)