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

Handle incoming JSON Data from gateways

parent efebc2f3
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,8 @@ using Public_API.Shared.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
......@@ -17,6 +19,12 @@ namespace Public_API.Server.Controllers
ApiContext db = new ApiContext();
public Sensor? getSensorByUID(string sensorUID)
{
return db.Sensor.Where(s => s.sensorUID.Equals(sensorUID)).FirstOrDefault();
}
// GET: api/<APIController>
[HttpGet]
public IEnumerable<string> Get()
......@@ -24,33 +32,50 @@ namespace Public_API.Server.Controllers
return new string[] { "value1", "value2" };
}
// TODO Post
// TODO Auth required
// GET api/sensor/logValue/...
[HttpGet("sensor/logValue/{sensorID}/{temperature}/{humidity}/{co2}/{ppm25}")]
public string Get(int sensorID, string temperature, string humidity, string co2, string ppm25)
public HttpResponseMessage Get(int sensorID, string temperature, string humidity, string co2, string ppm25)
{
// TODO Error Handling!
SensorValue sensorValue = new SensorValue(sensorID, decimal.Parse(temperature), decimal.Parse(humidity), decimal.Parse(co2), decimal.Parse(ppm25));
//SensorValue sensorValue = new SensorValue(sensorID, decimal.Parse(temperature), decimal.Parse(humidity), decimal.Parse(co2), decimal.Parse(ppm25));
db.SensorValue.Add(sensorValue);
db.SaveChanges();
//db.SensorValue.Add(sensorValue);
//db.SaveChanges();
return "Got value from " + sensorID + ". Sorted with ID " + sensorValue.valueID;
return new HttpResponseMessage(HttpStatusCode.OK);
}
// POST api/<APIController>
[HttpPost]
public void Post([FromBody] string value)
// POST api/sensor/reading (With POST JSON Data)
[HttpPost("sensor/reading")]
public HttpResponseMessage Post([FromBody] SensorResponse response)
{
}
var sensor = getSensorByUID(response.sensorMetadata.uid);
if (sensor == null) {
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
// PUT api/<APIController>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
//// TODO Error Handling!
SensorValue sensorValue = new SensorValue(
sensor.sensorID,
DateTimeOffset.FromUnixTimeSeconds(response.sensorMetadata.sampleTime).DateTime,
(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
);
db.SensorValue.Add(sensorValue);
db.SaveChanges();
return new HttpResponseMessage(HttpStatusCode.OK);
}
// DELETE api/<APIController>/5
[HttpDelete("{id}")]
public void Delete(int id)
......
namespace Public_API.Shared.Models
{
public class SensorResponse
{
public SensorMetadata sensorMetadata { get; set; }
public SensorReading sensorReading { get; set; }
}
public class SensorMetadata
{
public string uid { get; set; }
public int samplePeriod { get; set; }
public int sampleTime { get; set; }
}
public class SensorReading
{
public Co2 co2 { get; set; }
public Ppm ppm { get; set; }
public Sht sht { get; set; }
}
public class Co2
{
public string tmp { get; set; }
}
public class Ppm
{
public double p10 { get; set; }
public double p100 { get; set; }
public double p25 { get; set; }
}
public class Sht
{
public double humidity { get; set; }
public double temperature { get; set; }
}
}
......@@ -9,6 +9,7 @@ namespace Public_API.Shared.Models
{
[Key]
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; }
......
......@@ -16,15 +16,14 @@ namespace Public_API.Shared.Models
public decimal co2 { get; set; }
public decimal ppm25 { get; set; }
public SensorValue(int sensorID, decimal temperature, decimal humidity, decimal co2, decimal ppm25)
public SensorValue(int sensorID, DateTime dateTime, decimal temperature, decimal humidity, decimal co2, decimal ppm25)
{
this.sensorID = sensorID;
this.dateTime = dateTime;
this.temperature = temperature;
this.humidity = humidity;
this.co2 = co2;
this.ppm25 = ppm25;
this.dateTime = DateTime.Now;
}
}
}
......@@ -10,20 +10,20 @@ GO
CREATE TABLE [dbo].[Sensor](
[sensorID] [int] IDENTITY(1,1) NOT NULL,
[locationLong] decimal(11, 8) NOT NULL,
[locationLat] decimal(11, 8) NOT NULL,
[locationCity] decimal(11, 8) NOT NULL,
[sensorUID] [nvarchar](50) NOT NULL,
[locationLong] [decimal](11, 8) NOT NULL,
[locationLat] [decimal](11, 8) NOT NULL,
[locationCity] [decimal](11, 8) NOT NULL,
[friendlyName] [nvarchar](50) NULL
) ON [PRIMARY]
GO
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
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
) ON [PRIMARY]
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