Example #1
0
        private async Task Reconnect()
        {
            ReconnectDelay -= 500;

            if (ReconnectDelay <= 0)
            {
                Handler = await _pc.Connect(-1);

                if (_pc.IsConnected)
                {
                    AddToLog("Reconnected with Pebble Time");

                    _pc.StartReceivingMessages();
                }
                else
                {
                    AddToLog("Reconnection failed with Pebble Time. Retrying in 5 minutes");

                    ReconnectDelay = 300000;
#if DEBUG
                    AddToLog("Debug mode. Retrying in 5 seconds");

                    ReconnectDelay = 300;
#endif
                }
            }
        }
Example #2
0
        /// <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>
        /// Reset the watch / timeline
        /// </summary>
        /// <returns></returns>
        public async Task <bool> Wipe()
        {
            try
            {
                if (await _pc.IsBackgroundTaskRunning())
                {
                    _Log.Add("Stop current activities");

                    _pc.StopBackgroundTask(PebbleConnector.Initiator.Manual);

                    await _pc.WaitBackgroundTaskStopped(5);
                }

                await Connect();

                String Message;

                //Get current watch face ID
                //WatchFaceMessage _wfm = new WatchFaceMessage();
                Guid      CurrentWatchFace     = _pc.Pebble.CurrentWatchFace; //await _pc.Pebble.RequestWatchFaceMessageAsync(_wfm);
                WatchItem CurrentWatchFaceItem = _pc.WatchItems.Get(CurrentWatchFace);
                System.Diagnostics.Debug.WriteLine(String.Format("Current watch face: {0}", CurrentWatchFace));

                if (CurrentWatchFaceItem != null)
                {
                    _Log.Add(String.Format("Current watch face: {0}", CurrentWatchFaceItem.Name));
                }
                else
                {
                    _Log.Add(String.Format("Current watch face: {0}", CurrentWatchFace));
                }


                _pc.StartReceivingMessages();
                _pc.Pebble._protocol.MessageReceived += AppMessageReceived;

                //Set the watch face
                byte[] TicTocByte = new byte[16] {
                    0x8F, 0x3C, 0x86, 0x86, 0x31, 0xA1, 0x4F, 0x5F, 0x91, 0xF5, 0x01, 0x60, 0x0C, 0x9B, 0xDC, 0x59
                };
                Guid TicTocGuid = new Guid(TicTocByte);

                if (CurrentWatchFace != Guid.Empty && CurrentWatchFace != TicTocGuid)
                {
                    WatchFaceSelectMessage _wsm = new WatchFaceSelectMessage(CurrentWatchFace, TicTocGuid);
                    await _pc.Pebble.WriteMessageAsync(_wsm);

                    System.Diagnostics.Debug.WriteLine(String.Format("TicToc Watch fase set as current"));
                }

                StandardV3Message _SV3M;

                for (int i = 1; i <= 9; i++)
                {
                    _SV3M = new StandardV3Message(_pc.Pebble.GetNextMessageIdentifier(), (byte)i);
                    await _pc.Pebble._protocol.WriteMessage(_SV3M);
                    await WaitForMessage(_SV3M.Identifier);
                }


                /*Message = "00:04:b1:db:05:d0:11:01";
                 * await _pc.WriteMessage(Message);
                 * await WaitForMessage("d0:11");
                 *
                 * Message = "00:04:b1:db:05:65:54:02";
                 * await _pc.WriteMessage(Message);
                 * await WaitForMessage("65:54");
                 *
                 * Message = "00:04:b1:db:05:8c:b5:03";
                 * await _pc.WriteMessage(Message);
                 * await WaitForMessage("8c:b5");
                 *
                 * Message = "00:04:b1:db:05:8c:b6:04";
                 * await _pc.WriteMessage(Message);
                 * await WaitForMessage("8c:b6");
                 *
                 * Message = "00:04:b1:db:05:8c:b7:05";
                 * await _pc.WriteMessage(Message);
                 * await WaitForMessage("8c:b7");
                 *
                 * Message = "00:04:b1:db:05:8c:b8:06";
                 * await _pc.WriteMessage(Message);
                 * await WaitForMessage("8c:b8");
                 *
                 * Message = "00:04:b1:db:05:8c:b9:07";
                 * await _pc.WriteMessage(Message);
                 * await WaitForMessage("8c:b9");
                 *
                 * Message = "00:04:b1:db:05:8c:ba:08";
                 * await _pc.WriteMessage(Message);
                 * await WaitForMessage("8c:ba");
                 *
                 * Message = "00:04:b1:db:05:8c:b0:09";
                 * await _pc.WriteMessage(Message);
                 * await WaitForMessage("8c:b0");*/

                _Log.Add("Pebble Time wiped.");

                System.Diagnostics.Debug.WriteLine("Pebble Time wiped.");

                foreach (var item in _pc.Pebble.WatchItems)
                {
                    WatchItemAddMessage _waam = new WatchItemAddMessage(_pc.Pebble.GetNextMessageIdentifier(), item);
                    await _pc.Pebble._protocol.WriteMessage(_waam);
                    await WaitForMessage(_waam.Transaction);

                    switch (item.Type)
                    {
                    case WatchItemType.WatchFace:

                        _Log.Add(String.Format("Watch face {0} added.", item.Name));
                        System.Diagnostics.Debug.WriteLine(String.Format("Watch face {0} added.", item.Name));

                        break;

                    case WatchItemType.WatchApp:

                        _Log.Add(String.Format("Watch app {0} added.", item.Name));
                        System.Diagnostics.Debug.WriteLine(String.Format("Watch app {0} added.", item.Name));

                        break;
                    }
                }

                _Log.Add("Watch items restored");
                System.Diagnostics.Debug.WriteLine("Watch items restored");

                //Clear caches
                Weather.ClearCache();
                await Calender.ClearCache();

                _Log.Add("Cache cleared on phone");
                System.Diagnostics.Debug.WriteLine("Cache cleared on phone");

                //Update timeline
                await _TimeLineSynchronizer.Synchronize();

                //Set the watch face
                if (CurrentWatchFace != Guid.Empty && CurrentWatchFace != TicTocGuid)
                {
                    WatchFaceSelectMessage _wsm = new WatchFaceSelectMessage(TicTocGuid, CurrentWatchFace);
                    await _pc.Pebble.WriteMessageAsync(_wsm);

                    _Log.Add(String.Format("Selected watch face: {0}", CurrentWatchFace));
                    if (CurrentWatchFaceItem != null)
                    {
                        _Log.Add(String.Format("Current watch face: {0}", CurrentWatchFaceItem.Name));
                    }
                    else
                    {
                        _Log.Add(String.Format("Current watch face: {0}", CurrentWatchFace));
                    }

                    System.Diagnostics.Debug.WriteLine(String.Format("Selected watch face: {0}", CurrentWatchFace));

                    _pc.Pebble.ItemSend += Pebble_ItemSend;
                }
                else
                {
                    Disconnect();
                }

                return(true);
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine(String.Format("Wipe exception: {0}", exp.Message));
                Disconnect();
            }

            return(false);
        }