Example #1
0
        public void ToStringNotInitializedTest()
        {
            var sensor = new Sensor();

            var actualResult = sensor.ToString();

            Assert.IsNotNull(actualResult);
        }
Example #2
0
        public void DescriptionTest()
        {
            var literal = "MySensor";

            var sensor = new Sensor { Description = literal };

            var actualResult = sensor.Description;

            Assert.AreEqual(literal, actualResult);
        }
Example #3
0
        public void NameTest()
        {
            var literal = "MySensor";

            var sensor = new Sensor {Name = literal};

            var actualResult = sensor.Name;

            Assert.AreEqual(literal, actualResult);
        }
Example #4
0
        public void SetToStringTest()
        {
            var literal = "MySensor";

            var sensor = new Sensor();

            sensor.SetToString(() => literal);

            var actualResult = sensor.ToString();

            Assert.AreEqual(literal, actualResult);
        }
Example #5
0
        /// <summary>
        /// Updates the list of sensors registered with the XS1.
        /// </summary>
        public void Update()
        {
            _sensors.Clear();

            for (int i = 1; i <= 64; i++)
            {
                string uri = string.Format("http://{0}/control?callback=mycallback&cmd=get_state_sensor&number={1}", _ipAddress, i);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "GET";

                // in case a web request fails - try 8 times
                int retries = 0;
                HttpWebResponse response = null;

                while (true)
                {
                    try
                    {
                        retries++;
                        response = (HttpWebResponse)request.GetResponse();
                        break;
                    }
                    catch (WebException ex)
                    {
                        if (retries > c_maxRequests)
                        {
                            _logger.Debug(string.Format("WebRequest to {0} failed. Retried {1} times.", uri, retries), ex);
                            // ignor this one, cont' with next sensor
                            break;
                        }
                    }
                }

                if (retries > c_maxRequests)
                {
                    _logger.Debug(string.Format("Cannot extablish connection to {0} failed. Request Skipped.", uri));
                    continue;
                }

                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        string content = reader.ReadToEnd();

                        // remove 'mycallback(' and ')'
                        content = content.Substring(11, content.Length - 15);

                        JObject obj = JObject.Parse(content);

                        var type = (string)obj.SelectToken("sensor.type");

                        if (type == "disabled")
                            continue;

                        string name = (string)obj.SelectToken("sensor.name");
                        double value = (double)obj.SelectToken("sensor.value");
                        int day = (int)obj.SelectToken("sensor.date.day");
                        int month = (int)obj.SelectToken("sensor.date.month");
                        int year = (int)obj.SelectToken("sensor.date.year");
                        int hour = (int)obj.SelectToken("sensor.time.hour");
                        int min = (int)obj.SelectToken("sensor.time.min");
                        int sec = (int)obj.SelectToken("sensor.time.sec");

                        DateTime time = new DateTime(year, month, day, hour, min, sec);

                        string description;

                        if (!_descriptionMap.TryGetValue(name, out description))
                        {
                            description = "N.N.";
                        }

                        var sensor = new Sensor()
                        {
                            Name = name,
                            Type = type,
                            Value = value,
                            Time = time,
                            Description = description
                        };

                        sensor.SetToString(() =>
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.Append("Name: ");
                            sb.Append(sensor.Name);
                            sb.Append(" Type: ");
                            sb.Append(sensor.Type);
                            sb.Append(" Value: ");
                            sb.Append(sensor.Value);
                            sb.Append(" Time: ");
                            sb.Append(sensor.Time);
                            return sb.ToString();
                        });

                        _sensors.Add(sensor);
                    }
                }

                Thread.Sleep(1000);

            }
        }