public async Task RunAsync(CancellationToken cancellationToken) { await GetVariablesAsync(); await GetCommandsAsync(); var m = new Message(); while (!cancellationToken.IsCancellationRequested) { await m.ReadAsync(stream); Debug.WriteLine("Got message: " + (ControlOp)m.Operation + "(" + string.Join(", ", m.Arguments.Select(x => x.ToString())) + ")"); switch ((ControlOp)m.Operation) { case ControlOp.Variable: { var id = (int)m.Arguments [0]; var v = variables.FirstOrDefault(x => x.Id == id); if (v == null) { var cv = new ClientVariable { Client = this, Id = id, Name = (string)m.Arguments [1], IsWriteable = (bool)m.Arguments [2], }; cv.SetValue(m.Arguments [3]); v = cv; Schedule(() => variables.Add(v)); } } break; case ControlOp.VariableValue: { var id = (int)m.Arguments [0]; var cv = variables.FirstOrDefault(x => x.Id == id) as ClientVariable; if (cv != null) { var newVal = m.Arguments [1]; Schedule(() => cv.SetValue(newVal)); } else { await GetVariablesAsync(); } } break; case ControlOp.Command: { var id = (int)m.Arguments [0]; var c = commands.FirstOrDefault(x => x.Id == id); if (c == null) { var cc = new Command { Id = id, Name = (string)m.Arguments [1], }; c = cc; Schedule(() => commands.Add(c)); } } break; // default: // Debug.WriteLine ("Ignoring message: " + m.Operation); // break; } } }
public async Task RunAsync(CancellationToken cancellationToken) { await GetVariablesAsync(); await GetCommandsAsync(); var message = new Message(); while (!cancellationToken.IsCancellationRequested) { await message.ReadAsync(stream); Debug.WriteLine("Got message: " + (ControlOp)message.Operation + "(" + string.Join(", ", message.Arguments.Select(x => x.ToString())) + ")"); switch ((ControlOp)message.Operation) { case ControlOp.Variable: { var id = (int)message.Arguments[0]; var variable = variables.FirstOrDefault(x => x.Id == id); if (variable == null) { var clientVariable = new ClientVariable { Client = this, Id = id, Name = (string)message.Arguments[1], IsWriteable = (bool)message.Arguments[2], }; clientVariable.SetValue(message.Arguments[3]); variable = clientVariable; Schedule(() => variables.Add(variable)); } } break; case ControlOp.VariableValue: { var id = (int)message.Arguments[0]; if (variables.FirstOrDefault(x => x.Id == id) is ClientVariable clientVariable) { var newVal = message.Arguments[1]; Schedule(() => clientVariable.SetValue(newVal)); } else { await GetVariablesAsync(); } } break; case ControlOp.Command: { var id = (int)message.Arguments[0]; var command = commands.FirstOrDefault(x => x.Id == id); if (command == null) { var clientCommand = new Command { Id = id, Name = (string)message.Arguments[1], }; command = clientCommand; Schedule(() => commands.Add(command)); } } break; default: Debug.WriteLine("Ignoring message: " + message.Operation); break; } } }
public async Task RunAsync (CancellationToken cancellationToken) { await GetVariablesAsync (); await GetCommandsAsync (); var m = new Message (); while (!cancellationToken.IsCancellationRequested) { await m.ReadAsync (stream); Debug.WriteLine ("Got message: " + (ControlOp)m.Operation + "(" + string.Join (", ", m.Arguments.Select (x => x.ToString ())) + ")"); switch ((ControlOp)m.Operation) { case ControlOp.Variable: { var id = (int)m.Arguments [0]; var v = variables.FirstOrDefault (x => x.Id == id); if (v == null) { var cv = new ClientVariable { Client = this, Id = id, Name = (string)m.Arguments [1], IsWriteable = (bool)m.Arguments [2], }; cv.SetValue (m.Arguments [3]); v = cv; Schedule (() => variables.Add (v)); } } break; case ControlOp.VariableValue: { var id = (int)m.Arguments [0]; var cv = variables.FirstOrDefault (x => x.Id == id) as ClientVariable; if (cv != null) { var newVal = m.Arguments [1]; Schedule (() => cv.SetValue (newVal)); } else { await GetVariablesAsync (); } } break; case ControlOp.Command: { var id = (int)m.Arguments [0]; var c = commands.FirstOrDefault (x => x.Id == id); if (c == null) { var cc = new Command { Id = id, Name = (string)m.Arguments [1], }; c = cc; Schedule (() => commands.Add (c)); } } break; // default: // Debug.WriteLine ("Ignoring message: " + m.Operation); // break; } } }