Ejemplo n.º 1
0
        /// <summary>
        /// Disconnect from the Pebble
        /// </summary>
        public void Disconnect()
        {
            //Disconnect
            _pc.Disconnect(_ConnectionToken);

            _ConnectionToken = -1;

            //if (!_pc.IsConnected) _Log.Add("Disconnected");
        }
Ejemplo n.º 2
0
        private void _pc_disconnectEventHandler(object sender, EventArgs e)
        {
            //Disconnected from Pebble
            //PebbleConnector.ClearBackgroundTaskRunningStatus(PebbleConnector.Initiator.Manual);

            _pc.Disconnect(Handler);

            AddToLog("Connection lost with Pebble Time. Retrying in 10 seconds.");

            ReconnectDelay = 10000;
        }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
0
            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);
                }
            }