DefaultDataProvider.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using GDNXFD.Data;
  2. using System;
  3. using System.Linq;
  4. using GDNXFD.Data.Repositories;
  5. using log4net;
  6. using EDosApiWrapper;
  7. namespace GDNXFD.Alert.Interpreter
  8. {
  9. public class DefaultDataProvider : IDataProvider
  10. {
  11. private string eDosServerIP;
  12. private ushort eDosServerPort;
  13. private EDosProxy eDos;
  14. private ILog logger = LogManager.GetLogger("AppLog");
  15. public DefaultDataProvider()
  16. {
  17. eDosServerIP = GlobalVar.EDosServerIP;
  18. eDosServerPort = GlobalVar.EDosServerPort;
  19. eDos = new EDosProxy(eDosServerIP, eDosServerPort);
  20. }
  21. public double FindAIValue(string PointAI, string objectId, AlertObjectType objectType, ref string dataInfo)
  22. {
  23. string pt = this.GetObjectPointAI(PointAI, objectId, objectType);
  24. if (!string.IsNullOrEmpty(pt))
  25. {
  26. double ret = eDos.GetAIRTValue(pt);
  27. if (string.IsNullOrEmpty(dataInfo) == false)
  28. dataInfo += ", ";
  29. dataInfo += string.Format("{0}={1}", pt, ret.ToString("f4"));
  30. return ret;
  31. }
  32. return 0;
  33. }
  34. public int GetLastUpdateTime(string pointKey, string objectId, AlertObjectType objectType, ref string dataInfo)
  35. {
  36. string key = "";
  37. if (pointKey.StartsWith("AI"))
  38. key = GetObjectPointAI(pointKey, objectId, objectType);
  39. else if (pointKey.StartsWith("DI"))
  40. key = GetObjectPointDI(pointKey, objectId, objectType);
  41. int ret = eDos.GetRTValueTime(key);
  42. if (string.IsNullOrEmpty(dataInfo) == false)
  43. dataInfo += ", ";
  44. dataInfo += string.Format("LastUpdateTime({0})={1}", key, CommonMethod.ConvertIntDateTime(ret).ToString("yyy-MM-dd hh:mm:ss"));
  45. return ret;
  46. }
  47. public bool FindDIValue(string PointDI, string objectId, AlertObjectType objectType, ref string dataInfo)
  48. {
  49. string pt = GetObjectPointDI(PointDI, objectId, objectType);
  50. if (!string.IsNullOrEmpty(pt))
  51. {
  52. bool ret = eDos.GetDIRTValue(pt);
  53. if (string.IsNullOrEmpty(dataInfo) == false)
  54. dataInfo += ", ";
  55. dataInfo += string.Format("{0}={1}", pt, ret);
  56. return ret;
  57. }
  58. return false;
  59. }
  60. public double[] FindAIHistorySnap(string PointAI, string objectId, AlertObjectType objectType, DateTime tStart, DateTime tEnd, int interval = 60)
  61. {
  62. string pt = this.GetObjectPointAI(PointAI, objectId, objectType);
  63. if (!string.IsNullOrEmpty(pt))
  64. {
  65. return eDos.GetHisSnap2(pt, tStart, tEnd, interval);
  66. }
  67. return null;
  68. }
  69. public bool[] FindDIHistorySnap(string PointDI, string objectId, AlertObjectType objectType, DateTime tStart, DateTime tEnd, int interval = 60)
  70. {
  71. string pt = GetObjectPointDI(PointDI, objectId, objectType);
  72. if (!string.IsNullOrEmpty(pt))
  73. {
  74. double[] tmp = eDos.GetHisSnap2(pt, tStart, tEnd, interval);
  75. if (tmp != null)
  76. {
  77. bool[] results = new bool[tmp.Length];
  78. for (int i = 0; i < results.Length; i++)
  79. {
  80. results[i] = tmp[i] == 0 ? false : true;
  81. }
  82. return results;
  83. }
  84. }
  85. return null;
  86. }
  87. public double[] FindHistoryRaw(string PointAI, string objectId, AlertObjectType objectType, DateTime tStart, DateTime tEnd)
  88. {
  89. string pt = string.Empty;
  90. if (PointAI.StartsWith("AI"))
  91. pt = this.GetObjectPointAI(PointAI, objectId, objectType);
  92. else
  93. pt = this.GetObjectPointDI(PointAI, objectId, objectType);
  94. if (!string.IsNullOrEmpty(pt))
  95. {
  96. return eDos.GetHisRaw2(pt, tStart, tEnd);
  97. }
  98. return null;
  99. }
  100. public string GetObjectPointAI(string PointAI, string objectId, AlertObjectType objectType)
  101. {
  102. if (PointAI.StartsWith("AI_"))
  103. PointAI = PointAI.Substring(3);
  104. switch (objectType)
  105. {
  106. case AlertObjectType.WindTurbine:
  107. string pointId = DataCache.Instance.WindTurbineAIPoints
  108. .Where(q => q.WindturbineId == objectId && q.UniformCode == PointAI)
  109. .Select(q => q.Id).FirstOrDefault();
  110. if (string.IsNullOrEmpty(pointId))
  111. {
  112. pointId = WindTurbineRepository.GetWindTurbineAIPointId(objectId, PointAI);
  113. if (string.IsNullOrWhiteSpace(pointId))
  114. {
  115. logger.ErrorFormat("风机{0}的测点{1}不存在!", objectId, PointAI);
  116. }
  117. }
  118. return BuildPointId(pointId);
  119. case AlertObjectType.WindPowerStation:
  120. case AlertObjectType.Project:
  121. case AlertObjectType.Line:
  122. string pointId1 = WindPowerStationRepository.GetWindPowerStationPointId(objectId, PointAI);
  123. if (string.IsNullOrWhiteSpace(pointId1))
  124. {
  125. logger.ErrorFormat("对象{0}的测点{1}不存在!", objectId, PointAI);
  126. }
  127. return BuildPointId(pointId1);
  128. case AlertObjectType.Electrical:
  129. string pointId5 = WindPowerStationRepository.GetElectricalAIPointId(objectId, PointAI);
  130. if (string.IsNullOrWhiteSpace(pointId5))
  131. {
  132. logger.ErrorFormat("对象{0}的测点{1}不存在!", objectId, PointAI);
  133. }
  134. return BuildPointId(pointId5);
  135. }
  136. return string.Empty;
  137. }
  138. public string GetObjectPointDI(string PointDI, string objectId, AlertObjectType objectType)
  139. {
  140. if (PointDI.StartsWith("DI_"))
  141. PointDI = PointDI.Substring(3);
  142. switch (objectType)
  143. {
  144. case AlertObjectType.WindTurbine:
  145. string pointId = DataCache.Instance.WindTurbineDIPoints
  146. .Where(q => q.WindturbineId == objectId && q.UniformCode == PointDI)
  147. .Select(q => q.Id).FirstOrDefault();
  148. if (string.IsNullOrEmpty(pointId))
  149. {
  150. pointId = WindTurbineRepository.GetWindTurbineDIPointId(objectId, PointDI);
  151. if (string.IsNullOrWhiteSpace(pointId))
  152. {
  153. logger.ErrorFormat("风机{0}的测点{1}不存在!", objectId, PointDI);
  154. }
  155. }
  156. return BuildPointId(pointId);
  157. case AlertObjectType.WindPowerStation:
  158. break;
  159. case AlertObjectType.Project:
  160. break;
  161. case AlertObjectType.Line:
  162. break;
  163. case AlertObjectType.Electrical:
  164. string pointId5 = WindPowerStationRepository.GetElectricalDIPointId(objectId, PointDI);
  165. if (string.IsNullOrWhiteSpace(pointId5))
  166. {
  167. logger.ErrorFormat("风机{0}的测点{1}不存在!", objectId, PointDI);
  168. }
  169. return BuildPointId(pointId5);
  170. }
  171. return string.Empty;
  172. }
  173. private string BuildPointId(string PointID)
  174. {
  175. if (string.IsNullOrWhiteSpace(PointID))
  176. return string.Empty;
  177. string[] ids = PointID.Split('.');
  178. if (ids != null && ids.Length == 3)
  179. {
  180. PointID = ids[1] + "_" + ids[2];
  181. }
  182. return PointID;
  183. }
  184. }
  185. }