Пример #1
0
        public void LinearWeather()
        {
            var frame  = new TimeFrame();
            var linear = new LinearWeather(frame);

            Assert.IsNaN(linear.Temperature());

            const Double        StartingTemp = 75.0;
            TemperatureSetPoint temp_a       = new TemperatureSetPoint(DateTime.Now, StartingTemp);

            linear.Add(temp_a);

            Assert.AreEqual(StartingTemp, linear.Temperature());
            Assert.AreEqual(1, linear.SetPoints().Count);
            linear.ClearSetPoints();
            Assert.AreEqual(0, linear.SetPoints().Count);

            var start = DateTime.Now;
            List <TemperatureSetPoint> temps = new List <TemperatureSetPoint>
            {
                new TemperatureSetPoint(start, 0),
                new TemperatureSetPoint(start + new TimeSpan(0, 0, 10), 10),
                new TemperatureSetPoint(start + new TimeSpan(0, 0, 20), 20),
                new TemperatureSetPoint(start + new TimeSpan(0, 0, 30), 10)
            };

            linear.AddRange(temps);
            Assert.AreEqual(temps.Count, linear.SetPoints().Count);

            Assert.AreEqual(temps[0].Temp, linear.Temperature(start));
            Assert.AreEqual(5, linear.Temperature(start + new TimeSpan(0, 0, 5)));
            Assert.AreEqual(15, linear.Temperature(start + new TimeSpan(0, 0, 25)));
        }
Пример #2
0
        static bool GenerateSimulatedHouse(string house_id, string scenario)
        {
            if (String.IsNullOrEmpty(house_id) || String.IsNullOrEmpty(scenario))
            {
                return(false);
            }

            JObject info = null;

            try
            {
                info = JObject.Parse(scenario);
            }
            catch (JsonException ex)
            {
                var error = String.Format("Scenario parsing error: {0}", ex.Message);
                Console.WriteLine(error);
                return(false);
            }
            JToken house_list;

            if (!info.TryGetValue("houses", out house_list))
            {
                return(false);
            }

            bool status = false;
            IJEnumerable <JToken> houses = house_list.Children();

            //search through houses. Pity this isn't a map.
            foreach (JToken house in houses)
            {
                JObject house_obj = JObject.Parse(house.ToString());
                JToken  id_tok;

                //found our house
                if (house_obj.TryGetValue("id", out id_tok) &&
                    id_tok.ToString() == house_id)
                {
                    JToken port_tok;
                    JToken dev_tok;
                    if (house_obj.TryGetValue("port", out port_tok))
                    {
                        _port = JsonConvert.DeserializeObject <int>(port_tok.ToString());
                        //must get a valid port value
                        if (_port > System.Net.IPEndPoint.MaxPort || _port < System.Net.IPEndPoint.MinPort)
                        {
                            return(false);
                        }
                    }
                    bool success = house_obj.TryGetValue("devices", out dev_tok);
                    System.Diagnostics.Debug.Assert(success);
                    IJEnumerable <JToken> devices = dev_tok.Children();
                    UInt64 id = 0;
                    foreach (JToken dev in devices)
                    {
                        //TODO: Create DeviceInput and DeviceOutput for control
                        Device device = Interfaces.DeserializeDevice(dev.ToString(), null, null, new TimeFrame());

                        if (device != null)
                        {
                            device.ID.DeviceID = id++;
                            DeviceModel.Instance.Devices.Add(device);
                        }
                    }

                    JToken weather_tok;
                    success = house_obj.TryGetValue("weather", out weather_tok);
                    System.Diagnostics.Debug.Assert(success);
                    _weather = new LinearWeather();
                    IJEnumerable <JToken> temps = weather_tok.Children();
                    foreach (JToken temp in temps)
                    {
                        _weather.Add(JsonConvert.DeserializeObject <TemperatureSetPoint>(temp.ToString()));
                    }

                    System.Diagnostics.Debug.Assert(DeviceModel.Instance.Devices.Count > 0);
                    status = true;
                    break;
                }
            }

            return(status);
        }