static async Task Main(string[] args) { Console.WriteLine("Hello World!"); // var gateways = await IkeaTradfriDriver.Discover(); var appName = Guid.NewGuid().ToString(); var key = IkeaTradfriDriver.GeneratePsk("192.168.8.103", appName, "Ej3Ta2AzrePZ9jcJ"); var con = new IkeaTradfriDriver("192.168.8.103", appName, key.PSK); await con.Connect(); Console.WriteLine("Conncted"); var devices = await con.LoadDevices(); await con.RegisterChange(token => { Console.WriteLine($"Item {token.Name} sent {token} {token.Control[0].State}"); }, 65539); while (true) { // await con.SwitchOff(65539); await Task.Delay(1000); } foreach (var dev in devices) { var deviceType = DeviceType.ControlOutlet; if (dev.DeviceType == DeviceType.ControlOutlet) { await con.SwitchOn(dev.ID); await con.SwitchOff(dev.ID); await con.SwitchOn(dev.ID); continue; } else if (dev.DeviceType == DeviceType.Remote) { continue; } await con.RegisterChange(token => { Console.WriteLine($"Item {dev.Name} sent {token}"); }, dev.ID); } Console.ReadLine(); }
public override async Task <bool> Start() { if (DriverContext.IsTest) { return(true); } if (String.IsNullOrEmpty(_id)) { DriverContext.Logger.LogInformation("Could not start driver, the gateway id is invalid"); return(false); } var scan = await IkeaTradfriDriver.Discover(); var gw = scan.Single(a => a.Item1 == _id.ToLowerInvariant()); if (gw == null) { DriverContext.Logger.LogInformation($"Could not find gateway with id {_id}"); return(false); } DriverContext.Logger.LogInformation($"Connecting to tradfri with ip {gw.Item2}"); await Task.Run(async() => { var conName = $"Automatica.Core-{DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds}"; if (String.IsNullOrEmpty(_appKey)) { var auth = IkeaTradfriDriver.GeneratePsk(gw.Item2, conName, _secret); if (auth == null) { DriverContext.Logger.LogError($"Could not generate psk key for tradfri {Name}"); return; } _appKey = auth.PSK; var prop = DriverContext.NodeInstance.GetProperty(IkeaTradfriFactory.ConnectionPropertyKey); DriverContext.NodeTemplateFactory.SetPropertyValue(prop.ObjId, _appKey); } Driver = new IkeaTradfriDriver(gw.Item2, conName, _appKey); await Driver.Connect(); }); return(await base.Start()); }