diff --git a/README.md b/README.md index d016233..bd00d83 100644 --- a/README.md +++ b/README.md @@ -221,13 +221,15 @@ The DFU update object that is passed to the DFUStateChanged event Contains data related to the DFU update process, such as progress and speed. -| Prop | Type | Description | Since | -| ----------------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- | -| **`percent`** | number | The current status of upload (0-99). | 1.0.0 | -| **`speed`** | number | The current speed in bytes per millisecond. | 1.0.0 | -| **`avgSpeed`** | number | The average speed in bytes per millisecond. | 1.0.0 | -| **`currentPart`** | number | The number of parts being sent. In case the ZIP file contains a Soft Device and/or a Bootloader together with the application the SD+BL are sent as part 1, then the service starts again and send the application as part 2. | 1.0.0 | -| **`partsTotal`** | number | The total number of parts. | 1.0.0 | +| Prop | Type | Description | Since | +| ------------------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- | +| **`percent`** | number | The current status of upload (0-99). | 1.0.0 | +| **`speed`** | number | The current speed in bytes per millisecond. | 1.0.0 | +| **`avgSpeed`** | number | The average speed in bytes per millisecond. | 1.0.0 | +| **`currentPart`** | number | The number of parts being sent. In case the ZIP file contains a Soft Device and/or a Bootloader together with the application the SD+BL are sent as part 1, then the service starts again and send the application as part 2. | 1.0.0 | +| **`partsTotal`** | number | The total number of parts. | 1.0.0 | +| **`duration`** | number | The total time elapsed since the start of the DFU process in milliseconds | 1.0.1 | +| **`remainingTime`** | number | The estimated remaining time to the end of the DFU process in milliseconds | 1.0.1 | ### Type Aliases diff --git a/android/src/main/java/com/example/plugin/nordicdfu/NordicDfu.java b/android/src/main/java/com/example/plugin/nordicdfu/NordicDfu.java index 520b089..bb6d455 100644 --- a/android/src/main/java/com/example/plugin/nordicdfu/NordicDfu.java +++ b/android/src/main/java/com/example/plugin/nordicdfu/NordicDfu.java @@ -30,6 +30,8 @@ public void setDFUEventListener(@Nullable DfuEventListener dfuEventListener) { @Nullable private DfuEventListener dfuEventListener; + private long startTime = 0; + public NordicDfu(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { DfuServiceInitiator.createDfuNotificationChannel(context); @@ -60,6 +62,8 @@ public void onDfuProcessStarting(@NonNull final String deviceAddress) { @Override public void onDfuProcessStarted(@NonNull final String deviceAddress) { + startTime = System.currentTimeMillis(); + JSObject ret = new JSObject(); ret.put("deviceAddress", deviceAddress); dfuEventListener.onDfuEvent("DFU_PROCESS_STARTED", ret); @@ -81,6 +85,15 @@ public void onProgressChanged( final int currentPart, final int partsTotal ) { + long currentTime = System.currentTimeMillis(); + long duration = currentTime - startTime; + long remainingTime = 0; + + if (percent > 0) { + long estimatedTotalTime = (duration * 100) / percent; + remainingTime = estimatedTotalTime - duration; + } + JSObject ret = new JSObject(); ret.put("deviceAddress", deviceAddress); ret.put("percent", percent); @@ -88,6 +101,8 @@ public void onProgressChanged( ret.put("avgSpeed", avgSpeed); ret.put("currentPart", currentPart); ret.put("partsTotal", partsTotal); + ret.put("duration", duration); + ret.put("remainingTime", remainingTime); dfuEventListener.onDfuEvent("DFU_PROGRESS", ret); } diff --git a/example/src/app/scan/dfu/dfu.page.html b/example/src/app/scan/dfu/dfu.page.html index 359d6d3..943711c 100644 --- a/example/src/app/scan/dfu/dfu.page.html +++ b/example/src/app/scan/dfu/dfu.page.html @@ -19,6 +19,8 @@ Average Speed: {{ (update?.data?.avgSpeed ?? 0)* 1000 / 1024 | number }} kBps
Current part: {{ (update?.data?.currentPart ?? 0)}}
Total Parts: {{ (update?.data?.partsTotal ?? 0)}}
+ Duration: {{ (update?.data?.duration ?? 0) | number }}
+ Remaining time: {{ (update?.data?.remainingTime ?? 0) | number }}
0 { + let estimatedTotalTimeInSeconds = durationInSeconds * (100.0 / Double(progress)) + let estimatedRemainingTimeInSeconds = estimatedTotalTimeInSeconds - durationInSeconds + remainingTime = estimatedRemainingTimeInSeconds * 1000 // Convert remaining time to milliseconds + } + let data: JSObject = [ "deviceAddress": "", "percent": progress, @@ -138,6 +156,8 @@ public class NordicDfuPlugin: CAPPlugin, CBCentralManagerDelegate, DFUServiceDel "avgSpeed": avgSpeedBytesPerSecond / 1000, "currentPart": part, "partsTotal": totalParts, + "duration": duration, + "remainingTime": remainingTime, ] sendStateUpdate("DFU_PROGRESS", data) } diff --git a/src/definitions.ts b/src/definitions.ts index f655052..7c67c2d 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -148,6 +148,20 @@ export interface DfuUpdateData { * @since 1.0.0 */ partsTotal?: number; + + /** + * The total time elapsed since the start of the DFU process in milliseconds + * + * @since 1.0.1 + */ + duration?: number; + + /** + * The estimated remaining time to the end of the DFU process in milliseconds + * + * @since 1.0.1 + */ + remainingTime?: number; } /**