/// <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)); } } } } }
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); } } } } } }