Exemple #1
0
        private Unit Plot(PlotData plotData)
        {
            var setTemperature     = plotData.SetTemperature;
            var currentTemperature = plotData.CurrentTemperature;
            var ipAddress          = plotData.IPAddress;
            var plottingBand       = plotData.PlottingBand;
            var plottingTimeSpan   = plotData.PlottingTimeSpan;

            display.WriteSetTemp(setTemperature);
            display.WriteCurrentTemp(currentTemperature);
            display.WriteIp(ipAddress);

            var yMin = setTemperature - plottingBand;
            var yMax = setTemperature + plottingBand;

            var temperaturesInPlottingRange = temperatureHistory.GetValuesBeforeNow(plottingTimeSpan, out var now);
            var bucketSizeInTicks           = (long)(plottingTimeSpan.Ticks / display.MaxValues);
            var indexedBuckets = temperaturesInPlottingRange.GroupBy(t => (now - t.Time).Ticks / bucketSizeInTicks).ToDictionary(g => g.Key, g => g.Select(v => v.Value).ToArray());

            if (indexedBuckets.Any())
            {
                var maxIndex = indexedBuckets.Keys.Max();
                var minIndex = indexedBuckets.Keys.Min();
                var values   = new double[maxIndex - minIndex + 1];
                values[0] = indexedBuckets[minIndex].Average();
                var startValueOfInterpolation = values[0];
                var indicesToInterpolate      = new List <int>();
                for (int i = 1; i < values.Length; i++)
                {
                    if (indexedBuckets.TryGetValue(i, out var bucket))
                    {
                        var endValueOfInterpolation = values[i] = bucket.Average();
                        if (indicesToInterpolate.Any())
                        {
                            var yDifference = endValueOfInterpolation - startValueOfInterpolation;
                            var xDifference = indicesToInterpolate.Count + 1;
                            var gain        = yDifference / xDifference;
                            for (int j = 0; j < indicesToInterpolate.Count; j++)
                            {
                                var index = indicesToInterpolate[j];
                                values[index] = startValueOfInterpolation + (j + 1) * gain;
                            }
                        }
                        startValueOfInterpolation = endValueOfInterpolation;
                        indicesToInterpolate.Clear();
                    }
                    else
                    {
                        indicesToInterpolate.Add(i);
                    }
                }

                display.DrawDiagram(new DiagramData(yMin, yMax, plottingTimeSpan, values, (int)minIndex));
            }
            else
            {
                display.DrawDiagram(new DiagramData(yMin, yMax, plottingTimeSpan, new double[0], 0));
            }

            display.Draw();

            return(Unit.Default);
        }