You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
9.8 KiB

  1. using DataVAPI.Model;
  2. using DataVAPI.ModelDataBus;
  3. using DataVAPI.ServerDB.MongoDB;
  4. using DataVAPI.Tool.IOT;
  5. using Microsoft.AspNetCore.Mvc;
  6. using System;
  7. using System.Collections.Generic;
  8. namespace DataVAPI.Controllers
  9. {
  10. /// <summary>
  11. /// MG数据库:统计接口
  12. /// </summary>
  13. public class DeviceStatsController : BaseController
  14. {
  15. MongoDbHelper<LargeScreenTable> mgLA = new MongoDbHelper<LargeScreenTable>(DataBus.connStr, DataBus.dbName);
  16. MongoDbHelper<AlarmTable> mggj = new MongoDbHelper<AlarmTable>(DataBus.connStr, DataBus.dbName);
  17. MongoDbHelper<LogTable> mglog = new MongoDbHelper<LogTable>(DataBus.connStr, DataBus.dbName);
  18. MongoDbHelper<DeviceTable> mgsb = new MongoDbHelper<DeviceTable>(DataBus.connStr, DataBus.dbName);
  19. string st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  20. /// <summary>
  21. /// 根据客户端ID查询最近上下线与告警时间
  22. /// </summary>
  23. /// <param name="clientId"></param>
  24. /// <param name="name"></param>
  25. /// <returns></returns>
  26. [HttpGet]
  27. public JsonMsg<object> StatsOnOff(string clientId,string name)
  28. {
  29. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  30. try
  31. {
  32. string gj_time = "未知";
  33. string sx_time = "未知";
  34. string xx_time = "未知";
  35. AlarmTable alarmTable = new AlarmTable();
  36. List<LogTable> log = new List<LogTable>();
  37. if (!string.IsNullOrEmpty(name))
  38. {
  39. alarmTable = mggj.QueryClientIdMaxName(clientId,name);
  40. log = mglog.QueryClientIdName(clientId, name);
  41. }
  42. else
  43. {
  44. alarmTable = mggj.QueryClientIdMax(clientId);
  45. log = mglog.QueryClientId(clientId);
  46. }
  47. if (alarmTable != null) gj_time = alarmTable.AlarmTime;
  48. LogTable logTable1 = log.Find(par => par.LogMessage.Contains("设备在线了"));
  49. LogTable logTable2 = log.Find(par => par.LogMessage.Contains("设备离线了"));
  50. if (logTable1 != null) sx_time = logTable1.LogTime;
  51. if (logTable2 != null) xx_time = logTable2.LogTime;
  52. List<object> list = new List<object>();
  53. list.Add(new
  54. {
  55. AlarmTime = gj_time,
  56. OnlineTime = sx_time,
  57. OfflineTime = xx_time
  58. });
  59. return JsonMsg<object>.OK(list, st);
  60. }
  61. catch (Exception ex)
  62. {
  63. return JsonMsg<object>.Error(null, st, ex.Message);
  64. }
  65. }
  66. /// <summary>
  67. /// 查询设备属性状态
  68. /// </summary>
  69. /// <param name="clientId"></param>
  70. /// <returns></returns>
  71. [HttpGet]
  72. public JsonMsg<object> StatsTargetStatus(string clientId, string deviceId)
  73. {
  74. object obj; List<returnStatus> returns = new List<returnStatus>();
  75. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  76. try
  77. {
  78. List<LargeScreenTable> largeScreens = mgLA.QueryClientIdNew(clientId);
  79. if (largeScreens.Count > 0)
  80. {
  81. LargeScreenTable largeScreen = largeScreens[0];
  82. if (largeScreen.json != null && largeScreen.json.Contains("data"))
  83. {
  84. TargetStatus _Target = Tools.JsonToObjectTools<TargetStatus>(largeScreen.json);
  85. TargetStatusList statusList = _Target?.data?.Find(ar => ar.DeviceId.ToString() == deviceId);
  86. if (statusList != null)
  87. {
  88. foreach (var item in statusList.Status)
  89. {
  90. //PLC.MorksMachine.IsConnected
  91. string name = item.Key;
  92. string Ms = string.Empty;
  93. if (item.Key.Contains("."))
  94. {
  95. string sta = item.Key.Split('.')[0];
  96. string sop = item.Key.Split('.')[item.Key.Split('.').Length - 1]; name = sop;
  97. string value = item.Value.ToString();
  98. if (keyValues.ContainsKey(sta)) sta = keyValues[sta];
  99. if (keyValues.ContainsKey(sop)) sop = keyValues[sop];
  100. if (keyValues.ContainsKey(value)) value = keyValues[value];
  101. Ms = $"[{sta}]-[{sop}]-[{value}]";
  102. }
  103. returns.Add(new returnStatus { Name = name, Status = item.Value.ToString(), Ms = Ms });
  104. }
  105. }
  106. }
  107. }
  108. obj = new
  109. {
  110. data = returns
  111. };
  112. return JsonMsg<object>.OK(obj, st);
  113. }
  114. catch (Exception ex)
  115. {
  116. return JsonMsg<object>.Error(null, st, ex.Message);
  117. }
  118. }
  119. /// <summary>
  120. /// 统计数量
  121. /// </summary>
  122. /// <param name="clientId"></param>
  123. /// <param name="name"></param>
  124. /// <returns></returns>
  125. [HttpGet]
  126. public JsonMsg<object> StatsConut(string clientId,string name)
  127. {
  128. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  129. try
  130. {
  131. List<LogTable> log = new List<LogTable>();
  132. List<AlarmTable> alarm = new List<AlarmTable>();
  133. if (string.IsNullOrEmpty(name))
  134. {
  135. log = mglog.ALLData(clientId);
  136. alarm = mggj.ALLData(clientId);
  137. }
  138. else
  139. {
  140. log = mglog.ALLDataName(clientId,name);
  141. alarm = mggj.ALLDataName(clientId, name);
  142. }
  143. List<object> list = new List<object>();
  144. int? count = log?.FindAll(par => par.LogMessage.Contains("设备在线了")).Count;
  145. //LogTable log1 = log?.Find(par => par.LogMessage.Contains("设备在线了"));
  146. //LogTable log2 = log?.Find(par => par.LogMessage.Contains("设备离线了"));
  147. list.Add(new { Name = "运行次数", Count = count?.ToString()});
  148. list.Add(new { Name = "告警次数", Count = alarm?.Count });
  149. list.Add(new { Name = "日警次数", Count = alarm?.FindAll(par=>par.CreateTime>=DateTime.Now.Date)?.Count });
  150. //if (log1 != null && log2 != null)
  151. //{
  152. // double dob = Math.Abs((log1.CreateTime - log2.CreateTime).TotalMinutes);
  153. // list.Add(new { Name = "故障次数", Count = 0 });
  154. // list.Add(new { Name = "故障率", Count = $"{(dob / 120)*100/100}%" });
  155. //}
  156. //else
  157. //{
  158. // list.Add(new { Name = "告警次数", Count = 0 });
  159. //}
  160. return JsonMsg<object>.OK(list, st);
  161. }
  162. catch (Exception ex)
  163. {
  164. return JsonMsg<object>.Error(null, st, ex.Message);
  165. }
  166. }
  167. public Dictionary<string, string> keyValues = new Dictionary<string, string>()
  168. {
  169. {"PLC","煮面机机器人" },
  170. {"Lebai","乐白机器人" },
  171. {"SCChip","单片机" },
  172. {"DRCoffee","咖博士咖啡机器" },
  173. {"KLMCoffee","伽乐美咖啡机器" },
  174. {"GSIceCream","乐白机器人" },
  175. {"IsConnected","连接状态" },
  176. {"IsWork","工作状态" },
  177. {"CompletedOpen_SE_1","舵机1打开" },
  178. {"CompletedOpen_SE_2","舵机2打开" },
  179. {"CompletedOpen_SE_3","舵机3打开" },
  180. {"CompletedClose_SE_1","舵机1关闭" },
  181. {"CompletedClose_SE_2","舵机2关闭" },
  182. {"CompletedClose_SE_3","舵机3关闭" },
  183. {"False","异常" },
  184. {"True","正常" }
  185. };
  186. }
  187. public class TargetStatus
  188. {
  189. public List<TargetStatusList> data { get; set; }
  190. }
  191. public class TargetStatusList
  192. {
  193. public object DeviceId { get; set; }
  194. public object Name { get; set; }
  195. public object DeviceType { get; set; }
  196. public object IsBusy { get; set; }
  197. public object IsBusyColor { get; set; }
  198. public object IsHealth { get; set; }
  199. public object IsHealthColor { get; set; }
  200. public Dictionary<string, object> Status { get; set; }
  201. }
  202. public class returnStatus
  203. {
  204. public string Name { get; set; }
  205. private string _status { get; set; }
  206. public string Status
  207. {
  208. get { return _status; }
  209. set
  210. {
  211. _status = value;
  212. if (_status == "False" || _status == "True")
  213. {
  214. if (_status == "True")
  215. StatusColor = new ALYColor { r = 51, g = 232, b = 34, a = 1 };
  216. else
  217. StatusColor = new ALYColor { r = 255, g = 0, b = 0, a = 1 };
  218. }
  219. }
  220. }
  221. public string Ms { get; set; }
  222. public ALYColor StatusColor { get; set; }
  223. public returnStatus()
  224. {
  225. //StatusColor = new ALYColor { r = 255,g = 0,b = 0,a = 1};
  226. StatusColor = new ALYColor { r = 51, g = 232, b = 34, a = 1 };
  227. }
  228. }
  229. }