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.
125 lines
4.7 KiB
125 lines
4.7 KiB
using Application.Domain.Entities;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using IoTCenter.ViewModels;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Drawing;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using Vibrant.InfluxDB.Client;
|
|
using Vibrant.InfluxDB.Client.Rows;
|
|
|
|
namespace UserCenter.Controllers
|
|
{
|
|
[ApiVersion("1.0")]
|
|
[Route("api/v{version:apiVersion}/[controller]/[action]")]
|
|
[ApiController]
|
|
public class DeviceController : ControllerBase
|
|
{
|
|
private readonly IConfiguration _cfg;
|
|
private readonly IRepository<Device> _deviceRepo;
|
|
|
|
public DeviceController(IConfiguration cfg,
|
|
IRepository<Device> deviceRepo)
|
|
{
|
|
this._cfg = cfg;
|
|
this._deviceRepo = deviceRepo;
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult GetDevice([Required]string number)
|
|
{
|
|
try
|
|
{
|
|
var model = this._deviceRepo.ReadOnlyTable()
|
|
.Include(o => o.Data)
|
|
.Include(o => o.Product)
|
|
.ThenInclude(o => o.Apis)
|
|
.ThenInclude(o => o.Parameters)
|
|
.Where(o => o.Number == number)
|
|
.FirstOrDefault();
|
|
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 url = this._cfg["influxdb:url"];
|
|
var usr = this._cfg["influxdb:usr"];
|
|
var pwd = this._cfg["influxdb:pwd"];
|
|
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 = new InfluxClient(new Uri(url), usr, pwd))
|
|
{
|
|
var query = $"select { model.Key} from {measurementName} where time>now() - {model.Key} and DeviceNumber = '{model.Number}' limit 10000";
|
|
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(data.Key)).ToList() : new List<object>(),
|
|
backgroundColor = this.GetColor(data.Key),
|
|
fill = false
|
|
});
|
|
if (rows != null)
|
|
{
|
|
var format = model.Time.StartsWith("1") ? "H:mm" : (model.Time.StartsWith("7") ? "ddd" : "MMM-d");
|
|
labels = rows.Select(o => o.Timestamp.Value).Select(o => o.AddHours(hours).ToString(format, 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;
|
|
}
|
|
}
|
|
} |