This directory contains a minimal example that shows how to use the AstarteDevice
.
The code provided in this sample includes comments that explicitly outline the key actions to be taken for setting up an Astarte device and sending data to Astarte.
If the device is already registered, skip this section.
The device must be registered beforehand to obtain its credentials-secret
.
To obtain it there are three ways:
- Using the astartectl command
astartectl
. - Using the
Astarte Dashboard
, which is located athttps://dashboard.<your-astarte-domain>
. - Using
AstartePairingService
class contained in sdk (see following paragraph for details).
The Astarte device id
can be generated either randomly or using a GUID namespace and some unique data.
String deviceId = AstarteDeviceIdUtils.GenerateId();
Guid nameSpace = Guid.Parse("f79ad91f-c638-4889-ae74-9d001a3b4cf8");
String macAddress = "98:75:a8:0d:96:db";
deviceId = AstarteDeviceIdUtils.GenerateId(nameSpace, macAddress);
The device can be registered either with JWT or with a private key file
In order to register a device, and obtain its credentials secret, a call to the registration API with a valid JWT is needed.
First generate JWT using astartectl:
astartectl utils gen-jwt pairing -k <private-key-file>.pem
Then use said token to register the device
String credentialsSecret = await new AstartePairingService(pairingUrl, realm).RegisterDeviceWithJwtToken(deviceId, jwt);
In order to register a device, and obtain its credentials secret, a call to the registration API with the use of the private key is needed.
String credentialsSecret = await new AstartePairingService(pairingUrl, realm).RegisterDeviceWithPrivateKey(deviceId, privateKeyFile);
AstarteDevice myDevice = new(
deviceId,
realm,
credentialsSecret,
interfaceProvider,
pairingUrl,
cryptoStoreDir);
await myDevice.Connect();
In order to operate with the device object safely, it is necessary to wait for the connection to be completed. This can be handled asynchronously in the Message Listener.
while (!device.IsConnected()) {
Thread.sleep(100);
}
Retrieve the interface from the device and call StreamData
on it.
AstarteDeviceDatastreamInterface valuesInterface =
(AstarteDeviceDatastreamInterface)myDevice.GetInterface(valuesInterfaceName);
while (true)
{
double value = Random.Shared.NextDouble();
Console.WriteLine("Streaming value: " + value);
valuesInterface.StreamData($"/{sensorUuid}/value", value, DateTime.Now);
Thread.Sleep(1000);
}
Retrieve the object aggregated interface and call Stream data
on it.
AstarteDeviceAggregateDatastreamInterface aggregateInterface =
(AstarteDeviceAggregateDatastreamInterface)myDevice
.GetInterface(geolocationInterfaceName)
while (true)
{
Dictionary<string, object> gpsValues = new()
{
{ "latitude", Random.Shared.NextDouble() * 50 },
{ "longitude", Random.Shared.NextDouble() * 50 },
{ "altitude", Random.Shared.NextDouble() },
{ "accuracy", Random.Shared.NextDouble() },
{ "altitudeAccuracy", Random.Shared.NextDouble() },
{ "heading", Random.Shared.NextDouble() },
{ "speed", Random.Shared.NextDouble() * 100}
};
Console.WriteLine("Streaming object:" + JsonConvert.SerializeObject(gpsValues));
aggregateInterface.StreamData($"/{sensor_id}", gpsValues, DateTime.Now);
Thread.Sleep(1000);
}
Retrieve the properties interface and call SetProperty
on it.
AstarteDevicePropertyInterface availableSensorsInterface =
(AstarteDevicePropertyInterface)myDevice
.GetInterface(availableSensorsInterfaceName);
availableSensorsInterface.SetProperty(
$"/{sensorUuid}/name", "randomThermometer");
availableSensorsInterface.SetProperty($"/{sensorUuid}/unit", "°C")
Retrieve the properties interface and call UnsetProperty
on it.
AstarteDevicePropertyInterface availableSensorsInterface =
(AstarteDevicePropertyInterface)myDevice
.GetInterface(availableSensorsInterfaceName);
availableSensorsInterface.UnsetProperty("/myPath/name");
The SDK offers an abstract class called AstarteGlobalEventListener
, which includes several interfaces for obtaining datastreams and properties from Astarte. Inheriting from AstarteGlobalEventListener
is essential to implement all of its methods.
public override void ValueReceived(AstarteDatastreamEvent e)
{
Console.WriteLine(
$"Received datastream value on interface { e.GetInterfaceName()}, " +
$"path: { e.GetPath()}, " +
$"value:{ e.GetValue()}");
}
public override void ValueReceived(AstarteAggregateDatastreamEvent e)
{
Console.WriteLine(
$"Received aggregate datastream object on interface {e.GetInterfaceName()}, " +
$"value:{JsonConvert.SerializeObject(e.GetValues())}");
}
public override void PropertyReceived(AstartePropertyEvent e)
{
Console.WriteLine(
$"Received property on interface { e.GetInterfaceName() }, " +
$"path: { e.GetPath() }, " +
$"value:{ e.GetValue() }");
}
public override void PropertyUnset(AstartePropertyEvent e)
{
Console.WriteLine(
$"Received unset on interface { e.GetInterfaceName() }, " +
$"path: { e.GetPath() }");
}
Run the code from the root of the example project with
dotnet run AstarteDeviceSDKExample.csproj --r "<realm>" --p "<pairing-url>" --t "<jwt>"
where pairing-url
is the URL to reach Pairing API in your Astarte instance, usually https://api.<your-astarte-domain>/pairing
.
Run the code from the root of the example project with
dotnet run AstarteDeviceSDKExample.csproj --r "<realm>" --p "<pairing-url>" --d "<device-id>" --c "<credential-secret>"
where pairing-url
is the URL to reach Pairing API in your Astarte instance, usually https://api.<your-astarte-domain>/pairing
.