/// <summary> /// Gets the <see cref="Command"/> encoded by given command string. /// </summary> /// <param name="commandString">The command string to parse.</param> /// <returns>The <see cref="Command"/> encoded by given command string.</returns> public static Command GetCommand(string commandString) { if (commandString.StartsWith("IRCODE")) { return(IrCommand.Parse(commandString)); } if (commandString.StartsWith("KEYBOARD")) { return(KeyboardCommand.Parse(commandString)); } if (commandString.StartsWith("SETCH")) { return(SetChCommand.Parse(commandString)); } if (commandString.StartsWith("FORCECH")) { return(ForceChCommand.Parse(commandString)); } if (commandString.StartsWith("TELEPORT")) { return(TeleportCommand.Parse(commandString)); } return(null); }
public ApplicationArchiVR applicationArchiVR; // TODO: Design defect: this should be 'private' but because of unit testing we cannot make it so! #endregion /// <summary> /// <see cref="Server.OnClientConnected(ClientConnection)"/> implementation. /// </summary> override protected void OnClientConnected(ClientConnection newClientConnection) { _log.Warning(string.Format("ServerArchiVR.OnClientConnected Client[{0}]", newClientConnection.ClientID)); // Now the client is connected, initialize it to the server's application state. // A) Make the new Client know about existing Players. lock (applicationArchiVR.Players) { if (applicationArchiVR.Players.Count == 0) { _log.Warning(string.Format("ServerArchiVR.OnClientConnected Client[{0}]: No pre-existing clients", newClientConnection.ClientID)); } // For each existing player, ... foreach (var player in applicationArchiVR.Players.Values) { //... except players hosted by the new client ... if (player.ClientID != newClientConnection.ClientID) { _log.Warning(string.Format("ServerArchiVR.OnClientConnected Client[{0}]: Notify new client about existing player {1}", newClientConnection.ClientID, player.LogID)); // ... send an AddPlayerCommand. SendCommand(new AddPlayerCommand(player), newClientConnection); // This makes the new client initialize a remote user for the existing client. } else { _log.Warning(string.Format("ServerArchiVR.OnClientConnected Client[{0}]: Do not notify new client about existing player {1} because it is his/hers", newClientConnection.ClientID, player.ID)); } } } //B) Initialize new client to... if (clientConnections.Count > 1) // Hack to distinguish the local client running on server host. -> must/will be taken care of in ServerClient implementation. { // ...correct project/POI: spawn at the current Project and POI. var teleportCommand = new TeleportCommand(); teleportCommand.ProjectIndex = applicationArchiVR.ActiveProjectIndex; teleportCommand.POIName = applicationArchiVR.ActivePOIName; SendCommand(teleportCommand, newClientConnection); // ...the same immersion mode. //var setImmersionModeCommand = new SetImmersionModeCommand(); //setImmersionModeCommand.ImmersionModeIndex = applicationArchiVR.ActiveImmersionModeIndex; //SendCommand(setImmersionModeCommand, newClientConnection); } bool playerForClientReceived = false; while (!playerForClientReceived) { lock (applicationArchiVR.Players) { foreach (var player in applicationArchiVR.Players.Values) { if (player.ClientID == newClientConnection.ClientID) { playerForClientReceived = true; break; } } } } }
/// <summary> /// Handles commands for the TiVo published to MQTT. /// </summary> /// <param name="e">Event args.</param> protected override async void Mqtt_MqttMsgPublishReceived(MqttApplicationMessageReceivedEventArgs e) { var message = e.ApplicationMessage.ConvertPayloadToString(); _log.LogInformation("MQTT message received for topic " + e.ApplicationMessage.Topic + ": " + message); var commandType = e.ApplicationMessage.Topic.Replace(TopicRoot + "/controls/", string.Empty).Replace("/set", string.Empty); Command command = null; if (commandType == "setCh") { var messageParts = message.Split('.'); if (int.TryParse(messageParts[0], out int channel)) { if (messageParts.Length == 1) { command = new SetChCommand { Channel = channel }; } else if (messageParts.Length == 2 && int.TryParse(messageParts[0], out int subchannel)) { command = new SetChCommand { Channel = channel, Subchannel = subchannel }; } } } if (commandType == "forceCh") { var messageParts = message.Split('.'); if (int.TryParse(messageParts[0], out int channel)) { if (messageParts.Length == 1) { command = new ForceChCommand { Channel = channel }; } else if (messageParts.Length == 2 && int.TryParse(messageParts[0], out int subchannel)) { command = new ForceChCommand { Channel = channel, Subchannel = subchannel }; } } } if (commandType == "irCode") { command = new IrCommand { IrCode = message } } ; if (commandType == "teleport") { command = new TeleportCommand { TeleportCode = message } } ; if (commandType == "keyboard") { command = new KeyboardCommand { KeyboardCode = message } } ; if (command != null) { await _client.SendCommandAsync(command) .ConfigureAwait(false); } }