diff --git a/DataVAPI.ServerDB/MongoDB/MongodbHelper.cs b/DataVAPI.ServerDB/MongoDB/MongodbHelper.cs index 5d9508b..3d1d32f 100644 --- a/DataVAPI.ServerDB/MongoDB/MongodbHelper.cs +++ b/DataVAPI.ServerDB/MongoDB/MongodbHelper.cs @@ -175,11 +175,21 @@ namespace DataVAPI.ServerDB.MongoDB return collection.Find(a => a.devicename == DeviceName && a.State == "y")?.ToList().FirstOrDefault(); } + public List QueryClientIdNew(string clientId) + { + return collection.Find(a => a.ClientId == clientId && a.State == "y")?.ToList().OrderByDescending(a => a.CreateTime).ToList(); + } + public List QueryClientId(string clientId) { return collection.Find(a => a.ClientId == clientId && a.CreateTime >= DateTime.Now.Date && a.State == "y")?.ToList().OrderByDescending(a => a.CreateTime).ToList(); } + public T QueryClientIdMax(string clientId) + { + return collection.Find(a => a.ClientId == clientId && a.CreateTime >= DateTime.Now.AddDays(-1).Date && a.State == "y")?.ToList().OrderByDescending(a => a.CreateTime).ToList()[0]; + } + public List QueryDeviceId(string clientId, string deviceId) { return collection.Find(a => a.DeviceId == deviceId && a.ClientId == clientId && a.CreateTime >= DateTime.Now.Date && a.State == "y")?.ToList().OrderByDescending(a => a.CreateTime).ToList(); diff --git a/DataVAPI.Tools/IOT/IOTDevServer.cs b/DataVAPI.Tools/IOT/IOTDevServer.cs index 207a22b..22b21a6 100644 --- a/DataVAPI.Tools/IOT/IOTDevServer.cs +++ b/DataVAPI.Tools/IOT/IOTDevServer.cs @@ -490,6 +490,20 @@ namespace DataVAPI.Tool.IOT } return "192.168.1.124"; } + + /// + /// 获取某个对象中的属性值 + /// + /// + /// + /// + public static object GetPropertyValue(object info, string field) + { + if (info == null) return null; + Type t = info.GetType(); + IEnumerable property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi; + return property.First().GetValue(info, null); + } } diff --git a/DataVAPI/Controllers/AlarmController.cs b/DataVAPI/Controllers/AlarmController.cs index e2f67aa..554d596 100644 --- a/DataVAPI/Controllers/AlarmController.cs +++ b/DataVAPI/Controllers/AlarmController.cs @@ -113,6 +113,24 @@ namespace DataVAPI.Controllers } } /// + /// 根据客户端ID查询最近告警 + /// + /// + /// + [HttpGet] + public JsonMsg QueryClientIdMax(string clientId) + { + st = System.Reflection.MethodBase.GetCurrentMethod().Name; + try + { + return JsonMsg.OK(mg.QueryClientIdMax(clientId), st); + } + catch (Exception ex) + { + return JsonMsg.Error(null, st, ex.Message); + } + } + /// /// 根据设备ID查询当日未处理信息 /// /// diff --git a/DataVAPI/Controllers/DeviceStatsController.cs b/DataVAPI/Controllers/DeviceStatsController.cs new file mode 100644 index 0000000..b3182e9 --- /dev/null +++ b/DataVAPI/Controllers/DeviceStatsController.cs @@ -0,0 +1,124 @@ +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; } + + } +} diff --git a/DataVAPI/UpAndDown/ProcessServer.cs b/DataVAPI/UpAndDown/ProcessServer.cs index a1b1338..1e4f459 100644 --- a/DataVAPI/UpAndDown/ProcessServer.cs +++ b/DataVAPI/UpAndDown/ProcessServer.cs @@ -67,8 +67,12 @@ namespace DataVAPI.UpAndDown { ConsoleHelper.WriteSuccessLine($"阿里云【Transit】连接失败"); } + //属性状态变更 + Subscribe(IOTDevServer.TargetStatusSubTopic); + //订阅上下线 Subscribe(IOTDevServer.HeartbeatSubTopic); + //订阅主播消息 Subscribe(IOTDevServer.BroadcastTopic); @@ -297,8 +301,8 @@ namespace DataVAPI.UpAndDown private void DevIOTActionHandler(string topic, string message) { if (string.IsNullOrEmpty(topic)) return; - if (topic == IOTDevServer.HeartbeatSubTopic)//上下线订阅主题 - { + if (topic == IOTDevServer.HeartbeatSubTopic)//上下线订阅主题 + { ReceiveModel receiveModel = Tools.JsonToObjectTools(message); if (receiveModel != null && receiveModel.status != null)//上下线通知 { @@ -308,8 +312,8 @@ namespace DataVAPI.UpAndDown SentData(receiveModel); } } - } - else if (topic == IOTDevServer.BroadcastTopic)//广播主题 + } + else if (topic == IOTDevServer.BroadcastTopic)//广播主题 { if (message.Contains("AlarmType")) { @@ -320,6 +324,13 @@ namespace DataVAPI.UpAndDown logController.Create(Tools.JsonToObjectTools(message)); } + } else if (topic == IOTDevServer.TargetStatusSubTopic)//属性变更 + { + ReceiveModel rEmodel = Tools.JsonToObjectTools(message); + string name= rEmodel.deviceContext.deviceName; + string status = rEmodel.props.NodeStatus.value; + string id = deviceController.QueryDeviceName(name)?.obj?.data?.ClientId; + screenController.CreateOrUpdate(new LargeScreenTable() { json = status, devicename = name, ClientId = id, DeviceId = "" }); } } }