public void CalculatePrice_SetPower_Test(double value)
        {
            UtilityViewModel utility = new UtilityViewModel();
            Utility          u       = new Utility {
                PayingPrice = 2, Power = 0, Price = 0
            };

            UtilityViewModel.Utilities = new System.Collections.ObjectModel.ObservableCollection <Utility>();
            UtilityViewModel.Utilities.Add(u);

            utility.CalculatePrice(value);
            Assert.AreEqual(value, u.Power);
        }
        public void CalculatePrice_GoodParameters_Test(double value)
        {
            UtilityViewModel utility = new UtilityViewModel();
            Utility          u       = new Utility {
                PayingPrice = 2, Power = 0, Price = 0
            };

            UtilityViewModel.Utilities = new System.Collections.ObjectModel.ObservableCollection <Utility>();
            UtilityViewModel.Utilities.Add(u);

            var retVal = utility.CalculatePrice(value);

            Assert.AreEqual(retVal, -value * u.PayingPrice);
        }
Ejemplo n.º 3
0
        private void createListener()
        {
            var tcp = new TcpListener(IPAddress.Any, 25565);

            tcp.Start();

            var listeningThread = new Thread(() =>
            {
                while (!shutDown)
                {
                    var tcpClient = tcp.AcceptTcpClient();
                    ThreadPool.QueueUserWorkItem(param =>
                    {
                        //Prijem poruke
                        NetworkStream stream = tcpClient.GetStream();
                        string incomming;
                        byte[] bytes = new byte[1024];
                        int i        = stream.Read(bytes, 0, bytes.Length);
                        //Primljena poruka je sacuvana u incomming stringu
                        incomming = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

                        //Ukoliko je primljena poruka pitanje koliko objekata ima u sistemu -> odgovor
                        if (incomming.Equals("Need object count"))
                        {
                            //Response

                            /* Umesto sto se ovde salje count.ToString(), potrebno je poslati
                             * duzinu liste koja sadrzi sve objekte pod monitoringom, odnosno
                             * njihov ukupan broj (NE BROJATI OD NULE, VEC POSLATI UKUPAN BROJ)
                             * */
                            Byte[] data = System.Text.Encoding.ASCII.GetBytes((ConsumersViewModel.Consumers.Count).ToString());
                            stream.Write(data, 0, data.Length);
                        }
                        else
                        {
                            //U suprotnom, server je poslao promenu stanja nekog objekta u sistemu
                            Console.WriteLine(incomming); //Na primer: "Objekat_1:272"

                            //################ IMPLEMENTACIJA ####################
                            // Obraditi poruku kako bi se dobile informacije o izmeni
                            // Azuriranje potrebnih stvari u aplikaciji
                            int deviceId       = Int32.Parse((incomming.Split('_', ':'))[1]);
                            double consumption = double.Parse((incomming.Split('_', ':'))[2]);

                            if (deviceId != ConsumersViewModel.Consumers.Count)
                            {
                                if (devicesList[deviceId].Working)
                                {
                                    devicesList[deviceId].Consumption = consumption;
                                }
                                else
                                {
                                    devicesList[deviceId].Consumption = 0;
                                }

                                try
                                {
                                    lock (ConsumersViewModel.Consumers)
                                    {
                                        if (ConsumersViewModel.Consumers[deviceId].Working)
                                        {
                                            ConsumersViewModel.Consumers[deviceId].Consumption = consumption;
                                        }
                                        else
                                        {
                                            ConsumersViewModel.Consumers[deviceId].Consumption = 0;
                                        }
                                    }
                                }
                                catch { }

                                wholeConsumption = 0;

                                foreach (var cons in devicesList)
                                {
                                    wholeConsumption += cons.Consumption;
                                }

                                consumersConsumption = wholeConsumption;

                                if (batteryState == Enums.BatteryState.Charging)
                                {
                                    importExportPower = wholeConsumption - solarPnaelsPower + batteryPower;
                                }
                                else if (batteryState == Enums.BatteryState.Discharging)
                                {
                                    importExportPower = wholeConsumption - solarPnaelsPower - batteryPower;
                                }
                                else
                                {
                                    importExportPower = wholeConsumption - solarPnaelsPower;
                                }


                                utilityPower = importExportPower;

                                lock (UtilityViewModel.Utilities)
                                {
                                    priceToPay = utility.CalculatePrice(importExportPower);
                                }
                            }
                            else
                            {
                                if (deviceId == ConsumersViewModel.Consumers.Count)
                                {
                                    lock (UtilityViewModel.Utilities)
                                    {
                                        UtilityViewModel.Utilities[0].PayingPrice = consumption;
                                    }
                                    price = consumption;
                                }
                            }
                        }
                    }, null);
                }
            });

            listeningThread.IsBackground = true;
            listeningThread.Start();
        }