RealTimeMonitoringService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using LeaRun.Application.IService.PipeNetworkManage;
  7. using LeaRun.Data;
  8. using System.Data;
  9. using LeaRun.Util.WebControl;
  10. using LeaRun.Util.Log;
  11. using LeaRun.Application.Entity.PipeNetworkManage;
  12. using LeaRun.Util;
  13. namespace LeaRun.Application.Service.PipeNetworkManage
  14. {
  15. public class RealTimeMonitoringService : IRealTimeMonitoringService
  16. {
  17. SqlHelper sqlHelper = new SqlHelper("PipeNetworkDb");
  18. Log log = LogFactory.GetLogger(typeof(RealTimeMonitoringService));
  19. /// <summary>
  20. /// 实时列表
  21. /// </summary>
  22. /// <param name="devId">监测点</param>
  23. /// <returns></returns>
  24. public string GetRealTimeData(string devId, Pagination pagination)
  25. {
  26. string strSql = "Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase Where MeterAssessmentId in (" + devId + ")";
  27. DateTime dateTime = DateTime.Now;
  28. int total = pagination.records;
  29. DataTable dt = sqlHelper.ExecuteDataTable(strSql, "MeterAssessmentId", true, pagination.rows, pagination.page, out total, null);
  30. pagination.records = total;
  31. StringBuilder builder = new StringBuilder();
  32. builder.Append("[");
  33. for (int i = 0; i < dt.Rows.Count; i++)
  34. {
  35. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  36. string name = dt.Rows[i]["MeterAssessmentName"].ToString();
  37. builder.Append("{");
  38. builder.Append("\"ID\":\"").Append(deviceId).Append("\",");
  39. builder.Append("\"DevName\":\"").Append(name).Append("\",");
  40. string strTableName = "MeterAssessment_" + deviceId;
  41. if (sqlHelper.DBTableIsExists(strTableName))
  42. {
  43. strSql = "SELECT TOP 1 GetDateTime,NetCumulativeFlow,PositiveCumulativeFlow,NegativeCumulativeFlow,InstantaneousFlow,Pressure,PressureUpAlarm,PressureDownAlarm,BatteryVoltageValue,BoxDoorAlarm FROM " + strTableName + " order BY GetDateTime desc ";
  44. DataTable table = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  45. if (table.Rows.Count > 0)
  46. {
  47. builder.Append("\"采集时间\":\"").Append(table.Rows[0]["GetDateTime"]).Append("\",");
  48. builder.Append("\"净累计流量\":\"").Append(table.Rows[0]["NetCumulativeFlow"]).Append("\",");
  49. builder.Append("\"正累计流量\":\"").Append(table.Rows[0]["PositiveCumulativeFlow"]).Append("\",");
  50. builder.Append("\"负累计流量\":\"").Append(table.Rows[0]["NegativeCumulativeFlow"]).Append("\",");
  51. builder.Append("\"瞬时流量\":\"").Append(table.Rows[0]["InstantaneousFlow"]).Append("\",");
  52. builder.Append("\"压力\":\"").Append(table.Rows[0]["Pressure"]).Append("\",");
  53. builder.Append("\"压力上限报警\":\"").Append(table.Rows[0]["PressureUpAlarm"]).Append("\",");
  54. builder.Append("\"压力下限报警\":\"").Append(table.Rows[0]["PressureDownAlarm"]).Append("\",");
  55. builder.Append("\"电池电压\":\"").Append(table.Rows[0]["BatteryVoltageValue"]).Append("\",");
  56. builder.Append("\"测控箱开门报警\":\"").Append(table.Rows[0]["BoxDoorAlarm"]).Append("\"");
  57. }
  58. else
  59. {
  60. builder.Append("\"采集时间\":\"").Append("--").Append("\",");
  61. builder.Append("\"净累计流量\":\"").Append("--").Append("\",");
  62. builder.Append("\"正累计流量\":\"").Append("--").Append("\",");
  63. builder.Append("\"负累计流量\":\"").Append("--").Append("\",");
  64. builder.Append("\"瞬时流量\":\"").Append("--").Append("\",");
  65. builder.Append("\"压力\":\"").Append("--").Append("\",");
  66. builder.Append("\"压力上限报警\":\"").Append("--").Append("\",");
  67. builder.Append("\"压力下限报警\":\"").Append("--").Append("\",");
  68. builder.Append("\"电池电压\":\"").Append("--").Append("\",");
  69. builder.Append("\"测控箱开门报警\":\"").Append("--").Append("\"");
  70. }
  71. }
  72. else
  73. {
  74. builder.Append("\"采集时间\":\"").Append("--").Append("\",");
  75. builder.Append("\"净累计流量\":\"").Append("--").Append("\",");
  76. builder.Append("\"正累计流量\":\"").Append("--").Append("\",");
  77. builder.Append("\"负累计流量\":\"").Append("--").Append("\",");
  78. builder.Append("\"瞬时流量\":\"").Append("--").Append("\",");
  79. builder.Append("\"压力\":\"").Append("--").Append("\",");
  80. builder.Append("\"压力上限报警\":\"").Append("--").Append("\",");
  81. builder.Append("\"压力下限报警\":\"").Append("--").Append("\",");
  82. builder.Append("\"电池电压\":\"").Append("--").Append("\",");
  83. builder.Append("\"测控箱开门报警\":\"").Append("--").Append("\"");
  84. }
  85. builder.Append("}");
  86. if (i < dt.Rows.Count - 1)
  87. builder.Append(",");
  88. }
  89. builder.Append("]");
  90. return builder.ToString();
  91. }
  92. /// <summary>
  93. /// 实时分析
  94. /// </summary>
  95. /// <param name="departId">部门</param>
  96. /// <param name="devId">监测点</param>
  97. /// <param name="selField">实时分析的字段</param>
  98. /// <param name="fieldName">字段名称</param>
  99. /// <returns></returns>
  100. public string GetRealTimeAnalysis(Pagination pagination, string selField, string fieldName)
  101. {
  102. try
  103. {
  104. string strSql = " Select MeterAssessmentId,MeterAssessmentName From MeterAssessmentBase ";
  105. DateTime dateTime = DateTime.Now;
  106. int total = pagination.records;
  107. DataTable dt = sqlHelper.ExecuteDataTable(strSql, "MeterAssessmentId", true, pagination.rows, pagination.page, out total, null);
  108. pagination.records = total;
  109. StringBuilder builder = new StringBuilder();
  110. builder.Append("[");
  111. for (int i = 0; i < dt.Rows.Count; i++)
  112. {
  113. string deviceId = dt.Rows[i]["MeterAssessmentId"].ToString();
  114. string name = dt.Rows[i]["MeterAssessmentName"].ToString();
  115. builder.Append("{");
  116. builder.Append("\"ID\":\"").Append(deviceId).Append("\",");
  117. builder.Append("\"DevName\":\"").Append(name).Append("\",");
  118. builder.Append("\"传感器\":\"").Append(fieldName).Append("\",");
  119. string strTableName = "MeterAssessment_" + deviceId;
  120. if (sqlHelper.DBTableIsExists(strTableName))
  121. {
  122. //取昨天跟今天两天得数据
  123. strSql = "SELECT * ,DATEDIFF(DAY,GETDATE(),GetDateTime) AS TimeDiff FROM " + strTableName + " WHERE DATEDIFF(DAY,GETDATE(),GetDateTime) IN (0,-1) ORDER BY GetDateTime DESC";
  124. DataTable table = sqlHelper.ExecuteDataTable(strSql, CommandType.Text, null);
  125. //今天数据取得
  126. DataRow[] todayData = table.Select("TimeDiff = 0");
  127. DataRow[] yestodayData = table.Select("TimeDiff = -1");
  128. if (yestodayData.Length > 0)
  129. {
  130. builder.Append(GetDadaFromData(yestodayData, selField, "昨日"));
  131. }
  132. else
  133. {
  134. builder.Append("\"昨日最高\":\"").Append("--").Append("\",");
  135. builder.Append("\"昨日最高时间\":\"").Append("--").Append("\",");
  136. builder.Append("\"昨日最低\":\"").Append("--").Append("\",");
  137. builder.Append("\"昨日最低时间\":\"").Append("--").Append("\",");
  138. builder.Append("\"昨日平均\":\"").Append("--").Append("\",");
  139. builder.Append("\"昨日振幅\":\"").Append("--").Append("\",");
  140. }
  141. if (todayData.Length > 0)
  142. {
  143. builder.Append(GetDadaFromData(todayData, selField, "今日"));
  144. builder.Append("\"上传时间\":\"").Append(Convert.ToDateTime(todayData[0]["GetDateTime"]).ToString("hh:ss")).Append("\",");
  145. builder.Append("\"" + fieldName + "\":\"").Append(todayData[0][selField]).Append("\"");
  146. }
  147. else
  148. {
  149. builder.Append("\"今日最高\":\"").Append("--").Append("\",");
  150. builder.Append("\"今日最高时间\":\"").Append("--").Append("\",");
  151. builder.Append("\"今日最低\":\"").Append("--").Append("\",");
  152. builder.Append("\"今日最低时间\":\"").Append("--").Append("\",");
  153. builder.Append("\"今日平均\":\"").Append("--").Append("\",");
  154. builder.Append("\"今日振幅\":\"").Append("--").Append("\",");
  155. builder.Append("\"上传时间\":\"").Append("--").Append("\",");
  156. builder.Append("\"" + fieldName + "\":\"").Append("--").Append("\"");
  157. }
  158. }
  159. builder.Append("}");
  160. if (i < dt.Rows.Count - 1)
  161. builder.Append(",");
  162. }
  163. builder.Append("]");
  164. return builder.ToString();
  165. }
  166. catch (Exception ex)
  167. {
  168. log.Error(ex.Message);
  169. return "";
  170. }
  171. }
  172. /// <summary>
  173. /// 获取其中的 最大值 最小值 平均值
  174. /// </summary>
  175. /// <param name="rows"></param>
  176. /// <returns></returns>
  177. public StringBuilder GetDadaFromData(DataRow[] rows, string selField, string name)
  178. {
  179. double maxValue = 0;
  180. string maxValueTime = "";
  181. double minValue = 0;
  182. string minValueTime = "";
  183. double avgValue = 0;
  184. double addPercent = 0;
  185. double allValue = 0;
  186. StringBuilder sbBuilder = new StringBuilder();
  187. for (int row = 0; row < rows.Length; row++)
  188. {
  189. double thisValue = Convert.ToDouble(rows[row][selField].ToString()==""?"0":rows[row][selField].ToString());
  190. allValue += thisValue;
  191. if (row == 0)
  192. {
  193. minValue = maxValue = thisValue;
  194. minValueTime = maxValueTime = rows[row]["GetDateTime"].ToString();
  195. }
  196. if (maxValue < thisValue)
  197. {
  198. maxValue = thisValue;
  199. maxValueTime = rows[row]["GetDateTime"].ToString();
  200. }
  201. if (minValue > thisValue)
  202. {
  203. minValue = thisValue;
  204. minValueTime = rows[row]["GetDateTime"].ToString();
  205. }
  206. }
  207. avgValue = allValue / rows.Length;
  208. addPercent = (maxValue - minValue) / maxValue;
  209. sbBuilder.Append("\"" + name + "最高\":\"").Append(maxValue.ToString("0.0")).Append("\",");
  210. sbBuilder.Append("\"" + name + "最高时间\":\"").Append(Convert.ToDateTime(maxValueTime).ToString("MM-dd HH:ss")).Append("\",");
  211. sbBuilder.Append("\"" + name + "最低\":\"").Append(minValue.ToString("0.0")).Append("\",");
  212. sbBuilder.Append("\"" + name + "最低时间\":\"").Append(Convert.ToDateTime(minValueTime).ToString("MM-dd HH:ss")).Append("\",");
  213. sbBuilder.Append("\"" + name + "平均\":\"").Append(avgValue.ToString("0.0")).Append("\",");
  214. sbBuilder.Append("\"" + name + "振幅\":\"").Append(addPercent.ToString("0.0")).Append("\",");
  215. return sbBuilder;
  216. }
  217. /// <summary>
  218. /// 获取数据
  219. /// </summary>
  220. /// <param name="tableNames"></param>
  221. /// <param name="type"></param>
  222. /// <returns></returns>
  223. public List<RealCurvesEntity> GetRealCurve(List<string> tableNames, string type)
  224. {
  225. StringBuilder sb = new StringBuilder("");
  226. try
  227. {
  228. for (int i = 0; i < tableNames.Count; i++)
  229. {
  230. sb.Append("select mab.MeterAssessmentId, mab.MeterAssessmentName, '" + type + "' Type, " + tableNames[i] + "." + type + " Value, " + tableNames[i] + ".GetDateTime Date from ");
  231. sb.Append(tableNames[i] + " INNER JOIN MeterAssessmentBase AS mab ON mab.MeterAssessmentId = " + tableNames[i] + ".MeterAssessmentId where DateDiff(DAY," + tableNames[i] + ".GetDateTime,getdate())=0 ");
  232. if (i < tableNames.Count - 1)
  233. {
  234. sb.Append(" union ");
  235. }
  236. }
  237. sb.Append(" ORDER BY [Date] ASC ");
  238. DataTable dt = sqlHelper.ExecuteDataTable(sb.ToString(), CommandType.Text, null);
  239. List<RealCurvesEntity> list = DataHelper.DataTableToT<RealCurvesEntity>(dt);
  240. return list == null ? new List<RealCurvesEntity>() : list;
  241. }
  242. catch (Exception)
  243. {
  244. throw;
  245. }
  246. }
  247. }
  248. }