public async Task TestReadProperty(string property) { Assert.True(BControl.IsProperty(property)); var status = await _bcontrol.ReadPropertyAsync(property); Assert.True(status.IsGood); }
/// <summary> /// Method to run when command is executed. /// </summary> /// <returns>Zero if ok.</returns> public async Task <int> OnExecuteAsync(CommandLineApplication app) { try { if (CheckOptions(app)) { // Overriding BControl options. _bcontrol.TcpSlave.Address = Parent.Address; _bcontrol.TcpSlave.Port = Parent.Port; _bcontrol.TcpSlave.ID = Parent.SlaveID; if (Property.Length == 0) { Console.WriteLine($"Reading all data from BControl energy meter."); DataStatus status; if (OptionB) { status = await _bcontrol.ReadBlockAllAsync(); } else { status = await _bcontrol.ReadAllAsync(); } if (status.IsGood) { Console.WriteLine($"BControl: {JsonConvert.SerializeObject(_bcontrol, Formatting.Indented)}"); } else { Console.WriteLine($"Error reading data from BControl energy meter."); Console.WriteLine($"Reason: {status.Explanation}."); } } else if (Property.Length > 0) { Console.WriteLine($"Reading property '{Property}' from BControl energy meter:"); await _bcontrol.ReadPropertyAsync(Property); if (_bcontrol.Data.Status.IsGood) { Console.WriteLine($"Value of property '{Property}' = {_bcontrol.Data.GetPropertyValue(Property)}"); } else { Console.WriteLine($"Error reading property '{Property}' from BControl energy meter."); Console.WriteLine($"Reason: {_bcontrol.Data.Status.Explanation}."); } } } } catch (Exception ex) { _logger?.LogError(ex, $"Exception ReadCommand."); return(-1); } return(0); }
public async Task <IActionResult> GetBControlData(string name, bool update = false) { if (string.IsNullOrEmpty(name)) { _logger?.LogDebug($"GetBControlData() invalid property."); return(StatusCode(StatusCodes.Status400BadRequest, $"Property is invalid.")); } try { _logger?.LogDebug($"GetBControlData({name})..."); if (BControlData.IsProperty(name)) { if (update) { if (BControlData.IsReadable(name)) { if (!_bcontrol.IsLocked) { return(StatusCode(StatusCodes.Status406NotAcceptable, "Locked: update not yet finished.")); } var status = await _bcontrol.ReadPropertyAsync(name); if (!status.IsGood) { return(StatusCode(StatusCodes.Status502BadGateway, status)); } } else { _logger?.LogDebug($"GetBControlData('{name}') property not readable."); return(StatusCode(StatusCodes.Status405MethodNotAllowed, $"Property '{name}' not readable.")); } } return(Ok(_bcontrol.GetPropertyValue(name))); } else { _logger?.LogDebug($"GetBControlData('{name}') property not found."); return(NotFound($"Property '{name}' not found.")); } } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } }