AlertRuleInterpreter.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using GDNXFD.Data;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace GDNXFD.Alert.Interpreter
  5. {
  6. public abstract class AlertRuleInterpreter
  7. {
  8. private AlertRule _alertRule;
  9. private string[] _windturbines;
  10. private string[] _stations;
  11. private string[] _projects;
  12. private string[] _lines;
  13. private string[] _electricals;
  14. protected IDataProvider dataProvider;
  15. public AlertRuleInterpreter(AlertRule ar)
  16. {
  17. _alertRule = ar;
  18. dataProvider = InterpreterFactory.Instance.DataProvider;
  19. }
  20. public string Category
  21. {
  22. get { return _alertRule.Category; }
  23. }
  24. public AlertRule AlertRule
  25. {
  26. get { return _alertRule; }
  27. }
  28. public string[] Windturbines
  29. {
  30. get
  31. {
  32. if (_windturbines == null && Category == "1" && !string.IsNullOrWhiteSpace(_alertRule.Windturbine))
  33. {
  34. _windturbines = _alertRule.Windturbine.Split(',');
  35. }
  36. return _windturbines;
  37. }
  38. }
  39. public string[] Stations
  40. {
  41. get
  42. {
  43. if (_stations == null && Category == "2" && !string.IsNullOrWhiteSpace(_alertRule.Station))
  44. {
  45. _stations = _alertRule.Station.Split(',');
  46. }
  47. return _stations;
  48. }
  49. }
  50. public string[] Projects
  51. {
  52. get
  53. {
  54. if (_projects == null && Category == "3" && !string.IsNullOrWhiteSpace(_alertRule.Project))
  55. {
  56. _projects = _alertRule.Project.Split(',');
  57. }
  58. return _projects;
  59. }
  60. }
  61. public string[] Lines
  62. {
  63. get
  64. {
  65. if (_lines == null && Category == "4" && !string.IsNullOrWhiteSpace(_alertRule.Line))
  66. {
  67. _lines = _alertRule.Line.Split(',');
  68. }
  69. return _lines;
  70. }
  71. }
  72. public string[] Electricals
  73. {
  74. get
  75. {
  76. if (_electricals == null && Category == "5" && !string.IsNullOrWhiteSpace(_alertRule.Station))
  77. {
  78. _electricals = _alertRule.Station.Split(',');
  79. }
  80. return _electricals;
  81. }
  82. }
  83. protected string dataInfo;
  84. /// <summary>
  85. /// 存放实时数据的值,带入dataProvider中进行更新
  86. /// </summary>
  87. public string DataInfo
  88. {
  89. get { return dataInfo; }
  90. set { dataInfo = value; }
  91. }
  92. /// <summary>
  93. /// 规则解释类
  94. /// 在自动生成的规则解析代码中实现该方法
  95. /// </summary>
  96. /// <param name="objectId"></param>
  97. /// <param name="objectType"></param>
  98. /// <returns></returns>
  99. public abstract bool Interpret(string objectId, AlertObjectType objectType);
  100. #region 扩展方法
  101. /// <summary>
  102. /// 判定状态保持的方法
  103. /// 应用场景:读取实时库某一点的历史值,时间区间为:当前时间向前追溯{duration}分钟,
  104. /// 若时间段范围内无值或只有一个值,返回false
  105. /// 若时间段内有若干值(大于1)且都相同,则认为该状态一直持续未变
  106. /// </summary>
  107. /// <param name="testPointName">测点名称</param>
  108. /// <param name="duration">持续时间,单位:分钟</param>
  109. /// <param name="objectId">对象ID</param>
  110. /// <param name="objectType">对象类型</param>
  111. /// <returns>
  112. /// true:时间段内状态不变
  113. /// false:时间段内数据不足或者状态变动过
  114. /// </returns>
  115. protected bool StatusContinued(string testPointName, int duration, string objectId, AlertObjectType objectType)
  116. {
  117. testPointName = testPointName.TrimStart('S', 'T', 'R');
  118. DateTime tEnd = DateTime.Now;
  119. DateTime tStart = tEnd.AddMinutes(0 - duration);
  120. double[] arr = dataProvider.FindHistoryRaw(testPointName, objectId, objectType, tStart, tEnd);
  121. if (arr != null && arr.Length >1)
  122. {
  123. for (int i=1;i<arr.Length;i++)
  124. {
  125. if (arr[i] != arr[0])
  126. return false;
  127. }
  128. return true;
  129. }
  130. return false;
  131. }
  132. /// <summary>
  133. /// 取测点与给定的时间之前的差值
  134. /// 算法:读取实时库某一点当前值及给定时间前的值,返回当前值与前值的差
  135. /// </summary>
  136. /// <param name="testPointName">测点名称</param>
  137. /// <param name="minutes">时间差, 单位: 分钟</param>
  138. /// <param name="objectId">对象ID</param>
  139. /// <param name="objectType">对象类型</param>
  140. /// <returns>
  141. /// 当前值 - 前值
  142. /// </returns>
  143. protected double DiffMinutes(string testPointName, int minutes, string objectId, AlertObjectType objectType)
  144. {
  145. if (minutes <= 0)
  146. return 0;
  147. testPointName = testPointName.TrimStart('S', 'T', 'R');
  148. DateTime tEnd = DateTime.Now;
  149. DateTime tStart = tEnd.AddMinutes(0 - minutes);
  150. double[] arr = dataProvider.FindAIHistorySnap(testPointName, objectId, objectType, tStart, tEnd, minutes*60);
  151. if (arr.Length != 2)
  152. return 0;
  153. else
  154. {
  155. return arr[1] - arr[0];
  156. }
  157. }
  158. protected double DiffSeconds(string testPointName, int seconds, string objectId, AlertObjectType objectType)
  159. {
  160. if (seconds <= 0)
  161. return 0;
  162. testPointName = testPointName.TrimStart('S', 'T', 'R');
  163. DateTime tEnd = DateTime.Now;
  164. DateTime tStart = tEnd.AddSeconds(0 - seconds);
  165. double[] arr = dataProvider.FindAIHistorySnap(testPointName, objectId, objectType, tStart, tEnd, seconds);
  166. if (arr.Length != 2)
  167. return 0;
  168. else
  169. {
  170. return arr[1] - arr[0];
  171. }
  172. }
  173. /// <summary>
  174. /// 判定状态突然大幅变化的方法
  175. /// 算法:读取实时库某一点的历史值(2次),时间区间分别为
  176. /// a) Datetime.Now —— Datetime.Now - duration
  177. /// b)Datetime.Now - duration —— Datetime.Now - duration*2
  178. /// 分别计算出两个时间段的平均值和方差,
  179. /// 若均值的比率 或者方差的比率大于制定的值时,则认为是数据突变
  180. /// </summary>
  181. /// <param name="testPointName">测点名称</param>
  182. /// <param name="duration">持续时间,单位:分钟</param>
  183. /// <param name="objectId">对象ID</param>
  184. /// <param name="objectType">对象类型</param>
  185. /// <returns>
  186. /// true:时间段内状态不变
  187. /// false:时间段内数据不足或者状态变动过
  188. /// </returns>
  189. protected bool SuddenChange(string testPointName, int duration, double ratio, string objectId, AlertObjectType objectType)
  190. {
  191. if (ratio <= 0)
  192. return false;
  193. double fc1, fc2, avg1, avg2, sum;
  194. testPointName = testPointName.TrimStart('S', 'T', 'R');
  195. DateTime tEnd = DateTime.Now;
  196. DateTime tStart = tEnd.AddMinutes(0 - duration);
  197. DateTime tStart1 = tStart.AddMinutes(0 - duration);
  198. double[] arr = dataProvider.FindHistoryRaw(testPointName, objectId, objectType, tStart, tEnd);
  199. double[] arr1 = dataProvider.FindHistoryRaw(testPointName, objectId, objectType, tStart1, tStart);
  200. if (arr == null || arr.Length < 1)
  201. return false;
  202. if (arr1 == null || arr1.Length < 1)
  203. {
  204. fc1 = 0;
  205. avg1 = arr[0];
  206. }
  207. else
  208. {
  209. sum = 0;
  210. for(int i=0;i<arr1.Length;i++)
  211. {
  212. sum += arr1[i];
  213. }
  214. avg1 = sum / arr1.Length;
  215. sum = 0;
  216. for (int i = 0; i < arr1.Length; i++)
  217. {
  218. sum += Math.Pow(arr1[i] - avg1, 2);
  219. }
  220. fc1 = Math.Sqrt(sum/arr1.Length);
  221. }
  222. sum = 0;
  223. for (int i = 0; i < arr.Length; i++)
  224. {
  225. sum += arr[i];
  226. }
  227. avg2 = sum / arr.Length;
  228. double ratio2 = avg2 / avg1;
  229. if (ratio2 > ratio || ratio2 < 1 / ratio)
  230. return true;
  231. sum = 0;
  232. for (int i = 0; i < arr.Length; i++)
  233. {
  234. sum += Math.Pow(arr[i] - avg2, 2);
  235. }
  236. fc2 = Math.Sqrt(sum / arr.Length);
  237. if (fc1 == 0 && fc2 != 0)
  238. return true;
  239. if (fc1 == 0 && fc2 == 0)
  240. return false;
  241. ratio2 = fc2 / fc1;
  242. if (ratio2 > ratio || ratio2 < 1 / ratio)
  243. return true;
  244. return false;
  245. }
  246. /// <summary>
  247. /// 获取测点最近收到数据的时间
  248. /// 应用场景:读取实时库某一点的当前值(对应EDos的GetRtValue方法),返回改值对应的时间,
  249. /// </summary>
  250. /// <param name="testPointName">测点名称</param>
  251. /// <param name="objectId">对象ID</param>
  252. /// <param name="objectType">对象类型</param>
  253. /// <returns>
  254. /// 时间,单位为秒(近似值)
  255. /// </returns>
  256. protected int LastUpdateTime(string testPointName, string objectId, AlertObjectType objectType)
  257. {
  258. testPointName = testPointName.TrimStart('S', 'T', 'R');
  259. int t1 = dataProvider.GetLastUpdateTime(testPointName, objectId, objectType, ref dataInfo);
  260. return CommonMethod.ConvertDateTimeInt(DateTime.Now) - t1;
  261. }
  262. #endregion
  263. }
  264. }