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); }
public static async Task <NodeDeviceCommands> GetCommand(this NodeDevice device, DeviceUpdate update) { if (!ZWaveCommands.RosieCommandsToZwaveDictionary.TryGetValue(update.PropertyKey, out var commandId)) { throw new NotSupportedException($"The following key is not supported in Zwave: {update.PropertyKey}"); } var data = commandId.Split('-'); var classId = int.Parse(data[0]); var index = int.Parse(data[1]); var commands = await NodeDatabase.Shared.GetDeviceCommands(device.NodeId); var matchingCommand = commands.FirstOrDefault(x => x.ClassId == classId && x.Index == index); if (matchingCommand != null) { return(matchingCommand); } //Multi level switches don't have switch state if (update.PropertyKey == DevicePropertyKey.SwitchState) { matchingCommand = commands.FirstOrDefault(x => x.ClassId == 38 && x.Index == 0); } if (matchingCommand != null) { return(matchingCommand); } throw new Exception($"Device doesn't support Command: {update.PropertyKey}"); }
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); }
static string DecentName(NodeDevice device) { if (!string.IsNullOrWhiteSpace(device.Name)) { return(device.Name); } if (!string.IsNullOrEmpty(device.Manufacturer)) { return($"{device.Manufacturer} {device.Type}"); } return(null); }
internal static string GetDeviceType(this NodeDevice device) { var type = device.Type; switch (type) { case "Binary Power Switch": case "Secure Keypad Door Lock": return(DeviceTypeKeys.Switch); case "Multilevel Scene Switch": return(DeviceTypeKeys.DimmerSwitch); } return("Unknown"); }
internal async Task InsertDevice(NodeDevice nodeDevice) { if (string.IsNullOrWhiteSpace(nodeDevice.PerferedCommand)) { var oldDevice = await GetDevice(nodeDevice.NodeId); if (oldDevice != null) { nodeDevice.PerferedCommand = oldDevice.PerferedCommand; } } await DatabaseConnection.InsertOrReplaceAsync(nodeDevice); var nodeValues = nodeDevice.Classes?.SelectMany(x => x.Value.Select(y => y.Value)).ToList(); var commands = nodeValues?.Select(x => new NodeCommand { ClassId = x.ClassId, Index = x.Index, Instance = x.Instance, Genre = Enum.Parse <CommandGenre> (x.Genre), Description = x.Label }).ToList(); var grouped = nodeValues?.Select(x => new NodeDeviceCommands { ClassId = x.ClassId, Index = x.Index, Instance = x.Instance, NodeId = x.NodeId, Help = x.Help, Description = x.Label, IsReadOnly = x.ReadOnly, IsWriteOnly = x.WriteOnly, Max = x.Max, Min = x.Min, Units = x.Units, Values = x.Values?.ToJson(), Genre = Enum.Parse <CommandGenre> (x.Genre), }).ToList(); if (commands != null) { await DatabaseConnection.InsertOrReplaceAllAsync(commands); } if (grouped != null) { await DatabaseConnection.InsertOrReplaceAllAsync(grouped); } //var commands = nodeDevice.Classes.Select(x=> new NodeCommand{CommandId = x.Key, }
public static bool Update(this Device device, NodeDevice nodeDevice) { bool didChange = false; device.ServiceDeviceId = nodeDevice.NodeId.ToString(); if (device.Description != nodeDevice.Name) { device.Description = nodeDevice.Name; didChange = true; } if (device.Location != nodeDevice.Loc) { device.Location = nodeDevice.Loc; didChange = true; } if (device.Manufacturer != nodeDevice.Manufacturer) { device.Manufacturer = nodeDevice.Manufacturer; didChange = true; } if (device.ManufacturerId != nodeDevice.ManufacturerId) { device.ManufacturerId = nodeDevice.ManufacturerId; didChange = true; } if (device.Product != nodeDevice.Product) { device.Product = nodeDevice.Product; didChange = true; } if (device.ProductType != nodeDevice.ProductType) { device.ProductType = nodeDevice.ProductType; didChange = true; } if (device.ProductId != nodeDevice.ProductId) { device.ProductId = nodeDevice.ProductId; didChange = true; } var decentName = nodeDevice.DecentName(); if (device.Name != decentName) { device.Name = decentName; didChange = true; } if (device.Type != nodeDevice.Type) { device.Type = nodeDevice.Type; didChange = true; } var deviceType = nodeDevice.GetDeviceType(); if (device.DeviceType != deviceType) { device.DeviceType = deviceType; didChange = true; } return(didChange || string.IsNullOrWhiteSpace(device.Id)); }