/// <summary>
        /// Run model updates in a certain interval.
        /// This function updates host properties like network interfaces and storage devices
        /// </summary>
        /// <returns>Asynchronous task</returns>
        public static async Task UpdatePeriodically()
        {
            do
            {
                // Run another update cycle
                using (await Provider.AccessReadWriteAsync())
                {
                    UpdateNetwork();
                    UpdateStorages();
                    CleanMessages();
                }

                // Wait for next update schedule
                DateTime lastUpdateTime = DateTime.Now;
                await Task.Delay(Settings.HostUpdateInterval, Program.CancelSource.Token);

                if (DateTime.Now - lastUpdateTime > TimeSpan.FromMilliseconds(Settings.HostUpdateInterval + 1000))
                {
                    // System time has been changed - adjust date and time on the Duet
                    Console.WriteLine("[info] System time has been changed");
                    Code code = new Code
                    {
                        InternallyExecuted = true,
                        Channel            = DuetAPI.CodeChannel.Daemon,
                        Type        = CodeType.MCode,
                        MajorNumber = 905
                    };
                    code.Parameters.Add(new CodeParameter('P', DateTime.Now.ToString("yyyy-MM-dd")));
                    code.Parameters.Add(new CodeParameter('S', DateTime.Now.ToString("HH:mm:ss")));
                    await code.Execute();
                }
            } while (!Program.CancelSource.IsCancellationRequested);
        }
        /// <summary>
        /// Run model updates in a certain interval.
        /// This function updates host properties like network interfaces and storage devices
        /// </summary>
        /// <returns>Asynchronous task</returns>
        public static async Task Run()
        {
            DateTime lastUpdateTime = DateTime.Now;
            string   lastHostname   = Environment.MachineName;

            do
            {
                // Run another update cycle
                using (await Provider.AccessReadWriteAsync())
                {
                    UpdateNetwork();
                    UpdateVolumes();
                    CleanMessages();
                }

                // Check if the system time has to be updated
                if (DateTime.Now - lastUpdateTime > TimeSpan.FromMilliseconds(Settings.HostUpdateInterval + 5000) &&
                    !System.Diagnostics.Debugger.IsAttached)
                {
                    _logger.Info("System time has been changed");
                    Code code = new Code
                    {
                        InternallyProcessed = !Settings.NoSpi,
                        Flags       = CodeFlags.Asynchronous,
                        Channel     = CodeChannel.Trigger,
                        Type        = CodeType.MCode,
                        MajorNumber = 905
                    };
                    code.Parameters.Add(new CodeParameter('P', DateTime.Now.ToString("yyyy-MM-dd")));
                    code.Parameters.Add(new CodeParameter('S', DateTime.Now.ToString("HH:mm:ss")));
                    await code.Execute();
                }

                // Check if the hostname has to be updated
                if (lastHostname != Environment.MachineName)
                {
                    _logger.Info("Hostname has been changed");
                    lastHostname = Environment.MachineName;
                    Code code = new Code
                    {
                        InternallyProcessed = !Settings.NoSpi,
                        Flags       = CodeFlags.Asynchronous,
                        Channel     = CodeChannel.Trigger,
                        Type        = CodeType.MCode,
                        MajorNumber = 550
                    };
                    code.Parameters.Add(new CodeParameter('P', lastHostname));
                    await code.Execute();
                }

                // Wait for next scheduled update check
                lastUpdateTime = DateTime.Now;
                await Task.Delay(Settings.HostUpdateInterval, Program.CancellationToken);
            }while (!Program.CancelSource.IsCancellationRequested);
        }
