public LinearAccelerationPageViewModel() { // Command to start the subscription ToggleSubscribeSwitchCommand = new Xamarin.Forms.Command( async() => { if (IsSubscribeSwitchOn) { ConnectionStatusText = "Connecting..."; await MovesenseDeviceVM.Connect(); ConnectionStatusText = "Subscribing..."; subscription = await MovesenseDeviceVM.MovesenseDevice.SubscribeAccelerometerAsync( (d) => { PlotData(d.Data.Timestamp, d.Data.AccData[0].X, d.Data.AccData[0].Y, d.Data.AccData[0].Z); }, 26); ConnectionStatusText = "Subscribed"; } else { // Unsubscribe subscription.Unsubscribe(); ConnectionStatusText = "Unsubscribed"; await MovesenseDeviceVM.Disconnect(); ConnectionStatusText = "Disconnected"; } } , () => (MovesenseDeviceVM != null) // Enable command only if we've got a device ); InitPlotModel(); }
private Task <IMdsSubscription> doSubscribe(Action <T> notificationCallback) { mTcs = new TaskCompletionSource <IMdsSubscription>(); mNotificationCallback = notificationCallback; #if __ANDROID__ var mds = (Com.Movesense.Mds.Mds)CrossMovesense.Current.MdsInstance; var subscription = mds.Subscribe( URI_EVENTLISTENER, FormatContractToJson(Util.GetVisibleSerial(mDeviceName), mPath), this ); Subscription = new MdsSubscription(subscription); #elif __IOS__ var mds = (Movesense.MDSWrapper)CrossMovesense.Current.MdsInstance; Movesense.MDSResponseBlock responseBlock = new Movesense.MDSResponseBlock((arg0) => OnSubscribeCompleted(arg0)); Movesense.MDSEventBlock eventBlock = (Movesense.MDSEvent arg0) => OnSubscriptionEvent(arg0); string path = Util.GetVisibleSerial(mDeviceName) + mPath; mds.DoSubscribe(path, new Foundation.NSDictionary(), responseBlock, eventBlock); // Save the path to the subscription for the device in the MdsSubscription Subscription = new MdsSubscription(path); #endif return(mTcs.Task); }
/// <summary> /// Subscribe to the resource /// </summary> /// <param name="notificationCallback">Callback function that will receive periodic notifications with data from the subscription resource</param> /// <returns>The subscription context</returns> public async Task <IMdsSubscription> SubscribeWithRetryAsync(Action <T> notificationCallback) { TaskCompletionSource <IMdsSubscription> retryTcs = new TaskCompletionSource <IMdsSubscription>(); IMdsSubscription result = null; bool doRetry = true; while (doRetry) { try { result = await doSubscribe(notificationCallback); retryTcs.SetResult(result); doRetry = false; } catch (Exception ex) { bool?mCancelled = RetryFunction?.Invoke(ex); if (mCancelled.HasValue && mCancelled.Value) { // User has cancelled - break out of loop Debug.WriteLine($"MAX RETRY COUNT EXCEEDED giving up Mds Api Call after exception: {ex.ToString()}"); retryTcs.SetException(ex); throw ex; } else { Debug.WriteLine($"RETRYING Mds Api Call after exception: {ex.ToString()}"); await Task.Delay(RETRY_DELAY); } } } return(result); }
/// <summary> /// Unsubscribe from the MdsLib resource /// </summary> public void UnSubscribe() { Debug.WriteLine("Unsubscribing Mds api subscription"); Subscription?.Unsubscribe(); Subscription = null; }