传感器限差设置包括:测斜仪、渗压计、水位计、雨量计等项限差设置。预警信息区为已经添加完成的信息,预警信息配置区可以实现对各预警分项的添加、删除和对添加完成的预警值的修改。
首先实现水位计的限差管理,首先查询设备表,将所有的水位计信息加载到主界面上,然后分别设置每个水位计对应的一级,二级,三级预警信息,包括预警值,逻辑关系,是否屏蔽等信息,所有的预警设置信息存到WarningSet表中,预警等级信息存放到预警字典表WaringDict中,然后用户通过操作实现对预警信息的设置、修改和删除操作。
具体实现代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using SMOS.Component; 10 using SMOS.Presention.Presenter; 11 using SMOS.Presention.IView; 12 using SMOS.Model.Device; 13 using SMOS.Model; 14 using System.Collections; 15 16 namespace SMOS.UI.WarningToleranceManage 17 { 18 public partial class WaterLevelToleranceManageForm : TabWindow, IWarningSetView, IDeviceSetView 19 { 20 /// <summary> 21 /// 控制处理类 22 /// </summary> 23 private WarningSetPresenter Presenter; 24 /// <summary> 25 /// DatagridView的数据源 26 /// </summary> 27 private DataTable dtSource; 28 ArrayList levelList = new ArrayList();//不带Key的动态数组,用于存放字典表的levelValue 29 30 31 #region DatagridView的列名值 32 private const string COLUMN_WARNINGSETINFO_NAME = "DeviceNme"; 33 private const string COLUMN_WARNINGSETINFO_REMARK = "Remark"; 34 private const string COLUMN_WARNINGSETINFO_LEVEL1 = "Level1"; 35 private const string COLUMN_WARNINGSETINFO_SHIELD1= "ShieldingLevel1"; 36 private const string COLUMN_WARNINGSETINFO_LEVEL2 = "Level2"; 37 private const string COLUMN_WARNINGSETINFO_SHIELD2 = "ShieldingLevel2"; 38 private const string COLUMN_WARNINGSETINFO_LEVEL3 = "Level3"; 39 private const string COLUMN_WARNINGSETINFO_SHIELD3 = "ShieldingLevel3"; 40 #endregion 41 42 #region IWorningSetView 接口实现 43 public IList<Model.WarningSetInfo> lstWarningSetInfo 44 { 45 get; 46 set; 47 } 48 public Model.Enums.DeviceType eDeviceType 49 { 50 get 51 { 52 return Model.Enums.DeviceType.WaterLevel; 53 } 54 } 55 56 public IList<Model.Device.DeviceInfo> lstDevice 57 { 58 get; 59 set; 60 } 61 #endregion 62 63 public WaterLevelToleranceManageForm() 64 { 65 InitializeComponent(); 66 } 67 /// <summary> 68 /// 加载水位计预警设置界面 69 /// </summary> 70 /// <param name="sender"></param> 71 /// <param name="e"></param> 72 private void WaterLevelToleranceManageForm_Load(object sender, EventArgs e) 73 { 74 //初始化数据源 75 dtSource = new DataTable(); 76 dtSource.Columns.Add(COLUMN_WARNINGSETINFO_NAME); 77 dtSource.Columns.Add(COLUMN_WARNINGSETINFO_REMARK); 78 dtSource.Columns.Add(COLUMN_WARNINGSETINFO_LEVEL1); 79 dtSource.Columns.Add(COLUMN_WARNINGSETINFO_SHIELD1); 80 dtSource.Columns.Add(COLUMN_WARNINGSETINFO_LEVEL2); 81 dtSource.Columns.Add(COLUMN_WARNINGSETINFO_SHIELD2); 82 dtSource.Columns.Add(COLUMN_WARNINGSETINFO_LEVEL3); 83 dtSource.Columns.Add(COLUMN_WARNINGSETINFO_SHIELD3); 84 85 //初始化处理类 86 Presenter = new WarningSetPresenter(this); 87 Presenter.RefreshEvent += new SMOS.Presention.Presenter.WarningSetPresenter.RefreshEventHandle(RefreshView); 88 Presenter.GetDeviceInfos(eDeviceType); 89 90 //ComBoBoxBanding,分开绑定解决联动问题 91 Bind(cmbSetLevel1); 92 Bind(cmbSetLevel2); 93 Bind(cmbSetLevel3); 94 95 //单元格选中改变事件绑定 96 dgvWarningSetInfo.datagridView.CurrentCellChanged += new EventHandler(dgvWarningSetInfo_CurrentCellChanged); 97 } 98 /// <summary> 99 /// 将comboBox与字典表绑定 100 /// </summary> 101 /// <param name="cmb"></param> 102 public void Bind(ComboBox cmb) 103 { 104 IList<WarningDict> lstWarnDict = Presenter.QueryWarningDict(); 105 DataTable dtWarnLogic = new DataTable(); 106 dtWarnLogic.Columns.Add("Value"); 107 dtWarnLogic.Columns.Add("DisplayValue"); 108 DataRow dr = null; 109 110 dr = dtWarnLogic.NewRow(); 111 dr["Value"] = -1; 112 dr["DisplayValue"] = string.Empty; 113 dtWarnLogic.Rows.Add(dr); 114 for (int i = 0; i < lstWarnDict.Count; i++) 115 { 116 if (lstWarnDict[i].LevelType == true) 117 { 118 dr = dtWarnLogic.NewRow(); 119 dr["Value"] = lstWarnDict[i].ID; 120 dr["DisplayValue"] = lstWarnDict[i].LevelName; 121 dtWarnLogic.Rows.Add(dr); 122 } 123 else 124 { 125 levelList.Add(lstWarnDict[i].LevelValue); 126 } 127 } 128 if (dtWarnLogic != null) 129 { 130 cmb.DisplayMember = "DisplayValue"; 131 cmb.ValueMember = "Value"; 132 cmb.DataSource = dtWarnLogic; 133 } 134 } 135 136 /// <summary> 137 /// UI刷新显示的事件响应 138 /// </summary> 139 /// <param name="sender"></param> 140 /// <param name="e"></param> 141 private void RefreshView(object sender, EventArgs e) 142 { 143 this.lstDevice = sender as List<DeviceInfo>; 144 if (this.lstDevice == null || this.lstDevice.Count <= 0) 145 return; 146 147 dgvWarningSetInfo.datagridView.Rows.Clear(); 148 ClearAllText(); 149 dtSource.Rows.Clear(); 150 //填充数据源(设备名称,设备备注信息) 151 foreach (DeviceInfo item in this.lstDevice) 152 { 153 if (item == null) 154 continue; 155 DataRow dr = dtSource.NewRow(); 156 string SetMessage = "未设置"; 157 int index = dgvWarningSetInfo.datagridView.Rows.Add(); 158 dgvWarningSetInfo.datagridView.Rows[index].Cells[COLUMN_WARNINGSETINFO_NAME].Value = item.DeviceName; 159 dgvWarningSetInfo.datagridView.Rows[index].Cells[COLUMN_WARNINGSETINFO_REMARK].Value = item.Remark; 160 if (item.WarningSetInfo == null || item.WarningSetInfo.Count<=0) 161 { 162 dgvWarningSetInfo.datagridView.Rows[index].Cells[COLUMN_WARNINGSETINFO_LEVEL1].Value = SetMessage; 163 dgvWarningSetInfo.datagridView.Rows[index].Cells[COLUMN_WARNINGSETINFO_LEVEL2].Value = SetMessage; 164 dgvWarningSetInfo.datagridView.Rows[index].Cells[COLUMN_WARNINGSETINFO_LEVEL3].Value = SetMessage; 165 } 166 else 167 { 168 dgvWarningSetInfo.datagridView.Rows[index].Cells[COLUMN_WARNINGSETINFO_LEVEL1].Value = item.WarningSetInfo[0].WarningValue; 169 dgvWarningSetInfo.datagridView.Rows[index].Cells[COLUMN_WARNINGSETINFO_LEVEL1].Tag = item.WarningSetInfo[0].LogicID; 170 dgvWarningSetInfo.datagridView.Rows[index].Cells[COLUMN_WARNINGSETINFO_SHIELD1].Value = item.WarningSetInfo[0].Shielding; 171 dgvWarningSetInfo.datagridView.Rows[index].Cells[COLUMN_WARNINGSETINFO_LEVEL2].Value = item.WarningSetInfo[1].WarningValue; 172 dgvWarningSetInfo.datagridView.Rows[index].Cells[COLUMN_WARNINGSETINFO_LEVEL2].Tag = item.WarningSetInfo[1].LogicID; 173 dgvWarningSetInfo.datagridView.Rows[index].Cells[COLUMN_WARNINGSETINFO_SHIELD2].Value = item.WarningSetInfo[1].Shielding; 174 dgvWarningSetInfo.datagridView.Rows[index].Cells[COLUMN_WARNINGSETINFO_LEVEL3].Value = item.WarningSetInfo[2].WarningValue; 175 dgvWarningSetInfo.datagridView.Rows[index].Cells[COLUMN_WARNINGSETINFO_LEVEL3].Tag = item.WarningSetInfo[2].LogicID; 176 dgvWarningSetInfo.datagridView.Rows[index].Cells[COLUMN_WARNINGSETINFO_SHIELD3].Value = item.WarningSetInfo[2].Shielding; 177 178 } 179 dgvWarningSetInfo.datagridView.Rows[index].Tag = item.DeviceID; 180 } 181 182 if (dgvWarningSetInfo.datagridView.RowCount > 0) 183 dgvWarningSetInfo.datagridView.Rows[0].Selected = true; 184 } 185 /// <summary> 186 /// 设置(修改)预警值信息 187 /// </summary> 188 /// <param name="sender"></param> 189 /// <param name="e"></param> 190 private void btnSet_Click(object sender, EventArgs e) 191 { 192 if (!VerifyInfo()) 193 return; 194 //修改操作,给用户提示,防止误操作 195 if (MessageBox.Show(string.Format("确定修改 {0}的预警设置吗?", dgvWarningSetInfo.datagridView.CurrentRow.Cells[COLUMN_WARNINGSETINFO_NAME].Value.ToString()), 196 "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.No) 197 return; 198 WarningSetInfo[] waterLevelWarningSetInfo = GetCurrentInfo(); 199 Presenter.AddWarningSetInfo(waterLevelWarningSetInfo); 200 } 201 202 /// <summary> 203 /// 删除预警值信息 204 /// </summary> 205 /// <param name="sender"></param> 206 /// <param name="e"></param> 207 private void btnDel_Click(object sender, EventArgs e) 208 { 209 if (dgvWarningSetInfo.datagridView.RowCount <= 0 || dgvWarningSetInfo.datagridView.CurrentRow == null) 210 return; 211 212 //删除操作,给用户提示,防止误操作 213 if (MessageBox.Show(string.Format("确定删除 {0}的预警设置吗?", dgvWarningSetInfo.datagridView.CurrentRow.Cells[COLUMN_WARNINGSETINFO_NAME].Value.ToString()), 214 "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.No) 215 return; 216 217 int deviceID = Convert.ToInt32(dgvWarningSetInfo.datagridView.CurrentRow.Tag); 218 Presenter.DelwarningSetInfo(deviceID); 219 Presenter.RefreshView(); 220 } 221 222 /// <summary> 223 /// 根据UI信息获取设备信息实例 224 /// </summary> 225 /// <param name="oldInfo"></param> 226 /// <returns></returns> 227 private WarningSetInfo[] GetCurrentInfo() 228 { 229 try 230 { 231 int deviceID = Convert.ToInt32(dgvWarningSetInfo.datagridView.CurrentRow.Tag); 232 WarningSetInfo[] info = new WarningSetInfo[] 233 { 234 new WarningSetInfo() 235 { 236 DeviceID = deviceID, 237 Enable = true, 238 LevelID = Convert.ToInt32(levelList[0]), 239 LogicID = Convert.ToInt32(cmbSetLevel1.SelectedValue), 240 WarningValue =Convert.ToDecimal(txtLevel1.Text.Trim()), 241 Shielding = cbLevel1.Checked, 242 CreateBy = Global.GlobalInfo.loginInfo.LoginAccount, 243 CreateTime = System.DateTime.Now, 244 }, 245 new WarningSetInfo() 246 { 247 DeviceID = deviceID, 248 Enable = true, 249 LevelID = Convert.ToInt32(levelList[1]), 250 LogicID =Convert.ToInt32(cmbSetLevel2.SelectedValue), 251 WarningValue = Convert.ToDecimal(txtLevel2.Text.Trim()), 252 Shielding = cbLevel2.Checked, 253 CreateBy = Global.GlobalInfo.loginInfo.LoginAccount, 254 }, 255 new WarningSetInfo() 256 { 257 DeviceID = deviceID, 258 Enable = true, 259 LevelID = Convert.ToInt32(levelList[2]), 260 LogicID = Convert.ToInt32(cmbSetLevel3.SelectedValue), 261 WarningValue = Convert.ToDecimal(txtLevel3.Text.Trim()), 262 Shielding = cbLevel3.Checked, 263 CreateBy = Global.GlobalInfo.loginInfo.LoginAccount, 264 }, 265 }; 266 return info; 267 } 268 catch (Exception ex) 269 { 270 271 MessageBox.Show(string.Format("错误提示:{0}", ex.Message)); 272 return null; 273 } 274 } 275 276 /// <summary> 277 /// 清除UI中的设备信息及提示信息 278 /// </summary> 279 private void ClearAllText() 280 { 281 txtLevel1.Text = string.Empty; 282 txtLevel2.Text = string.Empty; 283 txtLevel3.Text = string.Empty; 284 cmbSetLevel1.Text = string.Empty; 285 cmbSetLevel2.Text = string.Empty; 286 cmbSetLevel3.Text = string.Empty; 287 cbLevel1.Checked = false ; 288 cbLevel2.Checked = false; 289 cbLevel3.Checked = false; 290 lblSetLevel1.Text = string.Empty; 291 lblSetLevel2.Text = string.Empty; 292 lblSetLevel3.Text = string.Empty; 293 } 294 295 /// <summary> 296 /// 验证UI信息的完整性 297 /// </summary> 298 /// <returns></returns> 299 private bool VerifyInfo() 300 { 301 if (string.IsNullOrEmpty(txtLevel1.Text.Trim()) || string.IsNullOrEmpty(cmbSetLevel1.Text.Trim())) 302 { 303 lblSetLevel1.Text = "一级预警设置不能为空!"; 304 return false; 305 } 306 else 307 { 308 lblSetLevel1.Text = string.Empty; 309 } 310 311 if (string.IsNullOrEmpty(txtLevel2.Text.Trim()) || string.IsNullOrEmpty(cmbSetLevel2.Text.Trim())) 312 { 313 lblSetLevel2.Text = "二级预警设置不能为空!"; 314 return false; 315 } 316 else 317 { 318 lblSetLevel2.Text = string.Empty; 319 } 320 321 if (string.IsNullOrEmpty(txtLevel3.Text.Trim()) || string.IsNullOrEmpty(cmbSetLevel3.Text.Trim())) 322 { 323 lblSetLevel3.Text = "三级预警设置不能为空!"; 324 return false; 325 } 326 else 327 { 328 lblSetLevel3.Text = string.Empty; 329 } 330 331 return true; 332 } 333 334 /// <summary> 335 /// 列表选中单元格改变事件 336 /// </summary> 337 /// <param name="sender"></param> 338 /// <param name="e"></param> 339 private void dgvWarningSetInfo_CurrentCellChanged(object sender, EventArgs e) 340 { 341 if (dgvWarningSetInfo.datagridView.SelectedRows.Count <= 0 342 || dgvWarningSetInfo.datagridView.CurrentCell == null 343 || dgvWarningSetInfo.datagridView.CurrentCell.RowIndex < 0) 344 return; 345 346 DataGridViewRow dr = dgvWarningSetInfo.datagridView.Rows[dgvWarningSetInfo.datagridView.CurrentCell.RowIndex]; 347 if (dr.Cells[COLUMN_WARNINGSETINFO_LEVEL1].Tag != null) 348 { 349 txtLevel1.Text = dr.Cells[COLUMN_WARNINGSETINFO_LEVEL1].Value.ToString(); 350 cbLevel1.Checked = Convert.ToBoolean(dr.Cells[COLUMN_WARNINGSETINFO_SHIELD1].Value); 351 352 foreach (DataRowView drvLogic in cmbSetLevel1.Items)//获取当前选中行logicID对应的预警逻辑DisplayValue 353 { 354 if (drvLogic.Row["Value"].ToString() == dr.Cells[COLUMN_WARNINGSETINFO_LEVEL1].Tag.ToString()) 355 { 356 cmbSetLevel1.Text = drvLogic.Row["DisplayValue"].ToString(); 357 } 358 } 359 } 360 else 361 { 362 ClearAllText(); 363 } 364 if (dr.Cells[COLUMN_WARNINGSETINFO_LEVEL1].Tag != null) 365 { 366 txtLevel2.Text = dr.Cells[COLUMN_WARNINGSETINFO_LEVEL2].Value.ToString(); 367 cbLevel2.Checked = Convert.ToBoolean(dr.Cells[COLUMN_WARNINGSETINFO_SHIELD2].Value); 368 foreach (DataRowView drvLogic in cmbSetLevel2.Items) //获取当前选中行logicID对应的预警逻辑DisplayValue 369 { 370 if (drvLogic.Row["Value"].ToString() == dr.Cells[COLUMN_WARNINGSETINFO_LEVEL2].Tag.ToString()) 371 { 372 cmbSetLevel2.Text = drvLogic.Row["DisplayValue"].ToString(); 373 } 374 } 375 } 376 else 377 { 378 ClearAllText(); 379 } 380 if (dr.Cells[COLUMN_WARNINGSETINFO_LEVEL1].Tag != null) 381 { 382 txtLevel3.Text = dr.Cells[COLUMN_WARNINGSETINFO_LEVEL3].Value.ToString(); 383 cbLevel3.Checked = Convert.ToBoolean(dr.Cells[COLUMN_WARNINGSETINFO_SHIELD3].Value); 384 foreach (DataRowView drvLogic in cmbSetLevel3.Items) 385 { 386 if (drvLogic.Row["Value"].ToString() == dr.Cells[COLUMN_WARNINGSETINFO_LEVEL3].Tag.ToString()) 387 { 388 cmbSetLevel3.Text = drvLogic.Row["DisplayValue"].ToString(); 389 } 390 } 391 } 392 else 393 { 394 ClearAllText(); 395 } 396 } 397 } 398 }