Esempio n. 1
0
        public override string ReadLine()
        {
            string lineToSend = null;

            if (waitingForUserInput)
            {
                lineToSend = "";
                Thread.Sleep(100);

                if (timeHaveBeenWaiting.IsRunning &&
                    timeHaveBeenWaiting.Elapsed.TotalSeconds > maxTimeToWaitForOk)
                {
                    if (commandsToRepeat.Count > 0)
                    {
                        // We timed out without the user responding. Cancel the operation.
                        Reset();
                    }
                    else
                    {
                        // everything normal continue after time waited
                        Continue();
                    }
                }

                if (maxTimeToWaitForOk > 0 &&
                    timeHaveBeenWaiting.Elapsed.TotalSeconds < maxTimeToWaitForOk &&
                    commandsToRepeat.Count > 0)
                {
                    lineToSend = commandsToRepeat[repeatCommandIndex % commandsToRepeat.Count];
                    repeatCommandIndex++;
                }
            }
            else
            {
                // lock queue
                lock (locker)
                {
                    if (commandQueue.Count > 0)
                    {
                        lineToSend = commandQueue[0];
                        lineToSend = GCodeProcessing.ReplaceMacroValues(lineToSend);
                        commandQueue.RemoveAt(0);
                    }
                }

                if (lineToSend != null)
                {
                    if (lineToSend.StartsWith(MacroPrefix) && lineToSend.TrimEnd().EndsWith(")"))
                    {
                        if (!runningMacro)
                        {
                            runningMacro = true;
                            int extruderCount = ActiveSliceSettings.Instance.GetValue <int>(SettingsKey.extruder_count);
                            for (int i = 0; i < extruderCount; i++)
                            {
                                startingExtruderTemps.Add(PrinterConnectionAndCommunication.Instance.GetTargetExtruderTemperature(i));
                            }

                            if (ActiveSliceSettings.Instance.GetValue <bool>(SettingsKey.has_heated_bed))
                            {
                                startingBedTemp = PrinterConnectionAndCommunication.Instance.TargetBedTemperature;
                            }
                        }
                        int    parensAfterCommand = lineToSend.IndexOf('(', MacroPrefix.Length);
                        string command            = "";
                        if (parensAfterCommand > 0)
                        {
                            command = lineToSend.Substring(MacroPrefix.Length, parensAfterCommand - MacroPrefix.Length);
                        }

                        RunningMacroPage.MacroCommandData macroData = new RunningMacroPage.MacroCommandData();

                        string value = "";
                        if (TryGetAfterString(lineToSend, "title", out value))
                        {
                            macroData.title = value;
                        }
                        if (TryGetAfterString(lineToSend, "expire", out value))
                        {
                            double.TryParse(value, out macroData.expireTime);
                            maxTimeToWaitForOk = macroData.expireTime;
                        }
                        if (TryGetAfterString(lineToSend, "count_down", out value))
                        {
                            double.TryParse(value, out macroData.countDown);
                        }
                        if (TryGetAfterString(lineToSend, "image", out value))
                        {
                            macroData.image = LoadImageAsset(value);
                        }
                        if (TryGetAfterString(lineToSend, "wait_ok", out value))
                        {
                            macroData.waitOk = value == "true";
                        }
                        if (TryGetAfterString(lineToSend, "repeat_gcode", out value))
                        {
                            foreach (string line in value.Split('|'))
                            {
                                commandsToRepeat.Add(line);
                            }
                        }

                        switch (command)
                        {
                        case "choose_material":
                            waitingForUserInput            = true;
                            macroData.showMaterialSelector = true;
                            macroData.waitOk = true;
                            UiThread.RunOnIdle(() => RunningMacroPage.Show(macroData));
                            break;

                        case "close":
                            runningMacro = false;
                            UiThread.RunOnIdle(() => WizardWindow.Close("Macro"));
                            break;

                        case "ding":
                            MatterControlApplication.Instance.PlaySound("timer-done.wav");
                            break;

                        case "show_message":
                            waitingForUserInput = macroData.waitOk | macroData.expireTime > 0;
                            UiThread.RunOnIdle(() => RunningMacroPage.Show(macroData));
                            break;

                        default:
                            // Don't know the command. Print to terminal log?
                            break;
                        }
                    }
                }
                else
                {
                    lineToSend = base.ReadLine();
                }
            }

            return(lineToSend);
        }
        public override string ReadLine()
        {
            string lineToSend = null;

            if (WaitingForUserInput)
            {
                lineToSend = "";
                Thread.Sleep(100);

                if (timeHaveBeenWaiting.IsRunning &&
                    timeHaveBeenWaiting.Elapsed.TotalSeconds > maxTimeToWaitForOk)
                {
                    if (commandsToRepeat.Count > 0)
                    {
                        // We timed out without the user responding. Cancel the operation.
                        Reset();
                    }
                    else
                    {
                        // everything normal continue after time waited
                        Continue();
                    }
                }

                if (maxTimeToWaitForOk > 0 &&
                    timeHaveBeenWaiting.Elapsed.TotalSeconds < maxTimeToWaitForOk &&
                    commandsToRepeat.Count > 0)
                {
                    lineToSend = commandsToRepeat[repeatCommandIndex % commandsToRepeat.Count];
                    repeatCommandIndex++;
                }
            }
            else
            {
                lineToSend = base.ReadLine();

                if (!string.IsNullOrEmpty(lineToSend))
                {
                    if (lineToSend.StartsWith(MacroPrefix) && lineToSend.TrimEnd().EndsWith(")"))
                    {
                        if (!runningMacro)
                        {
                            runningMacro = true;
                            int extruderCount = printer.Settings.GetValue <int>(SettingsKey.extruder_count);
                            for (int i = 0; i < extruderCount; i++)
                            {
                                startingExtruderTemps.Add(printer.Connection.GetTargetHotendTemperature(i));
                            }

                            if (printer.Settings.GetValue <bool>(SettingsKey.has_heated_bed))
                            {
                                startingBedTemp = printer.Connection.TargetBedTemperature;
                            }
                        }
                        int    parensAfterCommand = lineToSend.IndexOf('(', MacroPrefix.Length);
                        string command            = "";
                        if (parensAfterCommand > 0)
                        {
                            command = lineToSend.Substring(MacroPrefix.Length, parensAfterCommand - MacroPrefix.Length);
                        }

                        RunningMacroPage.MacroCommandData macroData = new RunningMacroPage.MacroCommandData();

                        string value = "";
                        if (TryGetAfterString(lineToSend, "title", out value))
                        {
                            macroData.title = value;
                        }
                        if (TryGetAfterString(lineToSend, "expire", out value))
                        {
                            double.TryParse(value, out macroData.expireTime);
                            maxTimeToWaitForOk = macroData.expireTime;
                            timeHaveBeenWaiting.Restart();
                        }
                        if (TryGetAfterString(lineToSend, "count_down", out value))
                        {
                            double.TryParse(value, out macroData.countDown);
                        }
                        if (TryGetAfterString(lineToSend, "markdown", out value))
                        {
                            macroData.markdown = value.Replace("\\n", "\n");
                            macroData.markdown = value.Replace("<br>", "\n");
                        }
                        if (TryGetAfterString(lineToSend, "wait_ok", out value))
                        {
                            macroData.waitOk = value == "true";
                        }
                        if (TryGetAfterString(lineToSend, "repeat_gcode", out value))
                        {
                            foreach (string line in value.Split('|'))
                            {
                                commandsToRepeat.Add(line);
                            }
                        }

                        switch (command)
                        {
                        case "choose_material":
                            WaitingForUserInput            = true;
                            macroData.showMaterialSelector = true;
                            macroData.waitOk = true;

                            UiThread.RunOnIdle(() =>
                            {
                                // we are continuing normaly
                                if (currentPage != null)
                                {
                                    currentPage.ContinueToNextPage = true;
                                }
                                DialogWindow.Show(currentPage = new RunningMacroPage(printer, macroData, ApplicationController.Instance.Theme));
                            });
                            break;

                        case "close":
                            runningMacro = false;
                            // we are closing normaly
                            if (currentPage != null)
                            {
                                currentPage.ContinueToNextPage = true;
                            }
                            UiThread.RunOnIdle(() => DialogWindow.Close(typeof(RunningMacroPage)));
                            break;

                        case "ding":
                            AppContext.Platform.PlaySound("timer-done.wav");
                            break;

                        case "done_load_unload":
                            if (!printer.Connection.PrinterIsPrinting &&
                                !printer.Connection.PrinterIsPaused)
                            {
                                // turn off the temps
                                printer.Connection.TurnOffBedAndExtruders(TurnOff.AfterDelay);
                            }
                            break;

                        case "show_message":
                            WaitingForUserInput = macroData.waitOk | macroData.expireTime > 0;

                            UiThread.RunOnIdle(() =>
                            {
                                // we are continuing normaly
                                if (currentPage != null)
                                {
                                    currentPage.ContinueToNextPage = true;
                                }
                                DialogWindow.Show(currentPage = new RunningMacroPage(printer, macroData, ApplicationController.Instance.Theme));
                            });

                            break;

                        default:
                            // Don't know the command. Print to terminal log?
                            break;
                        }
                    }
                }
            }

            return(lineToSend);
        }