diff --git a/AmpAPI/AmpAPI.csproj b/AmpAPI/AmpAPI.csproj index cfc2f5f..c9bb94a 100644 --- a/AmpAPI/AmpAPI.csproj +++ b/AmpAPI/AmpAPI.csproj @@ -1,21 +1,21 @@  - netcoreapp3.1 - 8.0 + netcoreapp3.1 + 8.0 - + - - + + - + diff --git a/AmpAPI/Controllers/AmplifierController.cs b/AmpAPI/Controllers/AmplifierController.cs index 104d590..d72d5cd 100644 --- a/AmpAPI/Controllers/AmplifierController.cs +++ b/AmpAPI/Controllers/AmplifierController.cs @@ -1,4 +1,5 @@ -using AmpAPI.Services; +using AmpAPI.Models; +using AmpAPI.Services; using AmpAPI.Settings; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; @@ -8,9 +9,9 @@ namespace AmpAPI.Controllers { [Route("api/[controller]")] - [ApiController] - public class AmplifierController : ControllerBase - { + [ApiController] + public class AmplifierController : ControllerBase + { private AmplifierStackSettings Settings; private IAmplifierService AmplifierService; @@ -20,36 +21,47 @@ public AmplifierController(IOptions Settings, IAmplifier this.AmplifierService = AmplifierService; } - // GET: api/Amplifier - [HttpGet] - public IEnumerable Get() - { + // GET: api/Amplifier + [HttpGet] + public IEnumerable Get() + { return AmplifierService.Amplifiers; - } - - // GET: api/Amplifier/5 - [HttpGet("{id:int:range(1,3)}")] - public Amplifier Get(int id) - { - return AmplifierService.Amplifiers[id - 1]; - } - - // POST: api/Amplifier - [HttpPost] - public void Post([FromBody] string value) - { - } - - // PUT: api/Amplifier/5 - [HttpPut("{id}")] - public void Put(int id, [FromBody] string value) - { - } - - // DELETE: api/ApiWithActions/5 - [HttpDelete("{id}")] - public void Delete(int id) - { - } - } + } + + // GET: api/Amplifier/5 + [HttpGet("{id:int:range(1,3)}")] + public IActionResult Get(int id) + { + if (id > AmplifierService.Amplifiers.Length) + { + return NotFound(); + } + + return Ok(AmplifierService.Amplifiers[id - 1]); + } + + // POST: api/Amplifier + [NonAction] + [HttpPost] + public void Post([FromBody] AmpModel value) + { + } + + // PUT: api/Amplifier/5 + [HttpPut("{id}")] + public IActionResult Put(int id, [FromBody] AmpModel value) + { + return Conflict($"The object contains modifications to properties that are read only."); + AmplifierService.Amplifiers[id - 1].Enabled = value.Enabled; + AmplifierService.Amplifiers[id - 1].Name = value.Name; + return Ok(); + } + + // DELETE: api/ApiWithActions/5 + [HttpDelete("{id}")] + public void Delete(int id) + { + AmplifierService.Amplifiers[id - 1].Enabled = false; + } + } } diff --git a/AmpAPI/Controllers/SourceController.cs b/AmpAPI/Controllers/SourceController.cs index da6084b..3fe278a 100644 --- a/AmpAPI/Controllers/SourceController.cs +++ b/AmpAPI/Controllers/SourceController.cs @@ -1,4 +1,5 @@ -using AmpAPI.Services; +using AmpAPI.Models; +using AmpAPI.Services; using AmpAPI.Settings; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; @@ -7,10 +8,10 @@ namespace AmpAPI.Controllers { - [Route("api/[controller]")] - [ApiController] - public class SourceController : ControllerBase - { + [Route("api/[controller]")] + [ApiController] + public class SourceController : ControllerBase + { private AmplifierStackSettings Settings; private IAmplifierService AmplifierService; @@ -22,34 +23,44 @@ public SourceController(IOptions Settings, IAmplifierSer // GET: api/Source [HttpGet] - public IEnumerable Get() - { + public IEnumerable Get() + { return AmplifierService.Sources; - } + } - // GET: api/Source/5 - [HttpGet("{id:int:range(1,6)}")] - public Source Get(int id) - { + // GET: api/Source/5 + [HttpGet("{id:int:range(1,6)}")] + public Source Get(int id) + { return AmplifierService.Sources[id - 1]; } - // POST: api/Source - [HttpPost] - public void Post([FromBody] string value) - { - } + // POST: api/Source + [NonAction] + [HttpPost] + public void Post([FromBody] SourceModel value) + { + } - // PUT: api/Source/5 - [HttpPut("{id}")] - public void Put(int id, [FromBody] string value) - { - } + // PUT: api/Source/5 + [HttpPut("{id}")] + public IActionResult Put(int id, [FromBody] SourceModel value) + { + if (id != value.ID) + { + return Conflict("Supplied id does not match"); + } + + AmplifierService.Sources[id - 1].Name = value.Name; + AmplifierService.Sources[id - 1].Enabled = value.Enabled; + return Ok(); + } - // DELETE: api/ApiWithActions/5 - [HttpDelete("{id}")] - public void Delete(int id) - { - } - } + // DELETE: api/ApiWithActions/5 + [HttpDelete("{id}")] + public void Delete(int id) + { + AmplifierService.Sources[id - 1].Enabled = false; + } + } } diff --git a/AmpAPI/Controllers/ZoneController.cs b/AmpAPI/Controllers/ZoneController.cs index 67c782d..46d8f36 100644 --- a/AmpAPI/Controllers/ZoneController.cs +++ b/AmpAPI/Controllers/ZoneController.cs @@ -4,14 +4,13 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using MPRSGxZ.Hardware; -using System.Collections.Generic; namespace AmpAPI.Controllers { - [Route("api/amplifier/{amplifierID:int:range(1,3)}/[controller]")] - [ApiController] - public class ZoneController : ControllerBase - { + [Route("api/amplifier/{amplifierID:int:range(1,3)}/[controller]")] + [ApiController] + public class ZoneController : ControllerBase + { private AmplifierStackSettings Settings; private IAmplifierService AmplifierService; @@ -23,22 +22,37 @@ public ZoneController(IOptions Settings, IAmplifierServi // GET: api/Zone [HttpGet] - public IEnumerable Get([FromRoute]int AmplifierID) - { - return AmplifierService.Amplifiers[AmplifierID - 1].Zones; - } + public IActionResult Get([FromRoute]int AmplifierID) + { + if (AmplifierID > AmplifierService.Amplifiers.Length) + { + return NotFound(); + } + + return Ok(AmplifierService.Amplifiers[AmplifierID - 1].Zones); + } - // GET: api/Zone/5 - [HttpGet("{id:int:range(1,6)}")] - public Zone Get([FromRoute]int AmplifierID, int id) - { - return AmplifierService.Amplifiers[AmplifierID - 1].Zones[id - 1]; + // GET: api/Zone/5 + [HttpGet("{id:int:range(1,6)}")] + public IActionResult Get([FromRoute]int AmplifierID, int id) + { + if (AmplifierID > AmplifierService.Amplifiers.Length) + { + return NotFound(); + } + + return Ok(AmplifierService.Amplifiers[AmplifierID - 1].Zones[id - 1]); } // PUT: api/Zone/5 [HttpPut("{ZoneID:int:range(1,6)}")] - public void Put([FromRoute]int AmplifierID, int ZoneID, ZoneModel PutZone) + public IActionResult Put([FromRoute]int AmplifierID, int ZoneID, ZoneModel PutZone) { + if (AmplifierID > AmplifierService.Amplifiers.Length) + { + return NotFound(); + } + var Zone = AmplifierService.Amplifiers[AmplifierID - 1].Zones[ZoneID - 1]; Zone.Power = PutZone.Power; @@ -53,30 +67,20 @@ public void Put([FromRoute]int AmplifierID, int ZoneID, ZoneModel PutZone) Zone.Name = PutZone.Name; Zone.Enabled = PutZone.Enabled; Zone.VolumeFactor = PutZone.VolumeFactor; - } - // PUT: api/Zone/5 - //[HttpPut("{ZoneID:int:range(1,6)}/volume/{value:int:range(0,38)}")] - //public void Put([FromRoute]int AmplifierID, int ZoneID, int value) - //{ - //} + return Ok(); + } - // PUT: api/Zone/5 - //[HttpPut("{ZoneID:int:range(1,6)}/volume")] - //public void Put([FromRoute]int AmplifierID, int ZoneID, [FromBody]int Volume) - //{ - //} + [HttpDelete("{ZoneID:int:range(1,6)}")] + public IActionResult Delete([FromRoute] int AmplifierID, int ZoneID) + { + if (AmplifierID > AmplifierService.Amplifiers.Length) + { + return NotFound(); + } - // No POST/DELETE as zones are never created/deleted - // POST: api/Zone - //[HttpPost] - //public void Post([FromBody] string value) - //{ - //} - // DELETE: api/ApiWithActions/5 - //[HttpDelete("{id}")] - // public void Delete(int id) - // { - // } + AmplifierService.Amplifiers[AmplifierID - 1].Zones[ZoneID - 1].Enabled = false; + return Ok(); + } } } diff --git a/AmpAPI/Hubs/AmpHub.cs b/AmpAPI/Hubs/AmpHub.cs index ba6ed4d..91c7287 100644 --- a/AmpAPI/Hubs/AmpHub.cs +++ b/AmpAPI/Hubs/AmpHub.cs @@ -2,13 +2,13 @@ using System.Threading.Tasks; using AmpAPI.Models; -namespace AmpApi.Hubs +namespace AmpAPI.Hubs { - public class AmpHub : Hub - { - public async Task SendZoneUpdate(ZoneModel Zone) - { - await Clients.All.SendAsync("ReceiveZoneUpdate", Zone); - } - } + public class AmpHub : Hub + { + public async Task SendZoneUpdate(ZoneModel Zone) + { + await Clients.All.SendAsync("ReceiveZoneUpdate", Zone); + } + } } \ No newline at end of file diff --git a/AmpAPI/Models/AmpModel.cs b/AmpAPI/Models/AmpModel.cs new file mode 100644 index 0000000..25bdd5d --- /dev/null +++ b/AmpAPI/Models/AmpModel.cs @@ -0,0 +1,13 @@ +namespace AmpAPI.Models +{ + public class AmpModel + { + public int ID { get; set; } + public string Name { get; set; } + public bool Enabled { get; set; } + + public ZoneModel[] Zones { get; set; } + + public AmpModel() { } + } +} diff --git a/AmpAPI/Models/SourceModel.cs b/AmpAPI/Models/SourceModel.cs new file mode 100644 index 0000000..8decae0 --- /dev/null +++ b/AmpAPI/Models/SourceModel.cs @@ -0,0 +1,11 @@ +namespace AmpAPI.Models +{ + public class SourceModel + { + public int ID { get; set; } + public string Name { get; set; } + public bool Enabled { get; set; } + + public SourceModel() { } + } +} diff --git a/AmpAPI/Models/ZoneModel.cs b/AmpAPI/Models/ZoneModel.cs index 93d6f89..5197f7e 100644 --- a/AmpAPI/Models/ZoneModel.cs +++ b/AmpAPI/Models/ZoneModel.cs @@ -18,6 +18,8 @@ public class ZoneModel public decimal VolumeFactor { get; private set; } public int AdjustedVolume { get; private set; } + internal ZoneModel() { } + public ZoneModel(int AmpID, int ZoneID, bool Power, bool Mute, bool PublicAddress, bool DoNotDisturb, int Volume, int Treble, int Bass, int Balance, int Source, string Name, bool Enabled, decimal VolumeFactor, int AdjustedVolume) { diff --git a/AmpAPI/Properties/launchSettings.json b/AmpAPI/Properties/launchSettings.json index f22c88d..92820e4 100644 --- a/AmpAPI/Properties/launchSettings.json +++ b/AmpAPI/Properties/launchSettings.json @@ -1,30 +1,30 @@ { "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:56184", - "sslPort": 44344 - } + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:56184", + "sslPort": 44344 + } }, "$schema": "http://json.schemastore.org/launchsettings.json", "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "api/amplifier", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "AmpAPI": { - "commandName": "Project", - "launchBrowser": true, - "launchUrl": "api/values", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - }, - "applicationUrl": "https://localhost:5001;http://localhost:5000" - } + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "api/amplifier", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "AmpAPI": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "api/values", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:5001;http://localhost:5000" + } } } \ No newline at end of file diff --git a/AmpAPI/Startup.cs b/AmpAPI/Startup.cs index 32c1528..a9f36c7 100644 --- a/AmpAPI/Startup.cs +++ b/AmpAPI/Startup.cs @@ -26,6 +26,7 @@ public void ConfigureServices(IServiceCollection services) services.AddSingleton(); //services.AddSingleton(Configuration); services.AddRazorPages(); + //services.AddRouting(); //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); } @@ -47,6 +48,7 @@ public void Configure(IApplicationBuilder app, IHostEnvironment env) app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); + endpoints.MapControllers(); }); } } diff --git a/AmpAPI/appsettings.Development.json b/AmpAPI/appsettings.Development.json index e203e94..89277b9 100644 --- a/AmpAPI/appsettings.Development.json +++ b/AmpAPI/appsettings.Development.json @@ -1,9 +1,9 @@ { "Logging": { - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } } } diff --git a/AmpAPI/appsettings.json b/AmpAPI/appsettings.json index d71056d..9ceaf3f 100644 --- a/AmpAPI/appsettings.json +++ b/AmpAPI/appsettings.json @@ -5,110 +5,110 @@ } }, "AmplifierStackSettings": { - "PortType": "Virtual", - "PortAddress": "", - "PollingFrequency": 250, - "AmplifierCount": 1, - "Zones": [ - { - "Name": "Zone 1", - "Enabled": true - }, - { - "Name": "Zone 2", - "Enabled": true - }, - { - "Name": "Zone 3", - "Enabled": true - }, - { - "Name": "Zone 4", - "Enabled": true - }, - { - "Name": "Zone 5", - "Enabled": true - }, - { - "Name": "Zone 6", - "Enabled": true - }, - { - "Name": "Zone 7", - "Enabled": true - }, - { - "Name": "Zone 8", - "Enabled": true - }, - { - "Name": "Zone 9", - "Enabled": true - }, - { - "Name": "Zone 10", - "Enabled": true - }, - { - "Name": "Zone 11", - "Enabled": true - }, - { - "Name": "Zone 12", - "Enabled": true - }, - { - "Name": "Zone 13", - "Enabled": true - }, - { - "Name": "Zone 14", - "Enabled": true - }, - { - "Name": "Zone 15", - "Enabled": true - }, - { - "Name": "Zone 16", - "Enabled": true - }, - { - "Name": "Zone 17", - "Enabled": true - }, - { - "Name": "Zone 18", - "Enabled": true - } - ], - "Sources": [ - { - "Name": "Source 1", - "Enabled": true - }, - { - "Name": "Source 2", - "Enabled": true - }, - { - "Name": "Source 3", - "Enabled": true - }, - { - "Name": "Source 4", - "Enabled": true - }, - { - "Name": "Source 5", - "Enabled": true - }, - { - "Name": "Source 6", - "Enabled": true - } - ] + "PortType": "Virtual", + "PortAddress": "", + "PollingFrequency": 250, + "AmplifierCount": 1, + "Zones": [ + { + "Name": "Zone 1", + "Enabled": true + }, + { + "Name": "Zone 2", + "Enabled": true + }, + { + "Name": "Zone 3", + "Enabled": true + }, + { + "Name": "Zone 4", + "Enabled": true + }, + { + "Name": "Zone 5", + "Enabled": true + }, + { + "Name": "Zone 6", + "Enabled": true + }, + { + "Name": "Zone 7", + "Enabled": true + }, + { + "Name": "Zone 8", + "Enabled": true + }, + { + "Name": "Zone 9", + "Enabled": true + }, + { + "Name": "Zone 10", + "Enabled": true + }, + { + "Name": "Zone 11", + "Enabled": true + }, + { + "Name": "Zone 12", + "Enabled": true + }, + { + "Name": "Zone 13", + "Enabled": true + }, + { + "Name": "Zone 14", + "Enabled": true + }, + { + "Name": "Zone 15", + "Enabled": true + }, + { + "Name": "Zone 16", + "Enabled": true + }, + { + "Name": "Zone 17", + "Enabled": true + }, + { + "Name": "Zone 18", + "Enabled": true + } + ], + "Sources": [ + { + "Name": "Source 1", + "Enabled": true + }, + { + "Name": "Source 2", + "Enabled": true + }, + { + "Name": "Source 3", + "Enabled": true + }, + { + "Name": "Source 4", + "Enabled": true + }, + { + "Name": "Source 5", + "Enabled": true + }, + { + "Name": "Source 6", + "Enabled": true + } + ] }, "AllowedHosts": "*" } diff --git a/MPRSGxZ/Hardware/Amplifier.cs b/MPRSGxZ/Hardware/Amplifier.cs index 5756176..69b1b54 100644 --- a/MPRSGxZ/Hardware/Amplifier.cs +++ b/MPRSGxZ/Hardware/Amplifier.cs @@ -5,11 +5,10 @@ namespace MPRSGxZ.Hardware public class Amplifier { public int ID { get; private set; } - public string Name { get; private set; } - public bool Enabled { get; private set; } + public string Name { get; set; } + public bool Enabled { get; set; } public Zone[] Zones { get; private set; } - private int ZoneCount; // // Settings can be amp-wide, not currently in use or implemented @@ -24,21 +23,16 @@ public class Amplifier private int m_Balance; private int m_Source; */ + + internal Amplifier() { } - /// - /// Default constructor for missing configuration - /// - /// The ID of the amplifier internal Amplifier(int AmpID, QueueCommandEvent QueueCommand, ZoneChangedEvent ZoneChanged, int ZoneCount = 6) { this.ID = AmpID; this.Enabled = true; this.Name = $"Amp {ID}"; - this.ZoneCount = ZoneCount; - Zones = new Zone[ZoneCount]; - for(int i = 0; i < ZoneCount; i++) { Zones[i] = new Zone(ID, i + 1, QueueCommand, ZoneChanged); diff --git a/MPRSGxZ/Hardware/Source.cs b/MPRSGxZ/Hardware/Source.cs index 71a35ed..d6875c2 100644 --- a/MPRSGxZ/Hardware/Source.cs +++ b/MPRSGxZ/Hardware/Source.cs @@ -3,8 +3,10 @@ public class Source { public int ID { get; internal set; } - public string Name; - public bool Enabled; + public string Name { get; set; } + public bool Enabled { get; set; } + + internal Source() { } internal Source(int ID) { diff --git a/MPRSGxZ/Hardware/Zone.cs b/MPRSGxZ/Hardware/Zone.cs index 6ec4319..fd954f7 100644 --- a/MPRSGxZ/Hardware/Zone.cs +++ b/MPRSGxZ/Hardware/Zone.cs @@ -229,11 +229,8 @@ public int Source private event QueueCommandEvent QueueCommand; private event ZoneChangedEvent ZoneChanged; - /// - /// Default constructor for missing configuration - /// - /// The ID of the amplifier this zone is a member of - /// The ID of this zone internal to the owning amplifier + internal Zone() { } + internal Zone(int AmpID, int ZoneID, QueueCommandEvent QueueCommand, ZoneChangedEvent ZoneChanged) { this.AmpID = AmpID;