public OctoprintFullPrinterState(JObject data)
        {
            JToken temperaturedata = data.Value <JToken>("temperature");
            JToken bedtemperature  = temperaturedata.Value <JToken>("bed");
            JToken statedata       = data.Value <JToken>("state");

            TempState = new OctoprintTemperatureState()
            {
                Bed = new OctoprintTemperature(bedtemperature)
            };
            SDState         = data.Value <JToken>("sd").Value <bool?>("ready") ?? false;
            PrinterState    = new OctoprintPrinterState(statedata);
            TempState.Tools = new List <OctoprintTemperature>();
            for (int i = 0; i < 256; i++)
            {
                JToken tooltemp = temperaturedata.Value <JToken>("tool" + i);
                if (tooltemp != null)
                {
                    TempState.Tools.Add(new OctoprintTemperature(tooltemp));
                }
                else
                {
                    break;
                }
            }
            if (temperaturedata != null && temperaturedata.Value <JToken>("history") != null)
            {
                TempState.History = new List <OctoprintHistoricTemperatureState>();
                foreach (JObject historydata in temperaturedata["history"])
                {
                    OctoprintHistoricTemperatureState historicTempState = new OctoprintHistoricTemperatureState(historydata);
                    TempState.History.Add(historicTempState);
                }
            }
        }
        /// <summary>
        /// Gets the state of the printer current information only.
        /// </summary>
        /// <returns>The printer state.</returns>
        public OctoprintPrinterState GetPrinterState()
        {
            string jobInfo = "";

            try
            {
                jobInfo = Connection.Get("api/printer?exclude=temperature,sd");
            }
            catch (WebException e)
            {
                if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Conflict)
                {
                    OctoprintPrinterState returnval = new OctoprintPrinterState
                    {
                        Text = "Error 409 is the Printer Connected at all?\n"
                    };
                    return(returnval);
                }
            }
            JObject data = new JObject();

            data = JsonConvert.DeserializeObject <JObject>(jobInfo);
            JToken statedata             = data.Value <JToken>("state");
            OctoprintPrinterState result = new OctoprintPrinterState(statedata);

            if (currentstate != null)
            {
                currentstate.PrinterState = result;
            }
            return(result);
        }
Beispiel #3
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 CallPrinterState(OctoprintPrinterState ps)
 {
     PrinterstateHandlers.Invoke(ps);
 }
        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);
                            }
                        }
                    }
                }
            }
        }