using GDNXFD.Data; using System; using System.Linq; using GDNXFD.Data.Repositories; using log4net; using EDosApiWrapper; namespace GDNXFD.Alert.Interpreter { public class DefaultDataProvider : IDataProvider { private string eDosServerIP; private ushort eDosServerPort; private EDosProxy eDos; private ILog logger = LogManager.GetLogger("AppLog"); public DefaultDataProvider() { eDosServerIP = GlobalVar.EDosServerIP; eDosServerPort = GlobalVar.EDosServerPort; eDos = new EDosProxy(eDosServerIP, eDosServerPort); } public double FindAIValue(string PointAI, string objectId, AlertObjectType objectType, ref string dataInfo) { string pt = this.GetObjectPointAI(PointAI, objectId, objectType); if (!string.IsNullOrEmpty(pt)) { double ret = eDos.GetAIRTValue(pt); if (string.IsNullOrEmpty(dataInfo) == false) dataInfo += ", "; dataInfo += string.Format("{0}={1}", pt, ret.ToString("f4")); return ret; } return 0; } public int GetLastUpdateTime(string pointKey, string objectId, AlertObjectType objectType, ref string dataInfo) { string key = ""; if (pointKey.StartsWith("AI")) key = GetObjectPointAI(pointKey, objectId, objectType); else if (pointKey.StartsWith("DI")) key = GetObjectPointDI(pointKey, objectId, objectType); int ret = eDos.GetRTValueTime(key); if (string.IsNullOrEmpty(dataInfo) == false) dataInfo += ", "; dataInfo += string.Format("LastUpdateTime({0})={1}", key, CommonMethod.ConvertIntDateTime(ret).ToString("yyy-MM-dd hh:mm:ss")); return ret; } public bool FindDIValue(string PointDI, string objectId, AlertObjectType objectType, ref string dataInfo) { string pt = GetObjectPointDI(PointDI, objectId, objectType); if (!string.IsNullOrEmpty(pt)) { bool ret = eDos.GetDIRTValue(pt); if (string.IsNullOrEmpty(dataInfo) == false) dataInfo += ", "; dataInfo += string.Format("{0}={1}", pt, ret); return ret; } return false; } public double[] FindAIHistorySnap(string PointAI, string objectId, AlertObjectType objectType, DateTime tStart, DateTime tEnd, int interval = 60) { string pt = this.GetObjectPointAI(PointAI, objectId, objectType); if (!string.IsNullOrEmpty(pt)) { return eDos.GetHisSnap2(pt, tStart, tEnd, interval); } return null; } public bool[] FindDIHistorySnap(string PointDI, string objectId, AlertObjectType objectType, DateTime tStart, DateTime tEnd, int interval = 60) { string pt = GetObjectPointDI(PointDI, objectId, objectType); if (!string.IsNullOrEmpty(pt)) { double[] tmp = eDos.GetHisSnap2(pt, tStart, tEnd, interval); if (tmp != null) { bool[] results = new bool[tmp.Length]; for (int i = 0; i < results.Length; i++) { results[i] = tmp[i] == 0 ? false : true; } return results; } } return null; } public double[] FindHistoryRaw(string PointAI, string objectId, AlertObjectType objectType, DateTime tStart, DateTime tEnd) { string pt = string.Empty; if (PointAI.StartsWith("AI")) pt = this.GetObjectPointAI(PointAI, objectId, objectType); else pt = this.GetObjectPointDI(PointAI, objectId, objectType); if (!string.IsNullOrEmpty(pt)) { return eDos.GetHisRaw2(pt, tStart, tEnd); } return null; } public string GetObjectPointAI(string PointAI, string objectId, AlertObjectType objectType) { if (PointAI.StartsWith("AI_")) PointAI = PointAI.Substring(3); switch (objectType) { case AlertObjectType.WindTurbine: string pointId = DataCache.Instance.WindTurbineAIPoints .Where(q => q.WindturbineId == objectId && q.UniformCode == PointAI) .Select(q => q.Id).FirstOrDefault(); if (string.IsNullOrEmpty(pointId)) { pointId = WindTurbineRepository.GetWindTurbineAIPointId(objectId, PointAI); if (string.IsNullOrWhiteSpace(pointId)) { logger.ErrorFormat("风机{0}的测点{1}不存在!", objectId, PointAI); } } return BuildPointId(pointId); case AlertObjectType.WindPowerStation: case AlertObjectType.Project: case AlertObjectType.Line: string pointId1 = WindPowerStationRepository.GetWindPowerStationPointId(objectId, PointAI); if (string.IsNullOrWhiteSpace(pointId1)) { logger.ErrorFormat("对象{0}的测点{1}不存在!", objectId, PointAI); } return BuildPointId(pointId1); case AlertObjectType.Electrical: string pointId5 = WindPowerStationRepository.GetElectricalAIPointId(objectId, PointAI); if (string.IsNullOrWhiteSpace(pointId5)) { logger.ErrorFormat("对象{0}的测点{1}不存在!", objectId, PointAI); } return BuildPointId(pointId5); } return string.Empty; } public string GetObjectPointDI(string PointDI, string objectId, AlertObjectType objectType) { if (PointDI.StartsWith("DI_")) PointDI = PointDI.Substring(3); switch (objectType) { case AlertObjectType.WindTurbine: string pointId = DataCache.Instance.WindTurbineDIPoints .Where(q => q.WindturbineId == objectId && q.UniformCode == PointDI) .Select(q => q.Id).FirstOrDefault(); if (string.IsNullOrEmpty(pointId)) { pointId = WindTurbineRepository.GetWindTurbineDIPointId(objectId, PointDI); if (string.IsNullOrWhiteSpace(pointId)) { logger.ErrorFormat("风机{0}的测点{1}不存在!", objectId, PointDI); } } return BuildPointId(pointId); case AlertObjectType.WindPowerStation: break; case AlertObjectType.Project: break; case AlertObjectType.Line: break; case AlertObjectType.Electrical: string pointId5 = WindPowerStationRepository.GetElectricalDIPointId(objectId, PointDI); if (string.IsNullOrWhiteSpace(pointId5)) { logger.ErrorFormat("风机{0}的测点{1}不存在!", objectId, PointDI); } return BuildPointId(pointId5); } return string.Empty; } private string BuildPointId(string PointID) { if (string.IsNullOrWhiteSpace(PointID)) return string.Empty; string[] ids = PointID.Split('.'); if (ids != null && ids.Length == 3) { PointID = ids[1] + "_" + ids[2]; } return PointID; } } }