Sample Code lettest version of Dotnet Core SDK
# | Date | Name | Download |
---|---|---|---|
1 | 03/20/2020 | Symmetric Key Sample – Dotnet Core | Download |
2 | 03/20/2020 | Symmetric Key Gateway Sample – Dotnet Core | Download |
3 | 03/20/2020 | Self Sign Sample – Dotnet Core | Download |
4 | 03/20/2020 | Self Sign Gateway Sample – Dotnet Core | Download |
5 | 03/20/2020 | CA Sign Sample – Dotnet Core | Download |
6 | 03/20/2020 | CA Sign Gateway Sample – Dotnet Core | Download |
Sample Code lettest version of Dotnet Standard SDK
# | Date | Name | Download |
---|---|---|---|
1 | 03/20/2020 | Symmetric Key Sample – Dotnet Standard | Download |
2 | 03/20/2020 | Symmetric Key Gateway Sample – Dotnet Standard | Download |
3 | 03/20/2020 | Self Sign Sample – Dotnet Standard | Download |
4 | 03/20/2020 | Self Sign Gateway Sample – Dotnet Standard | Download |
5 | 03/20/2020 | CA Sign Sample – Dotnet Standard | Download |
6 | 03/20/2020 | CA Sign Gateway Sample – Dotnet Standard | Download |
How to build Dotnet sample code?
SDK zip file contains two Nuget packages(.Net Core 2.0 and .Net Standard Nuget package.)
Prerequisite tools:
- NET Core 2.0/.NET Framework
Installation:
- Open Visual Studio 2017, Create new .net core Console App
- Create new folder called Nuget and place IOTConnect SDK Nuget package
- Open Nuget Package Manager of Console App and click on Settings for Package source
- Add new Package source, name it as LocalPackage and in source give path of Nuget folder created in step #2
Public Methods and Implementations
In Program.cs add the namespace of IOTConnect SDK
using IOTConnectSDK;
Create an object of SDKClient
string cpId = "", uniqueId = ""; SDKClient client = new SDKClient(cpId, uniqueId, FirmwareDelegate, TwinUpdateCallBack);
Callback function to receive Cloud to Device message (C2D)
public static Task FirmwareDelegate(string message) { Console.WriteLine("Command Received."); Console.WriteLine(message); return Task.CompletedTask; }
Callback function to receive twin update
public static Task TwinUpdateCallBack(Dictionary<string, object> twins) { Console.WriteLine("Twin Update received."); foreach (var twin in twins) { Console.WriteLine($"{twin.Key}:{twin.Value}"); } return Task.CompletedTask; }
Sample data
[{ "cpId": "", //CPID of company "time": "", //Datetime Fromat: YYYY-MM-DDTHH:MM:SS.SSSSSSSS" "uniqueId": "", //Device ID "d": [{ //Telemetry data "voltage": 143.0, "current": 4.0, "frequency": 6.10, }] }]
To send the data from Device To Cloud(D2C)
client.SendData(File.ReadAllText(@"devicedata.json"));
Certificate Configuration
In order to set certificate and password you have to use bellow static properties:
SDKClient.SSL.Certificate = certiPath; SDKClient.SSL.Password = certiPass;
- SDKClient.SSL.Certificate : x509 certificate file path
- SDKClient.SSL.Password : Certificate password
To update the Twin Property
client.UpdateTwin("twin-property","new value");
To send the acknowledgment to cloud for Device command and Firmware OTA command
var commandData = JsonConvert.DeserializeObject(message); DeviceAckModel ackDetails = new DeviceAckModel() { AckId = commandData.AcknowledgeId, ChildId = "" }; //IOTConnect.Net SDK version < 2.0 bool isOta = (commandData != null && !string.IsNullOrWhiteSpace(commandData.Command) && commandData.Command.StartsWith(" ", StringComparison.CurrentCultureIgnoreCase)); //IOTConnect.Net SDK version >= 2.0 isOta = commandData.CommandType.Equals("0x02", StringComparison.CurrentCultureIgnoreCase); if (isOta) { //TODO : download and save ota file from url ackDetails.Msg = "OTA updated successfully."; ackDetails.Status = 7; } else { ackDetails.Msg = "Device command received successfully."; ackDetails.Status = 6; } var dataOta = JsonConvert.SerializeObject(ackDetails); //NOTE: SendAck method have different arguments as per the SDK version. Choose accordingly. //IOTConnect.Net SDK version < 2.0 client.SendAck(dataOta, isOta ? 11 : 5); //IOTConnect.Net SDK version >= 2.0 client.SendAck(dataOta, "2020-02-14T12:19:33.7769262Z", isOta ? 11 : 5); dataOta:- { "AcknowledgeId" : , #you will received at callback() method "Status" : 7, #its status 6= for commands and 7 for firmware(OTA) "Msg" : "", #Msg(optional) its used for send your custom message "childId" : "" # childId(optional) its used when you have gateway template } current_time:- '2018-05-24T10:06:17.857Z', //Date format should be as defined mt:- 11 # for firmware update.. 5 # for command ack.