Skip to content

Commit

Permalink
feat: add duration and remaining time to progress data
Browse files Browse the repository at this point in the history
  • Loading branch information
robsonos committed Dec 18, 2023
1 parent 9650b14 commit cb25dda
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`** | <code>number</code> | The current status of upload (0-99). | 1.0.0 |
| **`speed`** | <code>number</code> | The current speed in bytes per millisecond. | 1.0.0 |
| **`avgSpeed`** | <code>number</code> | The average speed in bytes per millisecond. | 1.0.0 |
| **`currentPart`** | <code>number</code> | 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`** | <code>number</code> | The total number of parts. | 1.0.0 |
| Prop | Type | Description | Since |
| ------------------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| **`percent`** | <code>number</code> | The current status of upload (0-99). | 1.0.0 |
| **`speed`** | <code>number</code> | The current speed in bytes per millisecond. | 1.0.0 |
| **`avgSpeed`** | <code>number</code> | The average speed in bytes per millisecond. | 1.0.0 |
| **`currentPart`** | <code>number</code> | 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`** | <code>number</code> | The total number of parts. | 1.0.0 |
| **`duration`** | <code>number</code> | The total time elapsed since the start of the DFU process in milliseconds | 1.0.1 |
| **`remainingTime`** | <code>number</code> | The estimated remaining time to the end of the DFU process in milliseconds | 1.0.1 |


### Type Aliases
Expand Down
15 changes: 15 additions & 0 deletions android/src/main/java/com/example/plugin/nordicdfu/NordicDfu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -81,13 +85,24 @@ 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);
ret.put("speed", speed);
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);
}

Expand Down
2 changes: 2 additions & 0 deletions example/src/app/scan/dfu/dfu.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
Average Speed: {{ (update?.data?.avgSpeed ?? 0)* 1000 / 1024 | number }} kBps <br />
Current part: {{ (update?.data?.currentPart ?? 0)}} <br />
Total Parts: {{ (update?.data?.partsTotal ?? 0)}} <br />
Duration: {{ (update?.data?.duration ?? 0) | number }} <br />
Remaining time: {{ (update?.data?.remainingTime ?? 0) | number }} <br />
</ion-item>
<ion-progress-bar
*ngIf="update && update.data && update.data.percent"
Expand Down
28 changes: 24 additions & 4 deletions ios/Plugin/NordicDfuPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class NordicDfuPlugin: CAPPlugin, CBCentralManagerDelegate, DFUServiceDel
public var dfuChangeEvent: String = "DFUStateChanged"
var notificationRequestLookup = [String: JSObject]()
private var manager: CBCentralManager?
private var dfuStartTime: TimeInterval?

override public func load() {
manager = CBCentralManager(delegate: self, queue: nil)
Expand Down Expand Up @@ -117,27 +118,46 @@ public class NordicDfuPlugin: CAPPlugin, CBCentralManagerDelegate, DFUServiceDel
sendStateUpdate("VALIDATING_FIRMWARE")
case .disconnecting:
sendStateUpdate("DEVICE_DISCONNECTING")
case .completed:
sendStateUpdate("DFU_COMPLETED")
case .aborted:
sendStateUpdate("DFU_ABORTED")
case .completed, .aborted:
// Reset dfuStartTime at the end of the process
dfuStartTime = nil
sendStateUpdate(state == .completed ? "DFU_COMPLETED" : "DFU_ABORTED")
case .uploading:
break // Ignore
}
}

public func dfuError(_: DFUError, didOccurWithMessage _: String) {
// Reset dfuStartTime if an error occurs
dfuStartTime = nil
sendStateUpdate("DFU_FAILED")
}

public func dfuProgressDidChange(for part: Int, outOf totalParts: Int, to progress: Int, currentSpeedBytesPerSecond: Double, avgSpeedBytesPerSecond: Double) {
if dfuStartTime == nil {
dfuStartTime = Date().timeIntervalSince1970
}

let currentTime = Date().timeIntervalSince1970
let durationInSeconds = currentTime - (dfuStartTime ?? currentTime)
let duration = durationInSeconds * 1000 // Convert duration to milliseconds

var remainingTime: TimeInterval = 0
if progress > 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,
"speed": currentSpeedBytesPerSecond / 1000,
"avgSpeed": avgSpeedBytesPerSecond / 1000,
"currentPart": part,
"partsTotal": totalParts,
"duration": duration,
"remainingTime": remainingTime,
]
sendStateUpdate("DFU_PROGRESS", data)
}
Expand Down
14 changes: 14 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit cb25dda

Please sign in to comment.