Exemplo n.º 1
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();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //otvori servis
            ServiceHost svc = new ServiceHost(typeof(DeviceMethods));

            svc.AddServiceEndpoint(typeof(IDevice),
                                   new NetTcpBinding(),
                                   new Uri("net.tcp://localhost:4000/DeviceMethods"));

            ServiceHost svc2 = new ServiceHost(typeof(AgregatorMethods));

            svc2.AddServiceEndpoint(typeof(IAgregator),
                                    new NetTcpBinding(),
                                    new Uri("net.tcp://localhost:5000/AgregatorMethods"));


            //otvori client
            //ChannelFactory<IAgregator> factory = new ChannelFactory<IAgregator>(
            //        new NetTcpBinding(),
            //        new EndpointAddress("net.tcp://localhost:5000/AgregatorMethods"));

            //IAgregator kanal = factory.CreateChannel();



            //otvori server
            svc.Open();
            svc2.Open();
            AgregatorMethods agm = new AgregatorMethods();

            int a = agm.formirajKljuc();

            List <DevClass> ldc = new List <DevClass>();

            foreach (var v in Baza.devices.Values)
            {
                ldc.Add(v);
            }

            Agregator ag = new Agregator(a, ldc);

            agm.dodajAgregator(a, ag);  //dodaje u baza klasu

            DeviceMethods dvcm = new DeviceMethods();

            dvcm.deleteXML();

            int i = 1;
            int j = 1;

            while (true)
            {
                Thread.Sleep(2000);

                /*if (Baza.devices.ContainsKey(1))
                 * {
                 *  Console.WriteLine("dev " + Baza.devices[1].AMIDevCode + " time " + Baza.devices[1].TimeStamp + " p " + Baza.devices[1].Power.ToString());
                 *  Console.WriteLine("tip "+ Baza.devices[1].ListMerenje[1].Tip + " Vrednost "+ Baza.devices[1].ListMerenje[1].Vrednost);
                 * }
                 * else {
                 *  break;
                 * }*/
                bool chk = Baza.devices.Any();
                if (chk)
                {
                    foreach (KeyValuePair <int, DevClass> kvp in Baza.devices)
                    {
                        i = kvp.Key;
                        break;
                    }
                }


                foreach (KeyValuePair <int, DevClass> dc in Baza.devices)
                {
                    if (!ag.devs.Contains(dc.Value))
                    {
                        ag.devs.Add(dc.Value);
                    }
                    i = dc.Key;
                    Console.WriteLine("-------------------------------------------------");
                    Console.WriteLine("dev " + Baza.devices[i].AMIDevCode + " time " + Baza.devices[i].TimeStamp + " p " + Baza.devices[i].Power.ToString());
                    foreach (var item in Baza.devices[i].ListMerenje)
                    {
                        j = item.Key;
                        Console.WriteLine("Tip: " + Baza.devices[i].ListMerenje[j].Tip + " Vrednost: " + Baza.devices[i].ListMerenje[j].Vrednost);
                    }

                    Console.WriteLine("-------------------------------------------------");
                }

                dvcm.deleteXML();

                foreach (DevClass dc in Baza.devices.Values)
                {
                    dvcm.XMLWrite(dc);
                }
            }
        }