Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\AppBroker.Core\AppBroker.Core.csproj" />
<ProjectReference Include="..\AppBrokerASP\AppBrokerASP.csproj" />
</ItemGroup>

<Target Name="CopyDLLs" AfterTargets="Build">
<Message Text="##### Copy Dlls #####" Importance="high" />
<ItemGroup>
<MySourceFiles Include="$(TargetDir)\**\*.*" />
<PluginDlls Include="$(TargetDir)\$(ProjectName).*" />
</ItemGroup>
<Copy SourceFiles="@(MySourceFiles)" DestinationFiles="@(MySourceFiles->'$(SolutionDir)plugins/$(ProjectName)/%(RecursiveDir)%(Filename)%(Extension)')" />
<Copy SourceFiles="@(PluginDlls)" DestinationFiles="@(PluginDlls->'$(SolutionDir)AppBrokerASP\$(OutputPath)\plugins\%(Filename)%(Extension)')" />
</Target>

</Project>
62 changes: 62 additions & 0 deletions AppBroker.App/Controller/AppController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using AppBroker.Core.Database;

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppBroker.App.Controller;
[Route("app")]
internal class AppController : ControllerBase
{
[HttpGet]
public Guid? GetIdByName(string name)
{
using var ctx = DbProvider.AppDbContext;
return ctx.Apps.FirstOrDefault(x => EF.Functions.Like(x.Name, name))?.Id;
}

[HttpPatch]
public async Task UpdateName(Guid id, string newName)
{
using var ctx = DbProvider.AppDbContext;

var app = ctx.Apps.FirstOrDefault(x => x.Id == id);
if (app is null)
{
ctx.Apps.Add(new Core.Database.Model.AppModel { Id = id, Name = newName });
}
else
{
app.Name = newName;
}
await ctx.SaveChangesAsync();
}

[HttpGet("settings")]
public string? GetByKey(Guid id, string key)
{
using var ctx = DbProvider.AppDbContext;
return ctx.AppConfigs.FirstOrDefault(x => x.AppId == id && x.Key == key)?.Value;
}

[HttpPost("settings")]
public string? SetValue(Guid id, string key, string value)
{
using var ctx = DbProvider.AppDbContext;
var setting = ctx.AppConfigs.FirstOrDefault(x => x.AppId == id && x.Key == key);
if (setting is null)
{
ctx.AppConfigs.Add(new Core.Database.Model.AppConfigModel {Key = key, Value = value , AppId = id });
}
else
{
setting.Value = value;
}
return value;
}
}
61 changes: 61 additions & 0 deletions AppBroker.App/Controller/DeviceController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using AppBroker.Core.Devices;
using AppBroker.Core;

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;

using Newtonsoft.Json;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AppBroker.App.Model;
using AppBroker.Core.Database;
using AppBroker.Core.Managers;
using Azure.Core;

namespace AppBroker.App.Controller;

public record struct DeviceRenameRequest(long Id, string NewName);

[Route("app/device")]
public class DeviceController : ControllerBase
{
private readonly IDeviceManager deviceManager;

public DeviceController(IDeviceManager deviceManager)
{
this.deviceManager = deviceManager;
}

[HttpGet]
public List<Device> GetAllAppDevices()
{
var devices = deviceManager.Devices.Select(x => x.Value).Where(x => x.ShowInApp).ToList();
var dev = JsonConvert.SerializeObject(devices);

return devices;
}

[HttpGet("overview")]
public List<DeviceOverview> GetDeviceOverview(bool onlyShowInApp = true) => deviceManager
.Devices
.Select(x => x.Value)
.Where(x => !onlyShowInApp || x.ShowInApp)
.Select(x => new DeviceOverview(x.Id, x.FriendlyName, x.TypeName, x.TypeNames))
.ToList();

[HttpPatch]
public void UpdateDevice([FromBody] DeviceRenameRequest request)
{
if (deviceManager.Devices.TryGetValue(request.Id, out Device? stored))
{
stored.FriendlyName = request.NewName;
_ = DbProvider.UpdateDeviceInDb(stored);
stored.SendDataToAllSubscribers();
}
}

}
91 changes: 91 additions & 0 deletions AppBroker.App/Controller/HistoryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using AppBroker.Core.Devices;
using AppBroker.Core.Models;
using AppBroker.Core;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using AppBroker.Core.Managers;

namespace AppBroker.App.Controller;

public record struct SetHistoryRequest(bool Enable, List<long> Ids, string Name);

