using DataVAPI.Model; using DataVAPI.ModelDataBus; using DataVAPI.ServerDB.MongoDB; using DataVAPI.Tool.IOT; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; namespace DataVAPI.Controllers { /// /// MG数据库:统计接口 /// public class DeviceStatsController : BaseController { MongoDbHelper mgLA = new MongoDbHelper(DataBus.connStr, DataBus.dbName); MongoDbHelper mggj = new MongoDbHelper(DataBus.connStr, DataBus.dbName); MongoDbHelper mglog = new MongoDbHelper(DataBus.connStr, DataBus.dbName); MongoDbHelper mgsb = new MongoDbHelper(DataBus.connStr, DataBus.dbName); string st = System.Reflection.MethodBase.GetCurrentMethod().Name; /// /// 根据客户端ID查询最近上下线与告警时间 /// /// /// [HttpGet] public JsonMsg StatsOnOff(string clientId) { st = System.Reflection.MethodBase.GetCurrentMethod().Name; try { string gj_time="未知"; string sx_time = "未知"; string xx_time = "未知"; AlarmTable alarmTable = mggj.QueryClientIdMax(clientId); List log = mglog.QueryClientId(clientId); if (alarmTable != null) gj_time = alarmTable.AlarmTime; LogTable logTable1= log.Find(par => par.LogMessage.Contains("设备在线了")); LogTable logTable2 = log.Find(par => par.LogMessage.Contains("设备离线了")); if(logTable1!=null) sx_time = logTable1.LogTime; if (logTable2 != null) xx_time = logTable2.LogTime; object obj = new { AlarmTime=gj_time, OnlineTime=sx_time, OfflineTime=xx_time }; return JsonMsg.OK(obj, st); } catch (Exception ex) { return JsonMsg.Error(null, st, ex.Message); } } /// /// 查询设备属性状态 /// /// /// [HttpGet] public JsonMsg StatsTargetStatus(string clientId,string deviceId) { object obj; List returns = new List(); st = System.Reflection.MethodBase.GetCurrentMethod().Name; try { List largeScreens = mgLA.QueryClientIdNew(clientId); if (largeScreens.Count > 0) { LargeScreenTable largeScreen = largeScreens[0]; if (largeScreen.json != null && largeScreen.json.Contains("data")) { TargetStatus _Target = Tools.JsonToObjectTools(largeScreen.json); TargetStatusList statusList= _Target?.data?.Find(ar => ar.DeviceId.ToString()== deviceId); if (statusList != null) { foreach (var item in statusList.Status) { returns.Add(new returnStatus { Name = item.Key, Status = item.Value,Ms = ""}); } } } } obj = new { data = returns }; return JsonMsg.OK(obj, st); } catch (Exception ex) { return JsonMsg.Error(null, st, ex.Message); } } } public class TargetStatus { public List data { get; set; } } public class TargetStatusList { public object DeviceId { get; set; } public object Name { get; set; } public object DeviceType { get; set; } public object IsBusy { get; set; } public object IsBusyColor { get; set; } public object IsHealth { get; set; } public object IsHealthColor { get; set; } public Dictionary Status { get; set; } } public class returnStatus { public string Name { get; set; } public object Status { get; set; } public string Ms { get; set; } } }