Ejemplo n.º 1
0
        private static void TestSensorDataStringValues(SensorDataEntity expected, SensorData actual, SensorType sensorType)
        {
            switch (sensorType)
            {
            case SensorType.BooleanSensor:
                BoolSensorData boolData = JsonSerializer.Deserialize <BoolSensorData>(expected.TypedData);
                Assert.Equal(SensorDataStringValuesFactory.GetSimpleSensorsString(expected.TimeCollected, boolData.Comment, boolData.BoolValue),
                             actual.StringValue);
                Assert.Equal(boolData.BoolValue.ToString(), actual.ShortStringValue);
                break;

            case SensorType.IntSensor:
                IntSensorData intData = JsonSerializer.Deserialize <IntSensorData>(expected.TypedData);
                Assert.Equal(SensorDataStringValuesFactory.GetSimpleSensorsString(expected.TimeCollected, intData.Comment, intData.IntValue),
                             actual.StringValue);
                Assert.Equal(intData.IntValue.ToString(), actual.ShortStringValue);
                break;

            case SensorType.DoubleSensor:
                DoubleSensorData doubleData = JsonSerializer.Deserialize <DoubleSensorData>(expected.TypedData);
                Assert.Equal(SensorDataStringValuesFactory.GetSimpleSensorsString(expected.TimeCollected, doubleData.Comment, doubleData.DoubleValue),
                             actual.StringValue);
                Assert.Equal(doubleData.DoubleValue.ToString(), actual.ShortStringValue);
                break;

            case SensorType.StringSensor:
                StringSensorData stringData = JsonSerializer.Deserialize <StringSensorData>(expected.TypedData);
                Assert.Equal(SensorDataStringValuesFactory.GetSimpleSensorsString(expected.TimeCollected, stringData.Comment, stringData.StringValue),
                             actual.StringValue);
                Assert.Equal(stringData.StringValue, actual.ShortStringValue);
                break;

            case SensorType.IntegerBarSensor:
                IntBarSensorData intBarData = JsonSerializer.Deserialize <IntBarSensorData>(expected.TypedData);
                Assert.Equal(SensorDataStringValuesFactory.GetBarSensorsString(expected.TimeCollected, intBarData.Comment, intBarData.Min, intBarData.Mean, intBarData.Max, intBarData.Count, intBarData.LastValue),
                             actual.StringValue);
                Assert.Equal(SensorDataStringValuesFactory.GetBarSensorsShortString(intBarData.Min, intBarData.Mean, intBarData.Max, intBarData.Count, intBarData.LastValue),
                             actual.ShortStringValue);
                break;

            case SensorType.DoubleBarSensor:
                DoubleBarSensorData doubleBarData = JsonSerializer.Deserialize <DoubleBarSensorData>(expected.TypedData);
                Assert.Equal(SensorDataStringValuesFactory.GetBarSensorsString(expected.TimeCollected, doubleBarData.Comment, doubleBarData.Min, doubleBarData.Mean, doubleBarData.Max, doubleBarData.Count, doubleBarData.LastValue),
                             actual.StringValue);
                Assert.Equal(SensorDataStringValuesFactory.GetBarSensorsShortString(doubleBarData.Min, doubleBarData.Mean, doubleBarData.Max, doubleBarData.Count, doubleBarData.LastValue),
                             actual.ShortStringValue);
                break;

            case SensorType.FileSensorBytes:
                FileSensorBytesData fileSensorBytesData = JsonSerializer.Deserialize <FileSensorBytesData>(expected.TypedData);
                Assert.Equal(SensorDataStringValuesFactory.GetFileSensorsString(expected.TimeCollected, fileSensorBytesData.Comment, fileSensorBytesData.FileName, fileSensorBytesData.Extension, fileSensorBytesData.FileContent.Length),
                             actual.StringValue);
                Assert.Equal(SensorDataStringValuesFactory.GetFileSensorsShortString(fileSensorBytesData.FileName, fileSensorBytesData.Extension, fileSensorBytesData.FileContent.Length),
                             actual.ShortStringValue);
                break;
            }
        }
        private SensorHistoryData Convert(DoubleBarSensorData typedData)
        {
            SensorHistoryData result = new SensorHistoryData();

            result.TypedData  = JsonSerializer.Serialize(typedData);
            result.Time       = typedData.EndTime;
            result.SensorType = SensorType.DoubleBarSensor;
            return(result);
        }
        private void AddDataFromLists(DoubleBarSensorData currentItem)
        {
            currentItem.Mean        = CountMean(_MeanList);
            currentItem.Percentiles = new List <PercentileValueDouble>();
            //var median = _MedianList[(int) (_MedianList.Count / 2)];
            if (_percentilesList.Count < 3)
            {
                currentItem.Percentiles.Add(new PercentileValueDouble()
                {
                    Percentile = 0.5, Value = currentItem.Mean
                });
                currentItem.Percentiles.Add(new PercentileValueDouble()
                {
                    Percentile = 0.25, Value = currentItem.Min
                });
                currentItem.Percentiles.Add(new PercentileValueDouble()
                {
                    Percentile = 0.75, Value = currentItem.Max
                });
                return;
            }

            _percentilesList.Sort();
            if (_percentilesList.Count == 3)
            {
                currentItem.Percentiles.Add(new PercentileValueDouble()
                {
                    Percentile = 0.5, Value = _percentilesList[1]
                });
                currentItem.Percentiles.Add(new PercentileValueDouble()
                {
                    Percentile = 0.25, Value = _percentilesList[0]
                });
                currentItem.Percentiles.Add(new PercentileValueDouble()
                {
                    Percentile = 0.75, Value = _percentilesList[2]
                });
                return;
            }
            currentItem.Percentiles.Add(new PercentileValueDouble()
            {
                Percentile = 0.5, Value = CountMedian()
            });
            currentItem.Percentiles.Add(new PercentileValueDouble()
            {
                Percentile = 0.25, Value = CountQ1()
            });
            currentItem.Percentiles.Add(new PercentileValueDouble()
            {
                Percentile = 0.75, Value = CountQ3()
            });
        }
        private void ProcessItem(DoubleBarSensorData data, DoubleBarSensorData currentItem)
        {
            currentItem.Count += data.Count;
            if (data.Max > currentItem.Max)
            {
                currentItem.Max = data.Max;
            }

            if (data.Min < currentItem.Min)
            {
                currentItem.Min = data.Min;
            }
        }
 private void AddDataToList(DoubleBarSensorData data)
 {
     try
     {
         _MeanList.Add(new KeyValuePair <double, int>(data.Mean, data.Count));
         if (data.Percentiles != null && data.Percentiles.Any())
         {
             _percentilesList.AddRange(data.Percentiles.Select(p => p.Value));
         }
     }
     catch (Exception e)
     {
     }
 }
        protected override List <SensorHistoryData> ProcessHistoryInternal(List <SensorHistoryData> uncompressedData,
                                                                           TimeSpan compressionInterval)
        {
            if (uncompressedData == null || !uncompressedData.Any())
            {
                return(new List <SensorHistoryData>());
            }

            if (uncompressedData.Count == 1)
            {
                return(uncompressedData);
            }

            List <DoubleBarSensorData> typedDatas  = GetTypeDatas(uncompressedData);
            List <SensorHistoryData>   result      = new List <SensorHistoryData>();
            DoubleBarSensorData        currentItem = new DoubleBarSensorData()
            {
                Count = 0, Max = double.MinValue, Min = double.MaxValue
            };
            DateTime startDate                = typedDatas[0].StartTime;
            int      processingCount          = 0;
            bool     needToAddCurrentAsSingle = false;
            bool     addingCurrent            = false;

            for (int i = 0; i < typedDatas.Count; ++i)
            {
                if (typedDatas[i].EndTime - typedDatas[i].StartTime > compressionInterval ||
                    (processingCount > 0 && startDate + compressionInterval < typedDatas[i].EndTime &&
                     i == typedDatas.Count - 1))
                {
                    needToAddCurrentAsSingle = true;
                }

                if (typedDatas[i].EndTime < startDate + compressionInterval && i == typedDatas.Count - 1)
                {
                    AddDataToList(typedDatas[i]);
                    ProcessItem(typedDatas[i], currentItem);
                    addingCurrent = true;
                }
                //Finish bar if necessary
                if (i > 0 && (startDate + compressionInterval < typedDatas[i].EndTime || needToAddCurrentAsSingle ||
                              i == typedDatas.Count - 1))
                {
                    if (processingCount > 0)
                    {
                        AddDataFromLists(currentItem);
                        ClearLists();
                        currentItem.StartTime = startDate;
                        currentItem.EndTime   = addingCurrent ? typedDatas[i].EndTime : typedDatas[i - 1].EndTime;
                        result.Add(Convert(currentItem));
                        currentItem = new DoubleBarSensorData()
                        {
                            Count = 0, Max = double.MinValue, Min = double.MaxValue
                        };
                        processingCount = 0;
                    }
                }

                if (needToAddCurrentAsSingle)
                {
                    result.Add(Convert(typedDatas[i]));
                    needToAddCurrentAsSingle = false;
                    if (i != typedDatas.Count - 1)
                    {
                        startDate = typedDatas[i + 1].StartTime;
                        continue;
                    }
                }

                //if (i == typedDatas.Count - 1)
                //{
                //    result.Add(Convert(typedDatas[i], typedDatas[i].EndTime));
                //}

                //Start new bar, might need this right after finished previous
                if (processingCount == 0 && i != typedDatas.Count - 1)
                {
                    startDate = typedDatas[i].StartTime;
                    AddDataToList(typedDatas[i]);
                    ProcessItem(typedDatas[i], currentItem);
                    ++processingCount;
                    continue;
                }

                AddDataToList(typedDatas[i]);
                ProcessItem(typedDatas[i], currentItem);
                ++processingCount;
            }
            return(result);
        }