예제 #1
0
        private async void ReceiveCloudToDeviceMessagesAsync()
        {
            Debug.WriteLine("\nReceiving cloud to device messages from service");

            EnsureDeviceClient();

            while (IsListening)
            {
                await Task.Delay(1000);

                Message receivedMessage = await deviceClient.ReceiveAsync();

                if (receivedMessage == null)
                {
                    continue;
                }

                var messageBytes  = receivedMessage.GetBytes();
                var messageString = Encoding.ASCII.GetString(messageBytes);

                //Fire the general message received event
                NotifyMessageReceived(receivedMessage, messageBytes, messageString);

                //Attempt to parse it into a CommandMessage
                CloudToDeviceMessage commandMessage = null;
                try
                {
                    commandMessage = JsonConvert.DeserializeObject <CloudToDeviceMessage>(messageString);
                }
                catch
                {
                }

                //if (serializationService.TryDeserialize<CommandMessage>(messageString, out commandMessage))
                if (commandMessage != null)
                {
                    string commandString = commandMessage.Command.Trim().ToLower();
                    string payload       = commandMessage.Parameters;
                    switch (commandString)
                    {
                    case "ping":
                        PingCommandReceived?.Invoke(null, new PingCommandEventArgs(receivedMessage, messageBytes, messageString, commandMessage, payload));
                        break;

                    case "brew":
                        BrewCommandReceived?.Invoke(null, new BrewCommandEventArgs(receivedMessage, messageBytes, messageString, commandMessage, payload));
                        break;
                    //case "takepicture":
                    //  int camera;
                    //  if(int.TryParse(commandMessage.Parameters,out camera))
                    //  {
                    //    TakePictureCommandReceived?.Invoke(null, new TakePictureCommandEventArgs(receivedMessage, messageBytes, messageString, commandMessage, camera));
                    //  }
                    //  else
                    //  {
                    //    NotifyBadMessageReceived(receivedMessage, messageBytes, messageString);
                    //  }
                    //  break;
                    //case "answer":
                    //  string answer = commandMessage.Parameters.Trim();
                    //  if(!String.IsNullOrWhiteSpace(answer))
                    //  {
                    //    AnswerCommandReceived?.Invoke(null, new AnswerCommandEventArgs(receivedMessage, messageBytes, messageString, commandMessage, answer));
                    //  }
                    //  else
                    //  {
                    //    NotifyBadMessageReceived(receivedMessage, messageBytes, messageString);
                    //  }
                    //  break;

                    default:
                        // Send a generic event about the message being received
                        //NotifyMessageReceived(receivedMessage, messageBytes, messageString);
                        UnknownCommandReceived?.Invoke(null, new CommandEventArgs(receivedMessage, messageBytes, messageString, commandMessage, payload));
                        break;
                    }
                }
                else
                {
                    NotifyBadMessageReceived(receivedMessage, messageBytes, messageString);
                }

                // Go ahead and "complete" every message, even if we don't know we've processed it
                // We'll take the "UDP" approach for now. Consider doing this only after you know a message
                // has been successfully processed in the future...
                await deviceClient.CompleteAsync(receivedMessage);
            }
        }
예제 #2
0
 protected virtual void OnUnknownCommandReceived(MessageInfo message)
 {
     UnknownCommandReceived?.Invoke(this, new MessageEventArgs(message));
 }
예제 #3
0
파일: Node.cs 프로젝트: 1iveowl/ZWave4Net
 protected virtual void OnUnknownCommandReceived(NodeEventArgs args)
 {
     UnknownCommandReceived?.Invoke(this, args);
 }