public static P3bbleMessage CreateMessage(P3bbleEndpoint endpoint, List <byte> payload) { P3bbleMessage frame = null; switch (endpoint) { case P3bbleEndpoint.Ping: frame = new PingMessage(); break; case P3bbleEndpoint.Version: frame = new VersionMessage(); break; case P3bbleEndpoint.Time: frame = new TimeMessage(); break; case P3bbleEndpoint.Logs: frame = new LogsMessage(); break; default: frame = new P3bbleMessage(endpoint); break; } frame.GetContentFromMessage(payload); return(frame); }
/// <summary> /// Sends a message to the Pebble and awaits the response. /// </summary> /// <typeparam name="T">The type of message</typeparam> /// <param name="message">The message content.</param> /// <param name="millisecondsTimeout">The milliseconds timeout.</param> /// <returns>A message response</returns> /// <exception cref="System.InvalidOperationException">A message is being waited for already</exception> /// <remarks>Beware when debugging that setting a breakpoint in Protocol.Run or ProtocolMessageReceived will cause the ResetEvent to time out</remarks> private Task <T> SendMessageAndAwaitResponseAsync <T>(P3bbleMessage message, int millisecondsTimeout = 10000) where T : P3bbleMessage { if (this._pendingMessageSignal != null || this.IsBusy) { throw new InvalidOperationException("A message is being waited for already"); } return(Task.Run <T>(async() => { this.IsBusy = true; int startTicks = Environment.TickCount; this._pendingMessageSignal = new ManualResetEventSlim(false); this._pendingMessage = message; // Send the message... await this._protocol.WriteMessage(message); // Wait for the response... this._pendingMessageSignal.Wait(millisecondsTimeout); T pendingMessage = null; if (this._pendingMessageSignal.IsSet) { // Store any response will be null if timed out... pendingMessage = this._pendingMessage as T; } // See if we have a protocol error LogsMessage logMessage = this._pendingMessage as LogsMessage; Type pendingMessageType = this._pendingMessage.GetType(); // Clear the pending variables... this._pendingMessageSignal = null; this._pendingMessage = null; int timeTaken = Environment.TickCount - startTicks; this.IsBusy = false; if (pendingMessage != null) { ServiceLocator.Logger.WriteLine(pendingMessage.GetType().Name + " message received back in " + timeTaken.ToString() + "ms"); } else { ServiceLocator.Logger.WriteLine(message.GetType().Name + " message timed out in " + timeTaken.ToString() + "ms - type received was " + pendingMessageType.ToString()); } return pendingMessage; })); }
/// <summary> /// Creates an incoming message. /// </summary> /// <param name="endpoint">The endpoint.</param> /// <param name="payload">The payload.</param> /// <returns>A specific message type</returns> public static P3bbleMessage CreateMessage(Endpoint endpoint, List <byte> payload) { P3bbleMessage frame = null; switch (endpoint) { case Endpoint.Ping: frame = new PingMessage(); break; case Endpoint.Version: frame = new VersionMessage(); break; case Endpoint.Time: frame = new TimeMessage(); break; case Endpoint.Logs: frame = new LogsMessage(); break; case Endpoint.AppManager: frame = new AppManagerMessage(); break; case Endpoint.MusicControl: frame = new MusicMessage(); break; case Endpoint.ApplicationMessage: frame = new AppMessage(endpoint); break; case Endpoint.PutBytes: frame = new PutBytesMessage(); break; } if (frame != null) { frame.GetContentFromMessage(payload); } return(frame); }
/// <summary> /// Initializes a new instance of the <see cref="ProtocolException"/> class. /// </summary> /// <param name="logMessage">The log message.</param> internal ProtocolException(LogsMessage logMessage) : base(logMessage.Message) { this.LogMessage = logMessage; }