/// <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); } }
public float[] getCurrentPos() { float[] coordinateResponseValue = { 0, 0, 0 }; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint + "/api/job X-Api-Key:" + apiKey); request.Method = httpMethod.ToString(); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()){ if (response.StatusCode != HttpStatusCode.OK) { throw new ApplicationException("Response Status Code:" + response.StatusCode.ToString()); } using (Stream responseStream = response.GetResponseStream()){ if (responseStream != null) { using (StreamReader reader = new StreamReader(responseStream)){ string strResponseValue = reader.ReadToEnd(); JObject data = JsonConvert.DeserializeObject <JObject>(strResponseValue); if (GCodeString == null) { string filelocation = "/api/files" + data["job"]["file"]["origin"] + "/" + data["job"]["file"]["name"]; GCodeString = makeRequest(filelocation); string[] preloadString = GCodeString.Substring(0, (int)data["progress"]["filepos"] - 1).Split(new[] { '\r', '\n' }); foreach (string line in preloadString) { ReadLine(line); } gcodePos = (int)data["progress"]["filepos"] - 1; } if (gcodePos != (int)data["progress"]["filepos"]) { string currline = GCodeString.Substring((int)data["progress"]["filepos"]).Split(new[] { '\r', '\n' })[0]; ReadLine(currline); gcodePos = (int)data["progress"]["filepos"]; coordinateResponseValue[0] = xpos; coordinateResponseValue[1] = ypos; coordinateResponseValue[2] = zpos; } } } } } return(coordinateResponseValue); }
/// <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; }