Ejemplo n.º 1
0
        private async Task HandleWebSocketDataAsync(WebSocket webSocket, string data)
        {
            PrusaSlicerCLICommands commands;

            try
            {
                //convert json to object
                commands = JsonSerializer.Deserialize <PrusaSlicerCLICommands>(data, new JsonSerializerOptions()
                {
                    IgnoreNullValues = true
                });
            }
            catch (Exception)
            {
                await SendErrorMessageForInvalidCommandsAsync(webSocket);

                return;
            }

            if (!commands.isValid())
            {
                await SendErrorMessageForInvalidCommandsAsync(webSocket);

                return;
            }

            // Ouput folder and config file path. These parameters need to be overwritten.
            commands.Output = GCodePath;
            if (!SetSlicingProfilPath(commands))
            {
                await SendErrorMessageForInvalidProfile(webSocket);

                return;
            }

            await SendSlicingStartedAsync(webSocket);

            var prusaSlicerBroker = new PrusaSlicerBroker(slicerPath);

            prusaSlicerBroker.FileSliced   += PrusaSlicerBroker_FileSliced(webSocket);
            prusaSlicerBroker.DataReceived += async(sender, args) =>
            {
                await SendSlicingProgressMessageAsync(webSocket, args);
            };

            var fileUri = new Uri(commands.FileURI);

            await DownloadModelAsync(fileUri, commands.FileName);

            var localPath = Path.Combine(ModelDownloadPath, commands.FileName);

            // use the local file on the disk
            commands.File = localPath;
            await prusaSlicerBroker.SliceAsync(commands);
        }
Ejemplo n.º 2
0
        // POST: api/Slice
        public async Task <string> Post([FromBody] string filepath, [FromUri] int fill = 20, [FromUri] double layer = 0.3, [FromUri] bool support = false)
        {
            var octoConnection = await WebApiApplication.GetOctoConnectionAsync();

            var octofile = OctoPrintFileServices.CreateOctoFile(octoConnection, filepath);

            if (octofile != null)
            {
                string downloadpath = octoConnection.ApplicationFolderPath;

                octofile.DownloadAssociatedOnlineFile("local", downloadpath, octoConnection);

                // Change to be a global class instance and just set the slicing variables with every request
                // would be better not to create a new instance with each request
                PrusaSlicerBroker prusaSlicer = new PrusaSlicerBroker(prusaSlicerPath, fill, layer, support);

                //await octofile.Slice(prusaSlicer, octofile.SlicedFilePath);

                await prusaSlicer.SliceAsync(octofile.LocalFilePath);

                octofile.SetSlicedInPlaceFileInfo();

                var uploadResponse = await octofile.UploadToOctoprintAsync(octofile.SlicedFilePath, octoConnection);

                if (uploadResponse.Contains("done\":true"))
                {
                    return($"{octofile.SlicedFileName}");
                }
                else
                {
                    var errorResponse = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Problem occured while uploading sliced file to Octoprint");
                    throw new HttpResponseException(errorResponse);
                }
            }

            else
            {
                var errorResponse = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Problem occured while downloading file from Octoprint");
                throw new HttpResponseException(errorResponse);
            }
        }