private void OnRemoteEvent(RemoteEventArgs e) { EventHandler<RemoteEventArgs> handler = RemoteEvent; if (handler != null) { handler(this, e); } }
//------------------------------------------------------------------------------------------------------------------ #region callbacks /// <summary> /// Socket-communication callback to receive message from the IRSS server. /// </summary> /// <param name="received">received message, defined mostly by a Type and Flags</param> private void ReceivedMessage(IrssMessage received) { // default (most common) values RemoteEventArgs args = new RemoteEventArgs(); args.Sender = RemoteEventArgs.IrServer; args.Key = received.Type.ToString(); args.Data = received.Flags.ToString() + ": " + received.GetDataAsString(); try { switch (received.Type) { case MessageType.RegisterClient: if ((received.Flags & MessageFlags.Success) == MessageFlags.Success) { Connected = true; ServerInfo = IRServerInfo.FromBytes(received.GetDataAsBytes()); irss.Send(new IrssMessage(MessageType.ActiveReceivers, MessageFlags.Request)); irss.Send(new IrssMessage(MessageType.ActiveBlasters, MessageFlags.Request)); _connectedEvent.Set(); } else if ((received.Flags & MessageFlags.Failure) == MessageFlags.Failure) { Connected = false; } break; case MessageType.ActiveBlasters: LogInfo(received.GetDataAsString()); break; case MessageType.ActiveReceivers: LogInfo(received.GetDataAsString()); break; case MessageType.RemoteEvent: // raise the event args.Sender = received.MessageData[IrssMessage.DEVICE_NAME] as string; args.Key = received.MessageData[IrssMessage.KEY_CODE] as string; args.Data = ""; break; case MessageType.LearnIR: _learnSuccess = (received.Flags & MessageFlags.Success) == MessageFlags.Success; if (_learnSuccess) { byte[] dataBytes = received.GetDataAsBytes(); if (_irFile != null && _irFile != "") { string DebugIRFile = Path.Combine(Common.FolderIRCommands, _irFile); using (FileStream file = File.Create(DebugIRFile)) file.Write(dataBytes, 0, dataBytes.Length); } } _learntEvent.Set(); break; case MessageType.ServerShutdown: Connected = false; LogInfo("Disconnected by host " + ServerHost); LogInfo(received.GetDataAsString()); break; case MessageType.Error: LogErr(received.GetDataAsString()); break; } } catch (Exception ex) { LogErr(ex.Message); args.Key = "Error"; args.Data = "ex.Message"; } LogDebug(String.Format("rise RemoteEvent: Sender: {0} - Key: {1} - Data: {2}", args.Sender, args.Key, args.Data)); OnRemoteEvent(args); }