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()); }
public override async Task <IList <NodeInstance> > Scan() { var ret = new List <NodeInstance>(); var gateways = await IkeaTradfriDriver.Discover(); foreach (var gw in gateways) { var node = DriverContext.NodeTemplateFactory.CreateNodeInstance(IkeaTradfriFactory.GatewayGuid); node.Name = gw.Item1; var id = node.GetProperty(IkeaTradfriFactory.IdAddressPropertyKey); id.Value = gw.Item1; ret.Add(node); } return(ret); }
static async Task Main(string[] args) { Console.WriteLine("Hello World!"); var gateways = await IkeaTradfriDriver.Discover(); var appName = $"45df1d511"; var con = new IkeaTradfriDriver("192.168.8.105", appName, "xAWniaZm74vIhEdZ"); con.Connect(); Console.WriteLine("Conncted"); var devices = con.LoadDevices(); foreach (var dev in devices) { var deviceType = TradfriDeviceType.LightControl; if (dev.ApplicationType == DeviceType.PowerOutlet) { continue; } else if (dev.ApplicationType == DeviceType.Remote || dev.ApplicationType == DeviceType.Unknown) { continue; } con.RegisterChange(token => { Console.WriteLine($"Item {dev.Name} sent {token}"); if (token is JArray array) { var valueProp = ((int)TradfriConstAttribute.LightColorHex).ToString(); var strValue = array.First()[valueProp].ToString(); } }, deviceType, dev.Id); } Console.ReadLine(); }