Cucumber/Cloud/Controllers/ValveController.cs

47 lines
1.1 KiB
C#

using Cloud.Models;
using Cloud.Requests;
using Cloud.Services;
using Cloud.Services.Broker;
using Cloud.Services.Broker.Implement.Kafka;
using Cloud.Services.Broker.Support;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
namespace Cloud.Controllers
{
[Authorize]
[ApiController]
[Route("api")]
public class ValveController : ControllerBase
{
//Контроллер вентиля
private readonly IBrokerService _kafkaService;
public ValveController(KafkaService kafkaService)
{
_kafkaService = kafkaService;
}
[HttpPost("farm/{farmId}/greenhouse/{ghId}/watering")]
public async Task<IActionResult> interactValve([FromBody] ValveRequest request, int farmId, int ghId)
{
var command = new Command
{
request_id = Guid.NewGuid(),
greenhouse_id = ghId,
farm_id = farmId,
command = "valve",
target_moisture = request.Moisture
};
await _kafkaService.ProduceAsync("commands", command);
return Ok($"Valve target moisture is set as {request.Moisture}%");
}
}
}