2023-11-27 00:06:07 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-11-28 21:08:40 +00:00
|
|
|
using MQTTnet;
|
|
|
|
using backend.Application;
|
2023-12-05 22:43:02 +00:00
|
|
|
using backend.Models;
|
2023-11-27 00:06:07 +00:00
|
|
|
|
|
|
|
namespace backend.Controllers;
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
public class DispenserController : ControllerBase
|
|
|
|
{
|
|
|
|
[HttpPost("Dispense")]
|
|
|
|
public void Dispense()
|
|
|
|
{
|
|
|
|
Console.WriteLine("Dispensing..");
|
2023-11-28 21:08:40 +00:00
|
|
|
|
|
|
|
var message = new MqttApplicationMessageBuilder()
|
|
|
|
.WithTopic("dispense")
|
|
|
|
.WithPayload("dispense")
|
|
|
|
.Build();
|
|
|
|
|
2023-12-05 22:43:02 +00:00
|
|
|
ApplicationState.DbContext!.Add(new DispenserLog { Timestamp = DateTime.Now });
|
|
|
|
ApplicationState.DbContext!.SaveChanges();
|
|
|
|
|
2023-11-28 21:08:40 +00:00
|
|
|
ApplicationState.MqttClient!.PublishAsync(message, CancellationToken.None);
|
2023-11-27 00:06:07 +00:00
|
|
|
}
|
|
|
|
}
|