using Application.Domain.Entities; using Application.Models; using Application.Services; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Jwt; using IoTShared; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using Vibrant.InfluxDB.Client; using Vibrant.InfluxDB.Client.Rows; namespace IoTCenter.Controllers { public class AppController : Controller { private readonly IConfiguration _configuration; private readonly IJwtHelper _jwtHelper; private readonly INodeService _nodeService; private readonly IRepository _nodeRepo; private readonly IRepository _deviceRepo; private readonly IHubContext _pageHubContext; public AppController(IConfiguration configuration, IJwtHelper jwtHelper, INodeService nodeService, IRepository nodeRepo, IRepository deviceRepo, IHubContext pageHubContext) { this._configuration = configuration; this._jwtHelper = jwtHelper; this._nodeService = nodeService; this._nodeRepo = nodeRepo; this._deviceRepo = deviceRepo; this._pageHubContext = pageHubContext; } [Authorize] public IActionResult Index() { return View(); } public IActionResult GetNodes(string token) { //var userName = this._jwtHelper.GetPayload(token)["UserName"].ToString(); var model = this._nodeRepo.ReadOnlyTable() .Select(o => new { o.Id, o.Number, o.Name, o.DisplayOrder, Count = o.Devices.Count }) .ToList(); return Json(model); } public IActionResult GetNode(string token, string number) { //var userName = this._jwtHelper.GetPayload(token)["UserName"].ToString(); var model = this._nodeRepo.ReadOnlyTable() .Include(o => o.Sences) .Include(o => o.Devices).ThenInclude(o => o.Data) .FirstOrDefault(o => o.Number == number); return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } public IActionResult GetDevice(string token, string number) { //var userName = this._jwtHelper.GetPayload(token)["UserName"].ToString(); var model = this._deviceRepo.ReadOnlyTable() .Include(o => o.Data) .Include(o => o.Apis).ThenInclude(o => o.Parameters) .FirstOrDefault(o => o.Number == number); return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } public IActionResult Exec(string connectionId, string node, string cmd, Guid id) { try { var query = string.Empty; foreach (var item in Request.Query.Where(o => o.Key != "node" && o.Key != "cmd" && !o.Key.StartsWith("id"))) { query = query.SetParam(item.Key, item.Value); } return Json(this._nodeService.Exec(id, cmd, query)); } catch (Exception ex) { ex.PrintStack(); return Json(ApiResponse.Error(ex)); } } public IActionResult ExecAll(string connectionId, string node, string cmd, List id) { try { var query = string.Empty; foreach (var item in Request.Query.Where(o => o.Key != "node" && o.Key != "cmd" && !o.Key.StartsWith("id["))) { query = query.SetParam(item.Key, item.Value); } this._nodeService.ExecAll(id, cmd, query); return Json(ApiResponse.AsyncSuccess()); } catch (Exception ex) { ex.PrintStack(); return Json(ApiResponse.Error(ex)); } } public IActionResult Sence(string connectionId, string node, Guid id) { try { this._nodeService.Sence(id); return Json(ApiResponse.AsyncSuccess()); } catch (Exception ex) { ex.PrintStack(); return Json(ApiResponse.Error(ex)); } } public IActionResult Data(Guid id, string time = "10m") { var device = this._deviceRepo.ReadOnlyTable().Include(o => o.Node).Include(o => o.Data).FirstOrDefault(o => o.Id == id); var url = this._configuration["influxdb:url"]; var usr = this._configuration["influxdb:usr"]; var pwd = this._configuration["influxdb:pwd"]; var dbName = "iot"; var measurementName = "data"; var list = new List(); using (var client = new InfluxClient(new Uri(url), usr, pwd)) { var fileds = String.Join(',', device.Data.Where(o => o.Type == "Int" || o.Type == "Float").Select(o => o.Key)); var query = $"select {fileds} from {measurementName} where time>now()-{time} and DeviceNumber = '{device.Number}' limit 10000"; var result = client.ReadAsync(dbName, query).Result; var rows = result.Results.FirstOrDefault()? .Series.FirstOrDefault()? .Rows; var labels = rows?.Select(o => o.Timestamp.Value).Select(o => o.ToString("MM-dd HH:mm:ss")).ToList() ?? new List(); foreach (var data in device.Data.Where(o => o.Type == "Int" || o.Type == "Float")) { list.Add(new { id = data.Key, label = data.Name, labels, data = rows?.Select(o => o.GetField(data.Key)).ToList() ?? new List() }); } } return Json(list); } } }