using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using IntelligentControlForsx.Service.WindturbineInfo; using IntelligentControlForsx.Service.WindturbineInfo.Domain; using log4net; using log4net.Util; using WisdomClient; using WisdomClient.data; namespace IntelligentControlForsx.MyControls.windturbine { /// /// 输入输出 /// public partial class FaultInfo2 : BaseControl { private static ILog logger = LogManager.GetLogger("AppInfoLog"); private BindingSource formBindingSource = new BindingSource(); private string partNow = "part1"; Dictionary> infoDic = new Dictionary>(); private static BindingList formDataList = new BindingList(); private string stationIdNow = ""; private string windturbineIdNow = ""; private string modelIdNow = ""; //数据是否绑定完毕 private bool isUpdateOverGv = true; public FaultInfo2() { InitializeComponent(); LeftBind(STATIONID, WINDTURBINEID, MODELID); } public override void Active(string stationId, string windturbineId, string modelId) { STATIONID = stationId; WINDTURBINEID = windturbineId; stationIdNow = stationId; windturbineIdNow = windturbineId; modelIdNow = modelId; //partNow = "part1"; timer1.Enabled = true; Refresh(); } public override void DeActive() { timer1.Enabled = false; } private void timer1_Tick(object sender, EventArgs e) { if (isRefreshOver) { isRefreshOver = false; //刷新数据线程 Thread refreshThread = new Thread(RefreshData); refreshThread.Start(); } //绑定数据线程 if (isUpdateOverGv) { isUpdateOverGv = false; Thread bindDataThread = new Thread(RefreshFormData); bindDataThread.IsBackground = true; bindDataThread.Start(); } } private void RefreshData() { WindturbineInfoSvc infoSvc = WindturbineInfoSvc.Instance; IList codeInfoList = new List(); IList keyList = infoDic.Keys.ToList(); foreach (string key in keyList) { codeInfoList = codeInfoList.Union(infoDic[key]).ToList(); } infoSvc.RefreshPointValue(codeInfoList, WINDTURBINEID); isRefreshOver = true; } private delegate void BindFormInfoDelegate(); private void RefreshFormData() { BindFormInfoDelegate del = new BindFormInfoDelegate(BindFormData); if (this.InvokeRequired) { this.BeginInvoke(del, new object[] { }); } else { BindFormData(); } } private void BindFormData() { DateTime st = DateTime.Now; IList list = WindturbineInfoSvc.Instance.GetPointData(); try { #region 控制上层菜单样式(若其中有一个为报警,则上层菜单样式颜色改变) IList partList = list.Select(s => s.PartIndex).Distinct().ToList(); for (int i = 0; i < partList.Count; i++) { int count = list.Where(s => s.PartIndex == partList[i] && s.PointValue != null && s.PointValue.Contains("1")).ToList().Count; for (int j = 0; j < this.plType.Controls.Count; j++) { if (this.plType.Controls[j] is LabelName) { LabelName ln = (LabelName)this.plType.Controls[j]; if (ln.Name == partList[i]) { if (count > 0) ln.WarningValue = true; else ln.WarningValue = false; } } } } #endregion BindingList formDataList = this.formBindingSource.DataSource as BindingList; IList showList = list.Where(s => s.PartIndex == partNow).ToList(); for (int i = 0; i < showList.Count; i++) { var data = formDataList.Where(s => s.WarningNum == showList[i].WarningNum).FirstOrDefault(); if (data != null) { data.Name = showList[i].Name; data.PlcName = showList[i].PlcName; try { double pointValue = Convert.ToDouble(showList[i].PointValue); if (pointValue == 0) data.PointValue = "正常"; else data.PointValue = "故障"; } catch (Exception ex) { logger.Info("数据转换错误:" + ex.Message); } } #region 修改故障显示样式 for (int j = 0; j < gv.RowCount; j++) { DataGridViewRow row = gv.Rows[i]; string faultString = ""; if (gv.Rows[i].Cells[3].Value != null) faultString = gv.Rows[i].Cells[3].Value.ToString().Trim(); if (faultString.Trim() == "故障") { row.Cells[0].Style.ForeColor = Color.FromArgb(255, 128, 0); row.Cells[1].Style.ForeColor = Color.FromArgb(255, 128, 0); row.Cells[2].Style.ForeColor = Color.FromArgb(255, 128, 0); row.Cells[3].Style.ForeColor = Color.FromArgb(255, 128, 0); } else { row.Cells[0].Style.ForeColor = Color.FromArgb(255, 255, 255); row.Cells[1].Style.ForeColor = Color.FromArgb(255, 255, 255); row.Cells[2].Style.ForeColor = Color.FromArgb(255, 255, 255); row.Cells[3].Style.ForeColor = Color.FromArgb(255, 255, 255); } } #endregion } } catch (Exception ex) { logger.Info("风机参数-故障页面异常"+ex.Message); } finally { isUpdateOverGv = true; DateTime et = DateTime.Now; TimeSpan sp = et - st; Console.WriteLine("绑定页面时间:" + sp.TotalMilliseconds); } } public void LeftBind(string stationId, string windturbineId, string model) { windturbineIdNow = windturbineId; modelIdNow = model; stationIdNow = stationId; plType.Controls.Clear(); IList list = UniformCodeInfoSvc.GetUniformCode(stationId, model, "Fault"); Dictionary> dic = new Dictionary>(); for (int i = 0; i < list.Count; i++) { IList dicList = new List(); if (!dic.ContainsKey(list[i].PartIndex)) { dicList.Add(list[i]); dic.Add(list[i].PartIndex, dicList); } else { dicList = dic[list[i].PartIndex]; dicList.Add(list[i]); } } IList partList = dic.Keys.ToList().OrderByDescending(s => Convert.ToInt32(s.Replace("part", ""))).ToList(); for (int i = 0; i < partList.Count; i++) { LabelName ln = new LabelName(); if (dic[partList[i]].Count > 0) ln.LblName = dic[partList[i]][0].PartName; ln.Name = partList[i]; ln.Dock = DockStyle.Top; ln.MyClick += Type_Clikc; plType.Controls.Add(ln); } //foreach (var item in dic) //{ // LabelName ln = new LabelName(); // if (item.Value.Count > 0) // ln.LblName = item.Value[0].PartName; // ln.Name = item.Key; // ln.Dock = DockStyle.Top; // ln.MyClick += Type_Clikc; // plType.Controls.Add(ln); //} infoDic = dic; if (infoDic.ContainsKey("part1")) partNow = "part1"; GvInit(); } private void Type_Clikc(object sender, EventArgs e) { partNow = sender.ToString(); GvInit(); //plInfo.Controls.Clear(); //string typeName = sender.ToString(); //IList list = infoDic[typeName]; //for (int i = 0; i < list.Count; i++) //{ // OutInLine ln = new OutInLine(); // ln.Name = list[i].PlcName; // ln.PointName = list[i].Name; // ln.PLCName = list[i].PlcName; // ln.Dock = DockStyle.Top; // plInfo.Controls.Add(ln); //} } /// /// gridview初始化工作 /// private void GvInit() { gv.DataSource = null; formDataList = new BindingList(); IList codeInfoList = infoDic[partNow]; for (int i = 0; i < codeInfoList.Count; i++) { FaultDataInfo data = new FaultDataInfo( codeInfoList[i].WarningNum, codeInfoList[i].Name, codeInfoList[i].PlcName, "正常" ); formDataList.Add(data); } formBindingSource.DataSource = formDataList; this.gv.DataSource = formBindingSource; gv.Columns[0].HeaderText = "报警编号"; gv.Columns[1].HeaderText = "PLC变量名"; gv.Columns[2].HeaderText = "报警信息"; gv.Columns[3].HeaderText = "报警状态"; } } }