Skip to content
Snippets Groups Projects
Commit e0cceee4 authored by Callum Inglis's avatar Callum Inglis
Browse files

Display data on frontend. Accomodate ppm10 and ppm100 values. Fix EntityFrameworkCore version

parent df4f999d
No related branches found
No related tags found
1 merge request!2Api authentication
@page "/latestdata"
@using Public_API.Shared.Models
@inject HttpClient http
<h3>LatestData</h3>
<p>View Most Recent Sensor Data!</p>
@if (sensorData == null)
{
<p><em>Loading...</em></p>
} else
{
<table class="table">
<thead>
<tr>
<th>Sensor UID</th>
<th>Friendly Name</th>
<th>Location</th>
<th>Date</th>
<th>Time</th>
<th>Temperature</th>
<th>Humidity</th>
<th>PPM 1.0</th>
<th>PPM 2.5</th>
<th>PPM 10</th>
</tr>
</thead>
<tbody>
@foreach (vw_SensorData data in sensorData)
{
<tr>
<td>@data.sensorUID</td>
<td>@data.friendlyName</td>
<td>@data.locationCity</td>
<td>@data.dateTime.ToShortDateString()</td>
<td>@data.dateTime.ToShortTimeString()</td>
<td>@data.temperature</td>
<td>@data.humidity</td>
<td>@data.ppm10</td>
<td>@data.ppm25</td>
<td>@data.ppm100</td>
</tr>
}
</tbody>
</table>
}
@code {
private vw_SensorData[] sensorData;
protected override async Task OnInitializedAsync()
{
sensorData = await http.GetFromJsonAsync<vw_SensorData[]>("frontend/LatestData");
}
}
......@@ -22,6 +22,11 @@
<span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="latestdata">
<span class="oi oi-list-rich" aria-hidden="true"></span> Latest Data
</NavLink>
</li>
</ul>
</div>
......
......@@ -24,14 +24,6 @@ namespace Public_API.Server.Controllers
return db.Sensor.Where(s => s.sensorUID.Equals(sensorUID)).FirstOrDefault();
}
// GET: api/<APIController>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// TODO Post
// TODO Auth required
// GET api/sensor/logValue/...
......@@ -65,7 +57,9 @@ namespace Public_API.Server.Controllers
(decimal)response.sensorReading.sht.temperature, // SHT
(decimal)response.sensorReading.sht.humidity, // SHT
0, // TODO C02
(decimal)response.sensorReading.ppm.p25 // PPM TODO ppm10 and ppm100
(decimal)response.sensorReading.ppm.p10, // PPM
(decimal)response.sensorReading.ppm.p25, // PPM
(decimal)response.sensorReading.ppm.p100 // PPM
);
db.SensorValue.Add(sensorValue);
......
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Public_API.Server.DAL;
using Public_API.Shared.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Public_API.Server.Controllers
{
[ApiController]
[Route("[controller]")]
public class FrontendController : Controller
{
private ApiContext db = new ApiContext();
[HttpGet("LatestData")]
public IEnumerable<vw_SensorData> Get()
{
return db.vw_SensorData.OrderByDescending(d => d.dateTime).Take(100).ToArray();
}
}
}
......@@ -11,6 +11,7 @@ namespace Public_API.Server.DAL
{
public DbSet<Sensor> Sensor { get; set; }
public DbSet<SensorValue> SensorValue { get; set; }
public DbSet<vw_SensorData> vw_SensorData { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
......
......@@ -8,7 +8,14 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="3.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.11" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.20" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.20" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.20">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.5" />
</ItemGroup>
<ItemGroup>
......
......@@ -14,16 +14,20 @@ namespace Public_API.Shared.Models
public decimal temperature { get; set; }
public decimal humidity { get; set; }
public decimal co2 { get; set; }
public decimal ppm10 { get; set; }
public decimal ppm25 { get; set; }
public decimal ppm100 { get; set; }
public SensorValue(int sensorID, DateTime dateTime, decimal temperature, decimal humidity, decimal co2, decimal ppm25)
public SensorValue(int sensorID, DateTime dateTime, decimal temperature, decimal humidity, decimal co2, decimal ppm10, decimal ppm25, decimal ppm100)
{
this.sensorID = sensorID;
this.dateTime = dateTime;
this.temperature = temperature;
this.humidity = humidity;
this.co2 = co2;
this.ppm10 = ppm10;
this.ppm25 = ppm25;
this.ppm100 = ppm100;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Public_API.Shared.Models
{
public class vw_SensorData
{
[Key]
public int valueID { get; set; }
public int sensorID { get; set; }
public string sensorUID { get; set; }
public decimal locationLong { get; set; }
public decimal locationLat { get; set; }
public string locationCity { get; set; }
public string friendlyName { get; set; }
public DateTime dateTime { get; set; }
public decimal? temperature { get; set; }
public decimal? humidity { get; set; }
public decimal? ppm10 { get; set; }
public decimal? ppm25 { get; set; }
public decimal? ppm100 { get; set; }
}
}
......@@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.20" />
</ItemGroup>
</Project>
......@@ -18,12 +18,15 @@ CREATE TABLE [dbo].[Sensor](
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Sensor](
[sensorID] [int] IDENTITY(1,1) NOT NULL,
[sensorUID] [nvarchar](50) NOT NULL,
[locationLong] [decimal](11, 8) NOT NULL,
[locationLat] [decimal](11, 8) NOT NULL,
[locationCity] [nvarchar](100) NOT NULL,
[friendlyName] [nvarchar](50) NULL
CREATE TABLE [dbo].[SensorValue](
[valueID] [int] IDENTITY(1,1) NOT NULL,
[dateTime] [datetime] NOT NULL,
[sensorID] [int] NOT NULL,
[temperature] [decimal](14, 4) NULL,
[humidity] [decimal](14, 4) NULL,
[co2] [decimal](14, 4) NULL,
[ppm25] [decimal](14, 4) NULL,
[ppm10] [decimal](14, 4) NULL,
[ppm100] [decimal](14, 4) NULL
) ON [PRIMARY]
GO
\ No newline at end of file
CREATE VIEW [dbo].[vw_SensorData]
AS
SELECT dbo.SensorValue.valueID, dbo.Sensor.sensorID, dbo.Sensor.sensorUID, dbo.Sensor.locationLong, dbo.Sensor.locationLat, dbo.Sensor.locationCity, dbo.Sensor.friendlyName, dbo.SensorValue.dateTime, dbo.SensorValue.temperature,
dbo.SensorValue.humidity, dbo.SensorValue.ppm10, dbo.SensorValue.ppm25, dbo.SensorValue.ppm100
FROM dbo.SensorValue LEFT OUTER JOIN
dbo.Sensor ON dbo.SensorValue.sensorID = dbo.Sensor.sensorID
GO
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment