private async Task <T> Property <T>(object value = null, bool checkPrefix = true, [CallerMemberName] string name = null) { var stringValue = AlienValueConverter.ToAlienValueString(value); string response; if (string.IsNullOrEmpty(stringValue)) { response = await SendReceive($"{name}?"); } else { response = await SendReceive($"{name} = {stringValue}"); } var responsePrefix = $"{name} = "; if (checkPrefix && !response.StartsWith(responsePrefix)) { throw new Exception($"Unexcpected response: {response}"); } if (checkPrefix) { response = response.Substring(responsePrefix.Length); } return(AlienValueConverter.ToStrongType <T>(response.TrimEnd('\0', '\r', '\n'))); }
public void Object_to_alien_string() { AlienValueConverter.ToAlienValueString(true).ShouldBe("ON"); AlienValueConverter.ToAlienValueString(false).ShouldBe("OFF"); AlienValueConverter.ToAlienValueString(1.3d).ShouldBe("1.3"); AlienValueConverter.ToAlienValueString(1.3f).ShouldBe("1.3"); AlienValueConverter.ToAlienValueString("str").ShouldBe("str"); AlienValueConverter.ToAlienValueString(StringComparison.OrdinalIgnoreCase).ShouldBe("OrdinalIgnoreCase"); AlienValueConverter.ToAlienValueString(null).ShouldBeNull(); AlienValueConverter.ToAlienValueString(new IPEndPoint(IPAddress.Parse("10.0.0.59"), 7000)).ShouldBe("10.0.0.59:7000"); AlienValueConverter.ToAlienValueString(new DateTimeOffset(2018, 03, 10, 23, 02, 57, TimeSpan.Zero)).ShouldBe("2018/03/10 23:02:57"); }
public void Alien_string_to_object() { AlienValueConverter.ToStrongType <bool>("ON").ShouldBeTrue(); AlienValueConverter.ToStrongType <bool>("OFF").ShouldBeFalse(); AlienValueConverter.ToStrongType <float>("1.3").ShouldBe(1.3f); AlienValueConverter.ToStrongType <double>("1.3").ShouldBe(1.3d); AlienValueConverter.ToStrongType <string>("str").ShouldBe("str"); AlienValueConverter.ToStrongType <StringComparison>("OrdinalIgnoreCase") .ShouldBe(StringComparison.OrdinalIgnoreCase); var ep = AlienValueConverter.ToStrongType <IPEndPoint>("10.0.0.59:7000"); ep.Address.ShouldBe(IPAddress.Parse("10.0.0.59")); ep.Port.ShouldBe(7000); AlienValueConverter.ToStrongType <DateTimeOffset>("2018/03/10 23:02:57").ShouldBe(new DateTimeOffset(2018, 03, 10, 23, 02, 57, TimeSpan.Zero)); }
private async Task <T> Command <T>([CallerMemberName] string name = null) { return(AlienValueConverter.ToStrongType <T>(await SendReceive(name))); }