@@ -175,11 +175,21 @@ namespace DataVAPI.ServerDB.MongoDB | |||
return collection.Find(a => a.devicename == DeviceName && a.State == "y")?.ToList().FirstOrDefault(); | |||
} | |||
public List<T> QueryClientIdNew(string clientId) | |||
{ | |||
return collection.Find(a => a.ClientId == clientId && a.State == "y")?.ToList().OrderByDescending(a => a.CreateTime).ToList(); | |||
} | |||
public List<T> 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<T> 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(); | |||
@@ -490,6 +490,20 @@ namespace DataVAPI.Tool.IOT | |||
} | |||
return "192.168.1.124"; | |||
} | |||
/// <summary> | |||
/// 获取某个对象中的属性值 | |||
/// </summary> | |||
/// <param name="info"></param> | |||
/// <param name="field"></param> | |||
/// <returns></returns> | |||
public static object GetPropertyValue(object info, string field) | |||
{ | |||
if (info == null) return null; | |||
Type t = info.GetType(); | |||
IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi; | |||
return property.First().GetValue(info, null); | |||
} | |||
} | |||
@@ -113,6 +113,24 @@ namespace DataVAPI.Controllers | |||
} | |||
} | |||
/// <summary> | |||
/// 根据客户端ID查询最近告警 | |||
/// </summary> | |||
/// <param name="clientId"></param> | |||
/// <returns></returns> | |||
[HttpGet] | |||
public JsonMsg<AlarmTable> QueryClientIdMax(string clientId) | |||
{ | |||
st = System.Reflection.MethodBase.GetCurrentMethod().Name; | |||
try | |||
{ | |||
return JsonMsg<AlarmTable>.OK(mg.QueryClientIdMax(clientId), st); | |||
} | |||
catch (Exception ex) | |||
{ | |||
return JsonMsg<AlarmTable>.Error(null, st, ex.Message); | |||
} | |||
} | |||
/// <summary> | |||
/// 根据设备ID查询当日未处理信息 | |||
/// </summary> | |||
/// <param name="clientId"></param> | |||
@@ -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 | |||
{ | |||
/// <summary> | |||
/// MG数据库:统计接口 | |||
/// </summary> | |||
public class DeviceStatsController : BaseController | |||
{ | |||
MongoDbHelper<LargeScreenTable> mgLA = new MongoDbHelper<LargeScreenTable>(DataBus.connStr, DataBus.dbName); | |||
MongoDbHelper<AlarmTable> mggj = new MongoDbHelper<AlarmTable>(DataBus.connStr, DataBus.dbName); | |||
MongoDbHelper<LogTable> mglog = new MongoDbHelper<LogTable>(DataBus.connStr, DataBus.dbName); | |||
MongoDbHelper<DeviceTable> mgsb = new MongoDbHelper<DeviceTable>(DataBus.connStr, DataBus.dbName); | |||
string st = System.Reflection.MethodBase.GetCurrentMethod().Name; | |||
/// <summary> | |||
/// 根据客户端ID查询最近上下线与告警时间 | |||
/// </summary> | |||
/// <param name="clientId"></param> | |||
/// <returns></returns> | |||
[HttpGet] | |||
public JsonMsg<object> 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<LogTable> 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<object>.OK(obj, st); | |||
} | |||
catch (Exception ex) | |||
{ | |||
return JsonMsg<object>.Error(null, st, ex.Message); | |||
} | |||
} | |||
/// <summary> | |||
/// 查询设备属性状态 | |||
/// </summary> | |||
/// <param name="clientId"></param> | |||
/// <returns></returns> | |||
[HttpGet] | |||
public JsonMsg<object> StatsTargetStatus(string clientId,string deviceId) | |||
{ | |||
object obj; List<returnStatus> returns = new List<returnStatus>(); | |||
st = System.Reflection.MethodBase.GetCurrentMethod().Name; | |||
try | |||
{ | |||
List<LargeScreenTable> largeScreens = mgLA.QueryClientIdNew(clientId); | |||
if (largeScreens.Count > 0) | |||
{ | |||
LargeScreenTable largeScreen = largeScreens[0]; | |||
if (largeScreen.json != null && largeScreen.json.Contains("data")) | |||
{ | |||
TargetStatus _Target = Tools.JsonToObjectTools<TargetStatus>(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<object>.OK(obj, st); | |||
} | |||
catch (Exception ex) | |||
{ | |||
return JsonMsg<object>.Error(null, st, ex.Message); | |||
} | |||
} | |||
} | |||
public class TargetStatus | |||
{ | |||
public List<TargetStatusList> 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<string, object> Status { get; set; } | |||
} | |||
public class returnStatus | |||
{ | |||
public string Name { get; set; } | |||
public object Status { get; set; } | |||
public string Ms { get; set; } | |||
} | |||
} |
@@ -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<ReceiveModel>(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<LogTable>(message)); | |||
} | |||
} else if (topic == IOTDevServer.TargetStatusSubTopic)//属性变更 | |||
{ | |||
ReceiveModel rEmodel = Tools.JsonToObjectTools<ReceiveModel>(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 = "" }); | |||
} | |||
} | |||
} | |||