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.
156 lines
6.0 KiB
156 lines
6.0 KiB
using Application.Domain.Entities;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Platform.Areas.IoTCenter.Controllers;
|
|
using Platform.Services;
|
|
using Platform.ViewModels;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using Vibrant.InfluxDB.Client;
|
|
using Vibrant.InfluxDB.Client.Rows;
|
|
|
|
namespace Platform.Api.Api
|
|
{
|
|
[ApiVersion("1.0")]
|
|
[Route("api/v{version:apiVersion}/[controller]/[action]")]
|
|
[ApiController]
|
|
public class DeviceController : ControllerBase
|
|
{
|
|
private readonly IConfiguration _cfg;
|
|
private readonly AjaxController _ajax;
|
|
private readonly IRepository<IoTDevice> _deviceRepo;
|
|
|
|
public DeviceController(IConfiguration cfg,
|
|
AjaxController ajax,
|
|
IRepository<IoTDevice> deviceRepo)
|
|
{
|
|
this._cfg = cfg;
|
|
this._ajax = ajax;
|
|
this._deviceRepo = deviceRepo;
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult GetDevices([FromBody] QueryDeviceModel model)
|
|
{
|
|
try
|
|
{
|
|
var organId = User.GetOrganId().Value;
|
|
var query = this._deviceRepo.ReadOnlyTable()
|
|
.Where(o => o.IoTGateway.Building.OrganId == organId)
|
|
.WhereIf(model.BuildingId.HasValue, o => o.IoTGateway.BuildingId == model.BuildingId.Value)
|
|
.WhereIf(model.CategoryId.HasValue, o => o.IoTProduct.IoTProductCategoryId == model.CategoryId.Value)
|
|
.WhereIf(!string.IsNullOrWhiteSpace(model.Name), o => o.Name.Contains(model.Name) || o.DisplayName.Contains(model.Name))
|
|
.Include(o=>o.Data)
|
|
.Include(o => o.IoTProduct.IoTProductCategory)
|
|
.Include(o => o.IoTGateway.Building);
|
|
model.TotalCount = query.Count();
|
|
model.List.AddRange(query.Skip(model.PageSize * (model.PageIndex - 1)).Take(model.PageSize));
|
|
model.Building = this._ajax.GetBuilding(organId, model.BuildingId).SelectList();
|
|
model.Category = this._ajax.GetIoTProductCategory(model.CategoryId).SelectList();
|
|
return Ok(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Problem(ex.Message);
|
|
}
|
|
}
|
|
|
|
public IActionResult GetDevice(Guid id)
|
|
{
|
|
try
|
|
{
|
|
var model = this._deviceRepo.ReadOnlyTable()
|
|
.Include(o => o.IoTProduct)
|
|
.Include(o => o.Data)
|
|
.Where(o => o.Id == id)
|
|
.FirstOrDefault();
|
|
model.Data = model.Data.OrderBy(o => o.Key).ToList();
|
|
return Ok(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Problem(ex.Message);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult GetChartData([FromBody] ChartDataRequest model)
|
|
{
|
|
try
|
|
{
|
|
var device = this._deviceRepo.ReadOnlyTable().Include(o => o.Data).FirstOrDefault(o => o.Number == model.Number);
|
|
var data = device.Data.FirstOrDefault(o => o.Key == model.Key);
|
|
var dbName = "iot";
|
|
var measurementName = "data";
|
|
var datasets = new List<object>();
|
|
var labels = new List<string>();
|
|
var hours = Convert.ToInt32(DateTime.Now.ToString("%z"));
|
|
using var client = InfluxDBHelper.CreateClient(this._cfg);
|
|
var days = Convert.ToInt32(model.Time.TrimEnd('d'));
|
|
var group = model.Group;
|
|
if (string.IsNullOrEmpty(group))
|
|
{
|
|
group = days == 1 ? "1m" : (days == 7 ? "10m" : "1h");
|
|
}
|
|
var query = $"select last({model.Key}) from {measurementName} where time>now() - {days}d and DeviceNumber = '{model.Number}' group by time({group}) fill(none) tz('Asia/Shanghai')";
|
|
var result = client.ReadAsync<DynamicInfluxRow>(dbName, query).Result;
|
|
var rows = result.Results.FirstOrDefault()?
|
|
.Series.FirstOrDefault()?
|
|
.Rows;
|
|
datasets.Add(new
|
|
{
|
|
label = data.Name,
|
|
data = rows != null ? rows.Select(o => o.GetField("last")).ToList() : new List<object>(),
|
|
backgroundColor = this.GetColor(data.Key),
|
|
fill = false
|
|
});
|
|
if (rows != null)
|
|
{
|
|
labels = rows.Select(o => o.Timestamp.Value).Select(o => o.AddHours(hours).ToString("MMM-d H:mm", new CultureInfo("zh-CN"))).ToList();
|
|
}
|
|
var response = new
|
|
{
|
|
datasets,
|
|
labels
|
|
};
|
|
return Ok(response);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Problem(ex.Message);
|
|
}
|
|
}
|
|
|
|
private string GetColor(string key)
|
|
{
|
|
//var randomGen = new Random();
|
|
//var names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
|
|
//var randomColorName = names[randomGen.Next(names.Length)];
|
|
//var randomColor = Color.FromKnownColor(randomColorName);
|
|
//return randomColor.Name;
|
|
if (key == "Humidity")
|
|
{
|
|
return Color.FromKnownColor(KnownColor.Green).Name;
|
|
}
|
|
else if (key == "Electricity")
|
|
{
|
|
return Color.FromKnownColor(KnownColor.Red).Name;
|
|
}
|
|
else if (key == "Light")
|
|
{
|
|
return Color.FromKnownColor(KnownColor.Orange).Name;
|
|
}
|
|
return Color.FromKnownColor(KnownColor.DarkBlue).Name;
|
|
}
|
|
}
|
|
}
|