Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

125 rader
4.6 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. /// <returns></returns>
  25. [HttpGet]
  26. public JsonMsg<object> StatsOnOff(string clientId)
  27. {
  28. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  29. try
  30. {
  31. string gj_time="未知";
  32. string sx_time = "未知";
  33. string xx_time = "未知";
  34. AlarmTable alarmTable = mggj.QueryClientIdMax(clientId);
  35. List<LogTable> log = mglog.QueryClientId(clientId);
  36. if (alarmTable != null) gj_time = alarmTable.AlarmTime;
  37. LogTable logTable1= log.Find(par => par.LogMessage.Contains("设备在线了"));
  38. LogTable logTable2 = log.Find(par => par.LogMessage.Contains("设备离线了"));
  39. if(logTable1!=null) sx_time = logTable1.LogTime;
  40. if (logTable2 != null) xx_time = logTable2.LogTime;
  41. object obj = new
  42. {
  43. AlarmTime=gj_time,
  44. OnlineTime=sx_time,
  45. OfflineTime=xx_time
  46. };
  47. return JsonMsg<object>.OK(obj, st);
  48. }
  49. catch (Exception ex)
  50. {
  51. return JsonMsg<object>.Error(null, st, ex.Message);
  52. }
  53. }
  54. /// <summary>
  55. /// 查询设备属性状态
  56. /// </summary>
  57. /// <param name="clientId"></param>
  58. /// <returns></returns>
  59. [HttpGet]
  60. public JsonMsg<object> StatsTargetStatus(string clientId,string deviceId)
  61. {
  62. object obj; List<returnStatus> returns = new List<returnStatus>();
  63. st = System.Reflection.MethodBase.GetCurrentMethod().Name;
  64. try
  65. {
  66. List<LargeScreenTable> largeScreens = mgLA.QueryClientIdNew(clientId);
  67. if (largeScreens.Count > 0)
  68. {
  69. LargeScreenTable largeScreen = largeScreens[0];
  70. if (largeScreen.json != null && largeScreen.json.Contains("data"))
  71. {
  72. TargetStatus _Target = Tools.JsonToObjectTools<TargetStatus>(largeScreen.json);
  73. TargetStatusList statusList= _Target?.data?.Find(ar => ar.DeviceId.ToString()== deviceId);
  74. if (statusList != null)
  75. {
  76. foreach (var item in statusList.Status)
  77. {
  78. returns.Add(new returnStatus { Name = item.Key, Status = item.Value,Ms = ""});
  79. }
  80. }
  81. }
  82. }
  83. obj = new
  84. {
  85. data = returns
  86. };
  87. return JsonMsg<object>.OK(obj, st);
  88. }
  89. catch (Exception ex)
  90. {
  91. return JsonMsg<object>.Error(null, st, ex.Message);
  92. }
  93. }
  94. }
  95. public class TargetStatus
  96. {
  97. public List<TargetStatusList> data { get; set; }
  98. }
  99. public class TargetStatusList
  100. {
  101. public object DeviceId { get; set; }
  102. public object Name { get; set; }
  103. public object DeviceType { get; set; }
  104. public object IsBusy { get; set; }
  105. public object IsBusyColor { get; set; }
  106. public object IsHealth { get; set; }
  107. public object IsHealthColor { get; set; }
  108. public Dictionary<string, object> Status { get; set; }
  109. }
  110. public class returnStatus
  111. {
  112. public string Name { get; set; }
  113. public object Status { get; set; }
  114. public string Ms { get; set; }
  115. }
  116. }