mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 17:51:20 +00:00
100 lines
2.4 KiB
C#
100 lines
2.4 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 : 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();
|
|
}
|
|
}*/
|
|
}
|
|
} |