Beispiel #1
0
        public async Task SendCommandAsync()
        {
            string value;

            if (ProtocolCommand.Options is StringOptions options)
            {
                value = options.GetTermForValue(this.Value.ToString());
            }
            else if (Value is bool b)
            {
                value = b ? "1" : "0";
            }
            else
            {
                value = $"{Value}";
            }
            var path = ProtocolCommand.FormatPath();

            if (value.Length > 0)
            {
                await AppLocator.TelnetService.SendCommandAsync($"{path} {value}", false);
            }
            else
            {
                await AppLocator.TelnetService.SendCommandAsync(path, false);
            }
        }
Beispiel #2
0
        public async Task SendQueryAsync()
        {
            IsEnabled = false;
            var path    = ProtocolCommand.FormatPath();
            var command = $"{path}?";

            var result = "";

            if (App.Mock)
            {
                await Task.Delay(1);

                result = DefaultResponse;
            }
            else
            {
                var response = await AppLocator.TelnetService.SendCommandAsync(command, true);

                // remove line terminator
                result = response?.TrimEnd();
            }
            if (result.Length == 0)
            {
                return;
            }

            if (result == "Command error")
            {
                throw new InvalidOperationException($"Command error: {command}");
            }

            var propInfo = _propInfo ?? this.GetType().GetProperty(nameof(Value));

            if (_propInfo == null)
            {
                _propInfo = propInfo;
            }
            var type = typeof(T);

            if (type == typeof(string) || type == typeof(StringOption))
            {
                var value = StringOptions.GetByTerm(ProtocolCommand.Options, result);
                propInfo.SetValue(this, $"{value}");
            }
            else if (type == typeof(bool))
            {
                propInfo.SetValue(this, result == "1");
            }
            else if (type == typeof(int) || type == typeof(IntegerOption))
            {
                if (int.TryParse(result, out var val))
                {
                    propInfo.SetValue(this, val);
                }
            }
            else if (type == typeof(double) || type == typeof(RealOption))
            {
                if (double.TryParse(result, out var val))
                {
                    propInfo.SetValue(this, val);
                }
            }
            IsEnabled = true;
            if (!ProtocolCommand.Name.Contains("Status"))
            {
                System.Diagnostics.Debug.WriteLine($"{ProtocolCommand.Name}: {Value}");
            }
        }