2019-07-14 02:39:28 +02:00

129 lines
3.6 KiB
C#

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 : ControllerBase
{
private readonly IoTDeviceService _ioTDeviceService;
public IOTController(IoTDeviceService ioTDeviceService)
{
_ioTDeviceService = ioTDeviceService;
}
// GET: IOT
/// <summary>
/// Retrieve all SmartPrinterMessage
/// </summary>
/// <param name="id">Id of the smart printer message</param>
[HttpGet("smartprinter/{idDevice}")]
public ActionResult<List<SmartPrinterMessage>> GetSmartPrinterMessages(int id)
{
return _ioTDeviceService.GetSmartPrinterMessages();
}
// POST: IOT/Create
/// <summary>
/// It's the method to post data from mqtt broker to Database (Thanks Rpi!)
/// </summary>
/// <param name="idDevice">Id of the device to upload to DB</param>
/// <param name="content">Content that will be uploaded</param>
/// <response code="201">Content successfully posted to DB</response>
/// <response code="500">Unexpected error</response>
[HttpPost("smartprinter/{idDevice}")]
public IActionResult PostToDBPrinter(int idDevice, [FromBody] SmartPrinterMessage[] content)
{
if(idDevice == 0)
{
foreach (SmartPrinterMessage message in content) {
message.Type = "SmartPrinter";
_ioTDeviceService.CreateSmartPrinterMessage(message);
}
return StatusCode(201);
}
return StatusCode(500);
}
// POST: IOT/Create
/// <summary>
/// It's the method to post data from mqtt broker to Database (Thanks Rpi!)
/// </summary>
[HttpPost("smartgarden/{idDevice}")]
public IActionResult PostToDBSmartGarden(int idDevice, [FromBody] SmartGardenMessage[] content)
{
if (idDevice == 0)
{
foreach (SmartGardenMessage message in content)
{
message.Type = "SmartGarden";
_ioTDeviceService.CreateSmartGardenMessage(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();
}
}*/
}
}