mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
WebService Serveur (API) - Json FromBody handle
This commit is contained in:
parent
d010167dff
commit
6370257154
Binary file not shown.
Binary file not shown.
100
MyCore/Controllers/IOTController.cs
Normal file
100
MyCore/Controllers/IOTController.cs
Normal file
@ -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
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve all SmartPrinterMessage
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet]
|
||||||
|
public ActionResult<List<SmartPrinterMessage>> Get(int id)
|
||||||
|
{
|
||||||
|
return _ioTDeviceService.Get();
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST: IOT/Create
|
||||||
|
/// <summary>
|
||||||
|
/// It's the method to post data from mqtt broker to Database (Thanks Rpi!)
|
||||||
|
/// </summary>
|
||||||
|
[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();
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
}
|
||||||
28
MyCore/Models/SmartPrinterMessage.cs
Normal file
28
MyCore/Models/SmartPrinterMessage.cs
Normal file
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
55
MyCore/Services/IoTDeviceService.cs
Normal file
55
MyCore/Services/IoTDeviceService.cs
Normal file
@ -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<SmartPrinterMessage> _SmartPrinterMessages;
|
||||||
|
|
||||||
|
public IoTDeviceService(IConfiguration config)
|
||||||
|
{
|
||||||
|
var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
|
||||||
|
var database = client.GetDatabase("MyCoreDb");
|
||||||
|
_SmartPrinterMessages = database.GetCollection<SmartPrinterMessage> ("IoTDevices");
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SmartPrinterMessage> Get()
|
||||||
|
{
|
||||||
|
return _SmartPrinterMessages.Find(m => true).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public SmartPrinterMessage Get(string id)
|
||||||
|
{
|
||||||
|
return _SmartPrinterMessages.Find<SmartPrinterMessage>(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);
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -34,6 +34,7 @@ namespace MyCore
|
|||||||
{
|
{
|
||||||
// Add the service (test purpose)
|
// Add the service (test purpose)
|
||||||
services.AddScoped<BookService>();
|
services.AddScoped<BookService>();
|
||||||
|
services.AddScoped<IoTDeviceService>();
|
||||||
|
|
||||||
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
|
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"BookstoreDb": "mongodb://localhost:27017"
|
"BookstoreDb": "mongodb://localhost:27017",
|
||||||
|
"MyCoreDb": "mongodb://localhost:27017"
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
|
|||||||
@ -4,6 +4,21 @@
|
|||||||
<name>MyCore</name>
|
<name>MyCore</name>
|
||||||
</assembly>
|
</assembly>
|
||||||
<members>
|
<members>
|
||||||
|
<member name="M:MyCore.Controllers.IOTController.Get(System.Int32)">
|
||||||
|
<summary>
|
||||||
|
Retrieve all SmartPrinterMessage
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="M:MyCore.Controllers.IOTController.PostToDB(System.Int32,MyCore.Models.SmartPrinterMessage[])">
|
||||||
|
<summary>
|
||||||
|
It's the method to post data from mqtt broker to Database (Thanks Rpi!)
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="M:MyCore.Controllers.MQTTController.GetToPublishMqtt">
|
||||||
|
<summary>
|
||||||
|
It's a mqtt publish test ! :)
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
<member name="M:MyCore.Controllers.ValuesController.Get">
|
<member name="M:MyCore.Controllers.ValuesController.Get">
|
||||||
<summary>
|
<summary>
|
||||||
It's a test ! :)
|
It's a test ! :)
|
||||||
|
|||||||
@ -4,6 +4,21 @@
|
|||||||
<name>MyCore</name>
|
<name>MyCore</name>
|
||||||
</assembly>
|
</assembly>
|
||||||
<members>
|
<members>
|
||||||
|
<member name="M:MyCore.Controllers.IOTController.Get(System.Int32)">
|
||||||
|
<summary>
|
||||||
|
Retrieve all SmartPrinterMessage
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="M:MyCore.Controllers.IOTController.PostToDB(System.Int32,MyCore.Models.SmartPrinterMessage[])">
|
||||||
|
<summary>
|
||||||
|
It's the method to post data from mqtt broker to Database (Thanks Rpi!)
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="M:MyCore.Controllers.MQTTController.GetToPublishMqtt">
|
||||||
|
<summary>
|
||||||
|
It's a mqtt publish test ! :)
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
<member name="M:MyCore.Controllers.ValuesController.Get">
|
<member name="M:MyCore.Controllers.ValuesController.Get">
|
||||||
<summary>
|
<summary>
|
||||||
It's a test ! :)
|
It's a test ! :)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user