Example #3
0
        /// <summary>
        /// Run model updates in a certain interval.
        /// This function updates host properties like network interfaces and storage devices
        /// </summary>
        /// <returns>Asynchronous task</returns>
        public static async Task Run()
        {
            DateTime lastUpdateTime = DateTime.Now;

            do
            {
                // Run another update cycle
                using (await Provider.AccessReadWriteAsync())
                {
                    UpdateNetwork();
                    UpdateStorages();
                    CleanMessages();
                }

                // Check if the system time has to be updated
                if (DateTime.Now - lastUpdateTime > TimeSpan.FromMilliseconds(Settings.HostUpdateInterval + 5000) &&
                    !System.Diagnostics.Debugger.IsAttached)
                {
                    _logger.Info("System time has been changed");
                    Code code = new Code
                    {
                        InternallyProcessed = true,
                        Channel             = DuetAPI.CodeChannel.Daemon,
                        Type        = CodeType.MCode,
                        MajorNumber = 905
                    };
                    code.Parameters.Add(new CodeParameter('P', DateTime.Now.ToString("yyyy-MM-dd")));
                    code.Parameters.Add(new CodeParameter('S', DateTime.Now.ToString("HH:mm:ss")));
                    await code.Execute();
                }

                // Wait for next scheduled update check
                lastUpdateTime = DateTime.Now;
                await Task.Delay(Settings.HostUpdateInterval, Program.CancelSource.Token);
            }while (!Program.CancelSource.IsCancellationRequested);
        }
Example #4
0
        private static async void RunPrint()
        {
            BaseFile file = _file;

            // Process "start.g" at the beginning of a print
            string startPath = await FilePath.ToPhysical("start.g", "sys");

            if (File.Exists(startPath))
            {
                MacroFile startMacro = new MacroFile(startPath, CodeChannel.File, null);
                do
                {
                    Code code = await startMacro.ReadCodeAsync();

                    if (code == null)
                    {
                        break;
                    }

                    try
                    {
                        CodeResult result = await code.Execute();

                        await Model.Provider.Output(result);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"[err] {code} -> {e}");
                    }
                } while (!Program.CancelSource.IsCancellationRequested);
            }

            // Process the job file
            Queue <Code> codes = new Queue <Code>();
            Queue <Task <CodeResult> > codeTasks = new Queue <Task <CodeResult> >();

            do
            {
                // Has the file been paused? If so, rewind to the pause position
                bool paused = false;
                using (await _lock.LockAsync())
                {
                    if (!file.IsFinished && IsPaused)
                    {
                        file.Position = _pausePosition;
                        paused        = true;
                    }
                }

                // Wait for print to resume
                if (paused)
                {
                    codes.Clear();
                    codeTasks.Clear();

                    await _resumeEvent.WaitAsync(Program.CancelSource.Token);
                }

                // Fill up the code buffer
                Code code;
                while (codeTasks.Count == 0 || codeTasks.Count < Settings.BufferedPrintCodes)
                {
                    code = await file.ReadCodeAsync();

                    if (code == null)
                    {
                        break;
                    }

                    // The trick here is that Code.Enqueue runs synchronously and returns a
                    // task instance that completes when a code result is available...
                    codes.Enqueue(code);
                    codeTasks.Enqueue(code.Enqueue());
                }

                // Is there anything to do?
                if (codes.TryDequeue(out code))
                {
                    // Keep track of the next file position so we know where to resume in case the print is paused while a macro is being performed
                    if (codes.TryPeek(out Code nextCode))
                    {
                        LastFilePosition = nextCode.FilePosition;
                    }
                    else
                    {
                        LastFilePosition = file.Position;
                    }

                    // Process the next code
                    try
                    {
                        CodeResult result = await codeTasks.Dequeue();

                        await Model.Provider.Output(result);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"[err] {code} -> {e}");
                    }
                }
                else
                {
                    // No more codes available - print must have finished
                    break;
                }
            }while (!Program.CancelSource.IsCancellationRequested);

            Console.WriteLine("[info] DCS has finished printing");

            // Notify the controller that the print has stopped
            SPI.Communication.PrintStoppedReason stopReason = !file.IsAborted ? SPI.Communication.PrintStoppedReason.NormalCompletion
                : (IsPaused ? SPI.Communication.PrintStoppedReason.UserCancelled : SPI.Communication.PrintStoppedReason.Abort);
            SPI.Interface.SetPrintStopped(stopReason);

            // Invalidate the file being printed
            _file            = null;
            LastFilePosition = null;
        }