|
- 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; }
-
- }
- }
|