Search in sources :

Example 6 with ConversionInfo

use of com.herewhite.sdk.domain.ConversionInfo in project whiteboard-android by netless-io.

the class Converter method startConvertTask.

public void startConvertTask(final String url, final ConvertType type, final ConverterCallbacks callback) {
    beginDate = new Date();
    final Converter that = this;
    poolExecutor.execute(new Runnable() {

        @Override
        public void run() {
            final CountDownLatch latch = new CountDownLatch(1);
            that.createConvertTask(url, type, new Callback() {

                @Override
                public void onFailure(Call call, IOException e) {
                    that.status = ConverterStatus.CreateFail;
                    ConvertException convertE = new ConvertException(ConvertErrorCode.CreatedFail, e);
                    callback.onFailure(convertE);
                    latch.countDown();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    JsonObject json = gson.fromJson(response.body().string(), JsonObject.class);
                    if (response.code() == 200) {
                        JsonObject msg = json.getAsJsonObject("msg");
                        boolean succeed = msg.getAsJsonPrimitive("succeed").getAsBoolean();
                        if (succeed) {
                            that.status = ConverterStatus.Created;
                            that.taskId = json.getAsJsonObject("msg").get("taskUUID").getAsString();
                        } else {
                            that.status = ConverterStatus.CreateFail;
                            ConvertException e = new ConvertException(ConvertErrorCode.CreatedFail, gson.toJson(json));
                            callback.onFailure(e);
                        }
                    } else {
                        that.status = ConverterStatus.CreateFail;
                        ConvertException e = new ConvertException(ConvertErrorCode.ConvertFail, gson.toJson(json));
                        callback.onFailure(e);
                    }
                    latch.countDown();
                }
            });
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (that.status == ConverterStatus.CreateFail) {
                return;
            }
            that.polling(taskId, type, new ConvertCallback() {

                @Override
                public void onConvertProgress(Double progress, ConversionInfo info) {
                    callback.onProgress(progress, info);
                }

                @Override
                public void onConvertFinish(final ConversionInfo info) {
                    that.status = ConverterStatus.Success;
                    callback.onFinish(that.getPpt(info, type), info);
                }

                @Override
                public void onConvertFailure(ConvertException e) {
                    // 错误时状态,各个上一级根据情况设置状态码
                    callback.onFailure(e);
                }
            });
        }
    });
}
Also used : Call(okhttp3.Call) ConvertException(com.herewhite.sdk.domain.ConvertException) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) Response(okhttp3.Response) Callback(okhttp3.Callback) ConversionInfo(com.herewhite.sdk.domain.ConversionInfo)

Example 7 with ConversionInfo

use of com.herewhite.sdk.domain.ConversionInfo in project whiteboard-android by netless-io.

the class ConverterV5 method checkProgress.

// Step 3: 轮询查询
private void checkProgress(String token) throws ConvertException {
    Request request = new Request.Builder().url(String.format(PROGRESS_URL_FORMAT, taskUuid, convertType(type))).header("token", token).header("region", convertRegion(region)).header("Content-Type", "application/json").header("Accept", "application/json").build();
    Call call = client.newCall(request);
    try {
        Response response = call.execute();
        String body = response.body().string();
        System.out.println(body);
        if (response.code() == 200) {
            QueryInfo queryInfo = gson.fromJson(body, QueryInfo.class);
            ConversionInfo info = queryInfo.progress;
            ConversionInfo.ServerConversionStatus status = queryInfo.status;
            if (status == ConversionInfo.ServerConversionStatus.Fail || status == ConversionInfo.ServerConversionStatus.NotFound) {
                ConvertErrorCode code = status == ConversionInfo.ServerConversionStatus.Fail ? ConvertErrorCode.ConvertFail : ConvertErrorCode.NotFound;
                onFailure(new ConvertException(code, queryInfo.failedReason));
                this.status = ConverterStatus.Fail;
            } else if (status == ConversionInfo.ServerConversionStatus.Finished) {
                onFinish(getPpt(info, type), info);
                this.status = ConverterStatus.Success;
            } else {
                onProgress(info.getConvertedPercentage(), queryInfo.progress);
            }
        } else {
            throw new ConvertException(ConvertErrorCode.ConvertFail, body);
        }
    } catch (IOException e) {
        throw new ConvertException(ConvertErrorCode.CheckFail, e);
    }
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) ConvertException(com.herewhite.sdk.domain.ConvertException) ConvertErrorCode(com.herewhite.sdk.domain.ConvertErrorCode) Request(okhttp3.Request) ConversionInfo(com.herewhite.sdk.domain.ConversionInfo) IOException(java.io.IOException)

Example 8 with ConversionInfo

use of com.herewhite.sdk.domain.ConversionInfo in project whiteboard-android by netless-io.

the class RoomActivity method staticConvert.

