Skip to content

Commit

Permalink
Save and Get data is brought back inte V2
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed May 24, 2020
1 parent dfbf3bf commit 2ef53e2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/App/NetDaemon.App/Common/Reactive/AppDaemonRxApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ public RxEntity Entity(string entityId)
return new RxEntity(this, new string[] { entityId });
}

/// <inheritdoc/>
public T? GetData<T>(string id) where T : class
{
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
return _daemon.GetDataAsync<T>(id).Result;
}

/// <inheritdoc/>
public IDisposable RunDaily(string time, Action action)
{
Expand Down Expand Up @@ -229,6 +236,13 @@ public void RunScript(params string[] script)
}
}

/// <inheritdoc/>
public void SaveData<T>(string id, T data)
{
_ = _daemon ?? throw new NullReferenceException($"{nameof(_daemon)} cant be null!");
_daemon.SaveDataAsync<T>(id, data).Wait();
}

/// <inheritdoc/>
public void SetState(string entityId, dynamic state, dynamic? attributes = null)
{
Expand Down
15 changes: 15 additions & 0 deletions src/App/NetDaemon.App/Common/Reactive/INetDaemonReactive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ public interface INetDaemonReactive : INetDaemonAppBase, ICallService, IRxEntity
/// </summary>
IEnumerable<EntityState> States { get; }

/// <summary>
/// Loads persistent data from unique id
/// </summary>
/// <param name="id">Unique Id of the data</param>
/// <returns>The data persistent or null if not exists</returns>
T? GetData<T>(string id) where T : class;

/// <summary>
/// Saves any data with unique id, data have to be json serializable
/// </summary>
/// <param name="id">Unique id for all apps</param>
/// <param name="data">Dynamic data being saved</param>
void SaveData<T>(string id, T data);

/// <summary>
/// Sets a state for entity
/// </summary>
Expand Down Expand Up @@ -137,6 +151,7 @@ public interface IRxSchedule
/// </summary>
/// <param name="second">The timespan to schedule</param>
IObservable<long> RunEveryMinute(short second);

/// <summary>
/// Delays excecution of an action (timespan) time
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions tests/NetDaemon.Daemon.Tests/Reactive/RxAppTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using JoySoftware.HomeAssistant.NetDaemon.Daemon;
using Moq;
using System;
using System.Dynamic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -323,5 +324,40 @@ public async Task WhenStateStaysSameForTimeItShouldCallFunction()

Assert.True(isRun);
}

[Fact]
public async Task SavedDataShouldReturnSameDataUsingExpando()
{
// ARRANGE
var daemonTask = await GetConnectedNetDaemonTask();
dynamic data = new ExpandoObject();
data.Item = "Some data";

// ACT
DefaultDaemonRxApp.SaveData("data_exists", data);
var collectedData = DefaultDaemonRxApp.GetData<ExpandoObject>("data_exists");

await daemonTask;

// ASSERT
Assert.Equal(data, collectedData);
}

[Fact]
public async Task GetDataShouldReturnCachedValue()
{
// ARRANGE
var daemonTask = await GetConnectedNetDaemonTask();
// ACT

DefaultDaemonRxApp.SaveData("GetDataShouldReturnCachedValue_id", "saved data");

DefaultDaemonRxApp.GetData<string>("GetDataShouldReturnCachedValue_id");

await daemonTask;
// ASSERT
DefaultDataRepositoryMock.Verify(n => n.Get<string>(It.IsAny<string>()), Times.Never);
DefaultDataRepositoryMock.Verify(n => n.Save<string>(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
}
}

0 comments on commit 2ef53e2

Please sign in to comment.