[Route("app/history")]
public class HistoryController : ControllerBase
{
private readonly IHistoryManager historyManager;
private readonly IDeviceManager deviceManager;

public HistoryController(IHistoryManager historyManager, IDeviceManager deviceManager)
{
this.historyManager = historyManager;
this.deviceManager = deviceManager;
}

[HttpGet("settings")]
public List<HistoryPropertyState> GetHistoryPropertySettings() => historyManager.GetHistoryProperties();


[HttpPatch]
public void SetHistories([FromBody] SetHistoryRequest request)
{
if (request.Enable)
{
foreach (var id in request.Ids)
historyManager.EnableHistory(id, request.Name);
}
else
{
foreach (var id in request.Ids)
historyManager.DisableHistory(id, request.Name);
}
}

[HttpGet]
public Task<List<History>> GetIoBrokerHistories([FromQuery] long id, [FromQuery] DateTime dt)
{
if (deviceManager.Devices.TryGetValue(id, out Device? device))
{
return device.GetHistory(dt.Date, dt.Date.AddDays(1).AddSeconds(-1));
}
return Task.FromResult(new List<History>());
}

//Currently not used on the app
//public Task<History> GetIoBrokerHistory(long id, string dt, string propertyName)
//{
// if (deviceManager.Devices.TryGetValue(id, out Device? device))
// {
// DateTime date = DateTime.Parse(dt).Date;
// return device.GetHistory(date, date.AddDays(1).AddSeconds(-1), propertyName);
// }
// return Task.FromResult(History.Empty);
//}


//Currently not used on the app
//public Task<List<History>> GetIoBrokerHistoriesRange(long id, string dt, string dt2)
//{
// if (deviceManager.Devices.TryGetValue(id, out Device? device))
// {
// return device.GetHistory(DateTime.Parse(dt), DateTime.Parse(dt2));
// }

// return Task.FromResult(new List<History>());
//}

[HttpGet("range")]
public async Task<History> GetIoBrokerHistoryRange(long id, DateTime from, DateTime to, string propertyName)
{
if (deviceManager.Devices.TryGetValue(id, out Device? device))
{
return await device.GetHistory(from, to, propertyName);
}

return History.Empty;
}
}
111 changes: 111 additions & 0 deletions AppBroker.App/Controller/LayoutController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using AppBroker.Core.DynamicUI;
using AppBroker.Core;
using AppBrokerASP;

using Microsoft.AspNetCore.Mvc;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AppBroker.App.Hubs;
using AppBroker.Core.Managers;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using AppBroker.Core.Devices;

namespace AppBroker.App.Controller;

public record LayoutRequest(string TypeName, string IconName, long DeviceId);
public record LayoutResponse(DeviceLayout? Layout, SvgIcon? Icon);


[Route("app/layout")]
public class LayoutController : ControllerBase
{
private readonly IconService iconService;
private readonly IDeviceManager deviceManager;

public LayoutController(IconService iconService, IDeviceManager deviceManager)
{
this.iconService = iconService;
this.deviceManager = deviceManager;
}

[HttpGet("single")]
public LayoutResponse GetSingle([FromQuery] LayoutRequest request)
{
var layout = GetLayout(request);
var icon = GetIcon(request, layout?.IconName);

return new LayoutResponse(layout, icon);
}
[HttpGet("multi")]
public List<LayoutResponse> GetMultiple([FromQuery] List<LayoutRequest> request)
{
var response = new List<LayoutResponse>();

foreach (var req in request)
{
var layout = GetLayout(req);
var icon = GetIcon(req, layout?.IconName);
if (icon is not null || layout is not null)
response.Add(new LayoutResponse(layout, icon));
}
return response;
}

[HttpGet("all")]
public List<LayoutResponse> GetAll()
{
return DeviceLayoutService
.GetAllLayouts()
.Select(x => new LayoutResponse(x, GetIcon(null, x.IconName)))
.ToList();
}

private DeviceLayout? GetLayout(LayoutRequest request)
{
DeviceLayout? layout = null;
if (request.DeviceId != 0)
layout = DeviceLayoutService.GetDeviceLayout(request.DeviceId)?.layout;
if (layout is null && !string.IsNullOrWhiteSpace(request.TypeName))
layout = DeviceLayoutService.GetDeviceLayout(request.TypeName)?.layout;
if (layout is null && deviceManager.Devices.TryGetValue(request.DeviceId, out var device))
{
foreach (var item in device.TypeNames)
{
if (DeviceLayoutService.GetDeviceLayout(item) is { } res && res.layout is { } resLayout)
{
layout = resLayout;
break;
}
}
}
return layout;
}

private SvgIcon? GetIcon(LayoutRequest? request, string? iconName)
{
SvgIcon? icon = null;
if (!string.IsNullOrWhiteSpace(iconName))
{
icon = iconService.GetIconByName(iconName);
}
if (request is not null)
{
if (icon is null && !string.IsNullOrWhiteSpace(request?.IconName))
{
icon = iconService.GetIconByName(request.IconName);
}
if (icon is null && !string.IsNullOrWhiteSpace(request?.TypeName))
{
icon = iconService.GetBestFitIcon(request.TypeName);
}
}
return icon;
}

[HttpPatch]
public void ReloadDeviceLayouts() => DeviceLayoutService.ReloadLayouts();
}
Loading