public void CreateXML(string p1, Dictionary <int, List <LocalDeviceClass> > dic, LocalDeviceClass di)
        {
            LocalControlerClass kont = new LocalControlerClass();

            kont.CreateXML(p1, dic, di);
        }
Example #2
0
        public void WriteAMSxml2BadParameters(LocalDeviceClass d1)
        {
            WriteAms w = new WriteAms();

            w.WriteAMSxml2(d1);
        }
        public void CreateXML(string p1, Dictionary <int, List <LocalDeviceClass> > dic, LocalDeviceClass di)
        {
            if (dic == null || di == null || p1 == null)
            {
                throw new NullReferenceException("Argument can't be null!");
            }
            if (dic.Count == 0)
            {
                throw new ArgumentException("Dictionary must have values!");
            }


            int j = 0;


            if (!CheckXMLFile(p1))
            {
                using (XmlWriter writer = XmlWriter.Create(p1))
                {
                    foreach (KeyValuePair <int, List <LocalDeviceClass> > item in dic)
                    {
                        if (item.Key == di.IdControler)
                        {
                            writer.WriteStartDocument();
                            writer.WriteStartElement("Devices");


                            while (j != 5)
                            {
                                foreach (LocalDeviceClass d in item.Value)
                                {
                                    writer.WriteStartElement("Device");


                                    int meas_value = 0;
                                    if (d.DeviceType == "A")
                                    {
                                        meas_value = GetType2();
                                    }
                                    else if (d.DeviceType == "D")
                                    {
                                        meas_value = GetType2() % 2;
                                    }



                                    writer.WriteElementString("Type", d.DeviceType);
                                    writer.WriteElementString("ID", d.LocalDeviceCode.ToString());
                                    writer.WriteElementString("SendTo", d.SendTo);
                                    writer.WriteElementString("ActualValue", d.ActualValue.ToString());
                                    writer.WriteElementString("ActualState", d.ActualState.ToString());
                                    writer.WriteElementString("TimeStamp", DateTime.Now.ToString());
                                    writer.WriteElementString("Measurment", meas_value.ToString());
                                    writer.WriteEndElement();
                                }
                                j++;
                            }
                            writer.WriteEndElement();
                            writer.WriteEndDocument();
                            //writer.Close();
                        }
                    }

                    IzlistajKontrolere(folder);
                }
            }
            else
            {
                foreach (KeyValuePair <int, List <LocalDeviceClass> > item in dic)
                {
                    if (item.Key == di.IdControler)
                    {
                        foreach (LocalDeviceClass d in item.Value)
                        {
                            XDocument xDocument = null;

                            bool savesuccess = false;
                            while (!savesuccess)
                            {
                                try
                                {
                                    xDocument   = XDocument.Load(p1);
                                    savesuccess = true;
                                }
                                catch (Exception e)
                                {
                                    //Console.WriteLine($"{e.Message}\n{e.StackTrace}");
                                    Thread.Sleep(100);
                                }
                            }

                            XElement root = xDocument.Element("Devices");

                            Thread t = new Thread(new ThreadStart(() =>
                            {
                                IEnumerable <XElement> rows = root.Descendants("Device");
                                XElement firstRow           = rows.First();

                                int meas_value = 0;
                                if (d.DeviceType == "A")
                                {
                                    meas_value = GetType2();
                                }
                                else if (d.DeviceType == "D")
                                {
                                    meas_value = GetType2() % 2;
                                }



                                firstRow.AddBeforeSelf(
                                    new XElement("Device",
                                                 new XElement("Type", d.DeviceType.ToString()),
                                                 new XElement("ID", d.LocalDeviceCode.ToString()),
                                                 new XElement("SendTo", d.SendTo.ToString()),
                                                 new XElement("ActualValue", d.ActualValue.ToString()),
                                                 new XElement("ActualState", d.ActualState.ToString()),
                                                 new XElement("TimeStamp", DateTime.Now.ToString()),
                                                 new XElement("Measurment", meas_value.ToString())));


                                /*bool*/
                                savesuccess = false;
                                while (!savesuccess)
                                {
                                    try
                                    {
                                        xDocument.Save(p1);
                                        savesuccess = true;
                                    }
                                    catch (Exception e)
                                    {
                                        //Console.WriteLine($"{e.Message}\n{e.StackTrace}");
                                        Thread.Sleep(100);
                                    }
                                }

                                Thread.Sleep(3000);
                            }));
                            t.IsBackground = true;
                            t.Start();
                        }
                    }
                }
            }
        }
Example #4
0
        public void WriteAMSxml2(LocalDeviceClass device)
        {
            if (device == null)
            {
                throw new ArgumentNullException("Argument can't be null");
            }


            int  j = 0;
            bool u = false;

            if (!File.Exists(@"..\..\..\AMSBaza\AMS.xml"))
            {
                using (XmlWriter writer = XmlWriter.Create(@"..\..\..\AMSBaza\AMS.xml"))
                {
                    u = true;
                    writer.WriteStartDocument();
                    writer.WriteStartElement("Devices");

                    while (j != 3)
                    {
                        writer.WriteStartElement("Device");
                        Enum e = device.ActualValue;


                        writer.WriteElementString("Type", device.DeviceType);
                        writer.WriteElementString("ID", device.LocalDeviceCode.ToString());
                        writer.WriteElementString("SendTo", device.SendTo);
                        writer.WriteElementString("ActualValue", device.ActualValue.ToString());
                        writer.WriteElementString("ActualState", device.ActualState.ToString());
                        writer.WriteElementString("TimeStamp", DateTime.Now.ToString());

                        if (device.DeviceType == "A")
                        {
                            writer.WriteElementString("Measurment", GetType2().ToString());
                        }
                        else if (device.DeviceType == "D")
                        {
                            writer.WriteElementString("Measurment", device.AnalogActualValue.ToString());
                        }
                        writer.WriteEndElement();
                        j++;
                    }

                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.Flush();
                }
            }
            else
            {
                XDocument xDocument         = XDocument.Load(@"..\..\..\AMSBaza\AMS.xml");
                XElement  root              = xDocument.Element("Devices");
                IEnumerable <XElement> rows = root.Descendants("Device");
                XElement firstRow           = rows.First();

                int meas_value = 0;
                if (device.DeviceType == "A")
                {
                    meas_value = GetType2();
                }
                else if (device.DeviceType == "D")
                {
                    meas_value = GetType2() % 2;
                }

                firstRow.AddBeforeSelf(
                    new XElement("Device",
                                 new XElement("Type", device.DeviceType),
                                 new XElement("ID", device.LocalDeviceCode.ToString()),
                                 new XElement("SendTo", device.SendTo),
                                 new XElement("ActualValue", device.ActualValue),
                                 new XElement("ActualState", device.ActualState),
                                 new XElement("TimeStamp", DateTime.Now.ToString()),
                                 new XElement("Measurment", meas_value)));


                xDocument.Save(@"..\..\..\AMSBaza\AMS.xml");
            }
        }