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();
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 condig
// set device config
// set tag config
bool result = _edgeAgent.UploadConfig( ActionType.Create, config ).Result;
Node Config:
config.Node = new EdgeConfig.NodeConfig();
Device Config:
EdgeConfig.DeviceConfig device = new EdgeConfig.DeviceConfig()
{
Id = "Device1",
Name = "Device1",
Type = "Smart Device 1",
Description = "Device 1"
};
Analog Tag Config:
EdgeConfig.AnalogTagConfig analogTag = new EdgeConfig.AnalogTagConfig()
{
Name = "Volt",
Description = "Volt",
ReadOnly = false,
ArraySize = 0
SpanHigh = 1000,
SpanLow = 0,
EngineerUnit = "V",
IntegerDisplayFormat = 4,
FractionDisplayFormat = 2,
SendWhenValueChanged = true // Data will only be sent when the value changed
};
Discrete Tag Config:
EdgeConfig.DiscreteTagConfig discreteTag = new EdgeConfig.DiscreteTagConfig()
{
Name = "DTag",
Description = "DTag" + j,
ReadOnly = false,
ArraySize = 0
State0 = "0",
State1 = "1",
State2 = "",
State3 = "",
State4 = "",
State5 = "",
State6 = "",
State7 = "",
SendWhenValueChanged = true // Data will only be sent when the value changed
};
Text Tag Config:
EdgeConfig.TextTagConfig textTag = new EdgeConfig.TextTagConfig()
{
Name = "Text",
Description = "Text",
ReadOnly = false,
ArraySize = 0,
SendWhenValueChanged = true // Data will only be sent when the value changed
};
Block Config
Create block config:
EdgeConfig.AnalogTagConfig analogTag = new EdgeConfig.AnalogTagConfig()
{
Name = "ATag",
Description = "ATag",
ReadOnly = false,
ArraySize = 0,
SpanHigh = 1000,
SpanLow = 0,
EngineerUnit = "V",
IntegerDisplayFormat = 4,
FractionDisplayFormat = 2,
SendWhenValueChanged = true // Data will only be sent when the value changed
};
EdgeConfig.DiscreteTagConfig discreteTag = new EdgeConfig.DiscreteTagConfig()
{
Name = "DTag",
Description = "DTag",
ReadOnly = false,
ArraySize = 0,
State0 = "0",
State1 = "1",
SendWhenValueChanged = true // Data will only be sent when the value changed
};
EdgeConfig.TextTagConfig textTag = new EdgeConfig.TextTagConfig()
{
Name = "TTag",
Description = "TTag",
ReadOnly = false,
ArraySize = 0,
SendWhenValueChanged = true // Data will only be sent when the value changed
};
var blockConfig = new EdgeConfig.BlockConfig("Pump");
blockConfig.AnalogTagList.Add(analogTag);
blockConfig.DiscreteTagList.Add(discreteTag);
blockConfig.TextTagList.Add(textTag);
This block config contain 3 tag, and the block type is Pump, using this block config create two block:
device.AddBlock("Pump01", blockConfig);
device.AddBlock("Pump02", blockConfig);
This will add 6 tag to your device, the first 3 tag will have a prefix of Pump01 and the last 3 tag will have a prefix of Pump02, for example:
SendData(EdgeData data)
Send the tag value to the cloud.
Random random = new Random();
EdgeData data = new EdgeData();
for (int i = 1; i T is defined according to the tag type (Analog: double, Discrete: int, Text: string).
```c#
// analog array tag
Dictionary dicVal = new Dictionary();
dicVal.Add("0", 0.5); // The tag index is 0 and the value is 0.5
dicVal.Add("1", 1.42); // The tag index is 1 and the value is 1.42
dicVal.Add("2", 2.89); // The tag index is 2 and the value is 2.89
EdgeData.Tag aTag = new EdgeData.Tag()
{
DeviceId = "Device1",
TagName = "ATag",
Value = dicVal
};
// discrete array tag
Dictionary dicVal = new Dictionary();
dicVal.Add("0", 0); // The tag index is 0 and the value is 0
dicVal.Add("1", 1); // The tag index is 1 and the value is 1
dicVal.Add("2", 2); // The tag index is 2 and the value is 2
EdgeData.Tag dTag = new EdgeData.Tag()
{
DeviceId = "Device1",
TagName = "DTag",
Value = dicVal
};
// text array tag
Dictionary dicVal = new Dictionary();
dicVal.Add("0", "zero"); // The tag index is 0 and the value is "zero"
dicVal.Add("1", "one"); // The tag index is 1 and the value is "one"
dicVal.Add("2", "two"); // The tag index is 2 and the value is "two"
EdgeData.Tag tTag = new EdgeData.Tag()
{
DeviceId = "Device1",
TagName = "TTag",
Value = dicVal
};
Send the tag values in batch to the cloud.
HashSet dataset = new HashSet();
for ( int n = 1; n <= 10; n++ )
{
Random random = new Random();
EdgeData data = new EdgeData();
for ( int i = 1; i <= 1; i++ )
{
for ( int j = 1; j <= 5; j++ )
{
EdgeData.Tag aTag = new EdgeData.Tag()
{
DeviceId = "Device" + i,
TagName = "ATag" + j,
Value = random.NextDouble()
};
EdgeData.Tag dTag = new EdgeData.Tag()
{
DeviceId = "Device" + i,
TagName = "DTag" + j,
Value = j % 2
};
EdgeData.Tag tTag = new EdgeData.Tag()
{
DeviceId = "Device" + i,
TagName = "TTag" + j,
Value = "TEST " + j.ToString()
};
data.TagList.Add( aTag );
data.TagList.Add( dTag );
data.TagList.Add( tTag );
}
}
dataset.Add( data );
}
bool result = edgeAgent.SendData( dataset ).Result;
SendDeviceStatus (EdgeDeviceStatus deviceStatus)
Send the device status to the cloud when the status changes.
EdgeDeviceStatus deviceStatus = new EdgeDeviceStatus();
for (int i = 1; i <= 2; i++)
{
EdgeDeviceStatus.Device device = new EdgeDeviceStatus.Device()
{
Id = "Device" + i,
Status = Status.Online
};
deviceStatus.DeviceList.Add(device);
}
deviceStatus.Timestamp = DateTime.Now;
bool result = edgeAgent.SendDeviceStatus(deviceStatus).Result;