/// <summary>
        /// Gets the progress of the current job
        /// </summary>
        /// <returns>The progress.</returns>
        public OctoprintJobProgress GetProgress()
        {
            string  jobInfo             = Connection.Get("api/job");
            JObject data                = JsonConvert.DeserializeObject <JObject>(jobInfo);
            JToken  progress            = data.Value <JToken>("progress");
            OctoprintJobProgress result = new OctoprintJobProgress(progress);

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Syncronizes the position with a Thread
        /// </summary>
        public void Syncpos()
        {
            OctoprintJobProgress progress = Connection.Jobs.GetProgress();
            OctoprintJobInfo     info     = Connection.Jobs.GetInfo();

            if (GCodeString == null)
            {
                if (info.File.Name != "" && info.File.Origin == "local")
                {
                    GetGCode(info.File.Origin + "/" + info.File.Name);
                }
            }
            if (GCodeString == null)
            {
                return;
            }
            int bitspassed = (progress.Filepos - 1) - LastSyncPos;

            string[] linesPassed = { };
            if (bitspassed > 0 && GCodeString != null && GCodeString.Length > LastSyncPos + bitspassed)
            {
                linesPassed = GCodeString.Substring(LastSyncPos, bitspassed).Split(new[] { '\r', '\n' });
            }
            else
            {
                Debug.WriteLine("Something Wrong in Postrackers Syncpos");
            }
            int   count         = 0;
            float secondspassed = 0;

            foreach (string line in linesPassed)
            {
                if (ReadLine(line)[0] < 0 || ReadLine(line)[1] < 0 || ReadLine(line)[2] < 0)
                {
                    if (MovesBuffer.Count <= count)
                    {
                        Debug.WriteLine("count seems to high");
                    }
                    else
                    {
                        secondspassed += MovesBuffer[count][3];
                    }
                    count++;
                }
            }
            if (count > 1 && MovesBuffer.Count >= count)
            {
                MovesBuffer.RemoveRange(0, count);
                LastSyncPos = progress.Filepos;
                //Console.WriteLine("10 seconds passed in: "+secondspassed+" seconds and the next move is this long: "+movesBuffer[0][3]);
                //movesBuffer.RemoveAt(0);
            }
        }
        /// <summary>
        /// Gets the current position Synchroniously at this exact time, should not be used many times per seconds though, that's why GetPosAsync exists.
        /// </summary>
        /// <returns>The current Position of the Printhead.</returns>
        public float[] GetCurrentPosSync()
        {
            float[] coordinateResponseValue = { 0, 0, 0 };
            OctoprintJobProgress progress = Connection.Jobs.GetProgress();
            OctoprintJobInfo info = Connection.Jobs.GetInfo();
            if (GCodeString == null)
            {
                if (info.File.Name != "")
                {
                    GetGCode(info.File.Origin + "/" + info.File.Name);
                }
            }
                string[] linesLeft = GCodeString.Substring(progress.Filepos).Split(new[] { '\r', '\n' });
                if (GcodePos != (progress.Filepos))
                {
                    if (GCodeString.Length > (progress.Filepos))
                    {
                        string currline = linesLeft[0];
                        ReadLineForwards(currline);
                    }
                    GcodePos = progress.Filepos;


                }
                if (MovesBuffer.Count == 0)
                {
                    BufferPos = new float[] { Xpos, Ypos, Zpos };
                    for (int i = 0; i < linesLeft.Length; i++)
                    {
                        ReadLineToBuffer(linesLeft[i]);
                    }
                }
            //}

            coordinateResponseValue[0] = Xpos;
            coordinateResponseValue[1] = Ypos;
            coordinateResponseValue[2] = Zpos;
            return coordinateResponseValue;
        }
        /// <summary>
        /// Gets the gcode from the given location
        /// </summary>
        /// <param name="location">Location the GCode file exists under, can only get this from local.</param>
        private void GetGCode(string location)
        {
            Debug.WriteLine("get gcode location "+ location );
            using (var wc = new System.Net.WebClient())
            {
                try
                {
                    Debug.WriteLine("downloading: " + Connection.EndPoint + "downloads/files/" + location + "?apikey=" + Connection.ApiKey);
                    GCodeString = wc.DownloadString(Connection.EndPoint + "downloads/files/" + location+ "?apikey=" + Connection.ApiKey);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("download failed with");
                    Debug.WriteLine(e);
                    return;
                }
            }
            Debug.WriteLine("got this long a String:" + GCodeString.Length);
            if (GCodeString.Length == 0)
            {
                GCodeString = null;
                return;
            }
            //Console.WriteLine("got "+GCodeString);
            OctoprintJobProgress progress = Connection.Jobs.GetProgress();
            if (progress.Filepos == 0)
                return;
            string[] preloadString = new string[0];
            if (GCodeString.Length > 0)
            {
                preloadString = GCodeString.Substring(0, Math.Max(Math.Min(GCodeString.Length, progress.Filepos) - 1,0)).Split(new[] { '\r', '\n' });
            }
            for (int i = preloadString.Length - 1; i >= 0 && (Math.Abs(Zpos) < 0.001 || Math.Abs(Ypos) < 0.001 || Math.Abs(Xpos) < 0.001); i -= 1)
            {
                ReadLineBackwards(preloadString[i]);
            }

            string[] linesLeft = GCodeString.Substring(progress.Filepos).Split(new[] { '\r', '\n' });
            if (GcodePos != progress.Filepos)
            {
                if (GCodeString.Length > progress.Filepos)
                {
                    string currline = linesLeft[0];
                    ReadLineForwards(currline);
                }
                Debug.WriteLine("setting Gcodepos to new value " + progress.Filepos);
                GcodePos = progress.Filepos;
            }
            if (MovesBuffer.Count == 0)
            {
                BufferPos = new float[] { Xpos, Ypos, Zpos };
                for (int i = 0; i < linesLeft.Length; i++)
                {
                    ReadLineToBuffer(linesLeft[i]);
                }
            }
            else
            {
                Debug.WriteLine("bufferpos isn't 0");
            }
            //GcodePos = progress.Filepos - 1;
        }
Beispiel #5
0
        /// <summary>
        /// The Websocket Thread function,runs and never stops
        /// </summary>
        private void WebsocketSync()
        {
            string            temporarystorage = "";
            var               buffer           = new byte[4096];
            CancellationToken cancellation     = CancellationToken.None;
            //var awaiter = task.GetAwaiter();
            WebSocketReceiveResult received;// = WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), cancellation).GetAwaiter().GetResult();

            while (!WebSocket.CloseStatus.HasValue && listening)
            {
                received = WebSocket.ReceiveAsync(new ArraySegment <byte>(buffer), cancellation).GetAwaiter().GetResult();
                string  text = System.Text.Encoding.UTF8.GetString(buffer, 0, received.Count);
                JObject obj  = null;// = JObject(text);
                //JObject.Parse(text);
                try
                {
                    obj = JObject.Parse(text);
                }catch {
                    temporarystorage += text;
                    try
                    {
                        obj = JObject.Parse(temporarystorage);
                        temporarystorage = "";
                    }
                    catch
                    {
                        Debug.WriteLine("had to read something in more lines");
                    }
                }
                if (obj != null)
                {
                    JToken current = obj.Value <JToken>("current");

                    if (current != null)
                    {
                        JToken progress = current.Value <JToken>("progress");
                        if (progress != null && Jobs.ProgressListens())
                        {
                            OctoprintJobProgress jobprogress = new OctoprintJobProgress(progress);
                            Jobs.CallProgress(jobprogress);
                        }

                        JToken job = current.Value <JToken>("job");
                        if (job != null && Jobs.JobListens())
                        {
                            OctoprintJobInfo jobInfo = new OctoprintJobInfo(job);
                            Jobs.CallJob(jobInfo);
                        }

                        JToken printerinfo = current.Value <JToken>("state");
                        if (printerinfo != null && Printers.StateListens())
                        {
                            OctoprintPrinterState opstate = new OctoprintPrinterState(printerinfo);
                            Printers.CallPrinterState(opstate);
                        }

                        float?currentz = current.Value <float>("currentZ");
                        if (currentz != null && Printers.ZListens())
                        {
                            Printers.CallCurrentZ((float)currentz);
                        }
                        JToken offsets = current.Value <JToken>("offsets");
                        if (offsets != null && Printers.OffsetListens())
                        {
                            List <int> offsetList = new List <int>();
                            for (int i = 0; i < 256; i++)
                            {
                                int?tooloffset = offsets.Value <int?>("tool" + i);
                                if (tooloffset != null)
                                {
                                    offsetList.Add((int)tooloffset);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            int?offsetBed = offsets.Value <int?>("bed");
                            if (offsetBed != null)
                            {
                                offsetList.Add((int)offsetBed);
                            }
                            Printers.CallOffset(offsetList);
                        }

                        JToken temps = current.Value <JToken>("temps");
                        if (temps != null && Printers.TempsListens() && temps.HasValues)
                        {
                            temps = temps.Value <JToken>(0);
                            Printers.CallTemp(new OctoprintHistoricTemperatureState(temps));
                        }
                    }
                }
            }
        }
 public void CallProgress(OctoprintJobProgress p)
 {
     ProgressinfoHandler.Invoke(p);
 }
        protected void ParseData(string input)
        {
            JObject obj = null;

            try
            {
                obj = JObject.Parse(input);
            }
            catch
            {
                System.Diagnostics.Debug.WriteLine("Couldent parse data. Not enough data?");
            }
            if (obj != null)
            {
                JToken current = obj.Value <JToken>("current");

                if (current != null)
                {
                    JToken progress = current.Value <JToken>("progress");
                    if (progress != null && Jobs.ProgressListens())
                    {
                        OctoprintJobProgress jobprogress = new OctoprintJobProgress(progress);
                        Jobs.CallProgress(jobprogress);
                    }

                    JToken job = current.Value <JToken>("job");
                    if (job != null && Jobs.JobListens())
                    {
                        OctoprintJobInfo jobInfo = new OctoprintJobInfo(job);
                        Jobs.CallJob(jobInfo);
                    }

                    JToken printerinfo = current.Value <JToken>("state");
                    if (printerinfo != null && Printer.StateListens())
                    {
                        OctoprintPrinterState opstate = new OctoprintPrinterState(printerinfo);
                        Printer.CallPrinterState(opstate);
                    }

                    float?currentz = current.Value <float?>("currentZ");
                    if (currentz != null && Printer.ZListens())
                    {
                        Printer.CallCurrentZ((float)currentz);
                    }
                    JToken offsets = current.Value <JToken>("offsets");
                    if (offsets != null && Printer.OffsetListens())
                    {
                        List <int> offsetList = new List <int>();
                        for (int i = 0; i < 256; i++)
                        {
                            int?tooloffset = offsets.Value <int?>("tool" + i);
                            if (tooloffset != null)
                            {
                                offsetList.Add((int)tooloffset);
                            }
                            else
                            {
                                break;
                            }
                        }
                        int?offsetBed = offsets.Value <int?>("bed");
                        if (offsetBed != null)
                        {
                            offsetList.Add((int)offsetBed);
                        }
                        Printer.CallOffset(offsetList);
                    }

                    JToken temps = current.Value <JToken>("temps");
                    if (temps != null && temps.HasValues && Printer.TempsListens())
                    {
                        temps = temps.Value <JToken>(0);
                        Printer.CallTemp(new OctoprintHistoricTemperatureState(temps));
                    }
                }
                else
                {
                    JToken _event = obj.Value <JToken>("event");

                    if (_event != null)
                    {
                        string eventtype = string.Empty;
                        try
                        {
                            eventtype = _event.Value <string>("type");
                        }
                        catch (Exception)
                        {
                        }


                        if (eventtype == "PrinterStateChanged")
                        {
                            var payload = _event.Value <JToken>("payload");
                            if (payload != null)
                            {
                                var state_string = payload.Value <string>("state_string");
                                if (state_string != null)
                                {
                                    var state = (PrinterState)Enum.Parse(typeof(PrinterState), state_string);
                                    Printer.State = state;
                                }
                            }
                        }
                        else if (eventtype == "PrintDone")
                        {
                            var payload = _event.Value <JToken>("payload");
                            if (payload != null)
                            {
                                var time = payload.Value <float>("time");
                                Printer.OnPrintFinished(time);
                            }
                        }
                    }
                }
            }
        }