在线监测的主要功能除了数据的采集、存储和分析之外,对分析结果及系统运行状况进行及时预警也是非常重要且必不可少的;
预警联系人管理 --UI层
WarningContactsManageForm.cs
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; 13 14 namespace SMOS.UI 15 { 16 public partial class WarningContactsManageForm : TabWindow,IWorningContactsView 17 { 18 /// <summary> 19 /// 控制处理类 20 /// </summary> 21 private WarningContactsPresenter presenter; 22 /// <summary> 23 /// DatagridView的数据源 24 /// </summary> 25 private DataTable dtSource; 26 27 28 #region DatagridView的列名值 29 private const string COLUMN_WARNINGCONTACTS_NAME = "WarningContactsName"; 30 private const string COLUMN_WARNINGCONTACTS_POSITION = "WarningContactsPosition"; 31 private const string COLUMN_WARNINGCONTACTS_PHONE = "WarningContactsPhone"; 32 private const string COLUMN_WARNINGCONTACTS_TEL = "WarningContactsTel"; 33 private const string COLUMN_WARNINGCONTACTS_EMAIL = "WarningContactsEmail"; 34 private const string COLUMN_REMARK = "Remark"; 35 #endregion 36 37 #region IWorningContactsView 接口实现 38 public Model.Enums.UserType eUserType 39 { 40 get 41 { 42 return Model.Enums.UserType.WarningContacts; 43 } 44 } 45 46 public IList<Model.User> lstUser 47 { 48 get; 49 set; 50 } 51 52 public Model.Enums.WarnType WarnType 53 { 54 get 55 { 56 return Model.Enums.WarnType.Default; 57 } 58 } 59 #endregion 60 61 62 63 public WarningContactsManageForm() 64 { 65 InitializeComponent(); 66 } 67 68 /// <summary> 69 /// 窗体加载事件 70 /// </summary> 71 /// <param name="sender"></param> 72 /// <param name="e"></param> 73 private void WarningContactsManageForm_Load(object sender, EventArgs e) 74 { 75 //cmbPort.DataBinding(); 76 //初始化数据源 77 dtSource = new DataTable(); 78 dtSource.Columns.Add(COLUMN_WARNINGCONTACTS_NAME); 79 dtSource.Columns.Add(COLUMN_WARNINGCONTACTS_POSITION); 80 dtSource.Columns.Add(COLUMN_WARNINGCONTACTS_PHONE); 81 dtSource.Columns.Add(COLUMN_WARNINGCONTACTS_TEL); 82 dtSource.Columns.Add(COLUMN_WARNINGCONTACTS_EMAIL); 83 dtSource.Columns.Add(COLUMN_REMARK); 84 dgvWarningContacts.AutoGenerateColumns = false; 85 86 //初始化处理类 87 presenter = new WarningContactsPresenter(this); 88 presenter.RefreshEvent += new SMOS.Presention.Presenter.WarningContactsPresenter.RefreshEventHandle(RefreshView); 89 presenter.GetAllUsersInfo(); 90 91 } 92 93 /// <summary> 94 /// UI刷新显示的事件响应 95 /// </summary> 96 /// <param name="sender"></param> 97 /// <param name="e"></param> 98 private void RefreshView(object sender, EventArgs e) 99 { 100 if (this.lstUser == null || this.lstUser.Count <= 0) 101 return; 102 103 ClearAllText(); 104 dgvWarningContacts.Rows.Clear(); 105 dgvWarmlevel1.Rows.Clear(); 106 dgvWarmlevel2.Rows.Clear(); 107 dgvWarmlevel3.Rows.Clear(); 108 109 //填充数据源 110 foreach (Model.User item in this.lstUser) 111 { 112 if (item == null) 113 continue; 114 115 int index = dgvWarningContacts.Rows.Add(); 116 dgvWarningContacts.Rows[index].Cells[COLUMN_WARNINGCONTACTS_NAME].Value = item.Name; 117 dgvWarningContacts.Rows[index].Cells[COLUMN_WARNINGCONTACTS_POSITION].Value = item.Position; 118 dgvWarningContacts.Rows[index].Cells[COLUMN_WARNINGCONTACTS_PHONE].Value = item.Phone; 119 dgvWarningContacts.Rows[index].Cells[COLUMN_WARNINGCONTACTS_TEL].Value = item.Tel; 120 dgvWarningContacts.Rows[index].Cells[COLUMN_WARNINGCONTACTS_EMAIL].Value = item.Email; 121 dgvWarningContacts.Rows[index].Cells[COLUMN_REMARK].Value = item.Remark; 122 dgvWarningContacts.Rows[index].Tag = item; 123 124 //填充一级预警联系人 125 if ((item.WarnType & Convert.ToByte(Enums.WarnType.WarnTypeLevel1))== (Convert.ToByte(Enums.WarnType.WarnTypeLevel1))) 126 { 127 int i = dgvWarmlevel1.Rows.Add(); 128 dgvWarmlevel1.Rows[i].Cells[0].Value = item.Name; 129 dgvWarmlevel1.Rows[i].Cells[1].Value = item.Position; 130 dgvWarmlevel1.Rows[i].Cells[2].Value = item.PhoneWarn; 131 dgvWarmlevel1.Rows[i].Cells[3].Value = item.EmailWarn; 132 dgvWarmlevel1.Rows[i].Tag = item; 133 } 134 //填充二级预警联系人 135 if ((item.WarnType & Convert.ToByte(Enums.WarnType.WarnTypeLevel2)) == (Convert.ToByte(Enums.WarnType.WarnTypeLevel2))) 136 { 137 int i = dgvWarmlevel2.Rows.Add(); 138 dgvWarmlevel2.Rows[i].Cells[0].Value = item.Name; 139 dgvWarmlevel2.Rows[i].Cells[1].Value = item.Position; 140 dgvWarmlevel2.Rows[i].Cells[2].Value = item.PhoneWarn; 141 dgvWarmlevel2.Rows[i].Cells[3].Value = item.EmailWarn; 142 dgvWarmlevel2.Rows[i].Tag = item; 143 } 144 //填充三级预警联系人 145 if ((item.WarnType & Convert.ToByte(Enums.WarnType.WarnTypeLevel3)) == (Convert.ToByte(Enums.WarnType.WarnTypeLevel3))) 146 { 147 int i = dgvWarmlevel3.Rows.Add(); 148 dgvWarmlevel3.Rows[i].Cells[0].Value = item.Name; 149 dgvWarmlevel3.Rows[i].Cells[1].Value = item.Position; 150 dgvWarmlevel3.Rows[i].Cells[2].Value = item.PhoneWarn; 151 dgvWarmlevel3.Rows[i].Cells[3].Value = item.EmailWarn; 152 dgvWarmlevel3.Rows[i].Tag = item; 153 } 154 } 155 if (dgvWarningContacts.RowCount > 0) 156 dgvWarningContacts.Rows[0].Selected = true; 157 } 158 159 /// <summary> 160 /// 添加预警联系人 161 /// </summary> 162 /// <param name="sender"></param> 163 /// <param name="e"></param> 164 private void btnAdd_Click(object sender, EventArgs e) 165 { 166 AddWarningContactsForm addWorningContacts = new AddWarningContactsForm(); 167 if (addWorningContacts.ShowDialog() == DialogResult.OK) 168 { 169 User info = addWorningContacts.Tag as User; 170 if (info == null) 171 return; 172 info = GetCurrentInfo(info); 173 info.WarnType = 0; 174 presenter.AddUser(info); 175 } 176 177 } 178 /// <summary> 179 /// 删除预警联系人 180 /// </summary> 181 /// <param name="sender"></param> 182 /// <param name="e"></param> 183 private void btnDel_Click(object sender, EventArgs e) 184 { 185 if (dgvWarningContacts.RowCount <= 0 || dgvWarningContacts.CurrentRow == null) 186 return; 187 //删除操作,给用户提示,防止误操作 188 if (MessageBox.Show(string.Format("确定删除 {0}吗?", dgvWarningContacts.CurrentRow.Cells[COLUMN_WARNINGCONTACTS_NAME].Value.ToString()), 189 "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.No) 190 return; 191 User user = dgvWarningContacts.CurrentRow.Tag as User; 192 presenter.DelUser(user); 193 194 } 195 /// <summary> 196 /// 修改预警联系人 197 /// </summary> 198 /// <param name="sender"></param> 199 /// <param name="e"></param> 200 private void btnModify_Click(object sender, EventArgs e) 201 { 202 if (dgvWarningContacts.CurrentRow == null || dgvWarningContacts.CurrentRow.Index < 0) 203 return; 204 User user = dgvWarningContacts.CurrentRow.Tag as User; 205 if (user == null) 206 return; 207 ModifyWarningContactsForm modifyWarningContactsForm = new ModifyWarningContactsForm(user); 208 if (modifyWarningContactsForm.ShowDialog() == DialogResult.OK) 209 { 210 User info = modifyWarningContactsForm.Tag as User; 211 if (info == null) 212 return; 213 info = GetCurrentInfo(info); 214 presenter.ModifyUser(info); 215 } 216 } 217 218 /// <summary> 219 /// 设置预警联系人预警级别 220 /// </summary> 221 /// <param name="sender"></param> 222 /// <param name="e"></param> 223 private void btnSet_Click(object sender, EventArgs e) 224 { 225 if (dgvWarningContacts.CurrentRow == null || dgvWarningContacts.CurrentRow.Index < 0) 226 return; 227 User user = dgvWarningContacts.CurrentRow.Tag as User; 228 if (user == null) 229 return; 230 if (tcnWarningLevel.SelectedTab.Text == "设置一级联系人" ) 231 { 232 dgvWarmlevel1.Rows.Add(user.Name,user.Position,user.PhoneWarn,user.EmailWarn); 233 user.WarnType |= Convert.ToByte(Model.Enums.WarnType.WarnTypeLevel1); 234 user = GetCurrentInfo(user); 235 presenter.SetUser(user); 236 } 237 if (tcnWarningLevel.SelectedTab.Text == "设置二级联系人") 238 { 239 dgvWarmlevel2.Rows.Add(user.Name, user.Position, user.PhoneWarn, user.EmailWarn); 240 user.WarnType |= Convert.ToByte(Model.Enums.WarnType.WarnTypeLevel2); 241 user = GetCurrentInfo(user); 242 presenter.SetUser(user); 243 } 244 if (tcnWarningLevel.SelectedTab.Text == "设置三级联系人") 245 { 246 dgvWarmlevel3.Rows.Add(user.Name, user.Position, user.PhoneWarn, user.EmailWarn); 247 user.WarnType |= Convert.ToByte(Model.Enums.WarnType.WarnTypeLevel3); 248 user = GetCurrentInfo(user); 249 presenter.SetUser(user); 250 } 251 } 252 /// <summary> 253 /// 取消预警联系人级别 254 /// </summary> 255 /// <param name="sender"></param> 256 /// <param name="e"></param> 257 private void btnCancelSet_Click(object sender, EventArgs e) 258 { 259 if (tcnWarningLevel.SelectedTab.Text == "设置一级联系人" ) 260 { 261 if (dgvWarmlevel1.CurrentRow == null || dgvWarmlevel1.CurrentRow.Index < 0) 262 return; 263 User user = dgvWarmlevel1.CurrentRow.Tag as User; 264 if (user == null) 265 return; 266 dgvWarmlevel1.Rows.Remove(dgvWarmlevel1.CurrentRow); 267 user.WarnType = Convert.ToByte(user.WarnType & ~Convert.ToByte(Model.Enums.WarnType.WarnTypeLevel1)); 268 user = GetCurrentInfo(user); 269 presenter.SetUser(user); 270 } 271 if (tcnWarningLevel.SelectedTab.Text == "设置二级联系人") 272 { 273 if (dgvWarmlevel2.CurrentRow == null || dgvWarmlevel2.CurrentRow.Index < 0) 274 return; 275 User user = dgvWarmlevel2.CurrentRow.Tag as User; 276 if (user == null) 277 return; 278 dgvWarmlevel2.Rows.Remove(dgvWarmlevel2.CurrentRow); 279 user.WarnType = Convert.ToByte(user.WarnType & ~Convert.ToByte(Model.Enums.WarnType.WarnTypeLevel2)); 280 user = GetCurrentInfo(user); 281 presenter.SetUser(user); 282 } 283 if (tcnWarningLevel.SelectedTab.Text == "设置三级联系人") 284 { 285 if (dgvWarmlevel3.CurrentRow == null || dgvWarmlevel3.CurrentRow.Index < 0) 286 return; 287 User user = dgvWarmlevel3.CurrentRow.Tag as User; 288 if (user == null) 289 return; 290 dgvWarmlevel3.Rows.Remove(dgvWarmlevel3.CurrentRow); 291 user.WarnType = Convert.ToByte(user.WarnType & ~Convert.ToByte(Model.Enums.WarnType.WarnTypeLevel3)); 292 user = GetCurrentInfo(user); 293 presenter.SetUser(user); 294 } 295 } 296 /// <summary> 297 /// 根据UI信息获取设备信息实例 298 /// </summary> 299 /// <param name="oldInfo"></param> 300 /// <returns></returns> 301 private User GetCurrentInfo(User oldInfo) 302 { 303 User info = oldInfo; 304 try 305 { 306 info.pwd = "123"; 307 info.UserAccount = "test预警联系人10"; 308 info.CreateBy = "ZhangXiubin"; 309 info.UserType = Enums.UserType.WarningContacts; 310 info.Enable = true; 311 } 312 catch (Exception ex) 313 { 314 MessageBox.Show(string.Format("错误提示:{0}", ex.Message)); 315 return null; 316 } 317 //为空时,表示为新增,给实例赋值用户ID 318 if (oldInfo != null) 319 { 320 info.UserID = oldInfo.UserID; 321 } 322 return info; 323 } 324 325 /// <summary> 326 /// 发送邮件测试 327 /// </summary> 328 /// <param name="sender"></param> 329 /// <param name="e"></param> 330 private void btnSendEmailTest_Click(object sender, EventArgs e) 331 { 332 333 334 } 335 /// <summary> 336 /// 清除UI中的设备信息及提示信息 337 /// </summary> 338 private void ClearAllText() 339 { 340 341 342 } 343 344 /// <summary> 345 /// 验证UI信息的完整性 346 /// </summary> 347 /// <returns></returns> 348 private bool VerifyInfo() 349 { 350 return true; 351 } 352 } 353 }
其中主要功能包括预警联系人的添加、删除、修改,以及对预警联系人级别的设置;对自己来说难点主要在对同一个预警联系人可以设置多种预警级别,而数据库中又只有一个字段WarnType来保存,所以这个字段就需要定义成Byte类型,并通过位操作来将他们区分。
presention层-----WarningContactsPresenter.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using SMOS.Presention.IView; 6 using SMOS.IServer; 7 using SMOS.Server.Impl; 8 using System.Windows.Forms; 9 using SMOS.Model; 10 using System.Transactions; 11 12 namespace SMOS.Presention.Presenter 13 { 14 /// <summary> 15 /// 预警联系人界面控制处理类 16 /// </summary> 17 public class WarningContactsPresenter 18 { 19 #region 公共访问成员 20 /// <summary> 21 /// UI层刷新显示事件 22 /// </summary> 23 /// <param name="sender"></param> 24 /// <param name="e"></param> 25 public delegate void RefreshEventHandle(object sender, EventArgs e); 26 27 public event RefreshEventHandle RefreshEvent; 28 #endregion 29 30 #region 内部成员变量 31 /// <summary> 32 /// 当前用户类型 33 /// </summary> 34 private SMOS.Model.Enums.UserType eUserType; 35 /// <summary> 36 /// 继承IView接口的UI窗体 37 /// </summary> 38 private IWorningContactsView view; 39 /// <summary> 40 /// 预警联系人配置服务类 41 /// </summary> 42 //private IWarningContactsSetServer WarningContactsSetServer; 43 /// <summary> 44 /// 预警联系人信息服务类 45 /// </summary> 46 private IUserServer warningContactsServer; 47 #endregion 48 49 public WarningContactsPresenter(IWorningContactsView view) 50 { 51 this.view = view; 52 this.eUserType = view.eUserType; 53 this.warningContactsServer = SMOS.Server.Impl.ServerFactory.GetServer<IUserServer>(); 54 } 55 56 public void GetAllUsersInfo() 57 { 58 try 59 { 60 //根据用户类型--预警联系人,查询该类型所有的用户信息 61 this.view.lstUser = warningContactsServer.GetUserInfos(this.eUserType); 62 RefreshEvent(this.view, new EventArgs()); 63 } 64 catch (Exception ex) 65 { 66 //写错误日志 67 SystemLogHelper.AddLog(Model.Enums.SystemLogType.DataViewErr, ex.Message, "DeviceSetPresenter.GetAllDeviceInfo"); 68 MessageBox.Show(string.Format("查询设备信息失败:{0}", ex.Message)); 69 } 70 } 71 72 /// <summary> 73 /// 新增一个预警联系人 74 /// </summary> 75 /// <param name="deviceInfo"></param> 76 public void AddUser(User userInfo) 77 { 78 if (userInfo == null) 79 { 80 MessageBox.Show("预警联系人信息不能为空!"); 81 return; 82 } 83 try 84 { 85 int userID = warningContactsServer.AddWarningContacts(userInfo); 86 if (userID <= 0) 87 { 88 MessageBox.Show("新增预警联系人失败!"); 89 return; 90 } 91 userInfo.UserID = userID; 92 MessageBox.Show("新增预警联系人成功!"); 93 RefreshView(); 94 } 95 catch (Exception ex) 96 { 97 //写错误日志 98 SystemLogHelper.AddLog(Model.Enums.SystemLogType.Default, ex.Message, "WarningContactsPresenter.AddUser"); 99 MessageBox.Show(string.Format("新增预警联系人信息失败:{0}", ex.Message)); 100 } 101 } 102 /// <summary> 103 /// 删除一个预警联系人 104 /// </summary> 105 /// <param name="userInfo"></param> 106 public void DelUser(User userInfo) 107 { 108 if (userInfo == null) 109 { 110 MessageBox.Show("请选择一个预警联系人!"); 111 return; 112 } 113 114 try 115 { 116 //写User表记录 117 int userID = warningContactsServer.DelUser(userInfo); 118 if (userID <= 0) 119 { 120 MessageBox.Show("删除预警联系人失败!"); 121 return; 122 } 123 userInfo.UserID = userID; 124 RefreshView(); 125 } 126 catch (Exception ex) 127 { 128 //写错误日志 129 SystemLogHelper.AddLog(Model.Enums.SystemLogType.Default, ex.Message, "warningContactsServer.DelUser"); 130 MessageBox.Show(string.Format("删除预警联系人信息失败:{0}", ex.Message)); 131 } 132 133 } 134 135 /// <summary> 136 /// 修改一个预警联系人 137 /// </summary> 138 /// <param name="deviceInfo"></param> 139 public void ModifyUser(User userInfo) 140 { 141 if (userInfo == null) 142 { 143 MessageBox.Show("预警联系人信息不能为空!"); 144 return; 145 } 146 try 147 { 148 int userID = warningContactsServer.ModifyUser(userInfo); 149 if (userID <= 0) 150 { 151 MessageBox.Show("修改预警联系人失败!"); 152 return; 153 } 154 userInfo.UserID = userID; 155 MessageBox.Show("修改预警联系人成功!"); 156 RefreshView(); 157 } 158 catch (Exception ex) 159 { 160 //写错误日志 161 SystemLogHelper.AddLog(Model.Enums.SystemLogType.Default, ex.Message, "WarningContactsPresenter.AddUser"); 162 MessageBox.Show(string.Format("新增预警联系人信息失败:{0}", ex.Message)); 163 } 164 } 165 /// <summary> 166 /// 设置一个预警联系人的预警级别 167 /// </summary> 168 /// <param name="deviceInfo"></param> 169 public void SetUser(User userInfo) 170 { 171 if (userInfo == null) 172 { 173 MessageBox.Show("预警联系人信息不能为空!"); 174 return; 175 } 176 try 177 { 178 int userID = warningContactsServer.ModifyUser(userInfo); 179 if (userID <= 0) 180 { 181 MessageBox.Show("设置预警联系人失败!"); 182 return; 183 } 184 userInfo.UserID = userID; 185 MessageBox.Show("设置预警联系人成功!"); 186 RefreshView(); 187 } 188 catch (Exception ex) 189 { 190 //写错误日志 191 SystemLogHelper.AddLog(Model.Enums.SystemLogType.Default, ex.Message, "WarningContactsPresenter.SetUser"); 192 MessageBox.Show(string.Format("设置预警联系人信息失败:{0}", ex.Message)); 193 } 194 } 195 /// <summary> 196 /// 刷新UI显示 197 /// </summary> 198 private void RefreshView() 199 { 200 //重新获取预警联系人列表 201 GetAllUsersInfo(); 202 //触发刷新事件 203 RefreshEvent(this.view, new EventArgs()); 204 } 205 } 206 }