Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions AssetTrackerDemo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.1.31911.260
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssetTrackerDemo", "AssetTrackerDemo\AssetTrackerDemo.csproj", "{252E24A5-0AC7-4AE7-9CD8-195F7A562D71}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssetTrackerDevice", "AssetTrackerDevice\AssetTrackerDevice.csproj", "{AFF7C0F0-3529-45C5-9238-598BD0AACCF1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{252E24A5-0AC7-4AE7-9CD8-195F7A562D71}.Debug|Any CPU.Build.0 = Debug|Any CPU
{252E24A5-0AC7-4AE7-9CD8-195F7A562D71}.Release|Any CPU.ActiveCfg = Release|Any CPU
{252E24A5-0AC7-4AE7-9CD8-195F7A562D71}.Release|Any CPU.Build.0 = Release|Any CPU
{AFF7C0F0-3529-45C5-9238-598BD0AACCF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AFF7C0F0-3529-45C5-9238-598BD0AACCF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AFF7C0F0-3529-45C5-9238-598BD0AACCF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AFF7C0F0-3529-45C5-9238-598BD0AACCF1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
19 changes: 19 additions & 0 deletions AssetTrackerDevice/AssetTrackerDevice.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
<Nullable>disable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-AssetTrackerDevice-FE8FBA84-5816-4675-B918-12C1B9B0838C</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Rido.IoTClient" Version="0.0.1-pre" />
</ItemGroup>
<ItemGroup>
<Compile Update="dtmi_assettrackerdemo_xfassettrackert0-1.g.cs">
<DependentUpon>XFAssetTracker.json</DependentUpon>
</Compile>
</ItemGroup>
</Project>
67 changes: 67 additions & 0 deletions AssetTrackerDevice/DeviceRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using dtmi_assettrackerdemo;
using Rido.IoTClient;
using Rido.IoTClient.AzIoTHub;

namespace AssetTrackerDevice
{
public class DeviceRunner : BackgroundService
{
private readonly ILogger<DeviceRunner> _logger;
private readonly IConfiguration _configuration;

public DeviceRunner(ILogger<DeviceRunner> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}

const int default_interval = 5;

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var locator = new Windows.Devices.Geolocation.Geolocator();
var client = await xfassettrackert0.CreateAsync(_configuration.GetConnectionString("cs"), stoppingToken);

client.Command_Reboot.OnCmdDelegate = async (cmd) =>
{
_logger.LogInformation("Command Reboot received");
return await Task.FromResult(new Rido.IoTClient.EmptyCommandResponse());
};

client.Property_Interval.OnProperty_Updated = async p =>
{
var ack = new PropertyAck<int>(p.Name)
{
Value = p.Value,
Status = 200,
Version = p.Version,
Description = "property accepted"
};
client.Property_Interval.PropertyValue.Value = p.Value;
return await Task.FromResult(ack);
};

await client.Property_Interval.InitPropertyAsync(client.InitialState, default_interval, stoppingToken);

client.Property_FrameworkVersion.PropertyValue = Environment.Version.ToString();
client.Property_Manufacturer.PropertyValue = "Contoso";
client.Property_SDKVersion.PropertyValue = (typeof(IoTHubPnPClient).Assembly.GetName().Version.ToString());

await client.ReportPropertyAsync(client.AllReadOnlyProperties, stoppingToken);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wickste would this work?

we might need a same approach to send multiple telemetries in one message


while (!stoppingToken.IsCancellationRequested)
{
var location = await locator.GetGeopositionAsync();
await client.Telemetry_Location.SendTelemetryAsync(new Location
{
lat = location.Coordinate.Latitude,
lon = location.Coordinate.Longitude,
alt = location.Coordinate.Altitude.Value

});
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
await Task.Delay(client.Property_Interval.PropertyValue.Value * 1000, stoppingToken);
}
}
}
}
10 changes: 10 additions & 0 deletions AssetTrackerDevice/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using AssetTrackerDevice;

IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddHostedService<DeviceRunner>();
})
.Build();

