using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using EntityDataSet; using IntelligentControlForsx.ChildForms; using IntelligentControlForsx.Service.WindturbineControl.Domain; using IntelligentControlForsx.Service.WindturbineControl.Domain.Cmd; using IntelligentControlForsx.Service.WindturbineControl.Domain.FormInfo; using IntelligentControlForsx.Service.WindturbineControl.FormInfoSvc; using Gyee_Control.Svc.Cache; using Gyee_Control.View; using log4net; using WisdomClient; using WisdomClient.data; namespace IntelligentControlForsx.MyControls.kz { public partial class XS_Control : UserControl { private ILog logger = LogManager.GetLogger("AppInfoLog"); IList windturbineList = new List(); private BindingSource formBindingSource = new BindingSource(); private static BindingList formDataList = new BindingList(); //数据是否刷新完毕 private bool isUpdateOverData = true; //数据是否绑定完毕 private bool isUpdateOverGv = true; private string projectId = "XS01_GC"; private bool isNeedInsertCheck = true; /// /// 已经勾选的风机 /// private IList isCheckWindturbineIdList = new List(); private int OldSelectRow = -1; public XS_Control() { InitializeComponent(); using (wisdom_cs_entity ctx = new wisdom_cs_entity()) { windturbineList = ctx.windturbine.Where(s => s.WINDPOWERSTATIONID == "XS_FDC").ToList(); } IList selectItemList = new List(); selectItemList.Add(new HungTypeSelectItemEntity(-1, "请选择")); selectItemList.Add(new HungTypeSelectItemEntity((int)HungType.CheckLock, "检修")); selectItemList.Add(new HungTypeSelectItemEntity((int)HungType.FaultLock, "故障检修")); selectItemList.Add(new HungTypeSelectItemEntity((int)HungType.StationCheckLock, "场内受累检修")); selectItemList.Add(new HungTypeSelectItemEntity((int)HungType.StationFaulLockt, "场内受累故障")); selectItemList.Add(new HungTypeSelectItemEntity((int)HungType.StationPowerLineLock, "场外受累电网")); selectItemList.Add(new HungTypeSelectItemEntity((int)HungType.StationWeatherLock, "场外受累天气")); this.cbHungReason.DataSource = selectItemList; this.cbHungReason.DisplayMember = "HungName"; this.cbHungReason.ValueMember = "HungType"; this.cbHungReason.SelectedValue = -1; btnMaintain.Enabled = false; btnUnMaintain.Enabled = false; btnRest.Enabled = false; Init(projectId); } public void Active() { timer1.Enabled = true; } public void DeActive() { timer1.Enabled = false; } private void lblProject1_Click(object sender, EventArgs e) { Init("XS01_GC"); projectId = "XS01_GC"; lblProject1.BackColor = Color.FromArgb(15, 141, 106); lblProject2.BackColor = Color.FromArgb(3, 24, 48); isCheckWindturbineIdList = new List(); btnMaintain.Enabled = false; btnUnMaintain.Enabled = false; btnRest.Enabled = false; } private void lblProject2_Click(object sender, EventArgs e) { Init("XS02_GC"); projectId = "XS02_GC"; lblProject1.BackColor = Color.FromArgb(3, 24, 48); lblProject2.BackColor = Color.FromArgb(15, 141, 106); isCheckWindturbineIdList = new List(); btnMaintain.Enabled = true; btnUnMaintain.Enabled = true; btnRest.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { if (isUpdateOverData) { isUpdateOverData = false; Thread getDataThread = new Thread(UpdatePointData);//创建数据更新线程 getDataThread.Start(); } if (isUpdateOverGv) { isUpdateOverGv = false; Thread t = new Thread(BindFormThread); t.IsBackground = true; t.Start(); } } private delegate void UpdateGridViewDelegate(); private void Init(string projectId) { gvWindturbine.DataSource = null; formDataList = new BindingList(); IList pointDataList = new List(); IList windturbineIdList = windturbineList.Where(s => s.PROJECTID == projectId).ToList().Select(s => s.ID).ToList(); for (int i = 0; i < windturbineIdList.Count; i++) { if (CacheInfo.CacheWindturbineDataDic.ContainsKey(windturbineIdList[i])) pointDataList.Add(CacheInfo.CacheWindturbineDataDic[windturbineIdList[i]]); } if (pointDataList.Count <= 0) { IList list = windturbineList.Where(s => s.WINDPOWERSTATIONID == "XS_FDC" && s.PROJECTID == projectId).ToList().OrderBy(s => Convert.ToInt32(Regex.Replace(s.ID, "[a-zA-Z]", "").Replace("_", ""))).ToList(); for (int i = 0; i < list.Count; i++) { WindturbineFormData data = new WindturbineFormData(list[i].ID, "0.0", "0.0", "未知", "未知", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0", "0.0" ); formDataList.Add(data); } formBindingSource.DataSource = formDataList; this.gvWindturbine.DataSource = formBindingSource; } else { IList orderList = pointDataList.OrderBy(s => Convert.ToInt32(Regex.Replace(s.WindturbineName, "[a-zA-Z]", "").Replace("_", ""))).ToList(); for (int i = 0; i < orderList.Count; i++) { string statusString = ""; #region 状态转化字符串 switch (pointDataList[i].Status) { case WindturbineStatus.Standby: statusString = "待机"; break; case WindturbineStatus.OnPower: statusString = "上电"; break; case WindturbineStatus.Maintain: statusString = "维护"; break; case WindturbineStatus.Fault: statusString = "故障"; break; case WindturbineStatus.Start: statusString = "启动"; break; case WindturbineStatus.Online: statusString = "并网"; break; case WindturbineStatus.Stop: statusString = "停机"; break; case WindturbineStatus.Offline: statusString = "离线"; break; } #endregion string hungTypeString = ""; switch (pointDataList[i].HungType) { case HungType.UnLock: hungTypeString = "正常"; break; case HungType.StationCheckLock: hungTypeString = "场内受累检修"; break; case HungType.StationFaulLockt: hungTypeString = "场内受累故障"; break; case HungType.StationPowerLineLock: hungTypeString = "场外受累电网"; break; case HungType.StationWeatherLock: hungTypeString = "场外受累天气"; break; case HungType.FaultLock: hungTypeString = "故障检修"; break; case HungType.CheckLock: hungTypeString = "检修"; break; } WindturbineFormData data = new WindturbineFormData(orderList[i].WindturbineName, orderList[i].WindSpeed.ToString("f2"), orderList[i].Power.ToString("f2"), statusString, hungTypeString, orderList[i].AlternatorSpeed.ToString("f2"), orderList[i].LaminaSpeed.ToString("f2"), orderList[i].LimitSpeed.ToString("f2"), orderList[i].LaminaAngle1.ToString("f2"), orderList[i].LaminaAngle2.ToString("f2"), orderList[i].LaminaAngle3.ToString("f2"), orderList[i].WindAngle.ToString("f2"), orderList[i].YawPosition.ToString("f2") ); formDataList.Add(data); } formBindingSource.DataSource = formDataList; this.gvWindturbine.DataSource = formBindingSource; } #region 添加勾选列 -首次加载才会添加 if (isNeedInsertCheck) { DataGridViewCheckBoxColumn checkColum = new DataGridViewCheckBoxColumn(); checkColum.HeaderText = ""; checkColum.Name = "Select"; checkColum.DataPropertyName = "Select"; checkColum.ReadOnly = false; checkColum.TrueValue = true; checkColum.FalseValue = false; gvWindturbine.Columns.Insert(0, checkColum); isNeedInsertCheck = false; } gvWindturbine.Columns[0].Width = 50; gvWindturbine.Columns[1].Width = 110; #endregion #region 修改表头 gvWindturbine.Columns[1].HeaderText = "风机编号"; gvWindturbine.Columns[2].HeaderText = "风速 m/s"; gvWindturbine.Columns[3].HeaderText = "功率 kw"; gvWindturbine.Columns[4].HeaderText = "状态"; gvWindturbine.Columns[5].HeaderText = "挂牌"; gvWindturbine.Columns[6].HeaderText = "发电机转速"; gvWindturbine.Columns[7].HeaderText = "限速值"; gvWindturbine.Columns[8].HeaderText = "叶轮转速"; gvWindturbine.Columns[9].HeaderText = "桨叶角度1"; gvWindturbine.Columns[10].HeaderText = "桨叶角度2"; gvWindturbine.Columns[11].HeaderText = "桨叶角度3"; gvWindturbine.Columns[12].HeaderText = "对风角度"; gvWindturbine.Columns[13].HeaderText = "偏航位置"; #endregion } /// /// 后台更新数据 /// private void UpdatePointData() { IList pList = new List(); pList.Add("XS_FDC"); pList.Add(projectId); WindturbinePointDataSvc.UpdatePointDataByStationAndProject(pList); isUpdateOverData = true; } public void BindFormThread() { UpdateGridViewDelegate del = new UpdateGridViewDelegate(FormDataBind); if (gvWindturbine.InvokeRequired) { gvWindturbine.BeginInvoke(del, new object[] { }); } else { FormDataBind(); } } public void FormDataBind() { IList pointDataList = new List(); BindingList dataList = this.formBindingSource.DataSource as BindingList; IList windturbineIdList = dataList.Select(s => s.WindturbineId).ToList(); for (int i = 0; i < windturbineIdList.Count; i++) { if (CacheInfo.CacheWindturbineDataDic.ContainsKey(windturbineIdList[i])) pointDataList.Add(CacheInfo.CacheWindturbineDataDic[windturbineIdList[i]]); } for (int i = 0; i < pointDataList.Count; i++) { var data = dataList.Where(s => s.WindturbineId == pointDataList[i].WindturbineName).FirstOrDefault(); if (data != null) { data.WindSpeed = pointDataList[i].WindSpeed.ToString("f2"); data.Power = pointDataList[i].Power.ToString("f2"); switch (pointDataList[i].Status) { case WindturbineStatus.Standby: data.StatusString = "待机"; break; case WindturbineStatus.OnPower: data.StatusString = "上电"; break; case WindturbineStatus.Maintain: data.StatusString = "维护"; break; case WindturbineStatus.Fault: data.StatusString = "故障"; break; case WindturbineStatus.Start: data.StatusString = "启动"; break; case WindturbineStatus.Online: data.StatusString = "并网"; break; case WindturbineStatus.Stop: data.StatusString = "停机"; break; case WindturbineStatus.Offline: data.StatusString = "离线"; break; } switch (pointDataList[i].HungType) { case HungType.UnLock: data.HungTypeString = "正常"; break; case HungType.StationCheckLock: data.HungTypeString = "场内受累检修"; break; case HungType.StationFaulLockt: data.HungTypeString = "场内受累故障"; break; case HungType.StationPowerLineLock: data.HungTypeString = "场外受累电网"; break; case HungType.StationWeatherLock: data.HungTypeString = "场外受累天气"; break; case HungType.FaultLock: data.HungTypeString = "故障检修"; break; case HungType.CheckLock: data.HungTypeString = "检修"; break; } if (pointDataList[i].ModelId != "UP105-2000-S") data.AlternatorSpeed = pointDataList[i].AlternatorSpeed.ToString("f2"); else data.AlternatorSpeed = (pointDataList[i].AlternatorSpeed * 9.55).ToString("f2"); data.LaminaSpeed = pointDataList[i].LaminaSpeed.ToString("f2"); data.LimitSpeed = pointDataList[i].LimitSpeed.ToString("f2"); if (pointDataList[i].ModelId == "UP105-2000-S") { data.LaminaAngle1 = (pointDataList[i].LaminaAngle1 * 57.3).ToString("f2"); data.LaminaAngle2 = (pointDataList[i].LaminaAngle2 * 57.3).ToString("f2"); data.LaminaAngle3 = (pointDataList[i].LaminaAngle3 * 57.3).ToString("f2"); } else { data.LaminaAngle1 = pointDataList[i].LaminaAngle1.ToString("f2"); data.LaminaAngle2 = pointDataList[i].LaminaAngle2.ToString("f2"); data.LaminaAngle3 = pointDataList[i].LaminaAngle3.ToString("f2"); } data.WindAngle = pointDataList[i].WindAngle.ToString("f2"); data.YawPosition = pointDataList[i].YawPosition.ToString("f2"); } } #region 修改风机状态背景色 for (int i = 0; i < gvWindturbine.RowCount; i++) { string statusString = gvWindturbine.Rows[i].Cells[4].Value.ToString(); string windturbineId = gvWindturbine.Rows[i].Cells[1].Value.ToString().Trim(); DataGridViewRow row = gvWindturbine.Rows[i]; switch (statusString) { case "待机": row.Cells[1].Style.BackColor = Color.FromArgb(15, 141, 106); break; case "上电": row.Cells[1].Style.BackColor = Color.FromArgb(177, 14, 126); break; case "维护": row.Cells[1].Style.BackColor = Color.FromArgb(204, 83, 51); break; case "故障": row.Cells[1].Style.BackColor = Color.FromArgb(170, 15, 59); break; case "启动": row.Cells[1].Style.BackColor = Color.FromArgb(14, 72, 91); break; case "并网": row.Cells[1].Style.BackColor = Color.FromArgb(15, 135, 170); break; case "停机": row.Cells[1].Style.BackColor = Color.FromArgb(176, 151, 63); break; case "离线": row.Cells[1].Style.BackColor = Color.FromArgb(134, 150, 165); break; } if (gvWindturbine.Rows[i].Cells[5].Value.ToString().Trim() != "正常" && gvWindturbine.Rows[i].Cells[5].Value.ToString().Trim() != "未知") { row.Cells[5].Style.BackColor = Color.FromArgb(255, 255, 0); row.Cells[5].Style.ForeColor = Color.FromArgb(0, 0, 0); } else { row.Cells[5].Style.BackColor = row.Cells[6].Style.BackColor; row.Cells[5].Style.ForeColor = row.Cells[6].Style.ForeColor; } } #endregion isUpdateOverGv = true; } //清除所有勾选风机 private void ClearCheck() { for (int i = 0; i < gvWindturbine.RowCount; i++) { gvWindturbine.Rows[i].Cells["Select"].Value = 0; } } private void btnLimitPower_Click(object sender, EventArgs e) { string limitPower = txtPower.Text.Trim(); if (string.IsNullOrEmpty(limitPower)) { MessageBox.Show("请输入限定功率"); return; } else { double power = 0.0; bool IsNumber = Double.TryParse(limitPower, out power); if (!IsNumber) { MessageBox.Show("输入限定功率不合法"); return; } else { string cmdDescribe = null; cmdDescribe = "限定功率"; IList cmdInfoList = new List(); for (int i = 0; i < isCheckWindturbineIdList.Count; i++) { WindturbineCmdInfo info = new WindturbineCmdInfo(); info.WindturbineId = isCheckWindturbineIdList[i]; info.StationId = "XS_FDC"; info.CmdType = CmdType.LimitPower; cmdInfoList.Add(info); } Confirm confirm = new Confirm("XS_FDC", cmdInfoList, cmdDescribe, power); confirm.ShowDialog(); } } txtPower.Text = ""; isCheckWindturbineIdList = new List(); ClearCheck(); } private void btnLimitSpeed_Click(object sender, EventArgs e) { string limitSpeed = txtSpeed.Text.Trim(); if (string.IsNullOrEmpty(limitSpeed)) { MessageBox.Show("请输入限定转速"); return; } else { double speed = 0.0; bool IsNumber = Double.TryParse(limitSpeed, out speed); if (!IsNumber) { MessageBox.Show("输入限定转速不合法"); return; } else { string cmdDescribe = null; cmdDescribe = "限定转速"; IList cmdInfoList = new List(); for (int i = 0; i < isCheckWindturbineIdList.Count; i++) { WindturbineCmdInfo info = new WindturbineCmdInfo(); info.WindturbineId = isCheckWindturbineIdList[i]; info.StationId = "XS_FDC"; info.CmdType = CmdType.LimitSpeed; cmdInfoList.Add(info); } Confirm confirm = new Confirm("XS_FDC", cmdInfoList, cmdDescribe, speed); confirm.ShowDialog(); } } txtSpeed.Text = ""; isCheckWindturbineIdList = new List(); ClearCheck(); } private void btnHang_Click(object sender, EventArgs e) { string hungInfo = cbHungReason.SelectedValue.ToString().Trim(); if (hungInfo == "-1") { MessageBox.Show("请选择挂牌原因!"); return; } HungType type = (HungType)Enum.Parse(typeof(HungType), hungInfo); bool isSuccess = WindturbineLock("XS_FDC", isCheckWindturbineIdList, type); if (isSuccess) { string message = ""; for (int i = 0; i < isCheckWindturbineIdList.Count; i++) { message = message + isCheckWindturbineIdList[i] + ","; } message = "已针对" + message + "风机进行挂牌操作"; MessageBox.Show(message); } isCheckWindturbineIdList = new List(); ClearCheck(); cbHungReason.SelectedValue = -1; } private void btnUnHang_Click(object sender, EventArgs e) { bool isSuccess = WindturbineLock("XS_FDC", isCheckWindturbineIdList, HungType.UnLock); if (isSuccess) { string message = ""; for (int i = 0; i < isCheckWindturbineIdList.Count; i++) { message = message + isCheckWindturbineIdList[i] + ","; } message = "已针对" + message + "风机进行取消挂牌操作"; MessageBox.Show(message); } isCheckWindturbineIdList = new List(); ClearCheck(); } private void btnStart_Click(object sender, EventArgs e) { string cmdDescribe = null; cmdDescribe = "正常启动"; IList cmdInfoList = new List(); for (int i = 0; i < isCheckWindturbineIdList.Count; i++) { WindturbineCmdInfo info = new WindturbineCmdInfo(); info.WindturbineId = isCheckWindturbineIdList[i]; info.StationId = "XS_FDC"; info.CmdType = CmdType.Start; cmdInfoList.Add(info); } Confirm confirm = new Confirm("XS_FDC", cmdInfoList, cmdDescribe); confirm.ShowDialog(); isCheckWindturbineIdList = new List(); ClearCheck(); } private void btnStop_Click(object sender, EventArgs e) { string cmdDescribe = null; cmdDescribe = "正常停机"; IList cmdInfoList = new List(); for (int i = 0; i < isCheckWindturbineIdList.Count; i++) { WindturbineCmdInfo info = new WindturbineCmdInfo(); info.WindturbineId = isCheckWindturbineIdList[i]; info.StationId = "XS_FDC"; info.CmdType = CmdType.Stop; cmdInfoList.Add(info); } Confirm confirm = new Confirm("XS_FDC", cmdInfoList, cmdDescribe); confirm.ShowDialog(); isCheckWindturbineIdList = new List(); ClearCheck(); } private void btnRest_Click(object sender, EventArgs e) { string cmdDescribe = null; cmdDescribe = "正常复位"; IList cmdInfoList = new List(); for (int i = 0; i < isCheckWindturbineIdList.Count; i++) { WindturbineCmdInfo info = new WindturbineCmdInfo(); info.WindturbineId = isCheckWindturbineIdList[i]; info.StationId = "XS_FDC"; info.CmdType = CmdType.Reset; cmdInfoList.Add(info); } Confirm confirm = new Confirm("XS_FDC", cmdInfoList, cmdDescribe); confirm.ShowDialog(); isCheckWindturbineIdList = new List(); ClearCheck(); } private void btnMaintain_Click(object sender, EventArgs e) { string cmdDescribe = null; cmdDescribe = "正常维护"; IList cmdInfoList = new List(); for (int i = 0; i < isCheckWindturbineIdList.Count; i++) { WindturbineCmdInfo info = new WindturbineCmdInfo(); info.WindturbineId = isCheckWindturbineIdList[i]; info.StationId = "XS_FDC"; info.CmdType = CmdType.Maintain; cmdInfoList.Add(info); } Confirm confirm = new Confirm("XS_FDC", cmdInfoList, cmdDescribe); confirm.ShowDialog(); isCheckWindturbineIdList = new List(); ClearCheck(); } private void btnUnMaintain_Click(object sender, EventArgs e) { string cmdDescribe = null; cmdDescribe = "正常取消维护"; IList cmdInfoList = new List(); for (int i = 0; i < isCheckWindturbineIdList.Count; i++) { WindturbineCmdInfo info = new WindturbineCmdInfo(); info.WindturbineId = isCheckWindturbineIdList[i]; info.StationId = "XS_FDC"; info.CmdType = CmdType.UnMaintain; cmdInfoList.Add(info); } Confirm confirm = new Confirm("XS_FDC", cmdInfoList, cmdDescribe); confirm.ShowDialog(); isCheckWindturbineIdList = new List(); ClearCheck(); } private void gvWindturbine_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { if (e.RowIndex != -1) { if (e.ColumnIndex == 0) { string isCheck = gvWindturbine.Rows[e.RowIndex].Cells["Select"].Value == null ? "0" : gvWindturbine.Rows[e.RowIndex].Cells["Select"].Value.ToString(); string windturbineId = this.gvWindturbine.Rows[e.RowIndex].Cells[1].Value.ToString(); if (isCheck == "1") { gvWindturbine.Rows[e.RowIndex].Cells["Select"].Value = 0; int count = isCheckWindturbineIdList.Where(s => s == windturbineId).ToList().Count; if (count > 0) isCheckWindturbineIdList.Remove(windturbineId); } else { gvWindturbine.Rows[e.RowIndex].Cells["Select"].Value = 1; int count = isCheckWindturbineIdList.Where(s => s == windturbineId).ToList().Count; if (count <= 0) isCheckWindturbineIdList.Add(windturbineId); } } } else { if (e.ColumnIndex == 0) { for (int i = 0; i < gvWindturbine.RowCount; i++) { string isCheck = gvWindturbine.Rows[i].Cells["Select"].Value == null ? "0" : gvWindturbine.Rows[i].Cells["Select"].Value.ToString(); string windturbineId = this.gvWindturbine.Rows[i].Cells[1].Value.ToString(); if (isCheck == "1") { gvWindturbine.Rows[i].Cells["Select"].Value = 0; int count = isCheckWindturbineIdList.Where(s => s == windturbineId).ToList().Count; if (count > 0) isCheckWindturbineIdList.Remove(windturbineId); } else { gvWindturbine.Rows[i].Cells["Select"].Value = 1; int count = isCheckWindturbineIdList.Where(s => s == windturbineId).ToList().Count; if (count <= 0) isCheckWindturbineIdList.Add(windturbineId); } } } } } private bool WindturbineLock(string pStationId, IList windturbineIdList, HungType type) { bool isSuccess = false; try { user userData = null; FingerCheck2 check = new FingerCheck2(); check.ShowDialog(); userData = FingerCheck2.checkSuccessUser; if (userData != null) { for (int i = 0; i < windturbineIdList.Count; i++) { using (wisdom_cs_entity ctx = new wisdom_cs_entity()) { string wId = windturbineIdList[i]; view_tspoint point = ctx.view_tspoint.Where(s => s.thing_id == wId && s.thing_type == "windturbine" && s.uniform_code == "XDSL").FirstOrDefault(); if (point != null) { #region 将挂牌数据写入实时库 TsPointData tData = new TsPointData(); tData.tagName = point.id; TsData t = new TsData(); t.doubleValue = (int)type; DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); TimeSpan sp = DateTime.Now.Subtract(dtStart); long time = long.Parse(sp.Ticks.ToString().Substring(0, sp.Ticks.ToString().Length - 4)); t.ts = time; tData.tsData = t; RestfulClient.writeLatest(tData); #endregion #region 写入操作日志 control_log logData = new control_log(); logData.windturbine_id = windturbineIdList[i]; logData.station_id = pStationId; logData.control_user_id = userData.id; logData.send_control_time = DateTime.Now; logData.control_type = (int)CmdType.UnHangLock; logData.is_send_control_success = true; ctx.Entry(logData).State = EntityState.Added; int count = ctx.SaveChanges(); if (count > 0) isSuccess = true; #endregion } } } } } catch (Exception ex) { isSuccess = false; logger.Error(ex); } return isSuccess; } } }