private async Task ToggleValue() { if (!Component.CType.IsAny(Models.Components.CType.Digital)) { return; } if (!(Component.Value is DigitalState)) { return; } try { CommandIssued = true; var val = (DigitalState)Component.Value; var result = await RemoteDevice.Send(Protocol.IODigitalWrite(ID, !val.Value)); ViewModelEventArgs e; if (result == Protocol.ActionSuccess) { e = ViewModelEventArgs.Information("Success"); } else { e = ViewModelEventArgs.Error("Toggling failed."); } FunctionExecuted?.Invoke(this, e); } catch { } finally { CommandIssued = false; } }
private async Task DeleteEvent() { bool success = false; try { CommandIssued = true; bool deleteConfirmed = await EventDeleting(); if (deleteConfirmed) { string response = await RemoteDevice.Send(Protocol.DeleteEvent(ID)); if (success = response == Protocol.ActionSuccess) { EventDeleted?.Invoke(this, ViewModelEventArgs.Information("Success")); } else { EventDeleted?.Invoke(this, ViewModelEventArgs.Error("The event was not deleted.")); } } } catch (Exception ex) { EventDeleted?.Invoke(this, ViewModelEventArgs.Error("Failed to delete the event. Reason:\r\n" + ex.Message)); } finally { if (!success) { CommandIssued = false; } } }
private async Task SetValue() { if (!Component.CType.IsAny(Models.Components.CType.Analog)) { return; } if (!(Component.Value is AnalogValue)) { return; } try { CommandIssued = true; double av = Convert.ToDouble(FunctionArgument); var val = (AnalogValue)Component.Value; var result = await RemoteDevice.Send(Protocol.IOAnalogWrite(ID, av, val.Unit)); ViewModelEventArgs e; if (result == Protocol.ActionSuccess) { e = ViewModelEventArgs.Information("Success"); } else { e = ViewModelEventArgs.Error("Failed to set the value."); } FunctionExecuted?.Invoke(this, e); } catch { } finally { CommandIssued = false; } }
private async Task TryConnect() { Action <string> ConnectionFailed = message => ConnectionEstablished?.Invoke(this, ViewModelEventArgs.Error(message)); Action <string> ConnectionSuccessful = message => ConnectionEstablished?.Invoke(this, ViewModelEventArgs.Information(message)); if (string.IsNullOrWhiteSpace(address)) { ConnectionFailed("Please, fill in the Address of the device."); return; } if (string.IsNullOrWhiteSpace(port)) { port = "80"; } IsConnecting = true; var client = Dependency.Resolve <HttpClient>(); try { using (var body = new StringContent(Protocol.HelloMessage, Encoding.UTF8, "application/xml")) { using (var response = await client.PostAsync("http://" + address + ":" + port + "/", body)) { if (response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStringAsync(); if (content != null) { if (content == Protocol.HelloReply) { var connection = Dependency.Resolve <Connection>(); connection.Address = address; connection.Port = Convert.ToInt32(port); HasConnection = true; ConnectionSuccessful("Success"); } else { ConnectionFailed("Device did not respond correctly. Error may be in the network."); } } else { ConnectionFailed("Device is not responding correctly."); } } else { ConnectionFailed("Connection was not properly established with the device."); } } } } catch (Exception ex) { ConnectionFailed("Device did not respond. Reason:\r\n" + ex.Message); } IsConnecting = false; }