// An event handler that runs when a "signalR.StateChanged" event is triggered in SignalRClient // (result of "START_SENSOR" or "STOP_SENSOR" message from hub, i.e. other clients asked hub to start/stop sensor) private async void SignalR_StateChanged(object sender, StateChangedEventArgs e) { SignalRClient signalR = sender as SignalRClient; await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { // If sensor is not already running (i.e. client requested to START_SENSOR) if (!e.IsRunning) { // Turns the toggle switch to ON toggleSwitch.IsOn = true; } // else if sensor is already running (i.e. client requested to STOP_SENSOR) else { // Turns the toggle switch to OFF toggleSwitch.IsOn = false; } // If the message received from hub is not empty if (!String.IsNullOrEmpty(e.State)) { // Sets the display text to the message received from hub messageFromClient.Text = e.State; } }); }
// An event handler that runs when a "sensor.StateChanged" event is triggered in SensorSource private async void Sensor_StateChanged(object sender, StateChangedEventArgs e) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { // If the sensor is running, i.e. turned ON if (e.IsRunning) { // Changes the display text on the XAML file to reflect that sensor is running sensorStatus.Text = "Sensor Status: Running"; icon.Foreground = new SolidColorBrush(Colors.Black); } // else if the sensor is not running, i.e. turned OFF else { // Changes the display text on the XAML file to reflect that sensor is not running sensorStatus.Text = "Sensor Status: Stopped"; tbkValue.Text = "[n/a]"; icon.Foreground = new SolidColorBrush(Colors.Gray); if (hub == null) return; try { // Executes a method "newUpdate" on the server side hub asynchronously, with params of type Object[] // (notifies clients that sensor is turned off) await hub.Invoke("newUpdate", new object[] { "SENSOR_OFF", "[n/a]" }); } catch { } } }); }