public void staticConvert(MenuItem item) {
    Converter c = new Converter(this.token);
    c.startConvertTask("https://white-cn-edge-doc-convert.oss-cn-hangzhou.aliyuncs.com/LightWaves.pdf", Converter.ConvertType.Static, new ConverterCallbacks() {

        @Override
        public void onFailure(ConvertException e) {
            logAction(e.getMessage());
        }

        @Override
        public void onFinish(ConvertedFiles ppt, ConversionInfo convertInfo) {
            mRoom.putScenes("/static", ppt.getScenes(), 0);
            mRoom.setScenePath("/static/1");
            logAction(convertInfo.toString());
        }

        @Override
        public void onProgress(Double progress, ConversionInfo convertInfo) {
            logAction(String.valueOf(progress));
        }
    });
    // ConvertV5
    ConverterV5.Builder builder = new ConverterV5.Builder();
    ConverterV5 converter = builder.setResource("https://white-cn-edge-doc-convert.oss-cn-hangzhou.aliyuncs.com/LightWaves.pdf").setType(ConvertType.Static).setScale(1.5).setOutputFormat(ImageFormat.JPEG).setSdkToken(demoAPI.getSdkToken()).setTaskUuid(null).setTaskToken(null).setCallback(new ConverterCallbacks() {

        @Override
        public void onProgress(Double progress, ConversionInfo convertInfo) {
        }

        @Override
        public void onFinish(ConvertedFiles ppt, ConversionInfo convertInfo) {
        }

        @Override
        public void onFailure(ConvertException e) {
        }
    }).build();
    converter.startConvertTask();
}
Also used : ConvertException(com.herewhite.sdk.domain.ConvertException) MapBuilder(com.herewhite.demo.utils.MapBuilder) Converter(com.herewhite.sdk.Converter) ConverterCallbacks(com.herewhite.sdk.ConverterCallbacks) ConvertedFiles(com.herewhite.sdk.domain.ConvertedFiles) ConversionInfo(com.herewhite.sdk.domain.ConversionInfo) ConverterV5(com.herewhite.sdk.converter.ConverterV5)

Example 9 with ConversionInfo

use of com.herewhite.sdk.domain.ConversionInfo in project whiteboard-android by netless-io.

the class Converter method polling.

private void polling(String taskId, ConvertType type, final ConvertCallback callbacks) {
    boolean canCheck = this.status == ConverterStatus.Timeout || this.status == ConverterStatus.CheckingFail || this.status == ConverterStatus.GetDynamicFail;
    if (this.status != ConverterStatus.Created && !canCheck) {
        return;
    }
    final Converter that = this;
    Date expireDate = new Date(this.beginDate.getTime() + this.timeout);
    while (converting && expireDate.after(new Date())) {
        final CountDownLatch latch = new CountDownLatch(1);
        that.status = ConverterStatus.Checking;
        checkProgress(taskId, type, new CheckCallback() {

            @Override
            public void onCheckResponse(ConversionInfo info) {
                ConversionInfo.ServerConversionStatus status = info.getConvertStatus();
                if (status == ConversionInfo.ServerConversionStatus.Fail || status == ConversionInfo.ServerConversionStatus.NotFound) {
                    converting = false;
                    that.status = ConverterStatus.Fail;
                    ConvertErrorCode code = status == ConversionInfo.ServerConversionStatus.Fail ? ConvertErrorCode.ConvertFail : ConvertErrorCode.NotFound;
                    ConvertException e = new ConvertException(code, info.getReason());
                    callbacks.onConvertFailure(e);
                } else if (status == ConversionInfo.ServerConversionStatus.Finished) {
                    // 成功时,可能还要额外获取动态 ppt 内容,不直接设置状态
                    converting = false;
                    callbacks.onConvertFinish(info);
                } else {
                    that.status = ConverterStatus.WaitingForNextCheck;
                    callbacks.onConvertProgress(info.getConvertedPercentage(), info);
                }
                try {
                    Thread.sleep(interval);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                latch.countDown();
            }

            @Override
            public void onCheckFailure(Exception e) {
                ConvertException exp = new ConvertException(ConvertErrorCode.CheckFail);
                callbacks.onConvertFailure(exp);
                converting = false;
                latch.countDown();
            }
        });
        try {
            latch.await();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (this.status == ConverterStatus.WaitingForNextCheck) {
        this.status = ConverterStatus.Timeout;
        ConvertException exp = new ConvertException(ConvertErrorCode.CheckTimeout);
        callbacks.onConvertFailure(exp);
    }
}
Also used : ConvertException(com.herewhite.sdk.domain.ConvertException) ConvertErrorCode(com.herewhite.sdk.domain.ConvertErrorCode) ConversionInfo(com.herewhite.sdk.domain.ConversionInfo) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) IOException(java.io.IOException) ConvertException(com.herewhite.sdk.domain.ConvertException)

Aggregations

ConversionInfo (com.herewhite.sdk.domain.ConversionInfo)9 ConvertException (com.herewhite.sdk.domain.ConvertException)9 ConverterCallbacks (com.herewhite.sdk.ConverterCallbacks)5 ConvertedFiles (com.herewhite.sdk.domain.ConvertedFiles)5 Converter (com.herewhite.sdk.Converter)4 IOException (java.io.IOException)4 MapBuilder (com.herewhite.demo.utils.MapBuilder)3 ConverterV5 (com.herewhite.sdk.converter.ConverterV5)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Call (okhttp3.Call)3 Response (okhttp3.Response)3 JsonObject (com.google.gson.JsonObject)2 ConvertErrorCode (com.herewhite.sdk.domain.ConvertErrorCode)2 Date (java.util.Date)2 Callback (okhttp3.Callback)2 Request (okhttp3.Request)2