Example #1
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 #2
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();
        }