public void Update(IDeviceState state, bool fromDatabase = false) { //TODO: update more properties? if (!fromDatabase) { if (Name == null && state.Name != null) { Name = state.Name; } if ((Type == null || Type.Equals(DeviceType.Unknown)) && state.Type != null) { Type = state.Type; } } Location = (state.Location == null) ? null : new Location(state.Location.GetParts()); IsConnected = state.IsConnected; CurrentAction = state.CurrentAction; BinarySwitch.Update(state.BinarySwitchState ?? ReadOnlyBinarySwitchSwitchState.Blank()); MultilevelSwitch.Update(state.MultilevelSwitchState ?? ReadOnlyMultilevelSwitchState.Blank()); ColorSwitch.Update(state.ColorSwitchState ?? ReadOnlyColorSwitchState.Blank()); BinarySensor.Update(state.BinarySensorState ?? ReadOnlyBinarySensorState.Blank()); PowerSensor.Update(state.PowerSensorState ?? ReadOnlyMultilevelSensorState <IPower> .Blank()); TemperatureSensor.Update(state.TemperatureSensorState ?? ReadOnlyMultilevelSensorState <ITemperature> .Blank()); HumiditySensor.Update(state.HumiditySensorState ?? ReadOnlyMultilevelSensorState <IHumidity> .Blank()); IlluminanceSensor.Update(state.IlluminanceSensorState ?? ReadOnlyMultilevelSensorState <IIlluminance> .Blank()); Thermostat.Update(state.ThermostatState ?? ReadOnlyThermostatState.Blank()); }
//TODO: unit test this public static ReadOnlyDeviceState FromXElement(XElement element) { var name = element.GetAttributeStringValue("Name"); var notes = element.GetAttributeStringValue("Notes"); var address = element.GetAttributeStringValue("Address"); var isConnected = element.GetAttributeBoolValue("IsConnected"); var type = element.GetAttributeStringValue("Type"); var currentAction = element.GetAttributeStringValue("CurrentAction"); var locationName = element.GetAttributeStringValue("Location"); IBinarySwitchState toggleSwitch = null; var toggleSwitchElement = element.Element("ToggleSwitch"); if (toggleSwitchElement != null) { toggleSwitch = toggleSwitchElement.ToToggleSwitch(); } IMultilevelSwitchState dimmerSwitch = null; var dimmerSwitchElement = element.Element("DimmerSwitch"); if (dimmerSwitchElement != null) { dimmerSwitch = dimmerSwitchElement.ToDimmerSwitch(); } IColorSwitchState colorSwitch = null; var colorSwitchElement = element.Element("ColorSwitch"); if (colorSwitchElement != null) { colorSwitch = colorSwitchElement.ToColorSwitch(); } IBinarySensorState binarySensor = null; var binarySensorElement = element.Element("BinarySensor"); if (binarySensorElement != null) { binarySensor = binarySensorElement.ToBinarySensor(); } ReadOnlyMultilevelSensorState <IPower> powerSensor = null; var powerSensorElement = element.Element("PowerSensor"); if (powerSensorElement != null) { powerSensor = powerSensorElement.ToMultilevelSensor <IPower>(); } ReadOnlyMultilevelSensorState <ITemperature> temperatureSensor = null; var temperatureSensorElement = element.Element("TemperatureSensor"); if (temperatureSensorElement != null) { temperatureSensor = temperatureSensorElement.ToMultilevelSensor <ITemperature>(); } ReadOnlyMultilevelSensorState <IHumidity> humiditySensor = null; var humiditySensorElement = element.Element("HumiditySensor"); if (humiditySensorElement != null) { humiditySensor = humiditySensorElement.ToMultilevelSensor <IHumidity>(); } ReadOnlyMultilevelSensorState <IIlluminance> illuminanceSensor = null; var illuminanceSensorElement = element.Element("IlluminanceSensor"); if (illuminanceSensorElement != null) { illuminanceSensor = illuminanceSensorElement.ToMultilevelSensor <IIlluminance>(); } IThermostatState thermostat = null; var thermostatElement = element.Element("Thermostat"); if (thermostatElement != null) { thermostat = thermostatElement.ToThermostat(); } IKeypadState keypad = null; var keypadElement = element.Element("Keypad"); if (keypadElement != null) { keypad = keypadElement.ToKeypad(); } var result = new ReadOnlyDeviceState { Name = name, Address = address, Location = new Location(locationName), IsConnected = isConnected, Type = DeviceType.GetTypeFromString(type), CurrentAction = currentAction, BinarySwitchState = toggleSwitch, MultilevelSwitchState = dimmerSwitch, ColorSwitchState = colorSwitch, BinarySensorState = binarySensor, PowerSensorState = powerSensor, TemperatureSensorState = temperatureSensor, HumiditySensorState = humiditySensor, IlluminanceSensorState = illuminanceSensor, ThermostatState = thermostat, KeypadState = keypad }; return(result); }
public static IDeviceState GenerateExampleDevice(DeviceType type, bool includeCurrentAction, bool includeToggle, bool includeDimmer, bool includeColorSwitch, bool includeBinarySensor, bool includePowerSensor, bool includeTemperatureSensor, bool includeHumiditySensor, bool includeIlluminanceSensor, bool includeThermostat, bool includeKeypad) { string currentAction = "Idle"; var toggle = new ReadOnlyBinarySwitchSwitchState(BinarySwitchPower.On); var dimmer = new ReadOnlyMultilevelSwitchState(25, 100); var colorSwitch = new ReadOnlyColorSwitchState(new NamedColor("Purple")); var binarySensor = new ReadOnlyBinarySensorState(BinarySensorType.Motion, true, DateTime.UtcNow.AddMinutes(-4)); var powerSensor = new ReadOnlyMultilevelSensorState<IPower>(new WattsPower(25), DateTime.UtcNow.AddSeconds(-2)); var temperatureSensor = new ReadOnlyMultilevelSensorState<ITemperature>(new CelsiusTemperature(3), DateTime.UtcNow.AddSeconds(-1)); var humiditySensor = new ReadOnlyMultilevelSensorState<IHumidity>(new RelativeHumidity(25), DateTime.UtcNow.AddSeconds(-5)); var illuminanceSensor = new ReadOnlyMultilevelSensorState<IIlluminance>(new LuxIlluminance(50), DateTime.UtcNow.AddSeconds(-4)); var thermostatCoreModes = new[] { ThermostatMode.Auto, ThermostatMode.Cool, ThermostatMode.Heat, ThermostatMode.FanOnly, ThermostatMode.Off }; var thermostatCore = new ReadOnlyThermostatCoreState(thermostatCoreModes, ThermostatMode.Cool, ThermostatCurrentAction.Cooling); var thermostatFanModes = new[] { ThermostatFanMode.Auto, ThermostatFanMode.On }; var thermostatFan = new ReadOnlyThermostatFanState(thermostatFanModes, ThermostatFanMode.Auto, ThermostatFanCurrentAction.On); var thermostatSetpoints = new ReadOnlyThermostatSetpointCollection(new Dictionary<ThermostatSetpointType, ITemperature> { {ThermostatSetpointType.Cool, new FahrenheitTemperature(74)}, {ThermostatSetpointType.Heat, new FahrenheitTemperature(70)} }); var thermostat = new ReadOnlyThermostatState(thermostatCore, thermostatFan, thermostatSetpoints); var keypadButtons = new [] { new ReadOnlyKeypadButtonState("1", false), new ReadOnlyKeypadButtonState("2", true), new ReadOnlyKeypadButtonState("3", null) }; var keypad = new ReadOnlyKeypadState(keypadButtons); var location = new Location("Here"); var address = _id.ToString(); _id++; if (!includeCurrentAction) { currentAction = null; } if (!includeToggle) { toggle = null; } if (!includeDimmer) { dimmer = null; } if (!includeColorSwitch) { colorSwitch = null; } if (!includeBinarySensor) { binarySensor = null; } if (!includePowerSensor) { powerSensor = null; } if (!includeTemperatureSensor) { temperatureSensor = null; } if (!includeHumiditySensor) { humiditySensor = null; } if (!includeIlluminanceSensor) { illuminanceSensor = null; } if (!includeThermostat) { thermostat = null; } if (!includeKeypad) { keypad = null; } var device = new ReadOnlyDeviceState("Sample Device", address, location, null, true, type, currentAction, toggle, dimmer, colorSwitch, binarySensor, powerSensor, temperatureSensor, humiditySensor, illuminanceSensor, thermostat, keypad); return device; }
public static IDeviceState GenerateExampleDevice(DeviceType type, bool includeCurrentAction, bool includeToggle, bool includeDimmer, bool includeColorSwitch, bool includeBinarySensor, bool includePowerSensor, bool includeTemperatureSensor, bool includeHumiditySensor, bool includeIlluminanceSensor, bool includeThermostat, bool includeKeypad) { string currentAction = "Idle"; var toggle = new ReadOnlyBinarySwitchSwitchState(BinarySwitchPower.On); var dimmer = new ReadOnlyMultilevelSwitchState(25, 100); var colorSwitch = new ReadOnlyColorSwitchState(new NamedColor("Purple")); var binarySensor = new ReadOnlyBinarySensorState(BinarySensorType.Motion, true, DateTime.UtcNow.AddMinutes(-4)); var powerSensor = new ReadOnlyMultilevelSensorState <IPower>(new WattsPower(25), DateTime.UtcNow.AddSeconds(-2)); var temperatureSensor = new ReadOnlyMultilevelSensorState <ITemperature>(new CelsiusTemperature(3), DateTime.UtcNow.AddSeconds(-1)); var humiditySensor = new ReadOnlyMultilevelSensorState <IHumidity>(new RelativeHumidity(25), DateTime.UtcNow.AddSeconds(-5)); var illuminanceSensor = new ReadOnlyMultilevelSensorState <IIlluminance>(new LuxIlluminance(50), DateTime.UtcNow.AddSeconds(-4)); var thermostatCoreModes = new[] { ThermostatMode.Auto, ThermostatMode.Cool, ThermostatMode.Heat, ThermostatMode.FanOnly, ThermostatMode.Off }; var thermostatCore = new ReadOnlyThermostatCoreState(thermostatCoreModes, ThermostatMode.Cool, ThermostatCurrentAction.Cooling); var thermostatFanModes = new[] { ThermostatFanMode.Auto, ThermostatFanMode.On }; var thermostatFan = new ReadOnlyThermostatFanState(thermostatFanModes, ThermostatFanMode.Auto, ThermostatFanCurrentAction.On); var thermostatSetpoints = new ReadOnlyThermostatSetpointCollection(new Dictionary <ThermostatSetpointType, ITemperature> { { ThermostatSetpointType.Cool, new FahrenheitTemperature(74) }, { ThermostatSetpointType.Heat, new FahrenheitTemperature(70) } }); var thermostat = new ReadOnlyThermostatState(thermostatCore, thermostatFan, thermostatSetpoints); var keypadButtons = new [] { new ReadOnlyKeypadButtonState("1", false), new ReadOnlyKeypadButtonState("2", true), new ReadOnlyKeypadButtonState("3", null) }; var keypad = new ReadOnlyKeypadState(keypadButtons); var location = new Location("Here"); var address = _id.ToString(); _id++; if (!includeCurrentAction) { currentAction = null; } if (!includeToggle) { toggle = null; } if (!includeDimmer) { dimmer = null; } if (!includeColorSwitch) { colorSwitch = null; } if (!includeBinarySensor) { binarySensor = null; } if (!includePowerSensor) { powerSensor = null; } if (!includeTemperatureSensor) { temperatureSensor = null; } if (!includeHumiditySensor) { humiditySensor = null; } if (!includeIlluminanceSensor) { illuminanceSensor = null; } if (!includeThermostat) { thermostat = null; } if (!includeKeypad) { keypad = null; } var device = new ReadOnlyDeviceState("Sample Device", address, location, null, true, type, currentAction, toggle, dimmer, colorSwitch, binarySensor, powerSensor, temperatureSensor, humiditySensor, illuminanceSensor, thermostat, keypad); return(device); }