...
Connexion 14.5+ includes a framework to communicate directly with your server-side device from the associated device user-interface. This is often useful in if you need to query for server-side state, perform a database lookup or connection availability, or run a specific method on the server.
...
Code Block | ||
---|---|---|
| ||
public interface IDiagnostics { stringTask<string> GetDiagnosticsGetDiagnosticsAsync(); } |
Next, implement the interface in your device:
Code Block | ||
---|---|---|
| ||
using System; using System.Threading; using Connexion.Core; using System.Threading.Tasks; namespace ProxySample1 { [DevicePlugin("ProxySample1", "Example of device proxying", DeviceDefinitionFlags.None, typeof(object), typeof(object), typeof(ProxySample1Factory))] public class ProxySample1 : BaseDevice<ProxySample1Configuration>, IDiagnostics { public override void Start() { } public override void Stop() { } public override async Task ProcessMessageAsync(IMessageContext context, CancellationToken token) { } public stringasync Task<string> GetDiagnosticsGetDiagnosticsAsync() { return Task.FromResult(Environment.MachineName); } } } |
Now, create a proxy variable in your viewmodel. Note that if you are calling a proxy method in your viewmodel constructor, we recommend you perform this proxy calls on a different thread so that you do not block the UI thread.
Code Block | ||
---|---|---|
| ||
using Connexion.Core; using System.Threading.Tasks; using System.Windows.Input; namespace ProxySample1 { public class ProxySample1UIViewModel : ViewModelBase { private ProxySample1Configuration m_Config; private IDeviceUIParams m_DeviceUIParams; private IDiagnostics m_Proxy; public ProxySample1UIViewModel(ProxySample1Configuration config, IDeviceUIParams deviceUIParams) { m_Config = config; m_DeviceUIParams = deviceUIParams; m_Proxy = deviceUIParams.GetServerDeviceProxy<IDiagnostics>(); // <-- creation of the proxy } public ProxySample1Configuration Configuration { get { return m_Config; } } private RelayCommand m_GetDiagnosticsCommand; public ICommand GetDiagnosticsCommand { get { return m_GetDiagnosticsCommand ?? (m_GetDiagnosticsCommand = new RelayCommand(async p => await GetDiagnostics())); } } private async Task GetDiagnostics() { await Task.Run(() => { try { DiagnosticResult = await m_Proxy.GetDiagnosticsGetDiagnosticsAsync(); // <-- Calling the server } catch(exception ex) { DiagnosticResult = ex.Message; }); } private string m_DiagnosticResult = "Click to run the diagnostics"; public string DiagnosticResult { get { return m_DiagnosticResult; } set { if(m_DiagnosticResult != value) { m_DiagnosticResult = value; RaisePropertyChanged(); } } } } } |
...