/// <summary> /// Retrieves all alarms associated with the monitor with ID as parameter /// </summary> /// <param name="monitorID"></param> /// <returns>List of alarms for monitor with ID as parameter</returns> public static List <Alarm> GetAllForMonitor(int monitorID) { List <Alarm> alarms = new List <Alarm>(); DataSet alarmDataSet = DatabaseConnection.Instance.GetDataSet(selectStatement); DataTable alarmDataTable = alarmDataSet.Tables[0]; foreach (DataRow alarmRow in alarmDataTable.Rows) { MonitorModule monitorModule = MonitorModule.GetOne(Int32.Parse(alarmRow["monitorModuleID"].ToString())); if (monitorModule.Monitor.Id == monitorID) { Alarm alarm = new Alarm(); alarm.Id = Int32.Parse(alarmRow["alarmId"].ToString()); alarm.MonitorModule = monitorModule; alarm.MonitorModule.Id = Int32.Parse(alarmRow["monitorModuleID"].ToString()); alarm.StartDateTime = DateTime.Parse(alarmRow["startDateTime"].ToString()); if (alarmRow["endDateTime"] != DBNull.Value) { alarm.EndDateTime = DateTime.Parse(alarmRow["endDateTime"].ToString()); } if (alarmRow["reading"] != DBNull.Value) { alarm.Reading = float.Parse(alarmRow["reading"].ToString()); } alarms.Add(alarm); } } return(alarms); }
/// <summary> /// Retrieves from database all monitorModule records associated with monitor having ID as parameter /// </summary> /// <param name="monitorID"></param> /// <returns>List of monitorModules associated with Monitor with ID as parameter</returns> public static List <MonitorModule> GetAllFromMonitor(Nullable <int> monitorID) { List <MonitorModule> monitorModulesList = new List <MonitorModule>(); if (monitorID.HasValue) { DataSet monitorModuleDataSet = DatabaseConnection.Instance.GetDataSet(selectStatement); DataTable monitorModuleDataTable = monitorModuleDataSet.Tables[0]; foreach (DataRow monitorModuleRow in monitorModuleDataTable.Rows) { if (Int32.Parse(monitorModuleRow["monitorID"].ToString()) == monitorID) { Monitor monitor = new Monitor(Int32.Parse(monitorModuleRow["monitorID"].ToString())); Module module = new Module(Int32.Parse(monitorModuleRow["moduleID"].ToString())); MonitorModule monitorModule = new MonitorModule(monitor, module); monitorModule.Id = Int32.Parse(monitorModuleRow["monitorModuleID"].ToString()); monitorModule.AssignedMin = float.Parse(monitorModuleRow["assignedMin"].ToString()); monitorModule.AssignedMax = float.Parse(monitorModuleRow["assignedMax"].ToString()); monitorModule.Deleted = Convert.ToBoolean(monitorModuleRow["deleted"].ToString()); monitorModulesList.Add(monitorModule); } } } return(monitorModulesList); }
/// <summary> /// Retrieve one MonitorModule from the database based on its ID /// </summary> /// <param name="id"></param> /// <returns>MonitorModule</returns> public static MonitorModule GetOne(int id) { MonitorModule monitorModule = new MonitorModule(); SqlDataAdapter sqlDataAdapter = DatabaseConnection.Instance.GetSqlAdapter(selectWhereStatement); sqlDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@monitorModuleID", id)); DataSet monitorModuleDataSet = DatabaseConnection.Instance.ExecuteSelect(sqlDataAdapter); DataTable monitorModuleDataTable = monitorModuleDataSet.Tables[0]; if (monitorModuleDataTable.Rows.Count == 1) { DataRow row = monitorModuleDataTable.Rows[0]; Monitor monitor = new Monitor(Int32.Parse(row["monitorID"].ToString())); Module module = new Module(Int32.Parse(row["moduleID"].ToString())); monitorModule = new MonitorModule(monitor, module) { Id = Int32.Parse(row["monitorID"].ToString()), AssignedMin = float.Parse(row["assignedMin"].ToString()), AssignedMax = float.Parse(row["assignedMax"].ToString()) }; } return(monitorModule); }
/// <summary> /// Stops alarm if triggered by module that is being unplugged /// </summary> /// <param name="monitorModuleToDelete">Module being unplugged</param> private void StopAlarmForUnpluggedModule(MonitorModule monitorModuleToDelete) { foreach (Alarm alarm in MonitorForm.alarms) { if (alarm.MonitorModule.Id == monitorModuleToDelete.Id) { alarm.Stop(); } } }
/// <summary> /// Retrieves monitorModules for specific monitor /// and populates NumericUpDown controls with assigned limits /// </summary> private void GetMonitorModules() { monitorModules = MonitorModule.GetAllFromMonitor(MonitorForm.MonitorId); int index = 0; foreach (MonitorModule monitorModule in monitorModules) { modules.ElementAt(index).Text = monitorModules.ElementAt(index).Module.Name; moduleIcon.ElementAt(index).ImageLocation = @"../../Resources/" + monitorModules.ElementAt(index).Module.Icon; limitMin.ElementAt(index).Value = Convert.ToDecimal(monitorModules.ElementAt(index).AssignedMin); limitMax.ElementAt(index).Value = Convert.ToDecimal(monitorModules.ElementAt(index).AssignedMax); ++index; } }
/// <summary> /// Retrieves monitorModules from database and sets min and max limit controls values /// </summary> public void GetMonitorModules() { monitorModules = MonitorModule.GetAllFromMonitor(MonitorId); int index = 0; foreach (MonitorModule monitorModule in monitorModules) { moduleName.ElementAt(index).Text = monitorModules.ElementAt(index).Module.Name; modulesIcon.ElementAt(index).ImageLocation = @"../../Resources/" + monitorModules.ElementAt(index).Module.Icon; limitMin.ElementAt(index).Text = Convert.ToString(monitorModules.ElementAt(index).AssignedMin); limitMax.ElementAt(index).Text = Convert.ToString(monitorModules.ElementAt(index).AssignedMax); ++index; } }
/// <summary> /// Automatically select modules in checkedListBox if already attached to monitor /// </summary> private void SelectExistingModules() { List <MonitorModule> monitorModules = MonitorModule.GetAllFromMonitor(MonitorForm.MonitorId); for (int i = 0; i < checkedListBox_Modules.Items.Count; ++i) { DataRowView module = checkedListBox_Modules.Items[i] as DataRowView; int moduleId = Int32.Parse(module["ModuleID"].ToString()); foreach (MonitorModule monitorModule in monitorModules) { if (moduleId == monitorModule.Module.Id) { checkedListBox_Modules.SetItemChecked(i, true); } } } }
/// <summary> /// Store selected modules in table "monitorModule" /// </summary> private void SaveSelectedModules() { List <int> monitorModulesToSkip = new List <int>(); // retrieve list of monitorModules for current monitor List <MonitorModule> monitorModules = MonitorModule.GetAllFromMonitor(MonitorForm.MonitorId); //List to store all monitorModules that need to be saved List <MonitorModule> modulesToSave = new List <MonitorModule>(); //List to store all monitorModules that need to be deleted List <MonitorModule> modulesToDelete = new List <MonitorModule>(); // loop over every checkbox for (int i = 0; i < checkedListBox_Modules.Items.Count; ++i) { DataRowView moduleCheckBox = checkedListBox_Modules.Items[i] as DataRowView; int moduleID = Int32.Parse(moduleCheckBox["ModuleID"].ToString()); // if module is checked if (checkedListBox_Modules.GetItemCheckState(i) == CheckState.Checked) { // and it is not found in the monitorModule table if (!monitorModules.Exists(x => x.Module.Id == moduleID)) { // then we will need to save it //create monitor and module objects Monitor monitor = new Monitor(MonitorForm.MonitorId); Module module = new Module(moduleID); MonitorModule monitorModule = new MonitorModule(monitor, module); monitorModule.AssignedMin = module.DefaultMin; monitorModule.AssignedMax = module.DefaultMax; modulesToSave.Add(monitorModule); } } else { // if the module is unchecked if (checkedListBox_Modules.GetItemCheckState(i) == CheckState.Unchecked) { // we need to check if it was previously checked and saved int indexOfMonitorModuleToDelete = monitorModules.FindIndex(x => x.Module.Id == moduleID); // monitorModule is saved in monitorModule table if (indexOfMonitorModuleToDelete != -1) { // In which case it will need to be deleted modulesToDelete.Add(monitorModules.ElementAt(indexOfMonitorModuleToDelete)); } } } } foreach (MonitorModule monitorModuleToSave in modulesToSave) { monitorModuleToSave.Save(); } foreach (MonitorModule monitorModuleToDelete in modulesToDelete) { monitorModuleToDelete.Delete(); StopAlarmForUnpluggedModule(monitorModuleToDelete); } }
/// <summary> /// Constructor. Sets MonitorModule from parameter and defines DateTimeStart and Reading /// </summary> /// <param name="monitorModule"></param> public Alarm(MonitorModule monitorModule, float reading) { MonitorModule = monitorModule; StartDateTime = DateTime.Now; Reading = reading; }