-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Twin Manager2 changes * TwinManager2 refactor * Reported properties Sync to cloud changes * Add ctors * ReportedProperties sync changes * Cleanup * Make Twin an option as well * Add events * Fix reported properties handling * Fix logs * Cleanup * Fix twin store entity * Fix reported properties validator * Remove commented code * More tests * Split out types for easier tests * Cleanup CloudSync * More tests * TwinStore tests * More tests * Fix merge * More tests * More checks * More tests * More tests * Fix config and add test * Cleanup * Fix config * Fix reported property updates * Fix build * fix test
- Loading branch information
1 parent
127fec0
commit d99f8ff
Showing
26 changed files
with
2,622 additions
and
48 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
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
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
121 changes: 121 additions & 0 deletions
121
edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/twin/CloudSync.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,121 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
namespace Microsoft.Azure.Devices.Edge.Hub.Core.Twin | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Devices.Edge.Hub.Core.Cloud; | ||
using Microsoft.Azure.Devices.Edge.Util; | ||
using Microsoft.Azure.Devices.Shared; | ||
using Microsoft.Extensions.Logging; | ||
|
||
class CloudSync : ICloudSync | ||
{ | ||
readonly IConnectionManager connectionManager; | ||
readonly IMessageConverter<TwinCollection> twinCollectionConverter; | ||
readonly IMessageConverter<Twin> twinConverter; | ||
|
||
public CloudSync( | ||
IConnectionManager connectionManager, | ||
IMessageConverter<TwinCollection> twinCollectionConverter, | ||
IMessageConverter<Twin> twinConverter) | ||
{ | ||
this.connectionManager = connectionManager; | ||
this.twinCollectionConverter = twinCollectionConverter; | ||
this.twinConverter = twinConverter; | ||
} | ||
|
||
public async Task<Option<Twin>> GetTwin(string id) | ||
{ | ||
try | ||
{ | ||
Events.GettingTwin(id); | ||
Option<ICloudProxy> cloudProxy = await this.connectionManager.GetCloudConnection(id); | ||
Option<Twin> twin = await cloudProxy.Map( | ||
async cp => | ||
{ | ||
IMessage twinMessage = await cp.GetTwinAsync(); | ||
Twin twinValue = this.twinConverter.FromMessage(twinMessage); | ||
Events.GetTwinSucceeded(id); | ||
return Option.Some(twinValue); | ||
}) | ||
.GetOrElse(() => Task.FromResult(Option.None<Twin>())); | ||
return twin; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Events.ErrorGettingTwin(id, ex); | ||
return Option.None<Twin>(); | ||
} | ||
} | ||
|
||
public async Task<bool> UpdateReportedProperties(string id, TwinCollection patch) | ||
{ | ||
try | ||
{ | ||
Events.UpdatingReportedProperties(id); | ||
Option<ICloudProxy> cloudProxy = await this.connectionManager.GetCloudConnection(id); | ||
bool result = await cloudProxy.Map( | ||
async cp => | ||
{ | ||
IMessage patchMessage = this.twinCollectionConverter.ToMessage(patch); | ||
await cp.UpdateReportedPropertiesAsync(patchMessage); | ||
Events.UpdatedReportedProperties(id); | ||
return true; | ||
}) | ||
.GetOrElse(() => Task.FromResult(false)); | ||
return result; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Events.ErrorUpdatingReportedProperties(id, ex); | ||
return false; | ||
} | ||
} | ||
|
||
static class Events | ||
{ | ||
const int IdStart = HubCoreEventIds.TwinManager; | ||
static readonly ILogger Log = Logger.Factory.CreateLogger<StoringTwinManager>(); | ||
|
||
enum EventIds | ||
{ | ||
GettingTwin = IdStart + 70, | ||
GetTwinSucceeded, | ||
ErrorGettingTwin, | ||
UpdatingReportedProperties, | ||
UpdatedReportedProperties, | ||
ErrorUpdatingReportedProperties | ||
} | ||
|
||
public static void ErrorUpdatingReportedProperties(string id, Exception ex) | ||
{ | ||
Log.LogDebug((int)EventIds.ErrorUpdatingReportedProperties, ex, $"Error updating reported properties for {id}"); | ||
} | ||
|
||
public static void UpdatedReportedProperties(string id) | ||
{ | ||
Log.LogInformation((int)EventIds.UpdatedReportedProperties, $"Updated reported properties for {id}"); | ||
} | ||
|
||
public static void UpdatingReportedProperties(string id) | ||
{ | ||
Log.LogDebug((int)EventIds.UpdatingReportedProperties, $"Updating reported properties for {id}"); | ||
} | ||
|
||
public static void ErrorGettingTwin(string id, Exception ex) | ||
{ | ||
Log.LogWarning((int)EventIds.ErrorGettingTwin, ex, $"Error getting twin for {id}"); | ||
} | ||
|
||
public static void GetTwinSucceeded(string id) | ||
{ | ||
Log.LogDebug((int)EventIds.GetTwinSucceeded, $"Got twin for {id}"); | ||
} | ||
|
||
public static void GettingTwin(string id) | ||
{ | ||
Log.LogDebug((int)EventIds.GettingTwin, $"Getting twin for {id}"); | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/twin/ICloudSync.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,14 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
namespace Microsoft.Azure.Devices.Edge.Hub.Core.Twin | ||
{ | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Devices.Edge.Util; | ||
using Microsoft.Azure.Devices.Shared; | ||
|
||
interface ICloudSync | ||
{ | ||
Task<Option<Twin>> GetTwin(string id); | ||
|
||
Task<bool> UpdateReportedProperties(string id, TwinCollection patch); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/twin/IReportedPropertiesStore.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,15 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
namespace Microsoft.Azure.Devices.Edge.Hub.Core.Twin | ||
{ | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Devices.Shared; | ||
|
||
interface IReportedPropertiesStore | ||
{ | ||
Task Update(string id, TwinCollection patch); | ||
|
||
void InitSyncToCloud(string id); | ||
|
||
Task SyncToCloud(string id); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/twin/ITwinStore.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,18 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
namespace Microsoft.Azure.Devices.Edge.Hub.Core.Twin | ||
{ | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Devices.Edge.Util; | ||
using Microsoft.Azure.Devices.Shared; | ||
|
||
interface ITwinStore | ||
{ | ||
Task<Option<Twin>> Get(string id); | ||
|
||
Task UpdateReportedProperties(string id, TwinCollection patch); | ||
|
||
Task UpdateDesiredProperties(string id, TwinCollection patch); | ||
|
||
Task Update(string id, Twin twin); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/twin/PassThroughTwinManager.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,46 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
namespace Microsoft.Azure.Devices.Edge.Hub.Core.Twin | ||
{ | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Devices.Edge.Hub.Core.Cloud; | ||
using Microsoft.Azure.Devices.Edge.Hub.Core.Device; | ||
using Microsoft.Azure.Devices.Edge.Util; | ||
using Microsoft.Azure.Devices.Shared; | ||
|
||
public class PassThroughTwinManager : ITwinManager | ||
{ | ||
readonly IConnectionManager connectionManager; | ||
readonly IMessageConverter<Twin> twinConverter; | ||
|
||
public PassThroughTwinManager(IConnectionManager connectionManager, IMessageConverterProvider messageConverterProvider) | ||
{ | ||
Preconditions.CheckNotNull(messageConverterProvider, nameof(messageConverterProvider)); | ||
this.connectionManager = Preconditions.CheckNotNull(connectionManager, nameof(connectionManager)); | ||
this.twinConverter = messageConverterProvider.Get<Twin>(); | ||
} | ||
|
||
public async Task<IMessage> GetTwinAsync(string id) | ||
{ | ||
Preconditions.CheckNonWhiteSpace(id, nameof(id)); | ||
Option<ICloudProxy> cloudProxy = await this.connectionManager.GetCloudConnection(id); | ||
IMessage twin = await cloudProxy | ||
.Map(c => c.GetTwinAsync()) | ||
.GetOrElse(() => Task.FromResult(this.twinConverter.ToMessage(new Twin()))); | ||
return twin; | ||
} | ||
|
||
public Task UpdateDesiredPropertiesAsync(string id, IMessage twinCollection) | ||
{ | ||
Preconditions.CheckNonWhiteSpace(id, nameof(id)); | ||
Option<IDeviceProxy> deviceProxy = this.connectionManager.GetDeviceConnection(id); | ||
return deviceProxy.ForEachAsync(dp => dp.OnDesiredPropertyUpdates(twinCollection)); | ||
} | ||
|
||
public async Task UpdateReportedPropertiesAsync(string id, IMessage twinCollection) | ||
{ | ||
Preconditions.CheckNonWhiteSpace(id, nameof(id)); | ||
Option<ICloudProxy> cloudProxy = await this.connectionManager.GetCloudConnection(id); | ||
await cloudProxy.ForEachAsync(cp => cp.UpdateReportedPropertiesAsync(twinCollection)); | ||
} | ||
} | ||
} |
Oops, something went wrong.