private async void Restore(object obj) { try { BackupBusy = true; BackupStatus = "Retrieving backup from OneDrive"; PebbleConnector _pc = PebbleConnector.GetInstance(); bool Result = await _pc.WatchItems.Restore(); if (Result) { BackupStatus = "Restoring watch items"; Result = await _pc.WatchItems.Load(); } if (Result) { BackupStatus = "Restore success"; } else { BackupStatus = "Restore failed"; } } catch (Exception exp) { BackupStatus = "Restore failed"; } BackupBusy = false; }
private async void Backup(object obj) { try { PebbleConnector _pc = PebbleConnector.GetInstance(); BackupBusy = true; BackupStatus = "Backing up watch items"; bool Result = await _pc.WatchItems.Backup(); if (Result) { LastBackup = DateTime.Now; BackupStatus = "Backup success"; } else { BackupStatus = "Backup failed"; } } catch (Exception exp) { BackupStatus = "Backup failed"; } BackupBusy = false; }
private async Task ProcessDelay() { //Wait a second await System.Threading.Tasks.Task.Delay(500); //Process delays if (_DelayDisonnect.Count > 0) { foreach (DelayDisconnect item in _DelayDisonnect) { item.Delay -= 500; if (item.Delay <= 0) { PebbleConnector.ClearBackgroundTaskRunningStatus(item.Initiator); } } _DelayDisonnect.RemoveAll(x => x.Delay <= 0); if (_DelayDisonnect.Count == 0) { PebbleConnector.ClearBackgroundTaskRunningStatus(PebbleConnector.Initiator.Delay); } } }
/// <summary> /// Start the activity /// </summary> /// <param name="obj"></param> private async void StartActivity(object obj) { if (!Purchased) { return; } var localSettings = ApplicationData.Current.LocalSettings; localSettings.Values[Constants.BackgroundPace] = true; _NotRunning = 0; _Timer.Start(); try { PebbleConnector _pc = PebbleConnector.GetInstance(); await _pc.StartBackgroundTask(PebbleConnector.Initiator.Pace); } catch (Exception exc) { ExtendedEventArgs _e = new ExtendedEventArgs(); _e.Error = exc.Message; OnFatalError(_e); } }
public WipeHandler(ObservableCollection <String> Log, TimeLineSynchronizer TimeLineSynchronizer) { _pc = PebbleConnector.GetInstance(); _Log = Log; _TimeLineSynchronizer = TimeLineSynchronizer; _ConnectionToken = -1; }
/// <summary> /// Stop the activity /// </summary> /// <param name="obj"></param> private void StopActivity(object obj) { //var localSettings = ApplicationData.Current.LocalSettings; //localSettings.Values[Constants.BackgroundPace] = false; PebbleConnector _pc = PebbleConnector.GetInstance(); _pc.StopBackgroundTask(PebbleConnector.Initiator.Pace); }
private void SuspendCommandInvokedHandler(IUICommand command) { PebbleConnector _pc = PebbleConnector.GetInstance(); if (command.Label == "Yes") { var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; localSettings.Values[Constants.TennisCommand] = "suspend"; } }
/// <summary> /// Wipe the Pebble Time /// </summary> /*private async Task Wipe() * { * if (PebbleConnector.IsBackgroundTaskRunningStatusSet(PebbleConnector.Initiator.Reset)) * { * * * _tlsynchronizer = new TimeLineSynchronizer(); * _tlsynchronizer.Log.CollectionChanged += Log_CollectionChanged; * await _tlsynchronizer.Wipe(); * * AddProcessDelay(30000, PebbleConnector.Initiator.Reset); * } * }*/ private async Task Synchronize() { if (PebbleConnector.IsBackgroundTaskRunningStatusSet(PebbleConnector.Initiator.Synchronize)) { _tlsynchronizer = new TimeLineSynchronizer(); _tlsynchronizer.Log.CollectionChanged += Log_CollectionChanged; await _tlsynchronizer.Synchronize(); PebbleConnector.ClearBackgroundTaskRunningStatus(PebbleConnector.Initiator.Synchronize); } }
private void AddProcessDelay(int milliSeconds, PebbleConnector.Initiator Initator) { DelayDisconnect _new = new DelayDisconnect(); _new.Delay = milliSeconds; _new.Initiator = Initator; _DelayDisonnect.Add(_new); PebbleConnector.ClearBackgroundTaskRunningStatus(Initator); PebbleConnector.SetBackgroundTaskRunningStatus(PebbleConnector.Initiator.Delay); }
private async void Page_Loaded(object sender, RoutedEventArgs e) { PebbleConnector _pc = PebbleConnector.GetInstance(); if (!await _pc.IsPebbleAssociated()) { MessageDialog msgBox = new MessageDialog("No Pebble device has been associated with Pebble Time Manager. Do you want to search and associate a device now?" + System.Environment.NewLine + System.Environment.NewLine + "This will take a couple of seconds to complete."); msgBox.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(this.AssociateInvokedHandler))); msgBox.Commands.Add(new UICommand("No", new UICommandInvokedHandler(this.AssociateInvokedHandler))); await msgBox.ShowAsync(); } }
/// <summary> /// Timer processing /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _timer_Tick(object sender, object e) { var localSettings = ApplicationData.Current.LocalSettings; Duration = (String)localSettings.Values[Constants.PaceDuration]; Pace = (String)localSettings.Values[Constants.PacePace]; Distance = (String)localSettings.Values[Constants.PaceDistance]; /*if (localSettings.Values.Keys.Contains(Constants.BackgroundCommunicatieIsRunning)) * { * IsRunning = (bool)localSettings.Values[Constants.BackgroundCommunicatieIsRunning]; * }*/ IsRunning = PebbleConnector.IsBackgroundTaskRunningStatusSet(PebbleConnector.Initiator.Pace); if (localSettings.Values.Keys.Contains(Constants.PacePaused)) { Paused = (bool)localSettings.Values[Constants.PacePaused]; } if (localSettings.Values.Keys.Contains(Constants.PaceGPX)) { Shareable = (bool)localSettings.Values[Constants.PaceGPX]; } if (!IsRunning) { _NotRunning++; if (_NotRunning > 15) { _Timer.Stop(); } } else { _NotRunning = 0; } //Check if error occurred if (localSettings.Values.Keys.Contains(Constants.BackgroundCommunicatieError) && (int)localSettings.Values[Constants.BackgroundCommunicatieError] == (int)BCState.ConnectionFailed) { ExtendedEventArgs _fe = new ExtendedEventArgs(); _fe.Error = "Connection with Pebble Time failed. Try again."; OnFatalError(_fe); _Timer.Stop(); localSettings.Values[Constants.BackgroundCommunicatieError] = (int)BCState.OK; } }
private async Task Launch() { var localSettings = ApplicationData.Current.LocalSettings; if (PebbleConnector.IsBackgroundTaskRunningStatusSet(PebbleConnector.Initiator.Launch)) { String item = (string)localSettings.Values[Constants.BackgroundCommunicatieLaunchItem]; Guid itemGuid = Guid.Parse(item); await _pc.Launch(itemGuid); AddToLog("Request send to launch app: " + item); AddProcessDelay(30000, PebbleConnector.Initiator.Launch); } }
private async Task AddItem() { var localSettings = ApplicationData.Current.LocalSettings; if (PebbleConnector.IsBackgroundTaskRunningStatusSet(PebbleConnector.Initiator.AddItem)) { String Filename = (String)localSettings.Values[Constants.BackgroundCommunicatieDownloadedItem]; var _newItem = await Pebble_Time_Manager.WatchItems.WatchItem.Load(Filename); await _pc.Pebble.AddWatchItemAsync(_newItem); AddToLog("New app send: " + _newItem.Name); PebbleConnector.ClearBackgroundTaskRunningStatus(PebbleConnector.Initiator.AddItem); } }
private async void PebbleAssociate(IUICommand command) { Log.Add("Associating " + PebbleDeviceName.Name); try { PebbleConnector _pc = PebbleConnector.GetInstance(); if (await _pc.AssociatePebble(PebbleDeviceName)) { Log.Add("Success"); var successDialog = new Windows.UI.Popups.MessageDialog(String.Format("Association {0} completed successfully.", PebbleDeviceName.Name)); successDialog.Commands.Add(new UICommand("Ok")); await successDialog.ShowAsync(); } else { throw new Exception(); } } catch (Exception exp) { if (exp.Message.Length > 0) { Log.Add(String.Format("An error occurred while associating {0}: {1}", PebbleDeviceName, exp.Message)); } else { Log.Add("Failed"); } var messageDialog = new Windows.UI.Popups.MessageDialog(String.Format("Association {0} failed.", PebbleDeviceName.Name)); messageDialog.Commands.Add(new UICommand("Ok")); await messageDialog.ShowAsync(); } finally { NotifyPropertyChanged("IsDeviceAssociated"); NotifyPropertyChanged("AssociatedDeviceFirmware"); NotifyPropertyChanged("AssociatedDeviceName"); NotifyPropertyChanged("AssociatedDeviceBoard"); NotifyPropertyChanged("AssociatedDeviceId"); } }
public async void Associate(object obj) { PebbleConnector _pc = PebbleConnector.GetInstance(); { PebbleDeviceName = await _pc.GetCandidatePebble(); if (PebbleDeviceName != null) { String Message = String.Format("Device {0} found. Do you want to associate it?", PebbleDeviceName.Name); var messageDialog = new Windows.UI.Popups.MessageDialog(Message); messageDialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(this.PebbleAssociate))); messageDialog.Commands.Add(new UICommand("No")); await messageDialog.ShowAsync(); } } }
private void App_Activated(object sender, string e) { ConfigWebView.Visibility = Visibility.Collapsed; var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; localSettings.Values[Constants.PebbleWebViewClosed] = e; if (_WatchAppConfig != null) { //_WatchFaceConfig.WebViewClosed(e); localSettings.Values[Constants.PebbleWatchItem] = _WatchAppConfig.ID.ToString(); } //Process the pebble webviewclosed in the background PebbleConnector _pc = PebbleConnector.GetInstance(); _pc.StartBackgroundTask(PebbleConnector.Initiator.PebbleWebViewClosed); }
private async void Tennis_OnResume(object sender, EventArgs e) { PebbleConnector _pc = PebbleConnector.GetInstance(); try { var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; localSettings.Values[Constants.BackgroundTennis] = true; await _pc.StartBackgroundTask(PebbleConnector.Initiator.Tennis); } catch (Exception exc) { var messageDialog = new Windows.UI.Popups.MessageDialog(exc.Message, "Error"); // Show the message dialog messageDialog.ShowAsync(); } }
public async void Configure(object obj) { Configuring = true; var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; localSettings.Values[Constants.PebbleWatchItem] = Item.ID.ToString(); localSettings.Values[Constants.PebbleShowConfiguration] = ""; PebbleConnector _pc = PebbleConnector.GetInstance(); await _pc.StartBackgroundTask(PebbleConnector.Initiator.PebbleShowConfiguration); _timer = new DispatcherTimer(); _timer.Interval = TimeSpan.FromMilliseconds(250); _timer.Tick += _timer_Tick; _timer.Start(); _timerCycles = 0; }
private async void Resync(object obj) { try { _vmBinder.Log.Clear(); _vmBinder.Log.Add("Initiating resync..."); WipeHandler = new WipeHandler(Log, TimeLineSynchronizer); await WipeHandler.Wipe(); PebbleConnector _pc = PebbleConnector.GetInstance(); IsConnected = _pc.IsConnected; } catch (Exception exp) { Log.Add("An exception occurred while resyncing."); Log.Add(exp.Message); } }
private async void DeleteCommandInvokedHandler(IUICommand command) { PebbleConnector _pc = PebbleConnector.GetInstance(); if (command.Label == "Yes") { await Tennis_Statistics.Helpers.LocalStorage.Delete("tennis_pebble.xml"); await Tennis_Statistics.Helpers.LocalStorage.Delete("tennismatchstate.json"); //Show new match page ShowPage(NewMatchGrid); _vmBinder.Tennis.vmMatch.IsExtendPossible = false; _vmBinder.Tennis.vmMatch.Paused = false; _vmBinder.Tennis.vmMatch.InProgress = false; _vmBinder.Tennis.vmMatch.Completed = false; _vmBinder.Tennis.TryInUse = false; _vmBinder.Tennis.vmMatch.Notify(); } }
private async Task PebbleKitExecution() { var localSettings = ApplicationData.Current.LocalSettings; if (PebbleConnector.IsBackgroundTaskRunningStatusSet(PebbleConnector.Initiator.PebbleShowConfiguration)) { String _watchItem = (string)localSettings.Values[Constants.PebbleWatchItem]; var WatchItem = _pc.WatchItems.FindLast(x => x.ID.ToString() == _watchItem); if (WatchItem != null) { System.Diagnostics.Debug.WriteLine("PebbleShowConfiguration: " + WatchItem.Name); PebbleKitJS.OpenURL += PebbleKitJS_OpenURL; await WatchItem.ShowConfiguration(); AddProcessDelay(180000, PebbleConnector.Initiator.PebbleShowConfiguration); } } if (PebbleConnector.IsBackgroundTaskRunningStatusSet(PebbleConnector.Initiator.PebbleWebViewClosed)) { String _watchItem = (string)localSettings.Values[Constants.PebbleWatchItem]; String _value = (string)localSettings.Values[Constants.PebbleWebViewClosed]; var WatchItem = _pc.WatchItems.FindLast(x => x.ID.ToString() == _watchItem); if (WatchItem != null) { System.Diagnostics.Debug.WriteLine("PebbleWebViewClosed: " + WatchItem.Name); WatchItem.WebViewClosed(_value); AddProcessDelay(30000, PebbleConnector.Initiator.PebbleWebViewClosed); } } }
private async void ReinitiateMatch() { _vmBinder.Tennis.vmMatch = new vmMatchState(); _vmBinder.Tennis.vmMatch.Paused = false; _vmBinder.Tennis.TryInUse = true; //_vmBinder.vmMatch.Start(_vmBinder.vmNewMatch); ApplicationData.Current.LocalSettings.Values[Constants.TennisState] = "1"; //ApplicationData.Current.LocalSettings.Values[Constants.BackgroundCommunicatieIsRunning] = false; PebbleConnector _pc = PebbleConnector.GetInstance(); if (!await _pc.IsBackgroundTaskRunning()) { //await _pc.StartBackgroundTask(PebbleConnector.Initiator.Tennis); } ShowPage(ProgressRing); _Timer = new DispatcherTimer(); _Timer.Interval = TimeSpan.FromSeconds(1); _Timer.Tick += _Timer_Tick; _Timer.Start(); }
private async void ExtendCommandInvokedHandler(IUICommand command) { PebbleConnector _pc = PebbleConnector.GetInstance(); if (command.Label == "Yes") { try { await _pc.StartBackgroundTask(PebbleConnector.Initiator.Tennis); var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; localSettings.Values[Constants.BackgroundTennis] = true; localSettings.Values[Constants.TennisCommand] = "extend"; } catch (Exception exc) { var messageDialog = new Windows.UI.Popups.MessageDialog(exc.Message, "Error"); // Show the message dialog messageDialog.ShowAsync(); } } }
/// <summary> /// An item was clicked, make the watch app active on the Pebble /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void lvWatchApps_ItemClick(object sender, ItemClickEventArgs e) { try { vmWatchApp _app = (vmWatchApp)e.ClickedItem; switch (_app.Name) { case "Pace": // Navigate to the new page Frame.Navigate(typeof(PaceApp), null); break; case "Tennis": // Navigate to the new page Frame.Navigate(typeof(TennisApp), null); break; default: _vmBinder.Log.Add("Select item: " + _app.Name); await PebbleConnector.GetInstance().Select(_app.Model, WatchItems.WatchItemType.WatchApp); break; } } catch (Exception exc) { System.Diagnostics.Debug.WriteLine("An exception occurred launching watch item: " + exc.Message); _vmBinder.Log.Add("An exception occurred launching watch item: " + exc.Message); } }
private async void StartButton_Click(object sender, RoutedEventArgs e) { PebbleConnector _pc = PebbleConnector.GetInstance(); //_vmBinder.vmNewMatch = new vmNewMatch(); _vmBinder.Tennis.vmMatch = new vmMatchState(); ApplicationData.Current.LocalSettings.Values.Remove(Constants.TennisState); ApplicationData.Current.LocalSettings.Values.Remove(Constants.TennisCommand); //_vmBinder.vmMatch.Start(_vmBinder.vmNewMatch); ShowPage(ProgressRing); _Timer = new DispatcherTimer(); _Timer.Interval = TimeSpan.FromSeconds(1); _Timer.Tick += _Timer_Tick; _Timer.Start(); try { await _vmBinder.vmNewMatch.Save(); var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; localSettings.Values[Constants.BackgroundTennis] = true; await _pc.StartBackgroundTask(PebbleConnector.Initiator.Tennis); } catch (Exception exc) { var messageDialog = new Windows.UI.Popups.MessageDialog(exc.Message, "Error"); // Show the message dialog messageDialog.ShowAsync(); ShowPage(NewMatchGrid); } }
/// <summary> /// Main thread for communication with pebble on a background task. /// /// Reading the state of the process is possible via the local settings BackgroundIsRunning. Synchronization via /// Mutex is not possible due to heavy use of await statements. The mutex will be abandoned and releasing gives /// an exception. /// </summary> /// <param name="taskInstance"></param> public async void Run(IBackgroundTaskInstance taskInstance) { var def = taskInstance.GetDeferral(); var localSettings = ApplicationData.Current.LocalSettings; Handler = -1; localSettings.Values[Constants.BackgroundCommunicatieError] = (int)BCState.OK; try { System.Diagnostics.Debug.WriteLine("Start BackgroundCommunication"); //Connect _pc = PebbleConnector.GetInstance(); Handler = await _pc.Connect(-1); if (_pc.IsConnected) { AddToLog("Connection made with Pebble Time"); Log = new ObservableCollection <string>(); Log.CollectionChanged += Log_CollectionChanged; _pc.Pebble.Log = Log; _pc.StartReceivingMessages(); _pc.disconnectEventHandler += _pc_disconnectEventHandler; _pc.Pebble._protocol.MessageReceived += AppMessageReceived; bool Continue = true; //initialise settings while (Continue) { try { localSettings.Values[Constants.BackgroundCommunicatieIsRunning] = true; if (_pc.IsConnected) { await PaceHandler(); await TennisHandler(); //await Wipe(); await Synchronize(); await Select(); await Launch(); await AddItem(); await PebbleKitExecution(); } else { await Reconnect(); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message + e.StackTrace); } await ProcessDelay(); //Check if continue Continue = ((int)localSettings.Values[Constants.BackgroundCommunicatieContinue] != 0); } await PaceHandlerCleanup(); localSettings.Values[Constants.BackgroundTennis] = false; localSettings.Values[Constants.BackgroundPace] = false; _pc.Pebble._protocol.MessageReceived -= AppMessageReceived; } else { AddToLog("Connection with Pebble Time Failed."); localSettings.Values[Constants.BackgroundCommunicatieError] = (int)BCState.ConnectionFailed; if (_pc.LastError.Length > 0) { AddToLog(_pc.LastError); } } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception BackgroundCommunication: " + e.Message); localSettings.Values[Constants.BackgroundCommunicatieError] = (int)BCState.ExceptionOccurred; } finally { localSettings.Values[Constants.BackgroundCommunicatieIsRunning] = false; //Disconnect if (_pc.IsConnected) { _pc.Disconnect(Handler); AddToLog("Disconnected from Pebble Time"); } } System.Diagnostics.Debug.WriteLine("End BackgroundCommunication"); def.Complete(); }
/// <summary> /// Tennis handler /// </summary> private async Task TennisHandler() { if (!PebbleConnector.IsBackgroundTaskRunningStatusSet(PebbleConnector.Initiator.Tennis)) { return; } //initialise settings var localSettings = ApplicationData.Current.LocalSettings; var roamingSettings = ApplicationData.Current.RoamingSettings; if (localSettings.Values.Keys.Contains(Constants.BackgroundTennis) && (bool)localSettings.Values[Constants.BackgroundTennis]) { //Initiate tennis match if (TennisMatch == null) { TennisMatch = new vmMatch(); await TennisMatch.Load("tennis_pebble.xml"); if (TennisMatch.Match == null) { System.Diagnostics.Debug.WriteLine("Initialize Tennis Match in background."); vmNewMatch _vmNewMatch = await vmNewMatch.Load(); TennisMatch = new vmMatch(); TennisMatch.Start(_vmNewMatch); } else { TennisMatch.NewPoint(); } vmMatchState _State = new vmMatchState(); _State.Fill(TennisMatch); await Tennis_Statistics.Helpers.LocalStorage.Save(_State.Serialize(), "tennismatchstate.json", false); localSettings.Values[Constants.TennisState] = "1"; System.Diagnostics.Debug.WriteLine("Tennis Match state stored"); _pc.Pebble._protocol.MessageReceived += this.TennisMessageReceived; Guid TennisWatchApp = Guid.Parse(Constants.TennisAppGuid); await _pc.Launch(TennisWatchApp); await SendPebbleTennisScore(_State, TennisMatch); // await SendPebbleTennisScore(_State); } } //A new state requested if (localSettings.Values.Keys.Contains(Constants.TennisState) && (string)localSettings.Values[Constants.TennisState] == "2") { await SaveMatchState(); } //Check for and process command if (localSettings.Values.Keys.Contains(Constants.TennisCommand) && TennisMatch != null) { String Command = (String)localSettings.Values[Constants.TennisCommand]; System.Diagnostics.Debug.WriteLine("Processing tennis command: " + Command); switch (Command) { case "switch": TennisMatch.cmdSwitchServer.Execute(null); AddToLog("Tennis: switch command"); break; case "stop": TennisMatch.Terminate(); PebbleConnector.ClearBackgroundTaskRunningStatus(PebbleConnector.Initiator.Tennis); AddToLog("Tennis: stop command"); break; case "suspend": TennisMatch.Pause(); PebbleConnector.ClearBackgroundTaskRunningStatus(PebbleConnector.Initiator.Tennis); AddToLog("Tennis: suspend command"); break; case "extend": TennisMatch.cmdExtendMatch.Execute(null); AddToLog("Tennis: extend command"); break; } localSettings.Values.Remove(Constants.TennisCommand); await SaveMatchState(); } }
/// <summary> /// Pace handler /// </summary> /// <returns></returns> private async Task PaceHandler() { //initialise settings var localSettings = ApplicationData.Current.LocalSettings; var roamingSettings = ApplicationData.Current.RoamingSettings; if (localSettings.Values.Keys.Contains(Constants.BackgroundPace) && (bool)localSettings.Values[Constants.BackgroundPace]) { if (!PebbleConnector.IsBackgroundTaskRunningStatusSet(PebbleConnector.Initiator.Pace)) { await PaceHandlerCleanup(); } else { if (_PaceHandler == null) { localSettings.Values[Constants.PaceSwitchPaused] = false; localSettings.Values[Constants.PaceGPX] = false; if (localSettings.Values.Keys.Contains(Constants.Miles)) { localSettings.Values[Constants.Miles] = localSettings.Values[Constants.Miles]; } else { localSettings.Values[Constants.Miles] = !System.Globalization.RegionInfo.CurrentRegion.IsMetric; } localSettings.Values[Constants.BackgroundCommunicatieIsRunning] = true; //initialise pace handler _PaceHandler = new Pebble_Time_Manager.Connector.PaceHandler(); _PaceHandler.Miles = (bool)localSettings.Values[Constants.Miles]; _PaceHandler.Start(); await _PaceHandler.UpdatePosition(); } //Update pace _PaceHandler.UpdatePosition(); if (!_PaceHandler.Paused) { //Send Pebble Message await _PaceHandler.SendPebblePaceMessage(); localSettings.Values[Constants.PaceDistance] = string.Format("{0:f2}", _PaceHandler.Distance); localSettings.Values[Constants.PaceDuration] = _PaceHandler.Duration.ToString(@"m\:ss"); localSettings.Values[Constants.PacePace] = _PaceHandler.Pace.ToString(@"m\:ss"); if (_PaceHandler.Pace.TotalMinutes > 45) { localSettings.Values[Constants.PacePace] = "-"; } if (_PaceHandler.Pace.TotalMilliseconds == 0) { localSettings.Values[Constants.PacePace] = "-"; } } localSettings.Values[Constants.PacePaused] = _PaceHandler.Paused; //Check the switch pause/resume if (localSettings.Values.Keys.Contains(Constants.PaceSwitchPaused)) { bool SwitchPaused = (bool)localSettings.Values[Constants.PaceSwitchPaused]; localSettings.Values[Constants.PaceSwitchPaused] = false; if (SwitchPaused) { if (_PaceHandler.Paused) { await _PaceHandler.Resume(); } else { await _PaceHandler.Pause(); } } } } } }
public async Task sendAppMessage(ExpandoObject data) { PebbleConnector _pc = PebbleConnector.GetInstance(); int newToken = await _pc.Connect(-1); try { if (_pc.IsConnected) { System.Diagnostics.Debug.WriteLine(String.Format("sendAppMessage(data={0})", data.ToString())); P3bble.Messages.AppMessage _am = new P3bble.Messages.AppMessage(P3bble.Constants.Endpoint.ApplicationMessage); uint iKey = 0; _am.Content = new Dictionary <int, object>(data.Count()); _am.Command = P3bble.Messages.AppCommand.Push; _am.AppUuid = ParentItem.ID; _am.TransactionId = (byte)_pc.GetNextMessageIdentifier(); foreach (var element in data) { if (element.Value != null) { if (ParentItem.AppKeys.ContainsKey(element.Key)) { iKey = (uint)ParentItem.AppKeys[element.Key]; Type VariableType = element.Value.GetType(); System.Diagnostics.Debug.WriteLine(String.Format(" key: {0}-{3}, value: {1}, type: {2}", element.Key, element.Value, VariableType.ToString(), iKey)); if (VariableType == typeof(String)) { String Value = (String)element.Value; //_am.AddTuple(iKey, P3bble.Messages.AppMessageTupleDataType.String, System.Text.Encoding.UTF8.GetBytes(Value)); _am.Content.Add((int)iKey, Value); } if (VariableType == typeof(Double)) { double dValue = (double)element.Value; int iValue = (int)dValue; byte[] bytes = BitConverter.GetBytes(iValue); //_am.AddTuple(iKey, P3bble.Messages.AppMessageTupleDataType.Int, bytes); _am.Content.Add((int)iKey, iValue); } } } } //byte[] package = _am.ToBuffer(); //System.Diagnostics.Debug.WriteLine("<< PAYLOAD: " + BitConverter.ToString(package).Replace("-", ":")); await _pc.Pebble._protocol.WriteMessage(_am); } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.Message); } finally { _pc.Disconnect(newToken); } }