Example #1
0
        public string CheckChosenAgregator(List <string> listagr, string agregatorID)
        {
            try
            {
                int.Parse(agregatorID);
            }
            catch
            {
                throw new ArgumentOutOfRangeException("It must be number.");
            }

            AgregatorBase entity;

            using (var aData = new AgregatorBaseDBContex())
            {
                do
                {
                    agregatorID = (Int32.Parse(agregatorID) - 1).ToString();
                    string agrID = listagr[Int32.Parse(agregatorID)];
                    entity = aData.AgregatorBaseData.Find(Int32.Parse(agrID));

                    if (entity == null)
                    {
                        Console.WriteLine("Incorect format. Try again. \n->");
                        agregatorID = Console.ReadLine();
                    }
                } while (entity == null);
            }

            return(entity.AgregatorCode);
        }
Example #2
0
        public string addAgregator(string agregatorCode)
        {
            if (agregatorCode.Length != 4)
            {
                throw new ArgumentException("Invalid code");
            }

            try
            {
                int.Parse(agregatorCode);
            }
            catch
            {
                throw new ArgumentOutOfRangeException("It must be number.");
            }


            using (var data = new AgregatorBaseDBContex())
            {
                bool exists = false;
                do
                {
                    exists = false;
                    foreach (var a in data.AgregatorBaseData)
                    {
                        if (a.AgregatorCode == agregatorCode)
                        {
                            Random r = new Random();
                            Console.WriteLine("Agregator with that code already exist-> changing code..");
                            agregatorCode = "50" + r.Next(0, 9) + r.Next(0, 9);
                            Console.WriteLine("New code is : {0}", agregatorCode);
                            exists = true;
                            break;
                        }
                    }
                } while (exists);

                var AgregatorBase = new AgregatorBase
                {
                    AgregatorCode = agregatorCode,
                    Time          = DateTime.Now.ToString()
                };

                data.AgregatorBaseData.Add(AgregatorBase);
                data.SaveChanges();
            }

            return(agregatorCode);
        }
Example #3
0
        public List <string> ReadAgregatorsFromBase()
        {
            List <string> listAgr = new List <string>();
            int           i       = 1;

            using (var data = new AgregatorBaseDBContex())
            {
                var AgregatorBase = from d in data.AgregatorBaseData select d;

                foreach (var lb in AgregatorBase)
                {
                    DateTime timeA = DateTime.Parse(lb.Time);
                    if (timeA >= DateTime.Now.AddSeconds(-10))
                    {
                        Console.WriteLine("{0}. {1}", i, lb.AgregatorCode);
                        listAgr.Add((lb.Id).ToString());
                        i++;
                    }
                }
            }

            return(listAgr);
        }
Example #4
0
        static void Main(string[] args)
        {
            IAMIAgregator          agregator = new Agregator(1);
            CreateChannelAgregator createChannelAgregator = new CreateChannelAgregator();
            ServicePart            service = new ServicePart();
            bool on = true;

            service.Open();

            Task t1 = new Task(() =>
            {
                while (true)
                {
                    if (Console.ReadKey(true).Key == ConsoleKey.Escape)
                    {
                        if (on)
                        {
                            on = false;
                            agregator.turnOff();
                            Console.WriteLine("Agregator is turned off at {0}", DateTime.Now);
                            service.Close();
                        }
                    }
                    if (Console.ReadKey(true).Key == ConsoleKey.Enter)
                    {
                        if (!on)
                        {
                            on = true;
                            agregator.turnOn();
                            Console.WriteLine("Agregator is turned on at {0}", DateTime.Now);
                            service = new ServicePart();
                            service.Open();
                        }
                    }
                }
            });

            Task t2 = new Task(() =>
            {
                while (true)
                {
                    Thread.Sleep(2000);
                    using (var data = new AgregatorBaseDBContex())
                    {
                        AgregatorBase agr = data.AgregatorBaseData.Where(d => d.AgregatorCode == agregator.agregatorCode).First();
                        agr.Time          = DateTime.Now.ToString();
                        data.SaveChanges();
                    }
                }
            });

            t1.Start();
            t2.Start();

            while (true)
            {
                if (agregator.state == Enums.State.on)
                {
                    string path = @"..\configurabileTime.txt";
                    string text = System.IO.File.ReadAllText(path);
                    int    time = Int32.Parse(text);
                    Thread.Sleep(time * 60000);

                    Dictionary <DateTime, Dictionary <string, Dictionary <DateTime, Dictionary <Enums.MeasureType, double> > > > agregatorDataLocal = new Dictionary <DateTime, Dictionary <string, Dictionary <DateTime, Dictionary <MeasureType, double> > > >();


                    try
                    {
                        agregatorDataLocal = ReadFromLocal(agregator); //citanje lokalne baze

                        CreateChannelAgregator.proxy = CreateChannelAgregator.factory.CreateChannel();
                        CreateChannelAgregator.proxy.Send(agregator.agregatorCode, agregatorDataLocal); //slanje globalnoj
                        Console.WriteLine("Measurements for Agregator [{0}] are sent.", agregator.agregatorCode);

                        DeleteFromLocal(agregator); // brisanje iz lokalne
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("System Management is off");
                    }
                }
            }



            Console.ReadLine();
        }