Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        /// <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);
        }