|
- 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>
- /// <param name="name"></param>
- /// <returns></returns>
- [HttpGet]
- public JsonMsg<object> StatsOnOff(string clientId,string name)
- {
- st = System.Reflection.MethodBase.GetCurrentMethod().Name;
- try
- {
- string gj_time = "未知";
- string sx_time = "未知";
- string xx_time = "未知";
-
- AlarmTable alarmTable = new AlarmTable();
- List<LogTable> log = new List<LogTable>();
- if (!string.IsNullOrEmpty(name))
- {
- alarmTable = mggj.QueryClientIdMaxName(clientId,name);
- log = mglog.QueryClientIdName(clientId, name);
- }
- else
- {
- alarmTable = mggj.QueryClientIdMax(clientId);
- 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;
-
- List<object> list = new List<object>();
- list.Add(new
- {
- AlarmTime = gj_time,
- OnlineTime = sx_time,
- OfflineTime = xx_time
- });
-
- return JsonMsg<object>.OK(list, 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)
- {
- //PLC.MorksMachine.IsConnected
-
- string name = item.Key;
- string Ms = string.Empty;
- if (item.Key.Contains("."))
- {
- string sta = item.Key.Split('.')[0];
- string sop = item.Key.Split('.')[item.Key.Split('.').Length - 1]; name = sop;
- string value = item.Value.ToString();
- if (keyValues.ContainsKey(sta)) sta = keyValues[sta];
- if (keyValues.ContainsKey(sop)) sop = keyValues[sop];
- if (keyValues.ContainsKey(value)) value = keyValues[value];
-
- Ms = $"[{sta}]-[{sop}]-[{value}]";
- }
-
- returns.Add(new returnStatus { Name = name, Status = item.Value.ToString(), Ms = Ms });
- }
- }
- }
- }
- obj = new
- {
- data = returns
- };
- return JsonMsg<object>.OK(obj, st);
- }
- catch (Exception ex)
- {
- return JsonMsg<object>.Error(null, st, ex.Message);
- }
- }
-
- /// <summary>
- /// 统计数量
- /// </summary>
- /// <param name="clientId"></param>
- /// <param name="name"></param>
- /// <returns></returns>
- [HttpGet]
- public JsonMsg<object> StatsConut(string clientId,string name)
- {
- st = System.Reflection.MethodBase.GetCurrentMethod().Name;
- try
- {
- List<LogTable> log = new List<LogTable>();
- List<AlarmTable> alarm = new List<AlarmTable>();
- if (string.IsNullOrEmpty(name))
- {
- log = mglog.ALLData(clientId);
- alarm = mggj.ALLData(clientId);
- }
- else
- {
- log = mglog.ALLDataName(clientId,name);
- alarm = mggj.ALLDataName(clientId, name);
- }
- List<object> list = new List<object>();
- int? count = log?.FindAll(par => par.LogMessage.Contains("设备在线了")).Count;
-
- //LogTable log1 = log?.Find(par => par.LogMessage.Contains("设备在线了"));
- //LogTable log2 = log?.Find(par => par.LogMessage.Contains("设备离线了"));
-
- list.Add(new { Name = "运行次数", Count = count?.ToString()});
- list.Add(new { Name = "告警次数", Count = alarm?.Count });
- list.Add(new { Name = "日警次数", Count = alarm?.FindAll(par=>par.CreateTime>=DateTime.Now.Date)?.Count });
-
- //if (log1 != null && log2 != null)
- //{
- // double dob = Math.Abs((log1.CreateTime - log2.CreateTime).TotalMinutes);
- // list.Add(new { Name = "故障次数", Count = 0 });
- // list.Add(new { Name = "故障率", Count = $"{(dob / 120)*100/100}%" });
- //}
- //else
- //{
- // list.Add(new { Name = "告警次数", Count = 0 });
-
- //}
- return JsonMsg<object>.OK(list, st);
- }
- catch (Exception ex)
- {
- return JsonMsg<object>.Error(null, st, ex.Message);
- }
- }
-
-
- public Dictionary<string, string> keyValues = new Dictionary<string, string>()
- {
- {"PLC","煮面机机器人" },
- {"Lebai","乐白机器人" },
- {"SCChip","单片机" },
- {"DRCoffee","咖博士咖啡机器" },
- {"KLMCoffee","伽乐美咖啡机器" },
- {"GSIceCream","乐白机器人" },
-
- {"IsConnected","连接状态" },
- {"IsWork","工作状态" },
-
-
- {"CompletedOpen_SE_1","舵机1打开" },
- {"CompletedOpen_SE_2","舵机2打开" },
- {"CompletedOpen_SE_3","舵机3打开" },
- {"CompletedClose_SE_1","舵机1关闭" },
- {"CompletedClose_SE_2","舵机2关闭" },
- {"CompletedClose_SE_3","舵机3关闭" },
-
- {"False","异常" },
- {"True","正常" }
-
- };
- }
-
- 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; }
- private string _status { get; set; }
- public string Status
- {
- get { return _status; }
- set
- {
- _status = value;
- if (_status == "False" || _status == "True")
- {
- if (_status == "True")
- StatusColor = new ALYColor { r = 51, g = 232, b = 34, a = 1 };
- else
- StatusColor = new ALYColor { r = 255, g = 0, b = 0, a = 1 };
- }
- }
- }
- public string Ms { get; set; }
- public ALYColor StatusColor { get; set; }
- public returnStatus()
- {
- //StatusColor = new ALYColor { r = 255,g = 0,b = 0,a = 1};
- StatusColor = new ALYColor { r = 51, g = 232, b = 34, a = 1 };
- }
- }
- }
|