diff --git a/Public API/Server/Controllers/APIController.cs b/Public API/Server/Controllers/APIController.cs index 57b43596c4986edc5f1dfb7aeac3c285e85f7433..c25deb45e6db3edf3f08c8b2c1c2b5dc87dbf59a 100644 --- a/Public API/Server/Controllers/APIController.cs +++ b/Public API/Server/Controllers/APIController.cs @@ -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) diff --git a/Public API/Shared/Models/SensorResponse.cs b/Public API/Shared/Models/SensorResponse.cs new file mode 100644 index 0000000000000000000000000000000000000000..912b117faadb37ae563b996f4ed52b7862f6d33a --- /dev/null +++ b/Public API/Shared/Models/SensorResponse.cs @@ -0,0 +1,40 @@ +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; } + } +} diff --git a/Public API/Shared/Models/Tables/Sensor.cs b/Public API/Shared/Models/Tables/Sensor.cs index f296d99a16b1ac42ef639e2e9eacd59e0955ba32..8865977eeda03ab106d5a9258b8656f636017d98 100644 --- a/Public API/Shared/Models/Tables/Sensor.cs +++ b/Public API/Shared/Models/Tables/Sensor.cs @@ -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; } diff --git a/Public API/Shared/Models/Tables/SensorValue.cs b/Public API/Shared/Models/Tables/SensorValue.cs index 46d9b07d4e90447e92f7b403695d2753a232b292..65768a2ccfed676a769f4da9ecb5dfa16858a78f 100644 --- a/Public API/Shared/Models/Tables/SensorValue.cs +++ b/Public API/Shared/Models/Tables/SensorValue.cs @@ -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; } } } diff --git a/Public API/Shared/SQL/1. CREATE TABLES.sql b/Public API/Shared/SQL/1. CREATE TABLES.sql index 6649059c79c5933c9b138a1d7b85dae5f8a2a68e..4acceb69e89b1296325a34a5cbdf21a8648bf236 100644 --- a/Public API/Shared/SQL/1. CREATE TABLES.sql +++ b/Public API/Shared/SQL/1. CREATE TABLES.sql @@ -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