/// <summary> /// Sends a message to the Pebble. /// </summary> /// <param name="message">The message to send.</param> /// <returns>An async task to wait</returns> public Task WriteMessage(P3bbleMessage message) { return Task.Factory.StartNew(() => { try { this._mutex.WaitOne(); byte[] package = message.ToBuffer(); ServiceLocator.Logger.WriteLine("<< SEND MESSAGE FOR ENDPOINT " + ((Endpoint)message.Endpoint).ToString() + " (" + ((int)message.Endpoint).ToString() + ")"); ServiceLocator.Logger.WriteLine("<< PAYLOAD: " + BitConverter.ToString(package).Replace("-", ":")); System.Diagnostics.Debug.WriteLine("<< SEND MESSAGE FOR ENDPOINT " + ((Endpoint)message.Endpoint).ToString() + " (" + ((int)message.Endpoint).ToString() + ")"); System.Diagnostics.Debug.WriteLine("<< PAYLOAD: " + BitConverter.ToString(package).Replace("-",":")); this._writer.WriteBytes(package); this._writer.StoreAsync().AsTask().Wait(); this._mutex.ReleaseMutex(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("<< EXCEPTION: " + e.Message); } }); }
public void WriteMessage(P3bbleMessage message) { _mutex.WaitOne(); byte[] package = message.ToBuffer(); Debug.WriteLine("Write Package: " + BitConverter.ToString(package)); _writer.WriteBytes(package); _writer.StoreAsync().AsTask().Wait(); _mutex.ReleaseMutex(); }
/// <summary> /// Process the tennis message /// </summary> /// <param name="message"></param> private void AppMessageReceived(P3bbleMessage message) { if (message.Endpoint == P3bble.Constants.Endpoint.StandardV3) { StandardV3Message _SV3M = (StandardV3Message)message; if (MessageIdentifier == _SV3M.Identifier) { MessageReceived = true; } } }
private async void Run(object host) { #endif var readMutex = new AsyncLock(); while (this._isRunning) { try { await this._reader.LoadAsync(4); ServiceLocator.Logger.WriteLine("[message available]"); using (await readMutex.LockAsync()) { ServiceLocator.Logger.WriteLine("[message unlocked]"); uint payloadLength; uint endpoint; if (this._reader.UnconsumedBufferLength > 0) { IBuffer buffer = this._reader.ReadBuffer(4); this.GetLengthAndEndpoint(buffer, out payloadLength, out endpoint); ServiceLocator.Logger.WriteLine(">> RECEIVED MESSAGE FOR ENDPOINT: " + ((Endpoint)endpoint).ToString() + " (" + endpoint.ToString() + ") - " + payloadLength.ToString() + " bytes"); if (endpoint > 0 && payloadLength > 0) { byte[] payload = new byte[payloadLength]; await this._reader.LoadAsync(payloadLength); this._reader.ReadBytes(payload); P3bbleMessage msg = this.ReadMessage(payload, endpoint); if (msg != null && this.MessageReceived != null) { this.MessageReceived(msg); } } else { ServiceLocator.Logger.WriteLine(">> RECEIVED MESSAGE WITH BAD ENDPOINT OR LENGTH: " + endpoint.ToString() + ", " + payloadLength.ToString()); } } } } catch { } #if NETFX_CORE && !WINDOWS_PHONE_APP await Task.Delay(100); #endif } #if NETFX_CORE && !WINDOWS_PHONE_APP },
/// <summary> /// Process the tennis message /// </summary> /// <param name="message"></param> private async void AppMessageReceived(P3bbleMessage message) { try { var localSettings = ApplicationData.Current.LocalSettings; switch (message.Endpoint) { case P3bble.Constants.Endpoint.WatchFaceSelect: WatchFaceMessage _watchFaceMessage = (WatchFaceMessage)message; System.Diagnostics.Debug.WriteLine("WatchFaceMessage received: " + _watchFaceMessage.CurrentWatchFace.ToString()); var WatchItem = _pc.WatchItems.FindLast(x => x.ID == _watchFaceMessage.CurrentWatchFace); if (WatchItem != null) { await WatchItem.Ready(); } break; case P3bble.Constants.Endpoint.ApplicationMessage: P3bble.Messages.AppMessage _appMessage = (P3bble.Messages.AppMessage)message; if (_appMessage.AppUuid != Guid.Parse("51a56b50-f87d-ce41-a0ff-30d03a88fa8d")) { System.Diagnostics.Debug.WriteLine("AppMessage received: " + _appMessage.AppUuid.ToString()); } WatchItem = _pc.WatchItems.FindLast(x => x.ID == _appMessage.AppUuid); if (WatchItem != null) { System.Diagnostics.Debug.WriteLine("AppMessage received: " + WatchItem.Name); await WatchItem.AppMessage(_appMessage.Content); } break; } } catch (Exception exp) { AddToLog(exp.Message); } }
/// <summary> /// Sends a message to the Pebble. /// </summary> /// <param name="message">The message to send.</param> /// <returns>An async task to wait</returns> public Task WriteMessage(P3bbleMessage message) { return Task.Factory.StartNew(() => { this._mutex.WaitOne(); byte[] package = message.ToBuffer(); Logger.WriteLine("<< SEND MESSAGE FOR ENDPOINT " + ((Endpoint)message.Endpoint).ToString() + " (" + ((int)message.Endpoint).ToString() + ")"); Logger.WriteLine("<< PAYLOAD: " + BitConverter.ToString(package)); this._writer.WriteBytes(package); this._writer.StoreAsync().AsTask().Wait(); this._mutex.ReleaseMutex(); }); }
/// <summary> /// Sends a message to the Pebble. /// </summary> /// <param name="message">The message to send.</param> /// <returns>An async task to wait</returns> public Task WriteMessage(P3bbleMessage message) { return Task.Factory.StartNew(() => { this._mutex.WaitOne(); byte[] package = message.ToBuffer(); ServiceLocator.Logger.WriteLine("<< SEND MESSAGE FOR ENDPOINT " + ((Endpoint)message.Endpoint).ToString() + " (" + ((int)message.Endpoint).ToString() + ")"); ServiceLocator.Logger.WriteLine("<< PAYLOAD: " + BitConverter.ToString(package)); this._writer.WriteBytes(package); this._writer.StoreAsync().AsTask().Wait(); this._mutex.ReleaseMutex(); }); }
private async void Run(object host) { #if NETFX_CORE Task.Factory.StartNew(() => { #endif while (_isRunning) { try { await _reader.LoadAsync(4); uint payloadLength; uint endpoint; if (_reader.UnconsumedBufferLength > 0) { IBuffer buffer = _reader.ReadBuffer(4); GetLengthAndEndpoint(buffer, out payloadLength, out endpoint); #if DEBUG Debug.WriteLine("ENDPOINT: " + endpoint); Debug.WriteLine("PAYLOADS: " + payloadLength); #endif await _reader.LoadAsync(payloadLength); IBuffer buf = _reader.ReadBuffer(payloadLength); P3bbleMessage msg = await ReadMessage(buf, endpoint); if (msg != null) { if (MessageReceived != null) MessageReceived(this, new P3bbleMessageReceivedEventArgs(msg)); } } } catch { } #if NETFX_CORE Task.Delay(100); #endif } #if NETFX_CORE }, TaskCreationOptions.LongRunning); #endif }
private Task<P3bbleMessage> ReadMessage(IBuffer buffer, uint endpoint) { List<byte> lstBytes = new List<byte>(); byte[] payloadContentByte = new byte[buffer.Length]; using (var dr = DataReader.FromBuffer(buffer)) { dr.ReadBytes(payloadContentByte); } lstBytes = payloadContentByte.ToList(); #if DEBUG byte[] array = lstBytes.ToArray(); Debug.WriteLine("PAYLOAD: " + BitConverter.ToString(array)); #endif return Task.FromResult<P3bbleMessage>(P3bbleMessage.CreateMessage((P3bbleEndpoint)endpoint, lstBytes)); }
/// <summary> /// Process the tennis message /// </summary> /// <param name="message"></param> private async void TennisMessageReceived(P3bbleMessage message) { var localSettings = ApplicationData.Current.LocalSettings; if (message.Endpoint == P3bble.Constants.Endpoint.ApplicationMessage) { P3bble.Messages.AppMessage _tennisMessage = (P3bble.Messages.AppMessage)message; if (_tennisMessage.AppUuid == Guid.Parse("51a56b50-f87d-ce41-a0ff-30d03a88fa8d")) { System.Diagnostics.Debug.WriteLine("TennisMessage received"); P3bble.Messages.AppMessage AckMessage = new P3bble.Messages.AppMessage(); AckMessage.Command = P3bble.Messages.AppCommand.Ack; AckMessage.TransactionId = _tennisMessage.TransactionId; await _pc.Pebble._protocol.WriteMessage(AckMessage); System.Diagnostics.Debug.WriteLine("TennisMessage Acknowledged"); if (TennisMatch.Paused) { System.Diagnostics.Debug.WriteLine("Match suspended; message ignored."); return; } if (_tennisMessage.Content.ContainsKey(1)) { int ActionCode = (int)_tennisMessage.Content[1]; System.Diagnostics.Debug.WriteLine(String.Format("Tennis action: {0}", ActionCode)); switch (ActionCode) { case 0: TennisMatch.ProcessAction("CommandLose"); break; case 1: TennisMatch.ProcessAction("CommandWin"); break; case 2: TennisMatch.ProcessAction("CommandSecondServe"); System.Diagnostics.Debug.WriteLine("CommandSecondServe"); break; case 3: TennisMatch.ProcessAction("CommandAce"); break; case 5: TennisMatch.ProcessAction("CommandDoubleFault"); System.Diagnostics.Debug.WriteLine("CommandDoubleFault"); break; case 4: TennisMatch.ProcessAction("CommandUndo"); break; case 16: TennisMatch.ProcessAction("CommandExtend"); break; case 128: string log = (string)_tennisMessage.Content[2]; System.Diagnostics.Debug.WriteLine(log); break; case 254: //request new state System.Diagnostics.Debug.WriteLine("Tennis: new state requested"); break; } await SaveMatchState(); } } } }
public P3bbleMessageReceivedEventArgs(P3bbleMessage message) :base() { Message = message; }