예제 #1
0
    private void ServerReceivedMessage(MessageManagerCombo combo)
    {
      IrssLog.Debug("Server message received: {0}, {1}", combo.Message.Type, combo.Message.Flags);

      try
      {
        switch (combo.Message.Type)
        {
          #region ForwardRemoteEvent

          case MessageType.ForwardRemoteEvent:
            if (Settings.Mode == IRServerMode.RelayMode)
            {
              IrssMessage forward = new IrssMessage(MessageType.ForwardRemoteEvent, MessageFlags.Request,
                                                    combo.Message.GetDataAsBytes());
              _client.Send(forward);
            }
            else
            {
              byte[] data = combo.Message.GetDataAsBytes();

              if (Settings.AbstractRemoteMode)
              {
                // Decode message ...
                string deviceName = combo.Message.MessageData[IrssMessage.DEVICE_NAME] as string;
                string keyCode = combo.Message.MessageData[IrssMessage.KEY_CODE] as string;

                // Check that the device maps are loaded for the forwarded device
                bool foundDevice = false;
                if (_receivePlugins != null)
                {
                  foreach (PluginBase plugin in _receivePlugins)
                  {
                    if (plugin is IRemoteReceiver && plugin.Name.Equals(deviceName, StringComparison.OrdinalIgnoreCase))
                    {
                      foundDevice = true;
                      break;
                    }
                  }
                }

                // If the remote maps are not already loaded for this device then attempt to load them
                if (!foundDevice)
                  foundDevice = LoadAbstractDeviceFiles(deviceName);

                // If the device map is loaded then try to convert the button to abstract
                if (foundDevice)
                {
                  // Find abstract button mapping
                  string abstractButton = LookupAbstractButton(deviceName, keyCode);
                  if (String.IsNullOrEmpty(abstractButton))
                  {
                    IrssLog.Info("Abstract Remote Button mapped from forwarded remote event: {0}", abstractButton);

                    // Encode new message ...
                    data = IrssMessage.EncodeRemoteEventData("Abstract", abstractButton);
                  }
                  else
                  {
                    IrssLog.Info("Abstract Remote Button not found for forwarded remote event: {0} ({1})", deviceName,
                                 keyCode);
                  }
                }
              }

              IrssMessage forward = new IrssMessage(MessageType.RemoteEvent, MessageFlags.Notify, data);
              SendToAllExcept(combo.Manager, forward);
            }
            break;

          #endregion ForwardRemoteEvent

          #region ForwardKeyboardEvent

          case MessageType.ForwardKeyboardEvent:
            if (Settings.Mode == IRServerMode.RelayMode)
            {
              IrssMessage forward = new IrssMessage(MessageType.ForwardKeyboardEvent, MessageFlags.Request,
                                                    combo.Message.GetDataAsBytes());
              _client.Send(forward);
            }
            else
            {
              IrssMessage forward = new IrssMessage(MessageType.KeyboardEvent, MessageFlags.Notify,
                                                    combo.Message.GetDataAsBytes());
              SendToAllExcept(combo.Manager, forward);
            }
            break;

          #endregion ForwardKeyboardEvent

          #region ForwardMouseEvent

          case MessageType.ForwardMouseEvent:
            if (Settings.Mode == IRServerMode.RelayMode)
            {
              IrssMessage forward = new IrssMessage(MessageType.ForwardMouseEvent, MessageFlags.Request,
                                                    combo.Message.GetDataAsBytes());
              _client.Send(forward);
            }
            else
            {
              IrssMessage forward = new IrssMessage(MessageType.MouseEvent, MessageFlags.Notify,
                                                    combo.Message.GetDataAsBytes());
              SendToAllExcept(combo.Manager, forward);
            }
            break;

          #endregion ForwardMouseEvent

          #region BlastIR

          case MessageType.BlastIR:
            {
              IrssMessage response = new IrssMessage(MessageType.BlastIR, MessageFlags.Response);

              if (Settings.Mode == IRServerMode.RelayMode)
              {
                response.Flags |= MessageFlags.Failure;
              }
              else
              {
                if (_registeredRepeaters.Count > 0)
                  SendToRepeaters(combo.Message);

                if (BlastIR(combo.Message.GetDataAsBytes()))
                  response.Flags |= MessageFlags.Success;
                else
                  response.Flags |= MessageFlags.Failure;
              }

              if ((combo.Message.Flags & MessageFlags.ForceNotRespond) != MessageFlags.ForceNotRespond)
                SendTo(combo.Manager, response);

              break;
            }

          #endregion BlastIR

          #region LearnIR

          case MessageType.LearnIR:
            {
              IrssMessage response = new IrssMessage(MessageType.LearnIR, MessageFlags.Response);

              if (Settings.Mode == IRServerMode.RelayMode)
              {
                response.Flags |= MessageFlags.Failure;
              }
              else
              {
                byte[] bytes;
                LearnStatus status = LearnIR(out bytes);

                switch (status)
                {
                  case LearnStatus.Success:
                    response.Flags |= MessageFlags.Success;
                    response.SetDataAsBytes(bytes);
                    break;

                  case LearnStatus.Failure:
                    response.Flags |= MessageFlags.Failure;
                    break;

                  case LearnStatus.Timeout:
                    response.Flags |= MessageFlags.Timeout;
                    break;
                }
              }

              SendTo(combo.Manager, response);
              break;
            }

          #endregion LearnIR

          #region ServerShutdown

          case MessageType.ServerShutdown:
            if ((combo.Message.Flags & MessageFlags.Request) == MessageFlags.Request)
            {
              IrssLog.Info("Shutdown command received");
              Stop();
            }
            break;

          #endregion ServerShutdown

          #region RegisterClient

          case MessageType.RegisterClient:
            {
              IrssMessage response = new IrssMessage(MessageType.RegisterClient, MessageFlags.Response);

              if (RegisterClient(combo.Manager))
              {
                IRServerInfo irServerInfo = new IRServerInfo();

                if (_receivePlugins != null)
                  irServerInfo.CanReceive = true;

                if (_transmitPlugin != null)
                {
                  irServerInfo.CanLearn = (_transmitPlugin is ILearnIR);
                  irServerInfo.CanTransmit = true;
                  irServerInfo.Ports = ((ITransmitIR)_transmitPlugin).AvailablePorts;
                }

                response.SetDataAsBytes(irServerInfo.ToBytes());
                response.Flags |= MessageFlags.Success;
              }
              else
              {
                response.Flags |= MessageFlags.Failure;
              }

              SendTo(combo.Manager, response);
              break;
            }

          #endregion RegisterClient

          #region UnregisterClient

          case MessageType.UnregisterClient:
            UnregisterClient(combo.Manager);
            break;

          #endregion UnregisterClient

          #region RegisterRepeater

          case MessageType.RegisterRepeater:
            {
              IrssMessage response = new IrssMessage(MessageType.RegisterRepeater, MessageFlags.Response);

              if (RegisterRepeater(combo.Manager))
                response.Flags |= MessageFlags.Success;
              else
                response.Flags |= MessageFlags.Failure;

              SendTo(combo.Manager, response);
              break;
            }

          #endregion RegisterRepeater

          #region UnregisterRepeater

          case MessageType.UnregisterRepeater:
            UnregisterRepeater(combo.Manager);
            break;

          #endregion UnregisterRepeater

          #region ActiveBlasters

          case MessageType.ActiveBlasters:
            {
              IrssMessage response = new IrssMessage(MessageType.ActiveBlasters, MessageFlags.Response);
              response.SetDataAsString(Settings.PluginNameTransmit);

              SendTo(combo.Manager, response);
              break;
            }

          #endregion ActiveBlasters

          #region ActiveReceivers

          case MessageType.ActiveReceivers:
            {
              IrssMessage response = new IrssMessage(MessageType.ActiveReceivers, MessageFlags.Response);

              if (Settings.PluginNameReceive != null)
              {
                StringBuilder receivers = new StringBuilder();
                for (int index = 0; index < Settings.PluginNameReceive.Length; index++)
                {
                  receivers.Append(Settings.PluginNameReceive[index]);

                  if (index < Settings.PluginNameReceive.Length - 1)
                    receivers.Append(',');
                }

                response.SetDataAsString(receivers.ToString());
              }
              else
              {
                response.SetDataAsString(null);
              }

              SendTo(combo.Manager, response);
              break;
            }

          #endregion ActiveReceivers

          #region AvailableBlasters

          case MessageType.AvailableBlasters:
            {
              IrssMessage response = new IrssMessage(MessageType.AvailableBlasters, MessageFlags.Response);

              PluginBase[] plugins = BasicFunctions.AvailablePlugins();
              StringBuilder blasters = new StringBuilder();

              for (int index = 0; index < plugins.Length; index++)
              {
                PluginBase plugin = plugins[index];

                if (plugin is ITransmitIR)
                {
                  blasters.Append(plugin.Name);
                  blasters.Append(',');
                }
              }

              if (blasters.Length == 0)
              {
                response.SetDataAsString(null);
              }
              else
              {
                blasters.Remove(blasters.Length - 1, 1);
                response.SetDataAsString(blasters.ToString());
              }

              SendTo(combo.Manager, response);
              break;
            }

          #endregion AvailableBlasters

          #region AvailableReceivers

          case MessageType.AvailableReceivers:
            {
              IrssMessage response = new IrssMessage(MessageType.AvailableReceivers, MessageFlags.Response);

              PluginBase[] plugins = BasicFunctions.AvailablePlugins();
              StringBuilder receivers = new StringBuilder();

              for (int index = 0; index < plugins.Length; index++)
              {
                PluginBase plugin = plugins[index];

                if (plugin is IRemoteReceiver || plugin is IKeyboardReceiver || plugin is IMouseReceiver)
                {
                  receivers.Append(plugin.Name);
                  receivers.Append(',');
                }
              }

              if (receivers.Length == 0)
              {
                response.SetDataAsString(null);
              }
              else
              {
                receivers.Remove(receivers.Length - 1, 1);
                response.SetDataAsString(receivers.ToString());
              }

              SendTo(combo.Manager, response);
              break;
            }

          #endregion AvailableReceivers

          #region DetectedBlasters

          case MessageType.DetectedBlasters:
            {
              IrssMessage response = new IrssMessage(MessageType.DetectedBlasters, MessageFlags.Response);
              string[] detectedBlasters = Shared.DetectBlasters();

              if (detectedBlasters != null && detectedBlasters.Length > 0)
              {
                StringBuilder blasters = new StringBuilder();
                for (int index = 0; index < detectedBlasters.Length; index++)
                {
                  blasters.Append(detectedBlasters[index]);

                  if (index < detectedBlasters.Length - 1)
                    blasters.Append(',');
                }

                response.SetDataAsString(blasters.ToString());
              }
              else
              {
                response.SetDataAsString(null);
              }

              SendTo(combo.Manager, response);
              break;
            }

          #endregion DetectedBlasters

          #region DetectedReceivers

          case MessageType.DetectedReceivers:
            {
              IrssMessage response = new IrssMessage(MessageType.DetectedReceivers, MessageFlags.Response);
              string[] detectedReceivers = Shared.DetectReceivers();

              if (detectedReceivers != null && detectedReceivers.Length > 0)
              {
                StringBuilder receivers = new StringBuilder();
                for (int index = 0; index < detectedReceivers.Length; index++)
                {
                  receivers.Append(detectedReceivers[index]);

                  if (index < detectedReceivers.Length - 1)
                    receivers.Append(',');
                }

                response.SetDataAsString(receivers.ToString());
              }
              else
              {
                response.SetDataAsString(null);
              }

              SendTo(combo.Manager, response);
              break;
            }

          #endregion DetectedReceivers
        }
      }
      catch (Exception ex)
      {
        IrssLog.Error(ex);
        IrssMessage response = new IrssMessage(MessageType.Error, MessageFlags.Notify, ex.Message);
        SendTo(combo.Manager, response);
      }
    }