Skip to content

Commit

Permalink
Merge pull request #113 from qJake/v1.1-prep
Browse files Browse the repository at this point in the history
Basic caching and Calendar fix
  • Loading branch information
qJake committed Jan 25, 2021
2 parents e18cf4d + 2aca0af commit 8c1f8dd
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Docker/BuildHaccContainers.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$version = '1.0.25'
$version = '1.0.26'

function Test-ExitCode ([int] $Expected = 0)
{
Expand Down
5 changes: 5 additions & 0 deletions HADotNet.CommandCenter/Controllers/CalendarTileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public CalendarTileController(IConfigStore configStore, EntityClient entityClien
public async Task<IActionResult> Add()
{
ViewBag.Entities = (await EntityClient.GetEntities("calendar")).OrderBy(e => e).Select(e => new SelectListItem(e, e));
ViewBag.CurrentConfig = await ConfigStore.GetConfigAsync();

return View();
}

Expand All @@ -35,6 +37,7 @@ public async Task<IActionResult> Edit([FromRoute] string page, [FromQuery] strin
var tile = config[page].Tiles.FirstOrDefault(t => t.Name == name);

ViewBag.Entities = (await EntityClient.GetEntities("calendar")).OrderBy(e => e).Select(e => new SelectListItem(e, e));
ViewBag.CurrentConfig = await ConfigStore.GetConfigAsync();

return View("Add", tile);
}
Expand All @@ -48,6 +51,8 @@ public async Task<IActionResult> Save([FromRoute] string page, CalendarTile tile
}

ViewBag.Entities = (await EntityClient.GetEntities("calendar")).OrderBy(e => e).Select(e => new SelectListItem(e, e));
ViewBag.CurrentConfig = await ConfigStore.GetConfigAsync();

return View("Add", tile);
}
}
Expand Down
4 changes: 2 additions & 2 deletions HADotNet.CommandCenter/HADotNet.CommandCenter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<RuntimeIdentifiers>win10;alpine.3.10-x64;debian.10-arm</RuntimeIdentifiers>
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<AssemblyVersion>1.0.25.0</AssemblyVersion>
<FileVersion>1.0.25.0</FileVersion>
<AssemblyVersion>1.0.26.0</AssemblyVersion>
<FileVersion>1.0.26.0</FileVersion>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Deterministic>false</Deterministic>
Expand Down
12 changes: 8 additions & 4 deletions HADotNet.CommandCenter/Hubs/TileHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ public class TileHub : Hub<ITileHub>
{
private StatesClient StatesClient { get; }
public ServiceClient ServiceClient { get; }
public CalendarClient CalendarClient { get; }
public IConfigStore ConfigStore { get; }

public TileHub(StatesClient statesClient, ServiceClient serviceClient, CalendarClient calendarClient, IConfigStore configStore)
public TileHub(StatesClient statesClient, ServiceClient serviceClient, IConfigStore configStore)
{
StatesClient = statesClient;
ServiceClient = serviceClient;
ConfigStore = configStore;
CalendarClient = calendarClient;
}

public async Task RequestTileState(string page, string tileName)
Expand Down Expand Up @@ -66,8 +64,14 @@ private async Task ProcessOverrides(BaseTile tile)
{
// Calendars use a special API that isn't (might not be?) exposed via the WebSocket API.
case CalendarTile ct:
// Required because for some reason, the calendar API is not available
// via the "inside" supervisor API, only the "external" fully-qualified
// API endpoint.
var config = await ConfigStore.GetConfigAsync();
var calClient = new CalendarClient(new Uri(config.Settings.OverrideAssetUri), config.Settings.AccessToken);

var state = await StatesClient.GetState(ct.EntityId);
var calItems = await CalendarClient.GetEvents(ct.EntityId);
var calItems = await calClient.GetEvents(ct.EntityId);
await Clients.Caller.SendCalendarInfo(ct, state, calItems);
break;

Expand Down
10 changes: 10 additions & 0 deletions HADotNet.CommandCenter/Services/JsonConfigStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class JsonConfigStore : IConfigStore
private HaccOptions Options { get; }
private ILogger<JsonConfigStore> Log { get; }

private static ConfigRoot CachedConfig { get; set; } = null;

public JsonConfigStore(IOptions<HaccOptions> haccOptions, ILogger<JsonConfigStore> log)
{
Options = haccOptions.Value;
Expand Down Expand Up @@ -67,6 +69,11 @@ public async Task ManipulateConfig(params Action<ConfigRoot>[] changes)

public async Task<ConfigRoot> GetConfigAsync()
{
if (CachedConfig != null)
{
return CachedConfig;
}

if (CheckPermissions())
{
// One-time config Linux platform migration
Expand Down Expand Up @@ -106,6 +113,8 @@ public async Task<ConfigRoot> GetConfigAsync()

var cfg = JsonConvert.DeserializeObject<ConfigRoot>(contents, SerializerSettings);

CachedConfig = cfg;

return cfg;
}
else
Expand All @@ -116,6 +125,7 @@ public async Task<ConfigRoot> GetConfigAsync()

public async Task SaveConfigAsync(ConfigRoot config)
{
CachedConfig = null;
if (CheckPermissions())
{
await File.WriteAllTextAsync(ConfigPath, JsonConvert.SerializeObject(config, SerializerSettings));
Expand Down
1 change: 0 additions & 1 deletion HADotNet.CommandCenter/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped(_ => ClientFactory.GetClient<StatesClient>());
services.AddScoped(_ => ClientFactory.GetClient<ServiceClient>());
services.AddScoped(_ => ClientFactory.GetClient<DiscoveryClient>());
services.AddScoped(_ => ClientFactory.GetClient<CalendarClient>());

services.AddSignalR()
.AddNewtonsoftJsonProtocol();
Expand Down
11 changes: 9 additions & 2 deletions HADotNet.CommandCenter/Views/Admin/Settings.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@
<i class="globe icon"></i>
<input asp-for="OverrideAssetUri" />
</div>
<small>If your image assets are not showing up correctly, you can enter a base URL to use. This URL must start with http(s):// and be accessible from your HACC device.</small>
<small>
If your image assets are not showing up correctly, you can enter a base URL to use. This URL must start with http(s):// and be accessible from your HACC device.<br /><br />
This is also required if you want to use a <b>calendar</b> tile, due to the way the API call is handled.
</small>
<span class="ui up pointing small red label" asp-validation-for="OverrideAssetUri"></span>
</div>
</div>
Expand Down Expand Up @@ -128,7 +131,11 @@
<i class="globe icon"></i>
<input asp-for="OverrideAssetUri" />
</div>
<small>If your image assets are not showing up correctly, you can enter a base URL to use. This URL must start with http(s):// and be accessible from your HACC device.</small>
<small>
If your image assets are not showing up correctly, you can enter a base URL to use. This URL must start with http(s):// and be accessible from your HACC device.<br /><br />
This is also required if you want to use a <b>calendar</b> tile, due to the way the API call is handled.<br /><br />
For non-supervised environments, this can be (and is usually) the same as your <b>Home Assistant Base URL</b>, so try that if you aren't sure.
</small>
<span class="ui up pointing small red label" asp-validation-for="OverrideAssetUri"></span>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion HADotNet.CommandCenter/Views/CalendarTile/Add.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Html.Raw(header)
</h1>

@if (SupervisorEnvironment.IsSupervisorAddon)
@if (SupervisorEnvironment.IsSupervisorAddon && string.IsNullOrWhiteSpace((ViewBag.CurrentConfig as ConfigRoot)?.Settings?.OverrideAssetUri))
{
<div class="ui visible icon labeled warning message">
<i class="triangle exclamation icon"></i>
Expand Down
2 changes: 1 addition & 1 deletion HADotNet.CommandCenter/wwwroot/css/admin.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions HADotNet.CommandCenter/wwwroot/css/dashboard.css
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ body.dashboard .debug-window {
}
.tile.tile-type--calendar {
justify-content: flex-start !important;
overflow-y: auto !important;
}
.tile.tile-type--calendar .top-label {
display: block;
Expand Down
1 change: 1 addition & 0 deletions HADotNet.CommandCenter/wwwroot/css/dashboard.less
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ body.dashboard
&.tile-type--calendar
{
justify-content: flex-start !important;
overflow-y: auto !important;

.top-label
{
Expand Down
2 changes: 1 addition & 1 deletion HADotNet.CommandCenter/wwwroot/css/dashboard.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8c1f8dd

Please sign in to comment.