/// <summary> /// Helper method to check options. /// </summary> /// <param name="app"></param> /// <returns>True if options are OK.</returns> private bool CheckOptions(CommandLineApplication app) { if (Property.Length > 0) { if (!KWLEC200Data.IsProperty(Property)) { _logger?.LogError($"The property '{Property}' has not been found."); return(false); } if (!KWLEC200Data.IsWritable(Property)) { _logger?.LogError($"The property '{Property}' is not writable."); return(false); } if (string.IsNullOrEmpty(Value)) { _logger?.LogError($"The value '{Value}' for the property '{Property}' is invalid."); return(false); } } return(true); }
public void TestKWLEC200ReadProperty(string property) { Assert.True(KWLEC200Data.IsProperty(property)); Assert.True(KWLEC200Data.IsReadable(property)); var status = _kwlec200.ReadProperty(property); Assert.True(status.IsGood); }
// SensorConfig1 is not writable. // SensorConfig2 is not writable. // SensorConfig3 is not writable. // SensorConfig4 is not writable. // SensorConfig5 is not writable. // SensorConfig6 is not writable. // SensorConfig7 is not writable. // SensorConfig8 is not writable. public void TestKWLEC200WriteProperty(string property, string data) { Assert.True(KWLEC200Data.IsProperty(property)); Assert.True(KWLEC200Data.IsWritable(property)); var status = _kwlec200.WriteProperty(property, data); Assert.True(status.IsGood); }
public IActionResult PutKWLEC200Data(string name, [FromQuery] string value) { if (string.IsNullOrEmpty(name)) { _logger?.LogDebug($"PutKWLEC200Data({name}, {value}) invalid property."); return(StatusCode(StatusCodes.Status400BadRequest, $"Property name is invalid.")); } if (string.IsNullOrEmpty(value)) { _logger?.LogDebug($"PutKWLEC200Data({name}, {value}) invalid value."); return(StatusCode(StatusCodes.Status400BadRequest, $"Property value is invalid.")); } try { _logger?.LogDebug($"PutKWLEC200Data({name}, {value})..."); if (KWLEC200Data.IsProperty(name)) { if (KWLEC200Data.IsWritable(name)) { if (!_kwlec200.IsLocked) { return(StatusCode(StatusCodes.Status406NotAcceptable, "Locked: update not yet finished.")); } var status = _kwlec200.WriteProperty(name, value); if (!status.IsGood) { return(StatusCode(StatusCodes.Status502BadGateway, status)); } return(Ok()); } else { _logger?.LogDebug($"PutKWLEC200Data('{name}, {value}') property not writable."); return(StatusCode(StatusCodes.Status405MethodNotAllowed, $"Property '{name}' not writable.")); } } else { _logger?.LogDebug($"PutKWLEC200Data('{name}, {value}') property not found."); return(NotFound($"Property '{name}' not found.")); } } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } }
public IActionResult GetKWLEC200Data(string name, bool update = false) { if (string.IsNullOrEmpty(name)) { _logger?.LogDebug($"GetKWLEC200Data() invalid property."); return(StatusCode(StatusCodes.Status400BadRequest, $"Property is invalid.")); } try { _logger?.LogDebug($"GetKWLEC200Data({name})..."); if (KWLEC200Data.IsProperty(name)) { if (update) { if (KWLEC200Data.IsReadable(name)) { if (!_kwlec200.IsLocked) { return(StatusCode(StatusCodes.Status406NotAcceptable, "Locked: update not yet finished.")); } var status = _kwlec200.ReadProperty(name); if (!status.IsGood) { return(StatusCode(StatusCodes.Status502BadGateway, status)); } } else { _logger?.LogDebug($"GetKWLEC200Data('{name}') property not readable."); return(StatusCode(StatusCodes.Status405MethodNotAllowed, $"Property '{name}' not readable.")); } } return(Ok(_kwlec200.GetPropertyValue(name))); } else { _logger?.LogDebug($"GetKWLEC200Data('{name}') property not found."); return(NotFound($"Property '{name}' not found.")); } } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } }
/// <summary> /// Helper method to check options. /// </summary> /// <param name="app"></param> /// <returns>True if options are OK.</returns> private bool CheckOptions(CommandLineApplication app) { if (Property.Length > 0) { if (!KWLEC200Data.IsProperty(Property)) { _logger?.LogError($"The property '{Property}' has not been found."); return(false); } if (!KWLEC200Data.IsReadable(Property)) { _logger?.LogError($"The property '{Property}' is not readable."); return(false); } } return(true); }
/// <summary> /// Updates the Properties used in OverviewData. /// </summary> /// <param name="data">The KWLEC200 data.</param> public void Refresh(KWLEC200Data data) { if (data != null) { Date = data.Date; Time = data.Time; TemperatureChannel = data.TemperatureChannel; TemperatureExhaust = data.TemperatureExhaust; TemperatureExtract = data.TemperatureExtract; TemperatureOutdoor = data.TemperatureOutdoor; TemperaturePostHeater = data.TemperaturePostHeater; TemperaturePreHeater = data.TemperaturePreHeater; TemperatureSupply = data.TemperatureSupply; OperationMode = data.OperationMode; VentilationLevel = data.VentilationLevel; SupplyLevel = data.SupplyLevel; ExhaustLevel = data.ExhaustLevel; VentilationPercentage = data.VentilationPercentage; SupplyFanSpeed = data.SupplyFanSpeed; ExhaustFanSpeed = data.ExhaustFanSpeed; } Status = data?.Status ?? Uncertain; }
/// <summary> /// Helper method to read the property value from the storage. /// </summary> /// <param name="property">The property name.</param> /// <param name="command">The raw command string.</param> public void UpdateData(string property, string command) { if (!string.IsNullOrEmpty(property)) { if (KWLEC200Data.IsProperty(property)) { if (KWLEC200Data.IsWritable(property)) { object value = Data.GetPropertyValue(property); string name = KWLEC200Data.GetName(property); ushort size = KWLEC200Data.GetSize(property); ushort count = KWLEC200Data.GetCount(property); _logger?.LogDebug($"Property '{property}' => Type: {value.GetType()}, Name: {name}, Size: {size}, Count: {count}."); if (value is bool) { var(data, status) = Helios.ParseBoolData(name, size, count, command); if (status.IsGood) { Data.SetPropertyValue(property, data); } } else if (value is int) { var(data, status) = Helios.ParseIntegerData(name, size, count, command); if (status.IsGood) { Data.SetPropertyValue(property, data); } } else if (value is double) { var(data, status) = Helios.ParseDoubleData(name, size, count, command); if (status.IsGood) { Data.SetPropertyValue(property, data); } } else if (value is DateTime) { var(data, status) = Helios.ParseDateData(name, size, count, command); if (status.IsGood) { Data.SetPropertyValue(property, data); } } else if (value is TimeSpan) { var(data, status) = Helios.ParseTimeData(name, size, count, command); if (status.IsGood) { Data.SetPropertyValue(property, data); } } else if (value.GetType().IsEnum) { var(data, status) = Helios.ParseIntegerData(name, size, count, command); if (status.IsGood) { Data.SetPropertyValue(property, data); } } else if (value is string) { var(data, status) = Helios.ParseStringData(name, size, count, command); if (status.IsGood) { Data.SetPropertyValue(property, data); } } } else { _logger?.LogDebug($"UpdateData property '{property}' not writable."); } } else { _logger?.LogWarning($"UpdateData property '{property}' not found."); } } else { _logger?.LogError($"UpdateData property invalid."); } }
/// <summary> /// Helper method to write the property value to the storage. /// </summary> /// <param name="property">The property name.</param> private void ReturnData(string property) { if (!string.IsNullOrEmpty(property)) { if (KWLEC200Data.IsProperty(property)) { if (KWLEC200Data.IsReadable(property)) { object value = Data.GetPropertyValue(property); string name = KWLEC200Data.GetName(property); ushort size = KWLEC200Data.GetSize(property); ushort count = KWLEC200Data.GetCount(property); string text = string.Empty; _logger?.LogDebug($"Property '{property}' => Type: {value.GetType()}, Name: {name}, Size: {size}, Count: {count}."); if (value is bool) { text = Helios.GetBoolData(name, size, count, (bool)value); } else if (value is int) { text = Helios.GetIntegerData(name, size, count, (int)value); } else if (value is double) { text = Helios.GetDoubleData(name, size, count, (double)value); } else if (value is DateTime) { text = Helios.GetDateData(name, size, count, (DateTime)value); } else if (value is TimeSpan) { text = Helios.GetTimeData(name, size, count, (TimeSpan)value); } else if (((dynamic)value).GetType().IsEnum) { text = Helios.GetIntegerData(name, size, count, (int)value); } else if (value is string) { text = Helios.GetStringData(name, size, count, (string)value); } if (!string.IsNullOrEmpty(text)) { _pending = true; _storage.HoldingRegisters.WritePoints(OFFSET, text.ToRegisters()); _logger?.LogDebug($"Property '{property}' => '{text}'."); } } else { _logger?.LogDebug($"ReturnData property '{property}' not readable."); } } else { _logger?.LogWarning($"ReturnData property '{property}' not found."); } } else { _logger?.LogError($"ReturnData property invalid."); } }
/// <summary> /// /// </summary> /// <param name="data"></param> /// <param name="property"></param> /// <returns></returns> public DataStatus WriteProperty(KWLEC200Data data, string property) => _helios.WriteProperty(data, ModbusMaster, TcpSlave.ID, property);
/// <summary> /// Helper method to write the property to the KWLEC200. /// </summary> /// <param name="data">The KWLEC200 data.</param> /// <param name="modbus">The Modbus device.</param> /// <param name="slave">The slave ID of the Modbus device.</param> /// <param name="property">The property name.</param> /// <returns>The status indicating success or failure.</returns> public DataStatus WriteProperty(KWLEC200Data data, IModbusMaster modbus, byte slave, string property) { DataStatus status = Good; try { dynamic value = data.GetPropertyValue(property); string name = KWLEC200Data.GetName(property); ushort size = KWLEC200Data.GetSize(property); ushort count = KWLEC200Data.GetCount(property); if (value is bool) { _logger?.LogDebug($"WriteProperty property '{property}' => Type: {value.GetType()}, Value: {value}, Name: {name}, Size: {size}, Count: {count}."); WriteBoolDataAsync(modbus, slave, name, size, count, (bool)value).Wait(); } else if (value is int) { _logger?.LogDebug($"WriteProperty property '{property}' => Type: {value.GetType()}, Value: {value}, Name: {name}, Size: {size}, Count: {count}."); WriteIntegerDataAsync(modbus, slave, name, size, count, (int)value).Wait(); } else if (value is double) { _logger?.LogDebug($"WriteProperty property '{property}' => Type: {value.GetType()}, Value: {value}, Name: {name}, Size: {size}, Count: {count}."); WriteDoubleDataAsync(modbus, slave, name, size, count, (double)value).Wait(); } else if (value is DateTime) { _logger?.LogDebug($"WriteProperty property '{property}' => Type: {value.GetType()}, Value: {((DateTime)value).Date}, Name: {name}, Size: {size}, Count: {count}."); WriteDateDataAsync(modbus, slave, name, size, count, (DateTime)value).Wait(); } else if (value is TimeSpan) { _logger?.LogDebug($"WriteProperty property '{property}' => Type: {value.GetType()}, Value: {value}, Name: {name}, Size: {size}, Count: {count}."); WriteTimeDataAsync(modbus, slave, name, size, count, (TimeSpan)value).Wait(); } else if (value.GetType().IsEnum) { _logger?.LogDebug($"WriteProperty property '{property}' => Type: {value.GetType()}, Value: {value}, Name: {name}, Size: {size}, Count: {count}."); WriteIntegerDataAsync(modbus, slave, name, size, count, (int)value).Wait(); } else if (value is string) { _logger?.LogDebug($"WriteProperty property '{property}' => Type: {value.GetType()}, Value: {value}, Name: {name}, Size: {size}, Count: {count}."); WriteStringDataAsync(modbus, slave, name, size, count, (string)value).Wait(); } } catch (ArgumentNullException anx) { _logger?.LogError(anx, $"ArgumentNullException in WriteProperty property '{property}'."); status = BadOutOfRange; } catch (ArgumentOutOfRangeException aor) { _logger?.LogError(aor, $"ArgumentOutOfRangeException in WriteProperty property '{property}'."); status = BadOutOfRange; } catch (ArgumentException aex) { _logger?.LogError(aex, $"ArgumentException in WriteProperty property '{property}'."); status = BadOutOfRange; } catch (ObjectDisposedException odx) { _logger?.LogError(odx, $"ObjectDisposedException in WriteProperty property '{property}'."); status = BadInternalError; } catch (FormatException fex) { _logger?.LogError(fex, $"FormatException in WriteProperty property '{property}'."); status = BadEncodingError; } catch (IOException iox) { _logger?.LogError(iox, $"IOException in WriteProperty property '{property}'."); status = BadCommunicationError; } catch (InvalidModbusRequestException imr) { _logger?.LogError(imr, $"InvalidModbusRequestException in WriteProperty property '{property}'."); status = BadCommunicationError; } catch (InvalidOperationException iox) { _logger?.LogError(iox, $"InvalidOperationException in WriteProperty property '{property}'."); status = BadInternalError; } catch (SlaveException slx) { _logger?.LogError(slx, $"SlaveException in WriteProperty property '{property}'."); status = BadDeviceFailure; } catch (Exception ex) { _logger?.LogError(ex, $"Exception in WriteProperty property '{property}'."); status = BadInternalError; } return(status); }
/// <summary> /// Helper method to read the property from the KWLEC200. /// </summary> /// <param name="data">The KWLEC200 data.</param> /// <param name="modbus">The Modbus device.</param> /// <param name="slave">The slave ID of the Modbus device.</param> /// <param name="property">The property name.</param> /// <returns>The status indicating success or failure.</returns> public DataStatus ReadProperty(KWLEC200Data data, IModbusMaster modbus, byte slave, string property) { DataStatus status = Good; try { object value = data.GetPropertyValue(property); string name = KWLEC200Data.GetName(property); ushort size = KWLEC200Data.GetSize(property); ushort count = KWLEC200Data.GetCount(property); _logger?.LogDebug($"ReadProperty property '{property}' => Type: {value.GetType()}, Name: {name}, Size: {size}, Count: {count}."); if (value is bool) { var result = ReadBoolData(modbus, slave, name, size, count); if (result.Status.IsGood) { data.SetPropertyValue(property, result.Data); _logger?.LogDebug($"ReadProperty boolean property '{property}' => Value: {result.Data}."); } status = result.Status; } else if (value is int) { var result = ReadIntegerData(modbus, slave, name, size, count); if (result.Status.IsGood) { data.SetPropertyValue(property, result.Data); _logger?.LogDebug($"ReadProperty integer property '{property}' => Value: {result.Data}."); } status = result.Status; } else if (value is double) { var result = ReadDoubleData(modbus, slave, name, size, count); if (result.Status.IsGood) { data.SetPropertyValue(property, result.Data); _logger?.LogDebug($"ReadProperty double property '{property}' => Value: {result.Data}."); } status = result.Status; } else if (value is DateTime) { var result = ReadDateData(modbus, slave, name, size, count); if (result.Status.IsGood) { data.SetPropertyValue(property, result.Data); _logger?.LogDebug($"ReadProperty datetime property '{property}' => Value: {result.Data.Date}."); } status = result.Status; } else if (value is TimeSpan) { var result = ReadTimeData(modbus, slave, name, size, count); if (result.Status.IsGood) { data.SetPropertyValue(property, result.Data); _logger?.LogDebug($"ReadProperty timespan property '{property}' => Value: {result.Data}."); } status = result.Status; } else if (((dynamic)value).GetType().IsEnum) { var result = ReadIntegerData(modbus, slave, name, size, count); if (result.Status.IsGood) { data.SetPropertyValue(property, result.Data); _logger?.LogDebug($"ReadProperty enum property '{property}' => Value: '{result.Data}'."); } status = result.Status; } else if (value is string) { var result = ReadStringData(modbus, slave, name, size, count); if (result.Status.IsGood) { data.SetPropertyValue(property, result.Data); _logger?.LogDebug($"ReadProperty string property '{property}' => Value: {result.Data}"); } status = result.Status; } } catch (ArgumentNullException anx) { _logger?.LogError(anx, $"ArgumentNullException in ReadProperty property '{property}'."); status = BadOutOfRange; } catch (ArgumentOutOfRangeException aor) { _logger?.LogError(aor, $"ArgumentOutOfRangeException in ReadProperty property '{property}'."); status = BadOutOfRange; } catch (ArgumentException aex) { _logger?.LogError(aex, $"ArgumentException in ReadAsync property '{property}'."); status = BadOutOfRange; } catch (ObjectDisposedException odx) { _logger?.LogError(odx, $"ObjectDisposedException in ReadProperty property '{property}'."); status = BadInternalError; } catch (FormatException fex) { _logger?.LogError(fex, $"FormatException in ReadProperty property '{property}'."); status = BadEncodingError; } catch (IOException iox) { _logger?.LogError(iox, $"IOException in ReadAsync property '{property}'."); status = BadCommunicationError; } catch (InvalidModbusRequestException imr) { _logger?.LogError(imr, $"InvalidModbusRequestException in ReadProperty property '{property}'."); status = BadCommunicationError; } catch (InvalidOperationException iox) { _logger?.LogError(iox, $"InvalidOperationException in ReadProperty property '{property}'."); status = BadInternalError; } catch (SlaveException slx) { _logger?.LogError(slx, $"SlaveException in ReadProperty property '{property}'."); status = BadDeviceFailure; } catch (Exception ex) { _logger?.LogError(ex, $"Exception in ReadProperty property '{property}'."); status = BadInternalError; } return(status); }