BananaPiOLEDMonitor.NetNfo.ReadFromProc C# (CSharp) Méthode

ReadFromProc() public méthode

public ReadFromProc ( ) : bool
Résultat bool
        public bool ReadFromProc()
        {
            var q = File.ReadLines("/proc/net/dev");

            foreach (var x in q)
            {
                if (x.Trim().StartsWith(Dev + ":"))
                {
                    var xq = x.Split(' ').Where(r => r.Trim().Length > 0).ToList();

                    Received = ulong.Parse(xq[1]);
                    Transmitted = ulong.Parse(xq[9]);

                    return true;
                }
            }

            return false;
        }

Usage Example

Exemple #1
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);
                }

            }
        }
All Usage Examples Of BananaPiOLEDMonitor.NetNfo::ReadFromProc