/devicepilot-api-net

A .NET API client for the Device Pilot analytics system

Primary LanguageC#MIT LicenseMIT

GitHub license GitHub issues GitHub stars GitHub forks GitHub forks

devicepilot-api

A .NET API client for the Device Pilot IoT analytics platform. Open permissive MIT license and requires a minimum of .NET Standard 1.3.

Getting Started

NuGet Status

You can install the package using either the CLI:

dotnet add package DevicePilot.Api

or from the NuGet package manager:

Install-Package DevicePilot.Api

Usage

Create an instance of the API client and configure your authentication token, you can find this on the API keys page of the platform.

ApiClient client = new ApiClient(Environment.GetEnvironmentVariable("DEVICEPILOT_TOKEN"));

The client will automatically retry each request 3 times if a transient error occurs.

Ingesting

You can start ingesting data immediately after creating the client, you can ingest either a single device update or as many as you want. If you ingest more than one device the batch API will be used, if you pass more than 500 devices the client will split up the request automatically.

var devices = new DeviceData[] {
	new DeviceData() {
		Id = "switch1",
		Properties = new Dictionary<string, object>() {
			{ "name", "Switch 1" },
			{ "isOn", true },
			{ "longitude", 53.7005d },
			{ "latitude", 2.3015d }
		}
	},
	new DeviceData() {
		Id = "switch2",
		Properties = new Dictionary<string, object>() {
			{ "name", "Switch 2" },
			{ "isOn", false },
			{ "longitude", 53.7005d },
			{ "latitude", 2.3015d }
		}
	}
};

await client.BulkIngestAsync(devices);

Mapped types

Optionally you can use .NET objects to represent your device data structure. You can use either client.IngestAsync<T> or client.BulkIngestAsync<T>.

class MyDevice 
{
	[DeviceId]
	public string ID { get; set; }

	[DeviceProperty("online")]
	public bool Online { get; set; }

	[DeviceProperty]
	public double Temperature { get; set; }

	[DeviceTimestamp]
	public DateTime? Timestamp { get; set; }
}
await client.IngestAsync<MyDevice>(new MyDevice() {
	ID = "temp1",
	Online = true,
	Temperature = 16.21d
});

Contributing

Any pull requests or bug reports are welcome, please try and keep to the existing style conventions and comment any additions.