Exemplo n.º 1
0
 //Konstruktor
 public Alarm(int cisloAlarmu, string hlaska, KategorieAlarmu kategorie)
 {
     casVzniku        = DateTime.Now;
     this.cisloAlarmu = cisloAlarmu;
     Hlaska           = hlaska;
     Kategorie        = kategorie;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Načtení textů alarmů ze zadaného xml souboru. Nutno ošetžit případnou vyjímku
        /// </summary>
        /// <param name="xmlFile"></param>
        public void NacistAlarmyZXlm(string xmlFile)
        {
            string    strRootElement   = "Data";
            string    strNazevAtributu = "Text";
            string    strElementName   = "Alarm";
            XDocument xmlXDoc          = XDocument.Load(xmlFile);
            Dictionary <int, KategorieAlarmu> vsechnyKategorie = NacistKategorieZXlm(xmlXDoc, strRootElement);
            var  query = from c in xmlXDoc.Elements(strRootElement).Descendants("Alarmy").Descendants() select c; //xmlXDoc.Elements(StrRootElement)
            int  i     = 1;
            bool chyba = false;

            seznamAlarmu = new Alarm[query.Count()];

            try
            {
                foreach (XElement alarmElement in query)
                {
                    Alarm alarm;

                    int?cisloKatogorie = (int?)alarmElement.Attribute("Kategorie");
                    if (cisloKatogorie == null)
                    {
                        cisloKatogorie = 1;
                    }
                    KategorieAlarmu kategorie = defaultniKategorie;
                    kategorie.Cislo = cisloKatogorie.Value;
                    if (vsechnyKategorie.ContainsKey(kategorie.Cislo))
                    {
                        kategorie = vsechnyKategorie[kategorie.Cislo];
                    }

                    if (alarmElement.Name == (strElementName + i.ToString()))
                    {
                        alarm = new Alarm(i, alarmElement.Attribute(strNazevAtributu).Value, kategorie);
                        seznamAlarmu[i - 1] = alarm;
                    }
                    else
                    {
                        chyba = true;
                    }
                    i++;
                }
                if (chyba)
                {
                    throw new Exception("Chybný název elementu alarmu nebo špatné pořadí.");
                }
            }
            catch (Exception ex)
            {
                seznamAlarmu = new Alarm[0];  //Zrušení celého načtení alarmů
                throw (ex);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Načte kategorie alarmů z XML dokumentu do kolekce, nebo vyhodí vyjímku.
        /// </summary>
        /// <param name="xmlXDoc"></param>
        /// <returns></returns>
        private Dictionary <int, KategorieAlarmu> NacistKategorieZXlm(XDocument xmlXDoc, string strRootElement)
        {
            Dictionary <int, KategorieAlarmu> vsechnyKategorie = new Dictionary <int, KategorieAlarmu>();

            try
            {
                var query = from c in xmlXDoc.Elements(strRootElement).Elements("KategorieAlarmu") select c;

                foreach (XElement book in query)
                {
                    KategorieAlarmu kat = new KategorieAlarmu();

                    int?   cislo        = (int?)book.Attribute("Kategorie");
                    string nazev        = (string)book.Attribute("Nazev");
                    string strBarva     = (string)book.Attribute("Barva");
                    int?   sortPriority = (int?)book.Attribute("SortPriority");

                    if (cislo == null)
                    {
                        throw new Exception("U \"KategorieAlarmu\" se nepodařilo načíst attribut \"Kategorie\"");
                    }
                    if (nazev == null)
                    {
                        throw new Exception(String.Format("U \"KategorieAlarmu\" Kategorie={0} se nepodařilo načíst attribut \"Nazev\"", cislo.Value));
                    }
                    if (strBarva == null)
                    {
                        throw new Exception(String.Format("U \"KategorieAlarmu\" Kategorie={0} se nepodařilo načíst attribut \"Barva\"", cislo.Value));
                    }
                    if (sortPriority == null)
                    {
                        throw new Exception(String.Format("U \"KategorieAlarmu\" Kategorie={0} se nepodařilo načíst attribut \"SortPriority\"", cislo.Value));
                    }

                    kat.Cislo        = cislo.Value;
                    kat.Nazev        = nazev;
                    kat.SortPriority = sortPriority.Value;
                    try //parsování barvy
                    {
                        Color col = (Color)ColorConverter.ConvertFromString(strBarva);
                        kat.Barva = new SolidColorBrush(col);
                    }
                    catch
                    {
                        throw new Exception(String.Format("U \"KategorieAlarmu\" Kategorie={0} je špatný formát attributu \"Barva\"", cislo.Value));
                    }

                    try //Vložení kategorie do hash tabulky
                    {
                        vsechnyKategorie.Add(kat.Cislo, kat);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(String.Format("U \"KategorieAlarmu\" Kategorie={0} je špatné číslo kategorie ({1}).", cislo.Value, ex.Message));
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Chyba při načítání kategorií alarmů: " + ex.Message);
            }

            return(vsechnyKategorie);
        }