123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- using GDNXFD.Data;
- using GDNXFD.Data.Model;
- using GDNXFD.Data.Repositories;
- using GDNXFD.WcfService.RealtimeState;
- using GDNXFD.WcfService.RealtimeState.Domain;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using WisdomClient.data;
- namespace GDNXFD.WcfService
- {
- public class AdviceCache
- {
- #region 单例方法
- public static AdviceCache Instance
- {
- get { return SingletonCreator.instance; }
- }
- class SingletonCreator
- {
- internal static readonly AdviceCache instance = new AdviceCache();
- }
- #endregion
-
-
-
- public IList<AdviceModel> AdviceList { get; set; }
-
-
-
- public IList<AdviceModel> PredictAdviceList { get; set; }
-
-
-
-
- public Dictionary<string, WindturbineInfo> dicWindturbineInfo;
- Dictionary<string, TagInfo> dicTag;
- private static string windSpeedCode_Up82 = ConfigurationManager.AppSettings["WindSpeed_Up82"];
- private static string windSpeedCode_Up105 = ConfigurationManager.AppSettings["WindSpeed_Up105"];
- private static string windSpeedCode_FiveMin = ConfigurationManager.AppSettings["WindSpeedCode_FiveMin"];
-
- private static int UserCancelTime = Convert.ToInt32(ConfigurationManager.AppSettings["UserCancelTime"]);
-
-
-
- private Dictionary<string, string> pointDic;
- private Object AdviceListAddLock = new object();
-
-
-
-
- public void AdviceListAdd(IList<AdviceModel> list)
- {
- lock (AdviceListAddLock)
- {
- if (AdviceList == null)
- AdviceList = new List<AdviceModel>();
-
- IList<AdviceModel> allowList = new List<AdviceModel>();
- if (pointDic == null)
- GetStatusPoint();
- if (PredictAdviceList != null)
- {
- DateTime nowTime = DateTime.Now;
- IList<AdviceModel> alist = PredictAdviceList.Where(s => s.AdviceExecuteTime < nowTime).ToList();
- for (int i = 0; i < alist.Count; i++)
- {
- double? statusValue = null;
- double? hungTypeValue = null;
- if (dicWindturbineInfo[alist[i].WindturbineId].StatusCode.TagData.doubleValue.HasValue)
- statusValue = dicWindturbineInfo[alist[i].WindturbineId].StatusCode.TagData.doubleValue.Value;
- if (dicWindturbineInfo[alist[i].WindturbineId].LockCode.TagData.doubleValue.HasValue)
- hungTypeValue = dicWindturbineInfo[alist[i].WindturbineId].LockCode.TagData.doubleValue.Value;
- if (statusValue.HasValue && hungTypeValue.HasValue)
- {
- WStatus status = HelpperMethod.GetWindturbineStatus(statusValue.Value);
- HungType hungType = HelpperMethod.GetHungType(hungTypeValue.Value);
-
- if (alist[i].AdviseOperation == OperateStyle.Start && status == WStatus.Standby && hungType == HungType.UnLock)
- {
- allowList.Add(alist[i]);
- }
- WStatus[] statusArr = { WStatus.Online, WStatus.OnPower, WStatus.Start };
-
- if (alist[i].AdviseOperation == OperateStyle.Stop && statusArr.Contains(status) && hungType == HungType.UnLock)
- {
- allowList.Add(alist[i]);
- }
- }
- }
- }
-
- IList<AdviceModel> finalList = list;
- for (int i = 0; i < allowList.Count; i++)
- {
- string windturbineId = allowList[i].WindturbineId;
- int count = finalList.Where(s => s.WindturbineId == windturbineId).ToList().Count;
- if (count == 0)
- finalList.Add(allowList[i]);
- }
- for (int i = 0; i < finalList.Count; i++)
- {
- string windturbineId = finalList[i].WindturbineId;
- AdviceModel m = AdviceList.Where(s => s.WindturbineId == windturbineId).FirstOrDefault();
- if (m == null)
- {
- AdviceList.Add(finalList[i]);
-
- InsertAdviceModelHistory(finalList[i]);
- }
- else
- {
-
- if (m.Status == 3)
- {
- TimeSpan sp = DateTime.Now - m.ExecuteTime.Value;
-
- if (sp.TotalMilliseconds > UserCancelTime)
- {
- m.AdviseOperation = finalList[i].AdviseOperation;
- m.AdviceType = finalList[i].AdviceType;
- m.LastUpdateTime = finalList[i].LastUpdateTime;
- m.AdviceExecuteTime = finalList[i].AdviceExecuteTime;
- m.ExecuteTime = finalList[i].ExecuteTime;
- m.ExecuteOperation = finalList[i].ExecuteOperation;
- m.Operater = finalList[i].Operater;
- m.Status = finalList[i].Status;
- m.Notes = finalList[i].Notes;
-
- InsertAdviceModelHistory(finalList[i]);
- }
- }
-
- else if (m.Status == 1)
- {
-
- if (m.AdviseOperation != finalList[i].AdviseOperation)
- {
- m.AdviseOperation = finalList[i].AdviseOperation;
- m.AdviceType = finalList[i].AdviceType;
- m.LastUpdateTime = finalList[i].LastUpdateTime;
- m.AdviceExecuteTime = finalList[i].AdviceExecuteTime;
- m.ExecuteTime = finalList[i].ExecuteTime;
- m.ExecuteOperation = finalList[i].ExecuteOperation;
- m.Operater = finalList[i].Operater;
- m.Status = finalList[i].Status;
- m.Notes = finalList[i].Notes;
-
- UpdateAdviceModelHistory(finalList[i].WindturbineId, 4, "系统", "0");
-
- InsertAdviceModelHistory(finalList[i]);
- }
- }
-
- else if (m.Status == 2)
- {
-
- if (m.AdviseOperation != finalList[i].AdviseOperation)
- {
- m.AdviseOperation = finalList[i].AdviseOperation;
- m.AdviceType = finalList[i].AdviceType;
- m.LastUpdateTime = finalList[i].LastUpdateTime;
- m.AdviceExecuteTime = finalList[i].AdviceExecuteTime;
- m.ExecuteTime = finalList[i].ExecuteTime;
- m.ExecuteOperation = finalList[i].ExecuteOperation;
- m.Operater = finalList[i].Operater;
- m.Status = finalList[i].Status;
- m.Notes = finalList[i].Notes;
-
- InsertAdviceModelHistory(finalList[i]);
- }
- }
-
- else
- {
-
- if (m.AdviseOperation != finalList[i].AdviseOperation)
- {
- m.AdviseOperation = finalList[i].AdviseOperation;
- m.AdviceType = finalList[i].AdviceType;
- m.LastUpdateTime = finalList[i].LastUpdateTime;
- m.AdviceExecuteTime = finalList[i].AdviceExecuteTime;
- m.ExecuteTime = finalList[i].ExecuteTime;
- m.ExecuteOperation = finalList[i].ExecuteOperation;
- m.Operater = finalList[i].Operater;
- m.Status = finalList[i].Status;
- m.Notes = finalList[i].Notes;
-
- InsertAdviceModelHistory(finalList[i]);
- }
- }
- }
- }
-
- IList<string> finalWindturbineIdList = finalList.Select(s => s.WindturbineId).ToList();
- for (int i = 0; i < AdviceList.Count; i++)
- {
- if (!finalWindturbineIdList.Contains(AdviceList[i].WindturbineId))
- {
- if (AdviceList[i].Status == 1&& (AdviceList[i].AdviseOperation==OperateStyle.Start|| AdviceList[i].AdviseOperation == OperateStyle.Stop))
- {
- AdviceList[i].Status = 4;
- UpdateAdviceModelHistory(AdviceList[i].WindturbineId, 4, "系统", "0");
- }
- }
- }
- #region 运行结果打印
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion
- }
- }
-
-
-
-
- public void AddMaintainAdvice(IList<AdviceModel> list)
- {
- lock (AdviceListAddLock)
- {
- if (AdviceList == null)
- AdviceList = new List<AdviceModel>();
-
- var tmpMaintainCopy = AdviceList.Where(a => a.AdviseOperation == OperateStyle.Maintain).ToArray();
- AdviceModel[] copyTmpMaintain = new AdviceModel[tmpMaintainCopy.Length];
- tmpMaintainCopy.CopyTo(copyTmpMaintain, 0);
-
- var tmpMaintainDictionary = copyTmpMaintain.Select(c => new KeyValuePair<string, AdviceModel>(c.WindturbineId, c)).ToDictionary(k => k.Key, v => v.Value);
-
- var tmpCancelMaintainCopy = AdviceList.Where(a => a.AdviseOperation == OperateStyle.UnMaintain).ToArray();
- AdviceModel[] copyTmpCancelMaintain = new AdviceModel[tmpCancelMaintainCopy.Length];
- tmpCancelMaintainCopy.CopyTo(copyTmpCancelMaintain, 0);
-
- var tmpCancelMaintainDictionary = copyTmpCancelMaintain.Select(c => new KeyValuePair<string, AdviceModel>(c.WindturbineId, c)).ToDictionary(k => k.Key, v => v.Value);
- foreach (var l in list)
- {
-
- if (tmpMaintainDictionary.ContainsKey(l.WindturbineId) && l.AdviseOperation == OperateStyle.Maintain)
- {
-
- tmpMaintainDictionary.Remove(l.WindturbineId);
-
- }
-
- else if (tmpMaintainDictionary.ContainsKey(l.WindturbineId) && l.AdviseOperation == OperateStyle.UnMaintain)
- {
-
-
- if (!tmpCancelMaintainDictionary.ContainsKey(l.WindturbineId))
- {
- AdviceList.Add(l);
-
- InsertAdviceModelHistory(l);
- }
- }
-
- else if (tmpCancelMaintainDictionary.ContainsKey(l.WindturbineId) && l.AdviseOperation == OperateStyle.UnMaintain)
- {
- tmpCancelMaintainDictionary.Remove(l.WindturbineId);
- }
-
- else if (tmpCancelMaintainDictionary.ContainsKey(l.WindturbineId) && l.AdviseOperation == OperateStyle.Maintain)
- {
-
- if (tmpMaintainDictionary.ContainsKey(l.WindturbineId))
- {
- AdviceList.Add(l);
-
- InsertAdviceModelHistory(l);
- }
- }
-
- else
- {
- AdviceList.Add(l);
-
- InsertAdviceModelHistory(l);
-
- }
- }
-
-
- foreach (var v in tmpMaintainDictionary.Values)
- {
-
- UpdateAdviceModelHistory(v.WindturbineId, 4, "系统", "0");
- AdviceList.Remove(v);
- }
-
- foreach (var v in tmpCancelMaintainDictionary.Values)
- {
-
- UpdateAdviceModelHistory(v.WindturbineId, 4, "系统", "0");
- AdviceList.Remove(v);
- }
- Console.WriteLine("全场无风 维护&取消维护 运行完成");
- }
- }
-
-
-
- private void GetStatusPoint()
- {
- if (pointDic == null)
- pointDic = new Dictionary<string, string>();
- using (GDNXFDDbContext ctx = new GDNXFDDbContext())
- {
- IList<WindTurbineTestingPointAI> pointList = ctx.WindTurbineTestingPointAI.Where(s => s.UniformCode == "FJZT8").ToList();
- for (int i = 0; i < pointList.Count; i++)
- {
- if (!pointDic.ContainsKey(pointList[i].WindturbineId))
- {
- pointDic.Add(pointList[i].WindturbineId, pointList[i].Id);
- }
- }
- }
- }
- public void GetWindturbinInfo()
- {
- if (dicWindturbineInfo == null)
- {
- dicWindturbineInfo = new Dictionary<string, WindturbineInfo>();
- using (GDNXFDDbContext ctx = new GDNXFDDbContext())
- {
- IList<WindTurbine> list = ctx.WindTurbine.Where(s => s.WindPowerStationId.Contains("_FDC") && s.WindPowerStationId != "QS_FDC").ToList().ToList();
- for (int i = 0; i < list.Count; i++)
- {
- WindturbineInfo d = new WindturbineInfo();
- d.Windturbine = list[i];
- d.WindturbineId = list[i].Id;
- d.StatusCode = new TagInfo();
- d.StatusCode.UniformCode = "FJZT8";
- d.BoxTemperature = new TagInfo();
- d.BoxTemperature.UniformCode = "AI057";
- d.WindturbinePower = new TagInfo();
- d.WindturbinePower.UniformCode = "AI130";
- d.WindSpeedCode = new TagInfo();
- d.WindSpeedCodeFiveMin = new TagInfo();
- if (list[i].ModelId == "UP105-2000-S")
- d.WindSpeedCode.UniformCode = windSpeedCode_Up105;
- else
- d.WindSpeedCode.UniformCode = windSpeedCode_Up82;
- d.LockCode = new TagInfo();
- d.LockCode.UniformCode = "XDSL";
- d.WindSpeedCodeFiveMin.UniformCode = windSpeedCode_FiveMin;
- d.WindDirectionCode = new TagInfo();
- string[] stationIdArr = { "SBQ_FDC", "MHS_FDC" };
- if (stationIdArr.Contains(list[i].WindPowerStationId) && list[i].ModelId == "UP105-2000-S")
- d.WindDirectionCode.UniformCode = "AI010";
- else
- d.WindDirectionCode.UniformCode = "AI008";
- if (!dicWindturbineInfo.ContainsKey(list[i].Id))
- dicWindturbineInfo.Add(d.WindturbineId, d);
- string[] arr = { d.StatusCode.UniformCode, d.WindSpeedCode.UniformCode, d.WindDirectionCode.UniformCode, d.LockCode.UniformCode,d.WindSpeedCodeFiveMin.UniformCode,d.BoxTemperature.UniformCode,d.WindturbinePower.UniformCode };
- string windturbineId = list[i].Id;
- IList<WindTurbineTestingPointAI> aiList = ctx.WindTurbineTestingPointAI.Where(s => s.WindturbineId == windturbineId && arr.Contains(s.UniformCode)).ToList();
- if (dicTag == null)
- dicTag = new Dictionary<string, TagInfo>();
- for (int j = 0; j < aiList.Count; j++)
- {
- if (!dicTag.ContainsKey(aiList[j].Id))
- {
- if (aiList[j].UniformCode == d.WindSpeedCode.UniformCode)
- dicTag.Add(aiList[j].Id, d.WindSpeedCode);
- if (aiList[j].UniformCode == d.WindDirectionCode.UniformCode)
- dicTag.Add(aiList[j].Id, d.WindDirectionCode);
- if (aiList[j].UniformCode == d.StatusCode.UniformCode)
- dicTag.Add(aiList[j].Id, d.StatusCode);
- if (aiList[j].UniformCode == d.LockCode.UniformCode)
- dicTag.Add(aiList[j].Id, d.LockCode);
- if (aiList[j].UniformCode == d.WindSpeedCodeFiveMin.UniformCode)
- dicTag.Add(aiList[j].Id, d.WindSpeedCodeFiveMin);
- if (aiList[j].UniformCode == d.BoxTemperature.UniformCode)
- {
- dicTag.Add(aiList[j].Id, d.BoxTemperature);
- }
- if (aiList[j].UniformCode == d.WindturbinePower.UniformCode)
- {
- dicTag.Add(aiList[j].Id, d.WindturbinePower);
- }
- }
- }
- }
- }
- }
- Dictionary<string, TsData> resultDic = WisdomClient.RestfulClient.findLatestByTagNames(dicTag.Keys.ToArray());
- foreach (var item in resultDic)
- {
- if (dicTag.ContainsKey(item.Key))
- dicTag[item.Key].TagData = item.Value;
- }
-
- Console.WriteLine(" [|]");
- }
- public void GetPredictAdviceModel(WindTurbine wt)
- {
- if (PredictAdviceList == null)
- PredictAdviceList = new List<AdviceModel>();
- foreach (var am in PredictAdviceList)
- {
- if (am.WindturbineId == wt.Id)
- {
- am.LastUpdateTime = DateTime.Now;
- am.Status = 1;
- am.AdviseOperation = OperateStyle.Start;
- am.AdviceExecuteTime = DateTime.Now;
- return;
- }
- }
- AdviceModel adm = new AdviceModel();
- adm.AdviceExecuteTime = DateTime.Now;
- adm.AdviceType = CalculationOriginType.PowerForecast;
- adm.AdviseOperation = OperateStyle.Start;
- adm.LastUpdateTime = DateTime.Now;
- adm.ModelId = wt.ModelId;
- adm.StationId = wt.WindPowerStationId;
- adm.Status = 1;
- adm.WindturbineId = wt.Id;
- PredictAdviceList.Add(adm);
- }
-
-
-
-
-
-
-
- public void InsertAdviceModelHistory(AdviceModel model)
- {
- AdviceHistoryModel hisModel = new AdviceHistoryModel();
- hisModel.Id = Guid.NewGuid().ToString();
- hisModel.WindturbineId = model.WindturbineId;
- hisModel.StationId = model.StationId;
- hisModel.ModelId = model.ModelId;
- hisModel.AdviseOperation = model.AdviseOperation;
- hisModel.AdviceType = model.AdviceType;
- hisModel.LastUpdateTime = model.LastUpdateTime;
- hisModel.AdviceExecuteTime = model.AdviceExecuteTime;
- hisModel.ExecuteTime = model.ExecuteTime;
- hisModel.ExecuteOperation = model.ExecuteOperation;
- hisModel.Status = model.Status;
- IList<AdviceHistoryModel> list = new List<AdviceHistoryModel>();
- list.Add(hisModel);
- AdviceHistoryModelRepository.InsertHistory(list);
- }
-
-
-
-
-
-
-
- public void UpdateAdviceModelHistory(string windturbineId, int actionType, string userName, string userId)
- {
- AdviceHistoryModelRepository.UpdateHistory(windturbineId, userId, userName, actionType);
- }
- }
- }
|