2024-12-04 01:57:29 +04:00
|
|
|
using Cloud.Models;
|
|
|
|
using Cloud.Services.Broker;
|
|
|
|
using Cloud.Services.Broker.Support;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
namespace Cloud.Services.Domain.Implement;
|
|
|
|
|
|
|
|
public class GreenhouseService : IGreenhouseService
|
|
|
|
{
|
|
|
|
private readonly IBrokerService _brokerService;
|
|
|
|
private readonly ApplicationContext _context;
|
|
|
|
|
|
|
|
public GreenhouseService(IBrokerService brokerService, ApplicationContext context)
|
|
|
|
{
|
|
|
|
_context = context;
|
|
|
|
_brokerService = brokerService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<Greenhouse> Create(Greenhouse greenhouse)
|
|
|
|
{
|
|
|
|
var res = await _context.Greenhouses.AddAsync(greenhouse);
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
return res.Entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<Greenhouse> Delete(int id)
|
|
|
|
{
|
|
|
|
var greenhouse = await _context.Greenhouses.FirstOrDefaultAsync(x => x.Id == id);
|
|
|
|
|
|
|
|
_context.Greenhouses.Remove(greenhouse);
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
return greenhouse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<Greenhouse?> GetGreenhouse(int id)
|
|
|
|
{
|
|
|
|
return await _context.Greenhouses.FirstOrDefaultAsync(x => x.Id == id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<Greenhouse> Update(Greenhouse greenhouse)
|
|
|
|
{
|
|
|
|
var res = _context.Greenhouses.Update(greenhouse);
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
return res.Entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IEnumerable<GreenhouseInfo>?> GetAll(int farmId)
|
|
|
|
{
|
2024-12-04 06:27:39 +04:00
|
|
|
// await _changeBrokerIp(farmId);
|
2024-12-04 01:57:29 +04:00
|
|
|
return _brokerService.WaitMessages<GreenhouseInfo>("data");
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<GreenhouseInfo?> GetGreenhouseInfo(int id, int farmId)
|
|
|
|
{
|
2024-12-04 06:27:39 +04:00
|
|
|
// await _changeBrokerIp(farmId);
|
2024-12-04 01:57:29 +04:00
|
|
|
var infos = _brokerService.WaitMessages<GreenhouseInfo>("data");
|
|
|
|
return infos?.FirstOrDefault(x => x.Id == id);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task _changeBrokerIp(int farmId)
|
|
|
|
{
|
|
|
|
var farm = await _context.Farms.FirstOrDefaultAsync(x => x.Id == farmId);
|
|
|
|
_brokerService.ChangeBrokerIp(farm.RaspberryIP);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|