You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
5.1 KiB
130 lines
5.1 KiB
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using IoT.Shared.Application.Domain.Entities;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
|
|
namespace Platform.Apis.Controllers
|
|
{
|
|
[ApiVersion("1.0")]
|
|
[Route("api/v{version:apiVersion}/[controller]/[action]")]
|
|
[ApiController]
|
|
public class ProductController : ControllerBase
|
|
{
|
|
private readonly IRepository<BuildingIoTGateway> _buildingIoTGatewayRepo;
|
|
private readonly IRepository<IoTProduct> _productRepo;
|
|
private readonly IRepository<IoTGateway> _nodeRepo;
|
|
private readonly IRepository<IoTDevice> _deviceRepo;
|
|
|
|
public ProductController(IRepository<BuildingIoTGateway> buildingIoTGatewayRepo,
|
|
IRepository<IoTProduct> productRepo,
|
|
IRepository<IoTGateway> nodeRepo,
|
|
IRepository<IoTDevice> deviceRepo)
|
|
{
|
|
this._buildingIoTGatewayRepo = buildingIoTGatewayRepo;
|
|
this._productRepo = productRepo;
|
|
this._nodeRepo = nodeRepo;
|
|
this._deviceRepo = deviceRepo;
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult GetProducts(string organNumber)
|
|
{
|
|
try
|
|
{
|
|
var model = this._buildingIoTGatewayRepo.ReadOnlyTable()
|
|
.WhereIf(!string.IsNullOrEmpty(organNumber), o => o.Building.Organ.Number == organNumber)
|
|
.SelectMany(o => o.IoTGateway.Devices)
|
|
.GroupBy(o => new
|
|
{
|
|
o.Product.Id,
|
|
o.Product.Name,
|
|
o.Product.Number,
|
|
o.Product.Image,
|
|
o.Product.DisplayOrder
|
|
})
|
|
.Select(o => new
|
|
{
|
|
o.Key.Id,
|
|
o.Key.Name,
|
|
o.Key.Number,
|
|
o.Key.Image,
|
|
o.Key.DisplayOrder,
|
|
Count = o.Count()
|
|
})
|
|
.ToList();
|
|
return Ok(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Problem(ex.Message);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult GetProduct([Required(ErrorMessage = nameof(RequiredAttribute))] string number, string organNumber)
|
|
{
|
|
try
|
|
{
|
|
var model = this._productRepo.ReadOnlyTable().Where(o => o.Number == number).FirstOrDefault();
|
|
model.Devices = this._buildingIoTGatewayRepo.ReadOnlyTable()
|
|
.WhereIf(!string.IsNullOrEmpty(organNumber), o => o.Building.Organ.Number == organNumber)
|
|
.SelectMany(o => o.IoTGateway.Devices)
|
|
.Include(o => o.Node)
|
|
.Include(o => o.Data)
|
|
.ToList()
|
|
.Where(o => o.ProductId == model.Id)
|
|
.ToList();
|
|
return Ok(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Problem(ex.Message);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult GetSummary(string organNumber)
|
|
{
|
|
try
|
|
{
|
|
var products = this._buildingIoTGatewayRepo.ReadOnlyTable()
|
|
.WhereIf(!string.IsNullOrEmpty(organNumber), o => o.Building.Organ.Number == organNumber)
|
|
.SelectMany(o => o.IoTGateway.Devices)
|
|
.GroupBy(o => o.Product.Name)
|
|
.Select(o => new
|
|
{
|
|
Name = o.Key,
|
|
Online = o.Sum(c => c.IsOnline ? 1 : 0),
|
|
Offline = o.Sum(c => c.IsOnline ? 0 : 1),
|
|
})
|
|
.ToList()
|
|
.Select(o => new
|
|
{
|
|
o.Name,
|
|
o.Online,
|
|
o.Offline,
|
|
Total = o.Online + o.Offline
|
|
});
|
|
return Ok(new
|
|
{
|
|
Total = products.Sum(o => o.Total),
|
|
Online = products.Sum(o => o.Online),
|
|
Offline = products.Sum(o => o.Offline),
|
|
Products = products
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Problem(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|