Skip to content

Commit

Permalink
Prep 0.6 (#92)
Browse files Browse the repository at this point in the history
* Feat/chain (#84)

* validating chain

* refactor after chain

* split dps and hub certs

* skip chain test

* import chain

* use gw in rido

Co-authored-by: rido-min <rido-min@users.noreply.github.com>

* upd memmon defaults

* Feat/formats (#86)

* moving serializers

* copy serializers to samples

* upd tests to new hub

* review proto bindings

* rev 0.6 (#87)

* upd memmon to don7

* upd memmon defaults

* rev 0.6

* ci -> 7

* upd tests to 7

* warnings for memmon

* mmpb -> 7

* Rel/prep06 (#88)

* upd memmon to don7

* upd memmon defaults

* rev 0.6

* ci -> 7

* upd tests to 7

* warnings for memmon

* mmpb -> 7

* actions -> 7

* Feat/onemsg (#90)

* init RO props

* init ro props in broker

* upd mqttnet

* upd AppInsights

* parse dps disable response (#91)

* handle iohub disconnect

* comment intermedita tests

* add azpubsub

* exclude int tests

* upd ci to 22.04

* upd mqttnet to 4.1.4

Co-authored-by: rido-min <rido-min@users.noreply.github.com>
  • Loading branch information
ridomin and rido-min authored Dec 12, 2022
1 parent 03ba71f commit b1fc284
Show file tree
Hide file tree
Showing 32 changed files with 366 additions and 129 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
environment: Test_IoT_Hub
timeout-minutes: 5
steps:
Expand All @@ -35,7 +35,7 @@ jobs:
env:
E2EHubConnectionString: ${{secrets.E2EHubConnectionString}}
TestHubName: ${{secrets.TestHubName}}
run: dotnet test --no-build --verbosity normal --logger trx
run: dotnet test --no-build --verbosity normal --logger trx --filter FullyQualifiedName\!~MQTTnet.Extensions.MultiCloud.IntegrationTests

- name: Process trx reports with default
if: always()
Expand Down
2 changes: 1 addition & 1 deletion samples/iothub-sample/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
_logger.LogWarning("Connecting to: {connectionSettings}", connectionSettings);

var client = new HubMqttClient(await HubDpsFactory.CreateFromConnectionSettingsAsync(connectionSettings, stoppingToken));

client.Connection.DisconnectedAsync += async d => await Task.Run(() => _logger.LogError("MQTT client disconnected {reason}", d.Reason));
var v = await client.UpdateTwinAsync(new { started = DateTime.Now }, stoppingToken);
_logger.LogInformation("Updated Twin to verison: {v}", v);
var twin = await client.GetTwinAsync(stoppingToken);
Expand Down
12 changes: 12 additions & 0 deletions samples/memmon-protobuff/Serializers/ReadOnlyPropertyProtobuff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Serializers;

public class ReadOnlyPropertyProtobuff<T> : DeviceToCloudBinder<T>, IReadOnlyProperty<T>
{
public T? Value { get; set; }
public int Version { get; set; }
public ReadOnlyPropertyProtobuff(IMqttClient mqttClient) : this(mqttClient, string.Empty) { }

public ReadOnlyPropertyProtobuff(IMqttClient mqttClient, string name)
Expand All @@ -16,4 +18,14 @@ public ReadOnlyPropertyProtobuff(IMqttClient mqttClient, string name)
WrapMessage = false;
Retain = true;
}

public void InitProperty(string initialState)
{
throw new System.NotImplementedException();
}

public Task SendMessageAsync(CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
18 changes: 13 additions & 5 deletions samples/memmon/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
client.Command_malloc.OnMessage = Command_malloc_Hanlder;
client.Command_free.OnMessage = Command_free_Hanlder;

client.Property_started.InitProperty(client.InitialState);

client.Property_timesRestarted.InitProperty(client.InitialState);
client.Property_timesRestarted.Value++;
await client.Property_timesRestarted.SendMessageAsync(stoppingToken);


await client.Property_started.SendMessageAsync(DateTime.Now, stoppingToken);

await client.Property_interval.InitPropertyAsync(client.InitialState, default_interval, stoppingToken);
Expand Down Expand Up @@ -242,11 +249,12 @@ string RenderData()
AppendLineWithPadRight(sb, $"{connectionSettings?.HostName}:{connectionSettings?.TcpPort}");
AppendLineWithPadRight(sb, $"{connectionSettings.ClientId} (Auth:{connectionSettings.Auth}/ TLS:{connectionSettings.UseTls}) GW: {connectionSettings.GatewayHostName}");
AppendLineWithPadRight(sb, " ");
AppendLineWithPadRight(sb, string.Format("{0:8} | {1:15} | {2}", "Property", "Value".PadRight(15), "Version"));
AppendLineWithPadRight(sb, string.Format("{0:8} | {1:15} | {2}", "--------", "-----".PadLeft(15, '-'), "------"));
AppendLineWithPadRight(sb, string.Format("{0:8} | {1:15} | {2}", "enabled".PadRight(8), enabled_value?.PadLeft(15), client?.Property_enabled?.Version));
AppendLineWithPadRight(sb, string.Format("{0:8} | {1:15} | {2}", "interval".PadRight(8), interval_value?.PadLeft(15), client?.Property_interval.Version));
//AppendLineWithPadRight(sb, string.Format("{0:8} | {1:15} | {2}", "started".PadRight(8), client.Property_started.T().PadLeft(15), client?.Property_started?.Version));
AppendLineWithPadRight(sb, string.Format("{0:8} | {1:15} | {2}", "Property".PadRight(15), "Value".PadRight(15), "Version"));
AppendLineWithPadRight(sb, string.Format("{0:8} | {1:15} | {2}", "-".PadRight(15, '-'), "-----".PadLeft(15, '-'), "------"));
AppendLineWithPadRight(sb, string.Format("{0:8} | {1:15} | {2}", "enabled".PadRight(15), enabled_value?.PadLeft(15), client?.Property_enabled?.Version));
AppendLineWithPadRight(sb, string.Format("{0:8} | {1:15} | {2}", "interval".PadRight(15), interval_value?.PadLeft(15), client?.Property_interval.Version));
AppendLineWithPadRight(sb, string.Format("{0:8} | {1:15} | {2}", "timesRestarted".PadRight(15), client.Property_timesRestarted.Value.ToString().PadLeft(15), client?.Property_timesRestarted.Version));
AppendLineWithPadRight(sb, string.Format("{0:8} | {1:15} | {2}", "started".PadRight(15), client.Property_started.Value.ToShortTimeString().PadLeft(15), client?.Property_started?.Version));
AppendLineWithPadRight(sb, " ");
AppendLineWithPadRight(sb, $"Reconnects: {reconnectCounter}");
AppendLineWithPadRight(sb, $"Telemetry: {telemetryCounter}");
Expand Down
2 changes: 1 addition & 1 deletion samples/memmon/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"cs" : "HostName=test.mosquitto.org;TcpPort=8886"
},
"ApplicationInsights": {
"ConnectionString": "InstrumentationKey=50cb7b11-9466-4348-a6eb-0ef66b3fb724;IngestionEndpoint=https://westus3-1.in.applicationinsights.azure.com/"
"ConnectionString": "InstrumentationKey=1bf7b95b-55b8-4623-bc00-4734a152c47c;IngestionEndpoint=https://westus2-2.in.applicationinsights.azure.com/;LiveEndpoint=https://westus2.livediagnostics.monitor.azure.com/"
},
"Logging": {
"ApplicationInsights": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace dtmi_rido_memmon.aws;
public class _memmon : AwsMqttClient, Imemmon
{
public IReadOnlyProperty<DateTime> Property_started { get; set; }
public IReadOnlyProperty<int> Property_timesRestarted { get; set; }
public IWritableProperty<bool> Property_enabled { get; set; }
public IWritableProperty<int> Property_interval { get; set; }
public ITelemetry<double> Telemetry_workingSet { get; set; }
Expand All @@ -24,6 +25,7 @@ public class _memmon : AwsMqttClient, Imemmon
internal _memmon(IMqttClient c) : base(c)
{
Property_started = new ReadOnlyProperty<DateTime>(c, "started");
Property_timesRestarted = new ReadOnlyProperty<int>(c, "timesRestarted");
Property_interval = new WritableProperty<int>(c, "interval");
Property_enabled = new WritableProperty<bool>(c, "enabled");
Telemetry_workingSet = new MQTTnet.Extensions.MultiCloud.BrokerIoTClient.Telemetry<double>(c, "workingSet");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ namespace dtmi_rido_memmon;

public interface Imemmon
{
public const string ModelId = "dtmi:rido:memmon;2";
public const string ModelId = "dtmi:rido:memmon;3";
public IMqttClient Connection { get; }
public string InitialState { get; }

public IReadOnlyProperty<DateTime> Property_started { get; set; }
public IReadOnlyProperty<int> Property_timesRestarted { get; set; }
public IWritableProperty<bool> Property_enabled { get; set; }
public IWritableProperty<int> Property_interval { get; set; }
public ITelemetry<double> Telemetry_workingSet { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class _memmon : HubMqttClient, Imemmon
{

public IReadOnlyProperty<DateTime> Property_started { get; set; }
public IReadOnlyProperty<int> Property_timesRestarted { get; set; }
public IWritableProperty<bool> Property_enabled { get; set; }
public IWritableProperty<int> Property_interval { get; set; }
public ITelemetry<double> Telemetry_workingSet { get; set; }
Expand All @@ -23,6 +24,7 @@ public class _memmon : HubMqttClient, Imemmon
public _memmon(IMqttClient c) : base(c)
{
Property_started = new ReadOnlyProperty<DateTime>(c, "started");
Property_timesRestarted = new ReadOnlyProperty<int>(c, "timesRestarted");
Property_interval = new WritableProperty<int>(c, "interval");
Property_enabled = new WritableProperty<bool>(c, "enabled");
Telemetry_workingSet = new Telemetry<double>(c, "workingSet");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
{
"@context": "dtmi:dtdl:context;2",
"@id": "dtmi:rido:memmon;2",
"@id": "dtmi:rido:memmon;3",
"@type": "Interface",
"contents": [
{
"@type": "Property",
"name": "started",
"schema": "dateTime"
},
{
"@type": "Property",
"name": "timesRestarted",
"schema": "integer"
},
{
"@type": "Property",
"name": "enabled",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class _memmon : Imemmon
public IMqttClient Connection { get; set; }
public string InitialState { get; set; }
public IReadOnlyProperty<DateTime> Property_started { get; set; }
public IReadOnlyProperty<int> Property_timesRestarted { get; set; }
public IWritableProperty<bool> Property_enabled { get; set; }
public IWritableProperty<int> Property_interval { get; set; }
public ITelemetry<double> Telemetry_workingSet { get; set; }
Expand All @@ -25,6 +26,7 @@ internal _memmon(IMqttClient c)
{
Connection = c;
Property_started = new ReadOnlyProperty<DateTime>(c, "started");
Property_timesRestarted = new ReadOnlyProperty<int>(c, "timesRestarted");
Property_interval = new WritableProperty<int>(c, "interval");
Property_enabled = new WritableProperty<bool>(c, "enabled");
Telemetry_workingSet = new Telemetry<double>(c, "workingSet");
Expand Down
2 changes: 1 addition & 1 deletion samples/memmon/memmon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</ItemGroup>
<ItemGroup>
<Compile Update="*.g.cs">
<DependentUpon>dtmi_rido_memmon-2.json</DependentUpon>
<DependentUpon>dtmi_rido_memmon-3.json</DependentUpon>
</Compile>
</ItemGroup>

Expand Down
14 changes: 14 additions & 0 deletions samples/mqtt-grpc-device/Serializers/ReadOnlyPropertyProtobuff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
using MQTTnet.Extensions.MultiCloud;
using MQTTnet.Extensions.MultiCloud.Binders;
using MQTTnet.Extensions.MultiCloud.Serializers;
using System.Threading;
using System.Threading.Tasks;

namespace mqtt_grpc_device.Serializers;

public class ReadOnlyPropertyProtobuff<T> : DeviceToCloudBinder<T>, IReadOnlyProperty<T>
{
public T? Value { get; set; }
public int Version { get; set; }
public ReadOnlyPropertyProtobuff(IMqttClient mqttClient) : this(mqttClient, string.Empty) { }

public ReadOnlyPropertyProtobuff(IMqttClient mqttClient, string name)
Expand All @@ -16,4 +20,14 @@ public ReadOnlyPropertyProtobuff(IMqttClient mqttClient, string name)
WrapMessage = false;
Retain = true;
}

public void InitProperty(string initialState)
{
throw new System.NotImplementedException();
}

public Task SendMessageAsync(CancellationToken cancellationToken = default)
{
throw new System.NotImplementedException();
}
}
17 changes: 17 additions & 0 deletions samples/payload-size/Binders/ReadOnlyPropertyAvro.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
using Avro;
using Google.Protobuf.WellKnownTypes;
using MQTTnet.Client;
using MQTTnet.Extensions.MultiCloud;
using MQTTnet.Extensions.MultiCloud.Binders;
using MQTTnet.Extensions.MultiCloud.Serializers;
using payload_size.Serializers;
using System.Text.Json;
using System.Xml.Linq;

namespace payload_size.Binders;

public class ReadOnlyPropertyAvro<T> : DeviceToCloudBinder<T>, IReadOnlyProperty<T>
{
private readonly string _name;
public T? Value { get; set; }
public int Version { get; set; }
public ReadOnlyPropertyAvro(IMqttClient mqttClient, Schema schema)
: this(mqttClient, string.Empty, schema) { }

public ReadOnlyPropertyAvro(IMqttClient mqttClient, string name, Schema schema)
: base(mqttClient, name, new AvroSerializer(schema))
{
_name = name;
TopicPattern = "device/{clientId}/props";
WrapMessage = false;
Retain = true;
}

public void InitProperty(string initialState)
{
throw new NotImplementedException();
}

public Task SendMessageAsync(CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
12 changes: 12 additions & 0 deletions samples/payload-size/Binders/ReadOnlyPropertyProtobuff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace payload_size.Binders;

public class ReadOnlyPropertyProtobuff<T> : DeviceToCloudBinder<T>, IReadOnlyProperty<T>
{
public T? Value { get; set; }
public int Version { get; set; }
public ReadOnlyPropertyProtobuff(IMqttClient mqttClient) : this(mqttClient, string.Empty) { }

public ReadOnlyPropertyProtobuff(IMqttClient mqttClient, string name)
Expand All @@ -17,4 +19,14 @@ public ReadOnlyPropertyProtobuff(IMqttClient mqttClient, string name)
WrapMessage = false;
Retain = true;
}

public void InitProperty(string initialState)
{
throw new System.NotImplementedException();
}

public Task SendMessageAsync(CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
13 changes: 13 additions & 0 deletions src/MQTTnet.Extensions.MultiCloud.AwsIoTClient/ReadOnlyProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,29 @@ public class ReadOnlyProperty<T> : IReadOnlyProperty<T>
{
readonly IMqttClient _connection;
readonly string _name;
public T? Value { get; set; }
public int Version { get; set; }
public ReadOnlyProperty(IMqttClient mqttClient, string name)
{
_connection = mqttClient;
_name = name;
}

public async Task SendMessageAsync(T payload, CancellationToken cancellationToken = default)
{
ShadowSerializer serializer = new();
var topic = $"$aws/things/{_connection.Options.ClientId}/shadow/update";
var payloadBytes = serializer.ToBytes(payload, _name);
await _connection.PublishBinaryAsync(topic, payloadBytes, Protocol.MqttQualityOfServiceLevel.AtLeastOnce, false, cancellationToken);
}

public void InitProperty(string initialState)
{
throw new System.NotImplementedException();
}

public Task SendMessageAsync(CancellationToken cancellationToken = default)
{
throw new System.NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public MqttDpsClient(IMqttClient c, string mid)
{
tcs.SetResult(dpsRes);
}
if (dpsRes != null && dpsRes.Status == "disabled")
{
tcs.SetException(new ApplicationException("Device ID disabled in DPS"));
}
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
using MQTTnet.Client;
using MQTTnet.Extensions.MultiCloud.Binders;
using System.Text.Json;
using System.Xml.Linq;

namespace MQTTnet.Extensions.MultiCloud.AzureIoTClient;

public class ReadOnlyProperty<T> : DeviceToCloudBinder<T>, IReadOnlyProperty<T>
{
private readonly string _name;
public T? Value { get; set; }
public int Version { get; set; }
public ReadOnlyProperty(IMqttClient mqttClient, string name)
: base(mqttClient, name)
{
_name = name;
TopicPattern = "$iothub/twin/PATCH/properties/reported/?$rid=1";
WrapMessage = true;
Retain = false;
}

public void InitProperty(string initialState)
{
JsonDocument doc = JsonDocument.Parse(initialState);
JsonElement reported = doc.RootElement.GetProperty("reported");
Version = reported.GetProperty("$version").GetInt32();
if (reported.TryGetProperty(_name, out JsonElement element))
{
Value = element.Deserialize<T>()!;
}
}

public Task SendMessageAsync(CancellationToken cancellationToken = default) => SendMessageAsync(Value!, cancellationToken);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public static async Task<IMqttClient> CreateFromConnectionSettingsAsync(Connecti
{
throw new ApplicationException($"Cannot connect to {cs}");
}
//if (withBirth)
//{
if (withBirth)
{
var birthPayload = new UTF8JsonSerializer().ToBytes(
new BirthConvention.BirthMessage(BirthConvention.ConnectionStatus.online)
{
Expand All @@ -39,7 +39,7 @@ public static async Task<IMqttClient> CreateFromConnectionSettingsAsync(Connecti
{
throw new ApplicationException($"Error publishing Birth {cs}");
}
//}
}
return mqtt;
}
}
Expand Down
Loading

0 comments on commit b1fc284

Please sign in to comment.