C# Edge SDK Manual
October 21, 2024
Environment
- IDE
- Visual Studio, version 2022 and above
- Runtime
- .Net 8
- NuGet Package
Installing the NuGet package for your project
dotnet add package EdgeSync360.EdgeHub.Edge.DotNet.SDK
EdgeAgent
Constructor (EdgeAgentOptions options)
New EdgeAgent object.
EdgeAgentOptions options = new EdgeAgentOptions()
{
ConnectType = ConnectType.DCCS, // Connection type (DCCS, MQTT). The default is DCCS.
DCCS = new DCCSOptions() // If ConnectType is DCCS, the following options must be entered:
{
CredentialKey = "", // Credential Key
APIUrl = "" // DCCS API Url
},
MQTT = new MQTTOptions() // If ConnectType is MQTT, the following options must be entered:
{
HostName = "127.0.0.1",
Port = 1883,
Username = "admin",
Password = "admin",
ProtocolType = Protocol.TCP
},
AzureIoTHub = new AzureIoTHubOptions() // if ConnectType is AzureIoTHub, the following options must be entered:
{
HostName = "",
SASToken = ""
},
UseSecure = false,
AutoReconnect = true,
ReconnectInterval = 1000,
NodeId = "5095cf13-f005-4c81-b6c9-68cf038e2b87", // Get from portal
Type = EdgeType.Gateway, // Configure the edge type as Gateway or Device. The default is Gateway.
DeviceId = "SmartDevice1", // If the type is Device, the DeviceId must be entered.
Heartbeat = 60000, // The default is 60 seconds.
DataRecover = true // Configure whether to recover data when disconnected.
};
EdgeAgent edgeAgent = new EdgeAgent(options);
Azure IoT Hub
Due to the connection limit of the Azure IoT Hub SDK, the C# SDK can only connect to the Azure IoT Hub using a SASToken. Therefore, you can only connect to the Edge Hub using a SCADA device.
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.
- WriteConfig: Change the config from the cloud.
- ConfigAck: Response to a config upload from the edge to the cloud.
edgeAgent.Connected += edgeAgent_Connected;
edgeAgent.Disconnected += edgeAgent_Disconnected;
edgeAgent.MessageReceived += edgeAgent_MessageReceived;
private void edgeAgent_Connected(object sender, EdgeAgentConnectedEventArgs e)
{
// Connected
Console.WriteLine("Connect success!");
}
private void edgeAgent_Disconnected(object sender, DisconnectedEventArgs e)
{
// Disconnected
Console.WriteLine("Disconnected.");
}
private void edgeAgent_MessageReceived(object sender, MessageReceivedEventArgs e)
{
switch (e.Type)
{
case MessageType.WriteValue:
WriteValueCommand wvcMsg = (WriteValueCommand) e.Message;
foreach (var device in wvcMsg.DeviceList)
{
Console.WriteLine("DeviceId: {0}", device.Id);
foreach (var tag in device.TagList)
{
Console.WriteLine("TagName: {0}, Value: {1}", tag.Name, tag.Value.ToString() );
}
}
break;
case MessageType.WriteConfig:
break
case MessageType.ConfigAck:
ConfigAck cfgAckMsg = (ConfigAck) e.Message;
Console.WriteLine("Upload Config Result: {0}", cfgAckMsg.Result.ToString() );
break;
}
}
Connect()
Connect to the IoTHub. When successfully connected, a connected event will be triggered.
edgeAgent.Connect();