コード例 #1
0
        private bool IsTimeSpan(ChannelHistoryRecord record)
        {
            if (record.DisplayValue != null)
            {
                var split = record.DisplayValue.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                var units = split.Where((v, i) => i % 2 != 0).ToArray(); //Due to 0 based indexing 0, 2, 4, etc has the number, 1, 3, 5, etc has the unit

                if (units.Length > 0 && units.All(v => v == "d" || v == "h" || v == "m" || v == "s") && split.Length % 2 == 0)
                {
                    var total = 0;

                    for (var i = 0; i < split.Length - 1; i += 2)
                    {
                        int intValue;

                        if (!int.TryParse(split[i], out intValue))
                        {
                            return(false);
                        }

                        var unit        = split[i + 1];
                        var unitSeconds = GetSecondsForTimeSpanUnit(unit);

                        total += intValue * unitSeconds;
                    }

                    //With large units (such as days) the display value will almost never be the same as the raw value
                    if (total == record.Value)
                    {
                        return(true);
                    }

                    var largestUnit = GetSecondsForTimeSpanUnit(split[1]);

                    //If we elapse the next hour or day, etc and it turns out we're bigger,
                    //then perhaps we are in fact a TimeSpan. Otherwise, whatever our units mean they don't
                    //relate to TimeSpans
                    if (total < record.Value && total + largestUnit > record.Value)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
コード例 #2
0
        private object GetChannelValue(ChannelHistoryRecord channel)
        {
            double?value = null;

            if (channel.DisplayValue != null)
            {
                if (channel.Value != null && IsTimeSpan(channel))
                {
                    return(TimeSpan.FromSeconds(channel.Value.Value));
                }

                if (IsValueLookup(channel.DisplayValue))
                {
                    return(channel.DisplayValue);
                }

                var space = channel.DisplayValue.IndexOf(' ');

                var valueStr = channel.DisplayValue;

                if (space > 0)
                {
                    valueStr = channel.DisplayValue.Substring(0, space);
                }

                if (valueStr == "<1")
                {
                    valueStr = "0";
                }
                else if (valueStr == ">99")
                {
                    valueStr = "100";
                }
                else if (valueStr == "<" || valueStr == ">")
                {
                    var first  = channel.DisplayValue.IndexOf(' ') + 1;
                    var second = channel.DisplayValue.IndexOf(' ', first);

                    valueStr = channel.DisplayValue.Substring(first, second - first);
                }

                value = Convert.ToDouble(valueStr);
            }

            return(value);
        }
コード例 #3
0
        private static double?GetSensorHistoryChannelValue(SensorHistoryRecord history, ChannelHistoryRecord record)
        {
            //PRTG does not return a raw record if the sensor did not return a value
            //(such as because it was in an error state)
            var rawRecords = history.ChannelRecordsRaw.Where(r => r.ChannelId == record.ChannelId).ToArray();

            if (rawRecords.Length < 2)
            {
                return(rawRecords.FirstOrDefault()?.Value);
            }

            //We have a history record that contains multiple records for a specified channel ID (e.g. a Traffic Sensor)
            var displayRecords = history.ChannelRecords.Where(r => r.ChannelId == record.ChannelId).ToList();

            Debug.Assert(rawRecords.Length == displayRecords.Count, "Had different number of display and raw records!");

            var thisIndex = displayRecords.IndexOf(record);

            return(rawRecords[thisIndex].Value);
        }
コード例 #4
0
        private static void SetNewSensorHistoryChannelName(SensorHistoryRecord history, ChannelHistoryRecord record)
        {
            record.Name = record.Name.Replace(" ", "");

            string newName;

            if (TryGetNewSensorHistoryChannelName(record.Name, out newName))
            {
                if (history.ChannelRecords.Where(r => r != record).All(r =>
                {
                    //Is this new name unique amongst all the other channel names (after updating their names)?
                    string other;

                    if (!TryGetNewSensorHistoryChannelName(r.Name.Replace(" ", ""), out other))
                    {
                        other = r.Name;
                    }

                    return(other != newName);
                }))
                {
                    record.Name = newName;
                }
            }
        }
コード例 #5
0
ファイル: ResponseParser.cs プロジェクト: vsbopi/PrtgAPI
        private static double?GetSensorHistoryChannelValue(SensorHistoryRecord history, ChannelHistoryRecord record)
        {
            //PRTG does not return a raw record if the sensor did not return a value
            //(such as because it was in an error state)
            var rawRecord = history.ChannelRecordsRaw.FirstOrDefault(r => r.ChannelId == record.ChannelId);

            return(rawRecord?.Value);
        }