Skip to content

Commit

Permalink
Bugfix: Store and restore app state correctly (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real authored Dec 23, 2020
1 parent b41cd31 commit 350feed
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
32 changes: 28 additions & 4 deletions src/App/NetDaemon.App/Common/NetDaemonAppBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,30 @@ public async Task RestoreAppStateAsync()
expStore.CopyFrom(obj);
}

bool isDisabled = this.Storage.__IsDisabled ?? false;
var appInfo = _daemon!.State.FirstOrDefault(s => s.EntityId == EntityId);
var appState = appInfo?.State as string;
if (appState == null || (appState != "on" && appState != "off"))

if (isDisabled)
{
IsEnabled = false;
if (appState != "off")
{
dynamic serviceData = new FluentExpandoObject();
serviceData.entity_id = EntityId;
await _daemon.CallServiceAsync("switch", "turn_off", serviceData);
}
return;
}
else if (appState == null || (appState != "on" && appState != "off"))
{
IsEnabled = true;
await _daemon.SetStateAsync(EntityId, "on").ConfigureAwait(false);
if (appState != "on")
{
dynamic serviceData = new FluentExpandoObject();
serviceData.entity_id = EntityId;
await _daemon.CallServiceAsync("switch", "turn_on", serviceData);
}

return;
}
Expand Down Expand Up @@ -221,7 +239,10 @@ public virtual Task StartUpAsync(INetDaemon daemon)
return Task.CompletedTask;
}

private string GetUniqueIdForStorage() => $"{this.GetType().Name}_{Id}".ToLowerInvariant();
/// <summary>
/// Returns unique Id for instance
/// </summary>
public string GetUniqueIdForStorage() => $"{this.GetType().Name}_{Id}".ToLowerInvariant();

private async Task HandleLazyStorage()
{
Expand All @@ -243,7 +264,10 @@ private async Task HandleLazyStorage()
{
break;
}
catch { } // Ignore errors in thread
catch (Exception e)
{
Logger.LogError("Error in storage queue {e}", e);
} // Ignore errors in thread
}
}

Expand Down
18 changes: 16 additions & 2 deletions src/Daemon/NetDaemon.Daemon/Daemon/NetDaemonHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,8 @@ private async Task SetDependentState(string entityId, string state)
await SetDependentState(depApp.EntityId, state).ConfigureAwait(false);
}
app.IsEnabled = false;
Logger.LogError("SET APP {app} state = disabled", app.Id);
await PersistAppStateAsync((NetDaemonAppBase)app);
Logger.LogDebug("SET APP {app} state = disabled", app.Id);
}
else if (state == "on")
{
Expand All @@ -1338,7 +1339,8 @@ private async Task SetDependentState(string entityId, string state)

}
}
Logger.LogError("SET APP {app} state = enabled", app.Id);
await PersistAppStateAsync((NetDaemonAppBase)app);
Logger.LogDebug("SET APP {app} state = enabled", app.Id);
}
}

Expand All @@ -1355,6 +1357,18 @@ private async Task SetDependentState(string entityId, string state)
await SetStateAsync(entityId, state).ConfigureAwait(false);

}

//TODO: Refactor this
private async Task PersistAppStateAsync(NetDaemonAppBase app)
{

var obj = await GetDataAsync<IDictionary<string, object?>>(app.GetUniqueIdForStorage()).ConfigureAwait(false) ??
new Dictionary<string, object?>();

obj["__IsDisabled"] = !app.IsEnabled;
await SaveDataAsync<IDictionary<string, object?>>(app.GetUniqueIdForStorage(), obj);
}

private async Task<bool> RestoreAppState(INetDaemonAppBase appInstance)
{
try
Expand Down

0 comments on commit 350feed

Please sign in to comment.