Skip to content

Commit

Permalink
.NET 6 migration
Browse files Browse the repository at this point in the history
Web API functional
Blazor web app mostly functional
  • Loading branch information
xxbiohazrdxx committed Apr 24, 2022
1 parent b9697ea commit c5f29f5
Show file tree
Hide file tree
Showing 72 changed files with 2,711 additions and 206 deletions.
16 changes: 10 additions & 6 deletions AmpAPI/AmpAPI.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8.0</LangVersion>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="6.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.2" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
<ProjectReference Include="..\MPRSGxZ\MPRSGxZ.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MPRSGxZ\MPRSGxZ.csproj" />
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<None Include="Pages\Index.razor" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions AmpAPI/Controllers/AmplifierController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class AmplifierController : ControllerBase
private AmplifierStackSettings Settings;
private IAmplifierService AmplifierService;

public AmplifierController(IOptions<AmplifierStackSettings> Settings, IAmplifierService AmplifierService)
public AmplifierController(IOptionsMonitor<AmplifierStackSettings> Settings, IAmplifierService AmplifierService)
{
this.Settings = Settings.Value;
this.Settings = Settings.CurrentValue;
this.AmplifierService = AmplifierService;
}

Expand Down
40 changes: 40 additions & 0 deletions AmpAPI/Controllers/ConnectionController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using AmpAPI.Settings;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace AmpAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ConnectionController : ControllerBase
{
private AmplifierStackSettings Settings;

public ConnectionController(IOptionsMonitor<AmplifierStackSettings> Settings)
{
this.Settings = Settings.CurrentValue;
}

[HttpGet]
public ConnectionSettings Get()
{
return Settings.Connection;
}

[NonAction]
[HttpPost]
public void Post([FromBody] AmplifierStackSettings value)
{
}

[HttpPut]
public void Put([FromBody] AmplifierStackSettings value)
{
}

[HttpDelete]
public void Delete()
{
}
}
}
4 changes: 2 additions & 2 deletions AmpAPI/Controllers/SourceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class SourceController : ControllerBase
private AmplifierStackSettings Settings;
private IAmplifierService AmplifierService;

public SourceController(IOptions<AmplifierStackSettings> Settings, IAmplifierService AmplifierService)
public SourceController(IOptionsMonitor<AmplifierStackSettings> Settings, IAmplifierService AmplifierService)
{
this.Settings = Settings.Value;
this.Settings = Settings.CurrentValue;
this.AmplifierService = AmplifierService;
}

Expand Down
6 changes: 3 additions & 3 deletions AmpAPI/Controllers/ZoneController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class ZoneController : ControllerBase
private AmplifierStackSettings Settings;
private IAmplifierService AmplifierService;

public ZoneController(IOptions<AmplifierStackSettings> Settings, IAmplifierService AmplifierService)
public ZoneController(IOptionsMonitor<AmplifierStackSettings> Settings, IAmplifierService AmplifierService)
{
this.Settings = Settings.Value;
this.Settings = Settings.CurrentValue;
this.AmplifierService = AmplifierService;
}

Expand Down Expand Up @@ -46,7 +46,7 @@ public IActionResult Get([FromRoute]int AmplifierID, int id)

