/// <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); }
/// <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; }
public void CallProgress(OctoprintJobProgress p) { ProgressinfoHandlers.Invoke(p); }