-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap TwinCollection into ITwinProperties interface
- Loading branch information
1 parent
a066c3c
commit 3b9a5c0
Showing
21 changed files
with
461 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
LoRaEngine/modules/LoRaWanNetworkSrvModule/LoraTools/ITwinProperties.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace LoRaTools | ||
{ | ||
using System; | ||
using Microsoft.Azure.Devices.Shared; | ||
|
||
public interface ITwinProperties | ||
{ | ||
long Version { get; } | ||
|
||
dynamic this[string propertyName] { get; set; } | ||
|
||
bool ContainsKey(string propertyName); | ||
|
||
DateTime GetLastUpdated(); | ||
|
||
Metadata GetMetadata(); | ||
|
||
bool TryGetValue(string propertyName, out object item); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
LoRaEngine/modules/LoRaWanNetworkSrvModule/LoraTools/ITwinPropertiesContainer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace LoRaTools | ||
{ | ||
public interface ITwinPropertiesContainer | ||
{ | ||
public ITwinProperties Desired { get; } | ||
|
||
public ITwinProperties Reported { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
LoRaEngine/modules/LoRaWanNetworkSrvModule/LoraTools/IoTHubImpl/IoTHubTwinProperties.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace LoRaTools.IoTHubImpl | ||
{ | ||
using System; | ||
using Microsoft.Azure.Devices.Shared; | ||
|
||
public class IoTHubTwinProperties : ITwinProperties | ||
{ | ||
private readonly TwinCollection twinCollection; | ||
|
||
public long Version => this.twinCollection.Version; | ||
|
||
public dynamic this[string propertyName] { get => this.twinCollection[propertyName]; set => this.twinCollection[propertyName] = value; } | ||
|
||
public IoTHubTwinProperties(TwinCollection twinCollection) | ||
{ | ||
this.twinCollection = twinCollection; | ||
} | ||
|
||
public DateTime GetLastUpdated() => | ||
this.twinCollection.GetLastUpdated(); | ||
|
||
public Metadata GetMetadata() => this.twinCollection.GetMetadata(); | ||
|
||
public bool ContainsKey(string propertyName) | ||
=> this.twinCollection.Contains(propertyName); | ||
|
||
public bool TryGetValue(string propertyName, out object item) | ||
{ | ||
item = null; | ||
|
||
if (!this.twinCollection.Contains(propertyName)) | ||
return false; | ||
|
||
item = this.twinCollection[propertyName]; | ||
|
||
return true; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ine/modules/LoRaWanNetworkSrvModule/LoraTools/IoTHubImpl/IoTHubTwinPropertiesContainer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace LoRaTools.IoTHubImpl | ||
{ | ||
using Microsoft.Azure.Devices.Shared; | ||
|
||
public class IoTHubTwinPropertiesContainer : ITwinPropertiesContainer | ||
{ | ||
public ITwinProperties Desired { get; } | ||
|
||
public ITwinProperties Reported { get; } | ||
|
||
public IoTHubTwinPropertiesContainer(Twin twin) | ||
{ | ||
this.Desired = new IoTHubTwinProperties(twin?.Properties?.Desired ?? new TwinCollection()); | ||
this.Reported = new IoTHubTwinProperties(twin?.Properties?.Reported ?? new TwinCollection()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
LoRaEngine/modules/LoRaWanNetworkSrvModule/LoraTools/Utils/TwinPropertiesExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#nullable enable | ||
|
||
namespace LoRaTools.Utils | ||
{ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.Extensions.Logging; | ||
|
||
public static class TwinPropertiesExtensions | ||
{ | ||
public static T? SafeRead<T>(this ITwinProperties twinCollection, string property, T? defaultValue = default, ILogger? logger = null) | ||
=> twinCollection.TryRead<T>(property, logger, out var someT) ? someT : defaultValue; | ||
|
||
public static bool TryRead<T>(this ITwinProperties twinCollection, string property, ILogger? logger, [NotNullWhen(true)] out T? value) | ||
{ | ||
_ = twinCollection ?? throw new ArgumentNullException(nameof(twinCollection)); | ||
|
||
value = default; | ||
|
||
if (!twinCollection.TryGetValue(property, out var some)) | ||
return false; | ||
|
||
return TwinPropertyParser.TryParse<T>(property, some, logger, out value); | ||
} | ||
|
||
public static bool TryReadJsonBlock(this ITwinProperties twinCollection, string property, [NotNullWhen(true)] out string? json) | ||
{ | ||
_ = twinCollection ?? throw new ArgumentNullException(nameof(twinCollection)); | ||
json = null; | ||
|
||
if (!twinCollection.TryGetValue(property, out var some)) | ||
return false; | ||
|
||
json = some.ToString(); | ||
return json != null; | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
LoRaEngine/modules/LoRaWanNetworkSrvModule/LoraTools/Utils/TwinPropertiesReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#nullable enable | ||
|
||
namespace LoRaTools.Utils | ||
{ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.Extensions.Logging; | ||
|
||
public sealed class TwinPropertiesReader | ||
{ | ||
private readonly ITwinProperties twinCollection; | ||
private readonly ILogger logger; | ||
|
||
public TwinPropertiesReader(ITwinProperties twinCollection, ILogger logger) | ||
{ | ||
this.twinCollection = twinCollection; | ||
this.logger = logger; | ||
} | ||
|
||
public T? SafeRead<T>(string property, T? defaultValue = default) | ||
=> this.twinCollection.SafeRead(property, defaultValue, this.logger); | ||
|
||
public bool TryRead<T>(string property, [NotNullWhen(true)] out T? value) | ||
=> this.twinCollection.TryRead(property, this.logger, out value); | ||
} | ||
} |
Oops, something went wrong.