public static int SeekUnitAlarmsQty(DataTable dtzone, int qty, int zoneId) { System.Configuration.AppSettingsReader appSettings = new System.Configuration.AppSettingsReader(); string UNIT_ID = (string)appSettings.GetValue("GroupsChilds.UnitId", typeof(string)); string swhere = ""; CmpGroupsChildsGisDB cGroupChildsDb = new CmpGroupsChildsGisDB(); CmpGroupsDB cGroupsDb = new CmpGroupsDB(); foreach (DataRow dr in dtzone.Rows) { // If there is not a UNIT we follow deeping in the tree...(Level 2) if (dr["CGRPG_TYPE"].ToString() != UNIT_ID) { swhere = "CGRPG_ID = "+ dr["CGRPG_CHILD"].ToString(); DataTable dtsubzone = cGroupChildsDb.GetData(null, swhere, null, null); qty = SeekUnitAlarmsQty(dtsubzone, qty, zoneId); } // if theres a unit let's get if he has an active alarm (or more) else { swhere = "ALA_UNI_ID = "+ dr["CGRPG_CHILD"].ToString(); DataTable dtAlarms = new CmpAlarmsDB().GetData(null, swhere, null, null); if (dtAlarms.Rows.Count > 0) { qty++; } } } return(qty); }
/// <summary> /// Read all the alarms of the current device from the DataBas /// and stores them in _alarms array /// </summary> internal void ReadAlarms() { CmpAlarmsDB cmp = new CmpAlarmsDB(); DataTable dt = cmp.GetData( new string[] { "ALA_DALA_ID, TO_CHAR(ALA_INIDATE,'hh24missddmmyy') ALA_INIDATE" }, "[email protected]_UNI_ID@", "ALA_DALA_ID ASC", new object[] { UnitId }); _alarms = new uint[(int)Math.Ceiling((double)_numAlarms / ALARMS_PER_BLOCK)]; _alarmInDatabase = new bool[_numAlarms]; _actions = new AlarmAction[_numAlarms]; for (int i = 0; i < _numAlarms; i++) { _actions[i] = AlarmAction.NOTHING; } foreach (DataRow dr in dt.Rows) { int bit = Convert.ToInt32(dr["ALA_DALA_ID"]); DateTime dtALA_INIDATE = OPS.Comm.Dtx.StringToDtx(dr["ALA_INIDATE"].ToString()); // Activates the current bit. int idx = (int)Math.Floor((double)bit / ALARMS_PER_BLOCK); _alarms[idx] |= ((uint)(1 << (bit % ALARMS_PER_BLOCK))); _alarmInDatabase[bit] = true; // mark that the current alarm was found in the database if (dtALA_INIDATE > Date) { Date = dtALA_INIDATE; } } }
/// <summary> /// Updates the alarms of the unit specified /// </summary> /// <param name="uniid">ID of the unit</param> /// <param name="alarms">new array of uints with new status</param> public bool UpdateAlarms(int uniid, uint[] alarms, DateTime date) { ArrayList alarmsAdd = new ArrayList(); ArrayList alarmsDelete = new ArrayList(); AlarmDeviceInfo ai = (AlarmDeviceInfo)_data[uniid]; if (ai == null) { ai = new AlarmDeviceInfo(uniid); ai.ReadAlarms(); _data.Add(uniid, ai); } bool bRet = false; if (ai.Date < date) { ai.UpdateAlarms(alarms, date); int [] alarmsToAct = new int[ai.AlarmsCount]; // Alarms are updated in memory, now we have to update the database for the current alarm. // We have to: // "Delete" ALL alarms with status DELETE // Insert ALL alarms with status INSERT bool bChange = false; for (int i = 0; i < ai.AlarmsCount; i++) { AlarmDeviceInfo.AlarmAction action = ai[i]; if (action == AlarmDeviceInfo.AlarmAction.INSERT) { alarmsAdd.Add(i); bChange = true; } else if (action == AlarmDeviceInfo.AlarmAction.DELETE) { alarmsDelete.Add(i); bChange = true; } } if (bChange) { CmpAlarmsDB cmp = new CmpAlarmsDB(); bRet = cmp.ProcessAlarms(alarmsAdd, alarmsDelete, uniid, ai.Date); if (bRet) { ai.ReadAlarms(); // Update the status, assuming that the update of the BD was correct } } else { bRet = true; } } else { //Disordered alarm from unit bRet = false; } return(bRet); }
/// <summary> /// Processes all alarms received by a device /// </summary> /// <param name="masksAlarms">Masks of alarms</param> public void ProcessAlarmsReceived(uint[] masksAlarms) { const int ALARMS_PER_MAX = 32; // ALARMS_PER_MAX = sizeof(uint) = 32 Alarm[] activeAlarms = GetActiveAlarms(); CmpAlarmsDB adb = new CmpAlarmsDB(); bool [] bAlarmsToActivate; bAlarmsToActivate = new bool[masksAlarms.Length * ALARMS_PER_MAX]; // Each mask is 32 alarms // Iterates over all masks and fills an array of bools storing the status of each alarm. for (int indexMask = 0; indexMask < ALARMS_PER_MAX; indexMask++) // each mask is 32 alarms... (bits) { uint mask = masksAlarms[indexMask]; for (int bit = 0; bit < ALARMS_PER_MAX; bit++) { // Check if bit-essim bit is activated ==> alarm ON bAlarmsToActivate [bit + (indexMask * ALARMS_PER_MAX)] = ((mask & (uint)(1 << bit)) > 0); } } // Now we have to store in the DB everyalarm set to true and delete everyalarm set to false. }
private Alarm[] GetActiveAlarms() { CmpAlarmsDB adb = new CmpAlarmsDB(); DataTable dt = adb.GetData(new string[] { "ALA_ID", "ALA_DALA_ID", "ALA_INIDATE" }, "ALA_UNI_ID = @ALARMS.ALA_UNI_ID@ AND ALA_ENDDATE IS NULL", "ALA_DALA_ID ASC", new object[] { _device }); if (dt.Rows.Count == 0) { return(null); } Alarm[] alarms = new Alarm[dt.Rows.Count]; int i = 0; foreach (DataRow dr in dt.Rows) { alarms[i] = new Alarm(_device, Convert.ToInt32(dr["ALA_ID"])); alarms[i]._end = DateTime.MaxValue; alarms[i]._start = Convert.ToDateTime(dr["ALA_INIDATE"]); alarms[i]._type = Convert.ToInt32(dr["ALA_DALA_ID"]); } return(alarms); }
/// <summary> /// Gets All Alarms for zones /// </summary> /// <returns>DataTable with info about alarms and Phisical zones</returns> /// public static DataTable GetAlarms() { // Components Used CmpAlarmsDB cAlarmsDb = new CmpAlarmsDB(); CmpGroupsChildsGisDB cGroupChildsDb = new CmpGroupsChildsGisDB(); CmpGroupsDB cGroupsDb = new CmpGroupsDB(); // STEP 1: Get the alarms DataTable dtalarms = cAlarmsDb.GetData(); // STEP 2: Recursive search of the unit to find his zone. DataTable dtReturn = new DataTable(); dtReturn.Columns.Add("UNI_ID"); dtReturn.Columns.Add("ZONE_ID"); object[] ovalues = new object[2]; foreach (DataRow dr in dtalarms.Rows) { // Use a recursive function to reach zone. We pass the unit (or group) and the functions return its sector string id = dr["ALA_UNI_ID"].ToString(); string sZone = ""; sZone = RecursiveSearchParent(id); ovalues[0] = new object(); ovalues[1] = new object(); if (String.Compare(sZone, "-1") != 0) { ovalues[0] = dr["ALA_UNI_ID"]; ovalues[1] = sZone; dtReturn.Rows.Add(ovalues); } } return(dtReturn); }