async Task AddDevice(NodeDevice nodeDevice) { var nodeId = nodeDevice.Classes.FirstOrDefault().Value?.FirstOrDefault().Value?.NodeId ?? -1; if (nodeId <= 0) { return; } var device = new Device { Id = nodeDevice.Id, Service = ServiceIdentifier, Description = nodeDevice.Name, Location = nodeDevice.Loc, Manufacturer = nodeDevice.Manufacturer, ManufacturerId = nodeDevice.ManufacturerId, Product = nodeDevice.Product, ProductType = nodeDevice.ProductType, ProductId = nodeDevice.ProductId, Name = DecentName(nodeDevice), Type = nodeDevice.Type, DeviceType = FromNodeType(nodeDevice.Type), }; device.Discoverable = !string.IsNullOrWhiteSpace(device.Name); await _deviceManager.AddDevice(device); nodeDevice.NodeId = nodeId; await NodeDatabase.Shared.InsertDevice(nodeDevice); }
async Task GetLights() { var existingLights = await _deviceManager.GetAllDevices(); var lights = await _hueClient.GetLightsAsync(); foreach (var light in lights) { var oldDevice = existingLights.FirstOrDefault(cw => cw.ServiceDeviceId == light.Id); if (oldDevice != null) { return; } var device = new Device { ServiceDeviceId = light.Id, Service = ServiceIdentifier, Description = light.Name, Manufacturer = light.ManufacturerName, ManufacturerId = light.ModelId, ProductId = light.ProductId, Name = light.Name, DeviceType = DeviceTypeKeys.Light, }; device.Discoverable = !string.IsNullOrWhiteSpace(device.Name); await _deviceManager.AddDevice(device); } }
async Task AddDevice(NodeDevice nodeDevice) { var nodeId = nodeDevice.Classes.FirstOrDefault().Value?.FirstOrDefault().Value?.NodeId ?? -1; if (nodeId <= 0) { return; } var oldNodeDevice = await NodeDatabase.Shared.GetDevice(nodeId); Device device = null; if (!string.IsNullOrWhiteSpace(oldNodeDevice?.Id)) { device = await _deviceManager.GetDevice(oldNodeDevice.Id); } if (device == null) { var oldDevices = await _deviceManager.GetAllDevices(); device = oldDevices.FirstOrDefault(x => x.Service == this.ServiceIdentifier && x.ServiceDeviceId == nodeId.ToString()) ?? new Device { Service = ServiceIdentifier, Id = oldNodeDevice?.Id }; } if (!device.Update(nodeDevice)) { return; } device.Discoverable = !string.IsNullOrWhiteSpace(device.Name); await _deviceManager.AddDevice(device); nodeDevice.Id = device.Id; await NodeDatabase.Shared.InsertDevice(nodeDevice); }
async Task GetLights() { var lights = await _hueClient.GetLightsAsync(); foreach (var light in lights) { var device = new Device { Id = light.Id, Service = ServiceIdentifier, Description = light.Name, Manufacturer = light.ManufacturerName, ManufacturerId = light.ModelId, ProductId = light.ProductId, Name = light.Name, DeviceType = DeviceType.Light, }; device.Discoverable = !string.IsNullOrWhiteSpace(device.Name); await _deviceManager.AddDevice(device); } }