Example #1
0
 public DeviceController(DeviceConfiguration config)
 {
     this.config     = config;
     plugInterface   = new PlugInterface(config.Host);
     emeter          = new EMeter(plugInterface);
     emeter.Updated += Emeter_Updated;
     emeter.Start();
 }
        static void Main(string[] args)
        {
            var options = new OptionSet();

            options.Add("ip=", "IP address of device", (string ip) => { IPAddress = ip; });

            if (args.Length == 0)
            {
                ShowUsage(options);
                return;
            }

            List <string> extra;

            try
            {
                extra = options.Parse(args);
            }
            catch (OptionException ex)
            {
                Console.WriteLine(ex.Message);
                ShowUsage(options);
                return;
            }

            Console.Clear();

            var iface  = new PlugInterface(IPAddress);
            var emeter = new EMeter(iface);

            Console.CancelKeyPress += (e, a) => { Environment.Exit(0); };

            emeter.Updated += Emeter_Updated;
            emeter.Start();

            while (true)
            {
                Console.CursorLeft = 0;
                Console.CursorTop  = 0;
                Console.WriteLine("Press CTRL+C to exit");
                Console.WriteLine();
                Console.WriteLine($"\t{emeter.Voltage}V  {emeter.Current}A  {emeter.Power}W  {emeter.TotalPower}kWh".PadRight(Console.BufferWidth - 1));
                Console.WriteLine($"\tVGain: {emeter.VGain}  IGain: {emeter.IGain}".PadRight(Console.BufferWidth - 1));
                Console.WriteLine();
                Console.WriteLine("Q - increase VGain, A - decrease VGain");
                Console.WriteLine("W - increase IGain, S - decrease IGain");
                Console.WriteLine("Shift - adjust by 1000 units, Alt - adjust by 10 units, no key - 100 units");

                if (Console.KeyAvailable && emeter.VGain > 0 && emeter.IGain > 0)
                {
                    var key = Console.ReadKey(true);

                    int adjustment = 100;
                    if ((key.Modifiers & ConsoleModifiers.Shift) == ConsoleModifiers.Shift)
                    {
                        adjustment = 1000;
                    }
                    if ((key.Modifiers & ConsoleModifiers.Alt) == ConsoleModifiers.Alt)
                    {
                        adjustment = 10;
                    }

                    switch (key.Key)
                    {
                    case ConsoleKey.Q:
                        Console.WriteLine($"Adjusting VGain by +{adjustment}...");
                        emeter.SetGain(emeter.VGain + adjustment, emeter.IGain);
                        break;

                    case ConsoleKey.A:
                        Console.WriteLine($"Adjusting VGain by -{adjustment}...");
                        emeter.SetGain(emeter.VGain - adjustment, emeter.IGain);
                        break;

                    case ConsoleKey.W:
                        Console.WriteLine($"Adjusting IGain by +{adjustment}...");
                        emeter.SetGain(emeter.VGain, emeter.IGain + adjustment);
                        break;

                    case ConsoleKey.S:
                        Console.WriteLine($"Adjusting IGain by -{adjustment}...");
                        emeter.SetGain(emeter.VGain, emeter.IGain - adjustment);
                        break;
                    }
                    Console.Clear();
                }

                Thread.Sleep(100);
            }
        }