await host.RunAsync();
12 changes: 12 additions & 0 deletions AssetTrackerDevice/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"AssetTrackerDevice": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development",
"ConnectionStrings__cs": "IdScope=0ne0047D00B;DeviceId=rido-surfaceBook;SharedAccessKey=t0AfrCo0jeQo8pad4bvYhBv6BpCoqun7dYeXlaYj+WU="
}
}
}
}
80 changes: 80 additions & 0 deletions AssetTrackerDevice/XFAssetTracker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[
{
"@id": "dtmi:assetTrackerDemo:XFAssetTrackert0;1",
"@type": "Interface",
"contents": [
{
"@id": "dtmi:assetTrackerDemo:XFAssetTrackert0:Location;1",
"@type": [
"Telemetry",
"Location"
],
"displayName": {
"en": "Location"
},
"name": "Location",
"schema": "geopoint"
},
{
"@id": "dtmi:assetTrackerDemo:XFAssetTrackert0:Interval;1",
"@type": [
"Property",
"TimeSpan"
],
"displayName": {
"en": "Interval"
},
"name": "Interval",
"schema": "integer",
"unit": "second",
"writable": true
},
{
"@id": "dtmi:assetTrackerDemo:XFAssetTrackert0:Reboot;1",
"@type": "Command",
"commandType": "synchronous",
"displayName": {
"en": "Reboot"
},
"name": "Reboot"
},
{
"@id": "dtmi:assetTrackerDemo:XFAssetTrackert0:FrameworkVersion;1",
"@type": "Property",
"displayName": {
"en": "FrameworkVersion"
},
"name": "FrameworkVersion",
"schema": "string",
"writable": false
},
{
"@id": "dtmi:assetTrackerDemo:XFAssetTrackert0:SDKVersion;1",
"@type": "Property",
"displayName": {
"en": "SDKVersion"
},
"name": "SDKVersion",
"schema": "string",
"writable": false
},
{
"@id": "dtmi:assetTrackerDemo:XFAssetTrackert0:Manufacturer;1",
"@type": "Property",
"displayName": {
"en": "Manufacturer"
},
"name": "Manufacturer",
"schema": "string",
"writable": false
}
],
"displayName": {
"en": "XFAssetTracker"
},
"@context": [
"dtmi:iotcentral:context;2",
"dtmi:dtdl:context;2"
]
}
]
8 changes: 8 additions & 0 deletions AssetTrackerDevice/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
8 changes: 8 additions & 0 deletions AssetTrackerDevice/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
56 changes: 56 additions & 0 deletions AssetTrackerDevice/dtmi_assettrackerdemo_xfassettrackert0-1.g.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// <auto-generated />

using MQTTnet.Client;
using Rido.IoTClient;
using Rido.IoTClient.AzIoTHub;
using Rido.IoTClient.AzIoTHub.TopicBindings;
using System.Reflection.Metadata;

namespace dtmi_assettrackerdemo
{
public class xfassettrackert0 : IoTHubPnPClient
{
const string modelId = "dtmi:assetTrackerDemo:XFAssetTrackert0;1";

public Telemetry<Location> Telemetry_Location;
public WritableProperty<int> Property_Interval;
public Command<EmptyCommandRequest, EmptyCommandResponse> Command_Reboot;
public ReadOnlyProperty<string> Property_FrameworkVersion;
public ReadOnlyProperty<string> Property_SDKVersion;
public ReadOnlyProperty<string> Property_Manufacturer;

public xfassettrackert0(IMqttClient c) : base(c)
{
Property_Interval = new WritableProperty<int>(c, "Interval");
Telemetry_Location = new Telemetry<Location>(c, "Location");
Command_Reboot = new Command<EmptyCommandRequest, EmptyCommandResponse>(c, "Reboot");
Property_FrameworkVersion = new ReadOnlyProperty<string>(c, "FrameworkVersion");
Property_SDKVersion = new ReadOnlyProperty<string>(c, "SDKVersion");
Property_Manufacturer = new ReadOnlyProperty<string>(c, "Manufacturer");
}

public static async Task<xfassettrackert0> CreateAsync(string connectionString, CancellationToken cancellationToken)
{
var cs = new ConnectionSettings(connectionString) { ModelId = modelId };
var client = new xfassettrackert0(await IoTHubConnectionFactory.CreateAsync(cs, cancellationToken)) { ConnectionSettings = cs };
client.InitialState = await client.GetTwinAsync(cancellationToken);
return client;
}

public Dictionary<string, object> AllReadOnlyProperties => new Dictionary<string, object>()
{
{ Property_FrameworkVersion.Name, Property_FrameworkVersion.PropertyValue },
{ Property_SDKVersion.Name, Property_SDKVersion.PropertyValue },
{ Property_Manufacturer.Name, Property_Manufacturer.PropertyValue }
};


}

public struct Location
{
public double lat { get; set; }
public double lon { get; set; }
public double alt { get; set; }
}
}