/// <summary> /// Adds a command to the configured commands. This properly initializes the class, subscribes to the command topic and writes it to the config file. /// </summary> /// <param name="commandType"></param> /// <param name="json"></param> public void AddCommand(AvailableCommands commandType, string json) { var serializerOptions = new JsonSerializerOptions { Converters = { new DynamicJsonConverter() } }; dynamic model = JsonSerializer.Deserialize <dynamic>(json, serializerOptions); AbstractCommand commandToCreate = null; switch (commandType) { case AvailableCommands.ShutdownCommand: commandToCreate = new ShutdownCommand(this._publisher, model.Name); break; case AvailableCommands.RestartCommand: commandToCreate = new RestartCommand(this._publisher, model.Name); break; case AvailableCommands.LogOffCommand: commandToCreate = new LogOffCommand(this._publisher, model.Name); break; case AvailableCommands.CustomCommand: commandToCreate = new CustomCommand(this._publisher, model.Command, model.Name); break; case AvailableCommands.PlayPauseCommand: commandToCreate = new MediaPlayPauseCommand(this._publisher, model.Name); break; case AvailableCommands.NextCommand: commandToCreate = new MediaNextCommand(this._publisher, model.Name); break; case AvailableCommands.PreviousCommand: commandToCreate = new MediaPreviousCommand(this._publisher, model.Name); break; case AvailableCommands.VolumeUpCommand: commandToCreate = new MediaVolumeUpCommand(this._publisher, model.Name); break; case AvailableCommands.VolumeDownCommand: commandToCreate = new MediaVolumeDownCommand(this._publisher, model.Name); break; case AvailableCommands.MuteCommand: commandToCreate = new MediaMuteCommand(this._publisher, model.Name); break; case AvailableCommands.KeyCommand: commandToCreate = new KeyCommand(this._publisher, Convert.ToByte(model.Key, 16), model.Name); break; default: Log.Logger.Error("Unknown sensortype"); break; } if (commandToCreate != null) { this._configurationService.AddConfiguredCommand(commandToCreate); } }
public async void ReadCommandSettings(MqttPublisher publisher) { while (this.CommandSettingsFileLocked) { await Task.Delay(500); } this.CommandSettingsFileLocked = true; List <ConfiguredCommand> commands = new List <ConfiguredCommand>(); using (var stream = new FileStream(Path.Combine(path, "configured-commands.json"), FileMode.Open)) { Log.Logger.Information($"reading configured commands from: {stream.Name}"); if (stream.Length > 0) { commands = await JsonSerializer.DeserializeAsync <List <ConfiguredCommand> >(stream); } stream.Close(); this.CommandSettingsFileLocked = false; } foreach (ConfiguredCommand configuredCommand in commands) { AbstractCommand command = null; switch (configuredCommand.Type) { case "ShutdownCommand": command = new ShutdownCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; case "RestartCommand": command = new RestartCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; case "LogOffCommand": command = new LogOffCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; case "CustomCommand": command = new CustomCommand(publisher, configuredCommand.Command, configuredCommand.Name, configuredCommand.Id); break; case "MediaPlayPauseCommand": command = new MediaPlayPauseCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; case "MediaNextCommand": command = new MediaNextCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; case "MediaPreviousCommand": command = new MediaPreviousCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; case "MediaVolumeUpCommand": command = new MediaVolumeUpCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; case "MediaVolumeDownCommand": command = new MediaVolumeDownCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; case "MediaMuteCommand": command = new MediaMuteCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; case "KeyCommand": command = new KeyCommand(publisher, configuredCommand.KeyCode, configuredCommand.Name, configuredCommand.Id); break; default: Log.Logger.Error("unsupported command type in config"); break; } if (command != null) { this.ConfiguredCommands.Add(command); } } }