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,string name)
{
st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
string gj_time = "未知";
string sx_time = "未知";
string xx_time = "未知";
AlarmTable alarmTable = new AlarmTable();
List log = new List();
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 list = new List();
list.Add(new
{
AlarmTime = gj_time,
OnlineTime = sx_time,
OfflineTime = xx_time
});
return JsonMsg.OK(list, 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)
{
//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.OK(obj, st);
}
catch (Exception ex)
{
return JsonMsg.Error(null, st, ex.Message);
}
}
///
/// 统计数量
///
///
///
///
[HttpGet]
public JsonMsg StatsConut(string clientId,string name)
{
st = System.Reflection.MethodBase.GetCurrentMethod().Name;
try
{
List log = new List();
List alarm = new List();
if (string.IsNullOrEmpty(name))
{
log = mglog.ALLData(clientId);
alarm = mggj.ALLData(clientId);
}
else
{
log = mglog.ALLDataName(clientId,name);
alarm = mggj.ALLDataName(clientId, name);
}
List list = new List();
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.OK(list, st);
}
catch (Exception ex)
{
return JsonMsg.Error(null, st, ex.Message);
}
}
public Dictionary keyValues = new Dictionary()
{
{"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 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; }
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 };
}
}
}