public static IHostBuilder CreateHostBuilder(string[] args) { return(Host.CreateDefaultBuilder(args) .ConfigureServices(/*async*/ (hostContext, services) => { string login = Environment.GetEnvironmentVariable("tplink_powerline_login", EnvironmentVariableTarget.User); string password = Environment.GetEnvironmentVariable("tplink_powerline_pwd", EnvironmentVariableTarget.User); // note: ensure the vpn is turned off / net // can also be checked here: Control Panel\Network and Internet\Network Connections // find ip of the powerline | if you have vpn or several adapter, make sure this is sending dicovery packet to // the network where powerline is connected to string ip = TpLinkClient.DiscoveryAsync().GetAwaiter().GetResult(); // NOTE: using async here may break the DI pattern and throw CreateHostBuilder(args).Build().Run(); // won't work (thread problem) //string ip = await TpLinkClient.DiscoveryAsync(); Console.WriteLine($"found ip: {ip}"); services.AddHostedService <Worker>(); //services.AddSingleton<IRestClient, RestClient>(); services.AddSingleton <ITpLinkClient>(new TpLinkClient(login, password, $"http://{ip}/")); })); }
public async Task DiscoverAsync() { string login = Environment.GetEnvironmentVariable("tplink_powerline_login", EnvironmentVariableTarget.User); string pwd = Environment.GetEnvironmentVariable("tplink_powerline_pwd", EnvironmentVariableTarget.User); string endpoint = await TpLinkClient.DiscoveryAsync(); powerLine = new TpLinkClient(login, pwd, $"http://{endpoint}"); }
static void Main(string[] args) { Console.WriteLine("Press `ESC` to exit..."); var username = Environment.GetEnvironmentVariable("ROUTER_USERNAME"); var password = Environment.GetEnvironmentVariable("ROUTER_PASSWORD"); var previousReceivedBytes = default(long); var previousSentBytes = default(long); using (var client = new TpLinkClient("http://192.168.0.1")) { client.Connect(username, password); var commands = new TpLinkCommands(client); do { while (!Console.KeyAvailable) { var status = commands.GetCurrentStatus(); if (previousReceivedBytes == default(int) && previousSentBytes == default(int)) { previousReceivedBytes = status.TrafficStatistics.Received.Bytes; previousSentBytes = status.TrafficStatistics.Sent.Bytes; } else { var currentReceivedBytes = status.TrafficStatistics.Received.Bytes; var currentSentBytes = status.TrafficStatistics.Sent.Bytes; var receiveRate = (((currentReceivedBytes - previousReceivedBytes) / 2) * 8) / 1000000f; var sendRate = (((currentSentBytes - previousSentBytes) / 2) * 8) / 1000000f; Console.WriteLine($"\rReceive Rate: {receiveRate.ToString("0.000")}Mbps | Sent Rate: {sendRate.ToString("0.000")}Mbps"); previousReceivedBytes = currentReceivedBytes; previousSentBytes = currentSentBytes; } Thread.Sleep(2000); } } while (Console.ReadKey(true).Key != ConsoleKey.Escape); } }