diff --git a/.vs/MyCore/v15/Server/sqlite3/storage.ide b/.vs/MyCore/v15/Server/sqlite3/storage.ide index 219e8cd..b62b3f2 100644 Binary files a/.vs/MyCore/v15/Server/sqlite3/storage.ide and b/.vs/MyCore/v15/Server/sqlite3/storage.ide differ diff --git a/.vs/MyCore/v15/Server/sqlite3/storage.ide-wal b/.vs/MyCore/v15/Server/sqlite3/storage.ide-wal index 4b5b78b..78bf986 100644 Binary files a/.vs/MyCore/v15/Server/sqlite3/storage.ide-wal and b/.vs/MyCore/v15/Server/sqlite3/storage.ide-wal differ diff --git a/MyCore/Controllers/IOTController.cs b/MyCore/Controllers/IOTController.cs new file mode 100644 index 0000000..05009b4 --- /dev/null +++ b/MyCore/Controllers/IOTController.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using MongoDB.Bson; +using MyCore.Models; +using MyCore.Services; + +namespace MyCore.Controllers +{ + [Authorize(Roles = "Admin")] + [Route("api/iot")] + [ApiController] + public class IOTController : Controller + { + private readonly IoTDeviceService _ioTDeviceService; + + public IOTController(IoTDeviceService ioTDeviceService) + { + _ioTDeviceService = ioTDeviceService; + } + // GET: IOT + /// + /// Retrieve all SmartPrinterMessage + /// + [HttpGet] + public ActionResult> Get(int id) + { + return _ioTDeviceService.Get(); + } + + // POST: IOT/Create + /// + /// It's the method to post data from mqtt broker to Database (Thanks Rpi!) + /// + [HttpPost("{idDevice}")] + public IActionResult PostToDB(int idDevice, [FromBody] SmartPrinterMessage[] content) + { + if(idDevice == 0) { + + foreach (SmartPrinterMessage message in content) { + _ioTDeviceService.Create(message); + } + + return StatusCode(201); + } + + return StatusCode(500); + } + + /*// GET: IOT/Edit/5 + public ActionResult Edit(int id) + { + return View(); + } + + // POST: IOT/Edit/5 + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Edit(int id, IFormCollection collection) + { + try + { + // TODO: Add update logic here + + return RedirectToAction(nameof(Index)); + } + catch + { + return View(); + } + } + + // GET: IOT/Delete/5 + public ActionResult Delete(int id) + { + return View(); + } + + // POST: IOT/Delete/5 + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Delete(int id, IFormCollection collection) + { + try + { + // TODO: Add delete logic here + + return RedirectToAction(nameof(Index)); + } + catch + { + return View(); + } + }*/ + } +} \ No newline at end of file diff --git a/MyCore/Models/SmartPrinterMessage.cs b/MyCore/Models/SmartPrinterMessage.cs new file mode 100644 index 0000000..e4a4071 --- /dev/null +++ b/MyCore/Models/SmartPrinterMessage.cs @@ -0,0 +1,28 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyCore.Models +{ + public class SmartPrinterMessage + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + + [BsonElement("Time")] + public string Time { get; set; } + + [BsonElement("Temperature")] + public double Temperature { get; set; } + + [BsonElement("Pressure")] + public double Pressure { get; set; } + + [BsonElement("Smoke")] + public int Smoke { get; set; } + } +} diff --git a/MyCore/Services/IoTDeviceService.cs b/MyCore/Services/IoTDeviceService.cs new file mode 100644 index 0000000..28ab946 --- /dev/null +++ b/MyCore/Services/IoTDeviceService.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Collections.Generic; +using System.Linq; +using MyCore.Models; +using Microsoft.Extensions.Configuration; +using MongoDB.Driver; + +namespace MyCore.Services +{ + public class IoTDeviceService + { + private readonly IMongoCollection _SmartPrinterMessages; + + public IoTDeviceService(IConfiguration config) + { + var client = new MongoClient(config.GetConnectionString("MyCoreDb")); + var database = client.GetDatabase("MyCoreDb"); + _SmartPrinterMessages = database.GetCollection ("IoTDevices"); + } + + public List Get() + { + return _SmartPrinterMessages.Find(m => true).ToList(); + } + + public SmartPrinterMessage Get(string id) + { + return _SmartPrinterMessages.Find(m => m.Id == id).FirstOrDefault(); + } + + public SmartPrinterMessage Create(SmartPrinterMessage message) + { + _SmartPrinterMessages.InsertOne(message); + return message; + } + + /*public void Update(string id, Book bookIn) + { + _books.ReplaceOne(book => book.Id == id, bookIn); + } + + public void Remove(Book bookIn) + { + _books.DeleteOne(book => book.Id == bookIn.Id); + } + + public void Remove(string id) + { + _books.DeleteOne(book => book.Id == id); + }*/ + } +} diff --git a/MyCore/Startup.cs b/MyCore/Startup.cs index 9fb0be9..2f976f3 100644 --- a/MyCore/Startup.cs +++ b/MyCore/Startup.cs @@ -34,6 +34,7 @@ namespace MyCore { // Add the service (test purpose) services.AddScoped(); + services.AddScoped(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); diff --git a/MyCore/appsettings.json b/MyCore/appsettings.json index 7e9409d..4507b8a 100644 --- a/MyCore/appsettings.json +++ b/MyCore/appsettings.json @@ -1,6 +1,7 @@ { "ConnectionStrings": { - "BookstoreDb": "mongodb://localhost:27017" + "BookstoreDb": "mongodb://localhost:27017", + "MyCoreDb": "mongodb://localhost:27017" }, "Logging": { "LogLevel": { diff --git a/MyCore/bin/Debug/netcoreapp2.1/MyCore.xml b/MyCore/bin/Debug/netcoreapp2.1/MyCore.xml index 7d8ae9c..5e34a12 100644 --- a/MyCore/bin/Debug/netcoreapp2.1/MyCore.xml +++ b/MyCore/bin/Debug/netcoreapp2.1/MyCore.xml @@ -4,6 +4,21 @@ MyCore + + + Retrieve all SmartPrinterMessage + + + + + It's the method to post data from mqtt broker to Database (Thanks Rpi!) + + + + + It's a mqtt publish test ! :) + + It's a test ! :) diff --git a/MyCore/obj/Debug/netcoreapp2.1/MyCore.xml b/MyCore/obj/Debug/netcoreapp2.1/MyCore.xml index 7d8ae9c..5e34a12 100644 --- a/MyCore/obj/Debug/netcoreapp2.1/MyCore.xml +++ b/MyCore/obj/Debug/netcoreapp2.1/MyCore.xml @@ -4,6 +4,21 @@ MyCore + + + Retrieve all SmartPrinterMessage + + + + + It's the method to post data from mqtt broker to Database (Thanks Rpi!) + + + + + It's a mqtt publish test ! :) + + It's a test ! :)