public override CmdLineResult handleCommand(CmdLineStruct pCmd) { // Gestion héritée des commandes CmdLineResult lCmdLineResult = base.handleCommand(pCmd); if (lCmdLineResult.CmdAccepted) { return(lCmdLineResult); } // Sortie if (pCmd.Line == "q") { return(new CmdLineResult(true, "", getCOMState(), true, true)); } // Accès aux P, I, D de manière individuelle StateBase lNewState = null; if (pCmd.Line == "port") { lNewState = new ComPortChangeState(); } else if (pCmd.Line == "baud") { lNewState = new ComBaudChangeState(); } if (lNewState != null) { setSubState(lNewState); } return(new CmdLineResult(true, "", "", true, false)); }
public override CmdLineResult handleCommand(CmdLineStruct pCmd) { // Gestion héritée des commandes CmdLineResult lCmdLineResult = base.handleCommand(pCmd); if (lCmdLineResult.CmdAccepted) { return(lCmdLineResult); } // Sortie if (pCmd.Line == "q") { SerialCommunication.get().close(); return(new CmdLineResult(true, "", "", true, true)); } // Accès aux P, I, D de manière individuelle StateBase lNewState = null; if (lNewState != null) { setSubState(lNewState); } return(new CmdLineResult(true, "", "", true, false)); }
private Task <int> RunProcessAsync(Process process, CmdLineResult cmdLine) { //Notify("started " + cmdLine.Name); var tcs = new TaskCompletionSource <int>(); var output = new StringBuilder(); var error = new StringBuilder(); try { process.Exited += (s, ea) => { cmdLine.Result = output.ToString(); cmdLine.Error = error.ToString(); cmdLine.RunComplete = true; cmdLine.CmdExitCode = process.ExitCode; tcs.SetResult(process.ExitCode); }; process.OutputDataReceived += (s, e) => { if (!string.IsNullOrEmpty(e.Data)) { output.AppendLine(e.Data); } }; process.ErrorDataReceived += (s, e) => { if (!string.IsNullOrEmpty(e.Data)) { error.AppendLine(e.Data); } }; bool started = process.Start(); if (!started) { //you may allow for the process to be re-used (started = false) //but I'm not sure about the guarantees of the Exited event in such a case _logger.Add($"cmd line cmd failed starting"); throw new InvalidOperationException("Could not start process: " + process); } process.BeginOutputReadLine(); process.BeginErrorReadLine(); return(tcs.Task); } catch (Exception e) { _logger.Add($"cmd line ex : {e}"); tcs.SetResult(-1); return(tcs.Task); } }
private async Task <int> RunProcessAsync(CmdLineResult cmdLine) { if (cmdLine.ExecType == CmdLineExecType.FileContent) { if (_filesystem.FileExists(cmdLine.Cmd)) { try { cmdLine.Result = await _filesystem.ReadFileContentAsync(cmdLine.Cmd).ConfigureAwait(false); cmdLine.RunComplete = true; cmdLine.CmdExitCode = 0; return(cmdLine.CmdExitCode); } catch (Exception e) { cmdLine.Error = e.Message; cmdLine.CmdExitCode = e.HResult; cmdLine.RunComplete = true; return(cmdLine.CmdExitCode); } } cmdLine.Error = "Couldn't find the file."; cmdLine.RunComplete = true; cmdLine.CmdExitCode = 666; return(cmdLine.CmdExitCode); } using (var process = new Process { StartInfo = { FileName = "C:\\Windows\\System32\\cmd.exe", Arguments = "/c " + cmdLine.Cmd, UseShellExecute = false, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, RedirectStandardOutput = true, RedirectStandardError = true, StandardOutputEncoding = Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage), StandardErrorEncoding = Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage), }, EnableRaisingEvents = true }) { return(await RunProcessAsync(process, cmdLine).ConfigureAwait(false)); } }
public override CmdLineResult handleCommand(CmdLineStruct pCmd) { // Gestion héritée des commandes CmdLineResult lCmdLineResult = base.handleCommand(pCmd); if (lCmdLineResult.CmdAccepted) { return(lCmdLineResult); } // Accès aux états de base if (pCmd.Line == "q") { return(new CmdLineResult(true, "", "Exiting", true, true)); } // Changement d'états StateBase lNewSate = null; if ((pCmd.Line == "thrust") || (pCmd.Line == "t")) { lNewSate = new ThrustChangingState(); } else if (pCmd.Line == "pid") { lNewSate = new PIDChangingState(); } else if (pCmd.Line == "com") { lNewSate = new ComState(); } else if (pCmd.Line == "open") { lNewSate = new InFlightState(); } if (lNewSate != null) { setSubState(lNewSate); return(new CmdLineResult(true, "", "", true, false)); } // Fin return(new CmdLineResult(false, "Unknown command", "", false, false)); }
public override CmdLineResult handleCommand(CmdLineStruct pCmd) { // Gestion héritée des commandes CmdLineResult lCmdLineResult = base.handleCommand(pCmd); if (lCmdLineResult.CmdAccepted) { return(lCmdLineResult); } // Sortie if (pCmd.Line == "q") { return(new CmdLineResult(true, "", getBaudrate(), true, true)); } // Regex Regex regex = new Regex(@"^(\d+)+$"); Match match = regex.Match(pCmd.Line); // Commande incorrecte if (match.Groups[1].Success == false) { return(new CmdLineResult(true, "Bad command", "", false, false)); } // De quelle commande s'agit-il ? int lValue; if (match.Groups[1].Value.Length > 0) { lValue = (int)Convert.ToInt32(match.Groups[1].Value); SerialCommunication.get().ComBaudrate = lValue; } return(new CmdLineResult(true, "", "", true, false)); }
public override CmdLineResult handleCommand(CmdLineStruct pCmd) { // Gestion héritée des commandes CmdLineResult lCmdLineResult = base.handleCommand(pCmd); if (lCmdLineResult.CmdAccepted) { return(lCmdLineResult); } /** * Formats acceptés : * q * s * v * +/- * +/- 1 * +/- 1. * +/- 1.2 * 1 * 1. * 1.2 */ // Sortie de l'état string lResult; if (pCmd.Line == "q") { lResult = String.Format("{0} : {1}", getStateName(), getCurrentValue()); return(new CmdLineResult(true, "", lResult, true, true)); } // Récupération de la valeur actuelle else if (pCmd.Line == "g") { lResult = String.Format("{0} : {1}", getStateName(), getCurrentValue()); return(new CmdLineResult(true, lResult, "", true, false)); } // Changement d'action type else if (pCmd.Line == "s") { CurrentActionType = ActionType.Continuous; return(new CmdLineResult(true, "", "", true, false)); } else if (pCmd.Line == "v") { CurrentActionType = ActionType.Once; return(new CmdLineResult(true, "", "", true, false)); } // Regex Regex regex = new Regex(@"^([\+\-]?)\s*(\d*\,?\d*)$"); Match match = regex.Match(pCmd.Line); // Commande incorrecte if ((match.Groups[1].Success == false) && (match.Groups[2].Success == false)) { return(new CmdLineResult(true, "Bad command", "", false, false)); } // De quelle commande s'agit-il ? float lValue; if (match.Groups[2].Value.Length > 0) { lValue = (float)Convert.ToDouble(match.Groups[2].Value); } else { lValue = getStepValue(); } if (match.Groups[1].Value.Length > 0) { if (match.Groups[1].Value == "-") { lValue = -lValue; } } // Application de la commande increateValue(lValue); // Affichage du résultat lResult = String.Format("{0} : {1}", getStateName(), getCurrentValue()); lCmdLineResult = new CmdLineResult(true, lResult, "", true, false); return(lCmdLineResult); }
public void handleCommands() { /** * Les 3 dernières lignes de la console contiennent : * - le chemin de l'état actuel, * - la commande en cours d'écriture, * - une ligne affichant le retour de l'état en cours, vis-à-vis de la commande en cours d'écriture */ System.Console.WriteLine("Enter your commands."); _CurrentConsoleBlockTop = System.Console.CursorTop; updateOutput("", "", ""); System.ConsoleKeyInfo cki = new System.ConsoleKeyInfo(); // Boucle de traitement infinie string lLineBuffer = ""; bool lLineDone = false; bool lDone = false; while (lDone == false) { if (System.Console.KeyAvailable == true) { // On récupère le caractère sans l'afficher cki = System.Console.ReadKey(true); // On traite les caractères spéciaux, notamment la fin de ligne switch (cki.Key) { case ConsoleKey.Enter: lLineDone = true; break; case ConsoleKey.Backspace: if (lLineBuffer.Length > 0) { lLineBuffer = lLineBuffer.Substring(0, lLineBuffer.Length - 1); } break; default: lLineBuffer += cki.KeyChar; break; } string lResultText = ""; string lRemarkText = ""; if ((_RootState.getStateTreeActionType() == StateBase.ActionType.Continuous) || (lLineDone == true)) { // On traite la ligne CmdLineStruct lCmdLine = new CmdLineStruct(); lCmdLine.Line = lLineBuffer; if (lLineBuffer.Length > 0) { CmdLineResult lCmdResult = _RootState.handleCommand(lCmdLine); lResultText = lCmdResult.ResultText; lRemarkText = lCmdResult.RemarkText; if (lCmdResult.CmdAccepted) { if (lCmdResult.ResetLine) { lLineDone = true; } } if (lCmdResult.StateEnded) { lDone = true; } } // if (lLineDone) { lLineBuffer = ""; lLineDone = false; } } // On met à jour les deux dernières lignes updateOutput(lResultText, lLineBuffer, lRemarkText); } else { //if (MessageManager.get().treatMessages() == true) { } System.Threading.Thread.Sleep(1); } } }