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 /// /// Id of the smart printer message [HttpGet("smartprinter/{idDevice}")] public ActionResult> GetSmartPrinterMessages(int id) { return _ioTDeviceService.GetSmartPrinterMessages(); } // POST: IOT/Create /// /// It's the method to post data from mqtt broker to Database (Thanks Rpi!) /// /// Id of the device to upload to DB /// Content that will be uploaded /// Content successfully posted to DB /// Unexpected error [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 /// /// It's the method to post data from mqtt broker to Database (Thanks Rpi!) /// [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(); } }*/ } }