Exemplo n.º 1
0
        public void MonitorPlotTest1()
        {
            IMonitorPlot plot = new MonitorPlot(3);

            var dsmean = plot.AddDataSet("6 seconds", TimeSpan.FromSeconds(6));

            var dtnow = DateTime.Now;
            var ts = TimeSpan.FromSeconds(1);

            plot.Add(new MonitorData(dtnow, 5), dtnow); dtnow += ts;
            plot.Add(new MonitorData(dtnow, 6), dtnow); dtnow += ts;
            plot.Add(new MonitorData(dtnow, 1), dtnow); dtnow += ts;
            plot.Add(new MonitorData(dtnow, 2), dtnow); dtnow += ts;
            plot.Add(new MonitorData(dtnow, 3), dtnow); dtnow += ts;
            plot.Add(new MonitorData(dtnow, 4), dtnow); dtnow += ts;

            var q1 = plot.DataSets.First(w => w.Name == "default");
            var q1lst = q1.Points.ToList();
            Assert.True(q1lst.Count == 3);
            // checks that the first "default" set window move to the last three data
            Assert.True(q1lst[0].Value.EqualsAutoTol(2));
            Assert.True(q1lst[1].Value.EqualsAutoTol(3));
            Assert.True(q1lst[2].Value.EqualsAutoTol(4));

            // now check that the "6 seconds" dataset mean 3 points over last 6 seconds
            // in other words it will get (5.5, 1.5, 3.5) that are means of ( (5,6), (1,2), (3,4) )
            var meanlst = dsmean.Points.ToList();
            Assert.True(meanlst.Count == 3);
            Assert.True(meanlst[0].Value.EqualsAutoTol(5.5));
            Assert.True(meanlst[1].Value.EqualsAutoTol(1.5));
            Assert.True(meanlst[2].Value.EqualsAutoTol(3.5));

            // proceed adding 2 more items
            plot.Add(new MonitorData(dtnow, 6), dtnow); dtnow += ts;
            plot.Add(new MonitorData(dtnow, 7), dtnow); dtnow += ts;

            q1lst = q1.Points.ToList();
            Assert.True(q1lst.Count==3);
            Assert.True(q1lst[0].Value.EqualsAutoTol(4));
            Assert.True(q1lst[1].Value.EqualsAutoTol(6));
            Assert.True(q1lst[2].Value.EqualsAutoTol(7));

            // check the mean, should be (1.5, 3.5, 6.5)
            meanlst = dsmean.Points.ToList();
            Assert.True(meanlst.Count == 3);
            Assert.True(meanlst[0].Value.EqualsAutoTol(1.5));
            Assert.True(meanlst[1].Value.EqualsAutoTol(3.5));
            Assert.True(meanlst[2].Value.EqualsAutoTol(6.5));
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            bool quietMode = false;
            if (args.Any(t => t == "-q")) quietMode = true;

            if (!quietMode) Console.WriteLine("To use command line (-h argument help)");

            if (args.Any(t => t == "-h")) { PrintUsage(); return; }

            var oledType = ArduiPi_OLED.OLED_Types.Adafruit_I2C_128x32;
            string netDev = null;
            bool oledTypeArgumentGiven = false;
            bool netDevArgumentGiven = false;
            int longTimeGraphMinutes = 15;

            for (int i = 0; i < args.Length; ++i)
            {
                if (args[i] == "-o" && i + 1 < args.Length && args[i + 1].Length == 1 && "0123456".IndexOf(args[i + 1]) != -1)
                {
                    oledType = (ArduiPi_OLED.OLED_Types)int.Parse(args[i + 1]);
                    oledTypeArgumentGiven = true;
                }

                if (args[i] == "-d" && i + 1 < args.Length)
                {
                    netDev = args[i + 1];
                    netDevArgumentGiven = true;
                }

                if (args[i] == "-t" && i + 1 < args.Length)
                {
                    longTimeGraphMinutes = int.Parse(args[i + 1]);
                }
            }

            if (!oledTypeArgumentGiven || !netDevArgumentGiven)
            {
                oledType = Util.ChooseOLEDType();
                netDev = Util.ChooseNetDev();
            }

            if (!quietMode)
            {
                Console.WriteLine($"Running test with selected display [{oledType}]");
                Console.WriteLine("Hit CTRL+C to stop");
            }

            // init oled display
            var wrapper = new ArduiPi_OLED.Wrapper(oledType);
            wrapper.SetTextColor(1);
            wrapper.SetTextSize(2);

            var w = wrapper.DisplayWidth();
            var h = wrapper.DisplayHeight();

            //--- here can change to 24h for example
            var longTxt = MinutesHumanReadableLabel(longTimeGraphMinutes);
            var longTimespan = TimeSpan.FromMinutes(longTimeGraphMinutes);
            //---

            if (!quietMode) Console.WriteLine($"display w:{w} x h:{h}");

            // -20 to skip 16 of first char + 4 margin
            var graphW = (short)(w - 20);
            var graphH = (short)12; // let 4 pixel margin

            IMonitorPlot plotNet = new MonitorPlot(graphW);
            var dsDefaultNet = plotNet.DataSets.First(k => k.Name == "default");
            var dsLongNet = plotNet.AddDataSet(longTxt, longTimespan);

            IMonitorPlot plotCpu = new MonitorPlot(graphW);
            var dsDefaultCpu = plotCpu.DataSets.First(k => k.Name == "default");
            var dsLongCpu = plotCpu.AddDataSet(longTxt, longTimespan);

            {
                var cpuPrev = new CpuNfo(); cpuPrev.ReadFromProc();
                var netPrev = new NetNfo(netDev); netPrev.ReadFromProc();

                while (true)
                {
                    wrapper.ClearDisplay();

                    wrapper.SetTextSize(2);
                    wrapper.SetCursor(0, 0);
                    wrapper.Print("C");

                    wrapper.SetTextSize(1);
                    wrapper.SetCursor(0, 16);
                    wrapper.Print(longTxt);

                    wrapper.SetTextSize(2);
                    wrapper.SetCursor(0, 32);
                    wrapper.Print("N");

                    wrapper.SetTextSize(1);
                    wrapper.SetCursor(0, 48);
                    wrapper.Print(longTxt);

                    // CPU
                    {
                        var nfoCur = new CpuNfo();
                        nfoCur.ReadFromProc(); // read updated info
                        var diff = nfoCur - cpuPrev;
                        var loadF = (double)diff.Usage / diff.Total;
                        var load = (int)(loadF * 100);

                        if (!quietMode) Console.Write($"cpu % = {load}");

                        plotCpu.Add(new MonitorData(DateTime.Now, loadF));

                        cpuPrev = nfoCur;

                        // DisplayMonitorPlot (wrapper, ds, graphX0, graphW, graphY0, graphH, normalize)
                        DisplayMonitorPlot(wrapper, dsDefaultCpu, 20, graphW, 15, graphH, false);
                        DisplayMonitorPlot(wrapper, dsLongCpu, 20, graphW, 31, graphH, false);
                    }

                    // NET
                    {
                        var nfoCur = new NetNfo(netDev);
                        nfoCur.ReadFromProc(); // read updated info
                        var diff = nfoCur - netPrev;

                        var rate = Math.Max(diff.Received, diff.Transmitted);
                        if (!quietMode) Console.WriteLine($" ; net rate = {rate}");

                        plotNet.Add(new MonitorData(DateTime.Now, rate));

                        netPrev = nfoCur;

                        DisplayMonitorPlot(wrapper, dsDefaultNet, 20, graphW, 47, graphH, true);
                        DisplayMonitorPlot(wrapper, dsLongNet, 20, graphW, 63, graphH, true);
                    }

                    wrapper.Display();

                    Thread.Sleep(1000);
                }

            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var oledType = Util.ChooseOLEDType();
            var netDev = Util.ChooseNetDev();

            Console.WriteLine($"Running test with selected display [{oledType}]");
            Console.WriteLine("Hit CTRL+C to stop");

            // init oled display
            var wrapper = new ArduiPi_OLED.Wrapper(oledType);
            wrapper.SetTextColor(1);
            wrapper.SetTextSize(2);

            var w = wrapper.DisplayWidth();
            var h = wrapper.DisplayHeight();

            //--- here can change to 24h for example
            var longTxt = "15m";
            var longTimespan = TimeSpan.FromMinutes(15);
            //---

            Console.WriteLine($"display w:{w} x h:{h}");

            // -20 to skip 16 of first char + 4 margin
            var graphW = (short)(w - 20);
            var graphH = (short)12; // let 4 pixel margin

            IMonitorPlot plotNet = new MonitorPlot(graphW);
            var dsDefaultNet = plotNet.DataSets.First(k => k.Name == "default");
            var dsLongNet = plotNet.AddDataSet(longTxt, longTimespan);

            IMonitorPlot plotCpu = new MonitorPlot(graphW);
            var dsDefaultCpu = plotCpu.DataSets.First(k => k.Name == "default");
            var dsLongCpu = plotCpu.AddDataSet(longTxt, longTimespan);

            {
                var cpuPrev = new CpuNfo(); cpuPrev.ReadFromProc();
                var netPrev = new NetNfo(netDev); netPrev.ReadFromProc();

                while (true)
                {
                    wrapper.ClearDisplay();

                    wrapper.SetTextSize(2);
                    wrapper.SetCursor(0, 0);
                    wrapper.Print("C");

                    wrapper.SetTextSize(1);
                    wrapper.SetCursor(0, 16);
                    wrapper.Print(longTxt);

                    wrapper.SetTextSize(2);
                    wrapper.SetCursor(0, 32);
                    wrapper.Print("N");

                    wrapper.SetTextSize(1);
                    wrapper.SetCursor(0, 48);
                    wrapper.Print(longTxt);

                    // CPU
                    {
                        var nfoCur = new CpuNfo();
                        nfoCur.ReadFromProc(); // read updated info
                        var diff = nfoCur - cpuPrev;
                        var loadF = (double)diff.Usage / diff.Total;
                        var load = (int)(loadF * 100);

                        Console.Write($"cpu % = {load}");

                        plotCpu.Add(new MonitorData(DateTime.Now, loadF));

                        cpuPrev = nfoCur;

                        // DisplayMonitorPlot (wrapper, ds, graphX0, graphW, graphY0, graphH, normalize)
                        DisplayMonitorPlot(wrapper, dsDefaultCpu, 20, graphW, 15, graphH, false);
                        DisplayMonitorPlot(wrapper, dsLongCpu, 20, graphW, 31, graphH, false);
                    }

                    // NET
                    {
                        var nfoCur = new NetNfo(netDev);
                        nfoCur.ReadFromProc(); // read updated info
                        var diff = nfoCur - netPrev;

                        var rate = Math.Max(diff.Received, diff.Transmitted);
                        Console.WriteLine($" ; net rate = {rate}");

                        plotNet.Add(new MonitorData(DateTime.Now, rate));

                        netPrev = nfoCur;

                        DisplayMonitorPlot(wrapper, dsDefaultNet, 20, graphW, 47, graphH, true);
                        DisplayMonitorPlot(wrapper, dsLongNet, 20, graphW, 63, graphH, true);
                    }

                    wrapper.Display();

                    Thread.Sleep(1000);
                }

            }
        }