using System; using System.Collections.Generic; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using EntityDataSet; using log4net; using IntelligentControlForsx.Service; using IntelligentControlForsx.Model; using WisdomClient.data; using WisdomClient; using System.Threading; using IntelligentControlForsx.Common; namespace IntelligentControlForsx.MyControls { public partial class MatrixStation2 : UserControl { private ILog logger = LogManager.GetLogger("AppInfoLog"); private string[] deviceArray; private Dictionary dictMatrixDevice2; private ContextMenuStrip contextMenuStrip; public MatrixStation2() { InitializeComponent(); //this.mpnlCancelMaintain.Recommond(); //this.mpnlMaintain.Recommond(); //this.mpnlReset.Recommond(); //this.mpnlStartup.Recommond(); //this.mpnlStop.Recommond(); } string stationName; string stationId; int cntAll; int cntBW; int cntSD; int cntTJ; int cntDJ; int cntGZ; int cntWH; int cntLX; int cntQD; #region 基本属性 [Browsable(true), Category("Data")] public string StationName { get { return stationName; } set { stationName = value; lblStationName.Text = value; } } [Browsable(true), Category("Data")] public string StationId { get { return stationId; } set { stationId = value; this.mpnlStartup.StationId = value; this.mpnlStop.StationId = value; this.mpnlReset.StationId = value; this.mpnlMaintain.StationId = value; this.mpnlCancelMaintain.StationId = value; } } public ContextMenuStrip ContextMenuStrip1 { get { return contextMenuStrip; } set { contextMenuStrip = value; } } #endregion #region 状态台数 [Browsable(true), Category("Data")] public int CntAll { get { return cntAll; } set { cntAll = value; lblCntAll.Text = value.ToString(); } } [Browsable(true), Category("Data")] public int CntBW { get { return cntBW; } set { cntBW = value; lblCntAll.Text = value.ToString(); } } [Browsable(true), Category("Data")] public int CntSD { get { return cntSD; } set { cntSD = value; lblCntSD.Text = value.ToString(); } } [Browsable(true), Category("Data")] public int CntTJ { get { return cntTJ; } set { cntTJ = value; lblCntTJ.Text = value.ToString(); } } [Browsable(true), Category("Data")] public int CntDJ { get { return cntDJ; } set { cntDJ = value; lblCntDJ.Text = value.ToString(); } } [Browsable(true), Category("Data")] public int CntGZ { get { return cntGZ; } set { cntGZ = value; lblCntGZ.Text = value.ToString(); } } [Browsable(true), Category("Data")] public int CntWH { get { return cntWH; } set { cntWH = value; lblCntWH.Text = value.ToString(); } } [Browsable(true), Category("Data")] public int CntLX { get { return cntLX; } set { cntLX = value; lblCntLX.Text = value.ToString(); } } [Browsable(true), Category("Data")] public int CntQD { get { return cntQD; } set { cntQD = value; lblCntQD.Text = value.ToString(); } } #endregion public void InitTablePanel() { if (String.IsNullOrWhiteSpace(stationId)) return; deviceArray = MatrixFormService.GetDeviceIdArrayByStationId(stationId); var wtArray = CacheService.Instance.GetWindturbineListByStationId(stationId).ToArray(); if (wtArray == null || wtArray.Length < 1) return; dictMatrixDevice2 = new Dictionary(); int col = 20; int row = (int)Math.Ceiling((decimal)wtArray.Length / 20); int arrIndex = 0; pnlTable.Height = row * 42 + 10; //this.Height = pnlTable.Height + 33; pnlTable.RowCount = row; for (int i = 0; i < row; i++) { pnlTable.RowStyles.Add(new RowStyle(SizeType.Absolute, 42)); for (int j = 0; j < col; j++) { if (arrIndex >= wtArray.Length) return; MatrixBlock device = new MatrixBlock(); device.ContextMenuStrip = contextMenuStrip; device.DeviceId = CommonMethod.GetShortWindturbineId(wtArray[arrIndex].ID); device.DeviceModel = wtArray[arrIndex].MODELID; device.StationId = wtArray[arrIndex].WINDPOWERSTATIONID; device.Status = 7; device.Ts = 0; device.MyMouseDown += MyMouseDownHandler; device.MyMouseUp += MyMouseUpHandler; pnlTable.Controls.Add(device); if (!dictMatrixDevice2.ContainsKey(device.DeviceId)) dictMatrixDevice2.Add(device.DeviceId, device); pnlTable.SetRow(device, i); pnlTable.SetColumn(device, j); arrIndex++; } } } public void DeActive() { this.Hide(); timer1.Stop(); this.mpnlCancelMaintain.DeActive(); this.mpnlMaintain.DeActive(); this.mpnlReset.DeActive(); this.mpnlStartup.DeActive(); this.mpnlStop.DeActive(); } public void Active() { this.Show(); Thread ayscThread = new Thread(LoadData); ayscThread.IsBackground = true; ayscThread.Start(); timer1.Start(); this.mpnlCancelMaintain.Active(); this.mpnlMaintain.Active(); this.mpnlReset.Active(); this.mpnlStartup.Active(); this.mpnlStop.Active(); } #region 异步加载数据 private void timer1_Tick(object sender, EventArgs e) { if (isLoadding) return; Thread ayscThread = new Thread(LoadData); ayscThread.IsBackground = true; ayscThread.Start(); } private bool isLoadding = true; private void LoadData() { isLoadding = true; try { UpdateStationStates(); UpdateWindturbineStates(); } catch (Exception ex) { logger.Info("读取实时数据失败!ex=" + ex.Message); } finally { isLoadding = false; } } private void UpdateStationStates() { var dict = CacheService.Instance.MatrixPointMap; if (dict == null) return; var pmms = dict.Values.Where(q => q.ThingType == PointType.Station && q.ThingId == stationId).ToArray(); var points = pmms.Select(q => q.PointId).ToArray(); Dictionary dictResult = RestfulClient.findLatestByTagNames(points); lock (this) { this.BeginInvoke( (Action)delegate { for (int i = 0; i < points.Length; i++) { if (dictResult.ContainsKey(points[i]) == false) continue; double value = dictResult[points[i]].doubleValue.Value; pmms[i].Value = value; switch (pmms[i].UniformCode) { #region Switch case "BWTS8": lblCntBW.Text = value.ToString("f0"); break; case "LXTS8": lblCntLX.Text = value.ToString("f0"); break; case "DJTS8": lblCntDJ.Text = value.ToString("f0"); break; case "WHTS8": lblCntWH.Text = value.ToString("f0"); break; case "GZTS8": lblCntGZ.Text = value.ToString("f0"); break; case "TJTS8": lblCntTJ.Text = value.ToString("f0"); break; case "QDTS8": lblCntQD.Text = value.ToString("f0"); break; case "SDTS8": lblCntSD.Text = value.ToString("f0"); break; #endregion } } }); } } //private void UpdateStationStates() { } private void UpdateWindturbineStates() { if (deviceArray == null || deviceArray.Length < 1) return; var dict = CacheService.Instance.MatrixPointMap; if (dict == null) return; var pmms = dict.Values.Where(q => q.ThingType == PointType.Windturbine && deviceArray.Contains(q.ThingId)).ToArray(); var points = pmms.Select(q => q.PointId).ToArray(); Dictionary dictResult = RestfulClient.findLatestByTagNames(points); lock (this) { this.BeginInvoke( (Action)delegate { for (int i = 0; i < points.Length; i++) { if (dictResult.ContainsKey(points[i]) == false) continue; double value = dictResult[points[i]].doubleValue.Value; long ts = dictResult[points[i]].ts; pmms[i].Value = value; var wBlock = dictMatrixDevice2[pmms[i].ThingId]; if (wBlock == null) continue; string windturbineId = CommonMethod.GetLongWindturbineId(pmms[i].ThingId); switch (pmms[i].UniformCode) { #region Switch //"AI021", //3秒平均风速 //"AI130", //功率 //"AI010", //风向角度 //"FJZT" //风机状态 case "AI022": wBlock.WindSpeed = value; break; case "AI130": wBlock.Power = value; break; //case "AI010": // wBlock.WindAngle = values[i].ToString("f2") + " 度"; // break; case "FJZT8": wBlock.Status = value; wBlock.Ts = ts; break; case "XDSL": if (value != 0) wBlock.IsLock = true; else wBlock.IsLock = false; wBlock.LockValue = value; break; #endregion } } refreshDevices(); }); } } #endregion #region 数据筛选 private double selectedStatus = 9; private bool autoFilter = false; private void pnlAll_Click(object sender, EventArgs e) { selectedStatus = 9; refreshDevices(); } private void refreshDevices() { foreach (var device in dictMatrixDevice2.Values) { double minSpeed, maxSpeed, minPower, maxPower; Double.TryParse(txtMinSpeed.Text, out minSpeed); Double.TryParse(txtMaxSpeed.Text, out maxSpeed); Double.TryParse(txtMinPower.Text, out minPower); Double.TryParse(txtMaxPower.Text, out maxPower); if (device.testShow(selectedStatus, autoFilter, minSpeed, maxSpeed, minPower, maxPower)) { device.Show(); } else { device.Hide(); } } } //风机六种状态: 限电5》运行1》待机0》维护4》故障2》离线3 private void pnlBW_Click(object sender, EventArgs e) { selectedStatus = 1; refreshDevices(); } private void pnlDJ_Click(object sender, EventArgs e) { selectedStatus = 0; refreshDevices(); } private void pnlGZ_Click(object sender, EventArgs e) { selectedStatus = 2; refreshDevices(); } private void pnlWH_Click(object sender, EventArgs e) { selectedStatus = 4; refreshDevices(); } private void pnlLX_Click(object sender, EventArgs e) { selectedStatus = 3; refreshDevices(); } private void pnlQD_Click(object sender, EventArgs e) { selectedStatus = 11; refreshDevices(); } private void pnlTJ_Click(object sender, EventArgs e) { selectedStatus = 12; refreshDevices(); } private void pnlSD_Click(object sender, EventArgs e) { selectedStatus = 13; refreshDevices(); } private void chkAutoFilter_CheckedChanged(object sender, EventArgs e) { autoFilter = chkAutoFilter.Checked; if (autoFilter) refreshDevices(); } #endregion // public event EventHandler MyMouseDown; private void MyMouseDownHandler(object sender, EventArgs e) { PointGroup obj = (PointGroup)sender; if (obj != null) { // MessageBox.Show(obj.ToString()); } } private void MyMouseUpHandler(object sender, EventArgs e) { PointGroup obj = (PointGroup)sender; if (obj != null) { //MessageBox.Show(obj.ToString()); } } } }