// PUT: api/Zone/5
[HttpPut("{ZoneID:int:range(1,6)}")]
public IActionResult Put([FromRoute]int AmplifierID, int ZoneID, ZoneModel PutZone)
public IActionResult Put([FromRoute]int AmplifierID, int ZoneID, [FromBody] ZoneModel PutZone)
{
if (AmplifierID > AmplifierService.Amplifiers.Length)
{
Expand Down
5 changes: 1 addition & 4 deletions AmpAPI/Hubs/AmpHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ namespace AmpAPI.Hubs
{
public class AmpHub : Hub
{
public async Task SendZoneUpdate(ZoneModel Zone)
{
await Clients.All.SendAsync("ReceiveZoneUpdate", Zone);
}
public async Task SendZoneUpdate(ZoneModel Zone) => await Clients.All.SendAsync("ReceiveZoneUpdate", Zone);
}
}
30 changes: 15 additions & 15 deletions AmpAPI/Models/ZoneModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
{
public class ZoneModel
{
public int AmpID { get; private set; }
public int ZoneID { get; private set; }
public bool Power { get; private set; }
public bool Mute { get; private set; }
public bool PublicAddress { get; private set; }
public bool DoNotDisturb { get; private set; }
public int Volume { get; private set; }
public int Treble { get; private set; }
public int Bass { get; private set; }
public int Balance { get; private set; }
public int Source { get; private set; }
public string Name { get; private set; }
public bool Enabled { get; private set; }
public decimal VolumeFactor { get; private set; }
public int AdjustedVolume { get; private set; }
public int AmpID { get; set; }
public int ZoneID { get; set; }
public bool Power { get; set; }
public bool Mute { get; set; }
public bool PublicAddress { get; set; }
public bool DoNotDisturb { get; set; }
public int Volume { get; set; }
public int Treble { get; set; }
public int Bass { get; set; }
public int Balance { get; set; }
public int Source { get; set; }
public string Name { get; set; }
public bool Enabled { get; set; }
public decimal VolumeFactor { get; set; }
public int AdjustedVolume { get; set; }

internal ZoneModel() { }

Expand Down
51 changes: 51 additions & 0 deletions AmpAPI/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
@page "/"
@namespace AmpAPI.Pages
@using Microsoft.AspNetCore.SignalR.Client
@using AmpAPI.Models
@inject NavigationManager NavigationManager
@implements IAsyncDisposable

<ul>
<li>Amp ID - @CurrentZone.AmpID</li>
<li>Zone ID - @CurrentZone.ZoneID</li>
<li>Source - @CurrentZone.Source</li>
<li>Volume - @CurrentZone.Volume</li>
</ul>

@code {
private HubConnection? hubConnection;
private ZoneModel CurrentZone;

protected override async Task OnInitializedAsync()
{
hubConnection = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri("/amphub"))
.Build();

hubConnection.On<ZoneModel>("ReceiveZoneUpdate", (Zone) =>
{
CurrentZone = Zone;
StateHasChanged();
});

await hubConnection.StartAsync();
}

private async Task Send()
{
if (hubConnection is not null)
{
await hubConnection.SendAsync("SendZoneChange", CurrentZone);
}
}

public bool IsConnected => hubConnection?.State == HubConnectionState.Connected;

public async ValueTask DisposeAsync()
{
if (hubConnection is not null)
{
await hubConnection.DisposeAsync();
}
}
}
9 changes: 2 additions & 7 deletions AmpAPI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace AmpAPI
{
Expand All @@ -19,6 +13,7 @@ public static void Main(string[] args)

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((builder) => builder.AddJsonFile("ampSettings.json", optional: true, reloadOnChange: true))
.UseStartup<Startup>();
}
}
2 changes: 1 addition & 1 deletion AmpAPI/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/amplifier",
"launchUrl": "api/connection",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
46 changes: 38 additions & 8 deletions AmpAPI/Services/AmplifierService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
using Microsoft.Extensions.Options;
using MPRSGxZ;
using MPRSGxZ.Hardware;
using System;
using Microsoft.AspNetCore.SignalR;
using AmpAPI.Hubs;

namespace AmpAPI.Services
{
public class AmplifierService : IAmplifierService
{
private readonly IHubContext<AmpHub> Hub;
public AmplifierStack AmplifierMiddleware { get; private set; }

public Amplifier[] Amplifiers
Expand All @@ -27,23 +29,51 @@ public Source[] Sources
}
private AmplifierStackSettings Settings;

public AmplifierService(IOptions<AmplifierStackSettings> Settings)
public void Save()
{
this.Settings = Settings.Value;
//File.WriteAllText(path, JsonSerializer.Serialize(appSettings));
}

public AmplifierService(IOptionsMonitor<AmplifierStackSettings> Settings, IHubContext<AmpHub> Hub)
{
this.Settings = Settings.CurrentValue;
this.Hub = Hub;
//Settings.OnChange

if (this.Settings.PortType == ConnectionType.Virtual)
if (this.Settings.Connection.PortType == ConnectionType.Virtual)
{
AmplifierMiddleware = new AmplifierStack(this.Settings.PollingFrequency, this.Settings.AmplifierCount);
AmplifierMiddleware = new AmplifierStack(this.Settings.Connection.PollingFrequency, this.Settings.Connection.AmplifierCount);
}
else if (this.Settings.PortType == ConnectionType.Serial)
else if (this.Settings.Connection.PortType == ConnectionType.Serial)
{
AmplifierMiddleware = new AmplifierStack(this.Settings.PortAddress, this.Settings.PollingFrequency, this.Settings.AmplifierCount);
AmplifierMiddleware = new AmplifierStack(this.Settings.Connection.PortAddress, this.Settings.Connection.PollingFrequency, this.Settings.Connection.AmplifierCount);
}
else
{
throw new NotImplementedException();
AmplifierMiddleware = new AmplifierStack(this.Settings.Connection.PortAddress, this.Settings.Connection.PollingFrequency, this.Settings.Connection.AmplifierCount);
}

for (int i = 0; i < this.Settings.Connection.AmplifierCount; i++)
{
AmplifierMiddleware.Amplifiers[i].Enabled = this.Settings.Amplifiers[i].Enabled;
AmplifierMiddleware.Amplifiers[i].Name = this.Settings.Amplifiers[i].Name;

for (int j = 0; j < 6; j++)
{
AmplifierMiddleware.Amplifiers[i].Zones[j].Enabled = this.Settings.Zones[(i * 6) + j].Enabled;
AmplifierMiddleware.Amplifiers[i].Zones[j].Name = this.Settings.Zones[(i * 6) + j].Name;
}
}

for (int i = 0; i < 6; i++)
{
AmplifierMiddleware.Sources[i].Name = this.Settings.Sources[i].Name;
AmplifierMiddleware.Sources[i].Enabled = this.Settings.Sources[i].Enabled;
}

AmplifierMiddleware.ZoneChanged += new MPRSGxZ.Events.ZoneChangedEvent(x =>
Hub.Clients.All.SendAsync("SendZoneUpdate", AmplifierMiddleware.Amplifiers[x.AmpID].Zones[x.ZoneID]));

AmplifierMiddleware.Open();
}
}
Expand Down
16 changes: 14 additions & 2 deletions AmpAPI/Settings/ApplicationSettings.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
namespace AmpAPI.Settings
{
public class AmplifierStackSettings
{
public ConnectionSettings Connection { get; set; }
public AmplifierSettings[] Amplifiers { get; set; }
public ZoneSettings[] Zones { get; set; }
public SourceSettings[] Sources { get; set; }
}

public class ConnectionSettings
{
public ConnectionType PortType { get; set; }
public string PortAddress { get; set; }
public int PollingFrequency { get; set; }
public int AmplifierCount { get; set; }
public ZoneSettings[] Zones { get; set; }
public SourceSettings[] Sources { get; set; }
}

public class AmplifierSettings
{
public string Name { get; set; }
public bool Enabled { get; set; }
}

public class ZoneSettings
Expand Down
10 changes: 6 additions & 4 deletions AmpAPI/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AmpAPI.Services;
using AmpAPI.Settings;
using AmpAPI.Hubs;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -24,10 +25,9 @@ public void ConfigureServices(IServiceCollection services)
{
services.Configure<AmplifierStackSettings>(Configuration.GetSection("AmplifierStackSettings"));
services.AddSingleton<IAmplifierService, AmplifierService>();
//services.AddSingleton<IConfiguration>(Configuration);
services.AddRazorPages();
//services.AddRouting();
//services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddServerSideBlazor();
services.AddSignalR();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -44,11 +44,13 @@ public void Configure(IApplicationBuilder app, IHostEnvironment env)

app.UseHttpsRedirection();
app.UseRouting();
//app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapHub<AmpHub>("/ampHub");
//endpoints.MapFallbackToPage("/Pages/Index");
});
}
}
Expand Down
Loading

0 comments on commit c5f29f5

Please sign in to comment.