trying file progress from remote, due to packet loss
This commit is contained in:
122
src/index.ts
122
src/index.ts
@@ -23,7 +23,7 @@ export default class Updater {
|
|||||||
public file: Int8Array;
|
public file: Int8Array;
|
||||||
private _fileSize: number;
|
private _fileSize: number;
|
||||||
private _fileProgress: number = 0;
|
private _fileProgress: number = 0;
|
||||||
|
|
||||||
private _packetSize: number;
|
private _packetSize: number;
|
||||||
|
|
||||||
constructor(archiveURL: string = "/", feedType: string = "atom", bleObject?: BLECentralPlugin.BLECentralPluginStatic, packetSize: number = 512) {
|
constructor(archiveURL: string = "/", feedType: string = "atom", bleObject?: BLECentralPlugin.BLECentralPluginStatic, packetSize: number = 512) {
|
||||||
@@ -208,14 +208,11 @@ export default class Updater {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async sendNextPacket(): Promise<boolean> {
|
private async sendNextPacket(): Promise<boolean> {
|
||||||
let packet = this.file.slice(this._fileProgress, this._fileProgress + this._packetSize);
|
let packet = this.file.slice(this._fileProgress, this._fileProgress+this._packetSize);
|
||||||
// this._fileProgress += this._packetSize;
|
// this._fileProgress += this._packetSize;
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.bleObject.writeWithoutResponse(this.bleDeviceId, this._updaterServiceUUID, this._updateFileCharacteristicUUID, packet.buffer,
|
this.bleObject.writeWithoutResponse(this.bleDeviceId, this._updaterServiceUUID, this._updateFileCharacteristicUUID, packet.buffer,
|
||||||
() => {
|
() => {
|
||||||
console.log("Wrote")
|
|
||||||
console.log(packet.length);
|
|
||||||
this._fileProgress += packet.length;
|
|
||||||
resolve(true);
|
resolve(true);
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
@@ -244,66 +241,63 @@ export default class Updater {
|
|||||||
// write file length
|
// write file length
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
// set mtu
|
// set mtu
|
||||||
this.bleObject.requestMtu(this.bleDeviceId, this._packetSize, (mtu: number) => {
|
this._packetSize = await this.bleObject.withPromises.requestMtu(this.bleDeviceId, this._packetSize);
|
||||||
this._packetSize = mtu;
|
|
||||||
this.bleObject.startNotification(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID,
|
|
||||||
(rawData: ArrayBuffer): void => {
|
|
||||||
let dataView = new Uint8Array(rawData);
|
|
||||||
if (dataView[0] == 1) {
|
|
||||||
// send file
|
|
||||||
this.sendNextPacket();
|
|
||||||
progressCallback(`MTU: ${this._packetSize}; Sending (${Math.floor((this._fileProgress * 100) / this._fileSize)}%), ${this._fileProgress} / ${this._fileSize}`);
|
|
||||||
} else if (dataView[0] == 2) {
|
|
||||||
// done logic
|
|
||||||
console.log(`progress >= filesize: ${this._fileProgress} vs ${this._fileSize}`);
|
|
||||||
if (this._fileProgress >= this._fileSize) {
|
|
||||||
console.log("true");
|
|
||||||
// send agree
|
|
||||||
this.sendEndCmd(true);
|
|
||||||
progressCallback(`Complete!`);
|
|
||||||
this.bleObject.stopNotification(this.bleDeviceId,
|
|
||||||
this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID,
|
|
||||||
() => {
|
|
||||||
// success
|
|
||||||
resolve(true);
|
|
||||||
},
|
|
||||||
(error) => {
|
|
||||||
reject("Error: Failed to stop notify");
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
console.log("False");
|
|
||||||
// send disagree
|
|
||||||
this.sendEndCmd(false);
|
|
||||||
progressCallback(`Error, starting over: ${this._fileProgress} / ${this._fileSize}`);
|
|
||||||
this._fileProgress = 0;
|
|
||||||
}
|
|
||||||
} else if (dataView[0] == 15) {
|
|
||||||
// error cmd
|
|
||||||
progressCallback(`Error on remote: ${this._fileProgress} / ${this._fileSize}`);
|
|
||||||
reject("Error on remote");
|
|
||||||
} else if (dataView[0] == 0) {
|
|
||||||
// ignore no command
|
|
||||||
progressCallback(`Board is on`);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// no command
|
|
||||||
progressCallback(`Error on remote`);
|
|
||||||
reject("Error: command does not exist");
|
|
||||||
}
|
|
||||||
}, (error) => {
|
|
||||||
reject("Error: Failed to start notify");
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
const buffer = new ArrayBuffer(4)
|
|
||||||
let view = new Int32Array(buffer);
|
|
||||||
view[0] = this._fileSize;
|
|
||||||
this.bleObject.withPromises.write(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, buffer);
|
|
||||||
});
|
|
||||||
|
|
||||||
// start notify
|
// start notify
|
||||||
|
this.bleObject.startNotification(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID,
|
||||||
|
(rawData: ArrayBuffer): void => {
|
||||||
|
let dataView = new Uint8Array(rawData);
|
||||||
|
console.log(dataView);
|
||||||
|
if (dataView[0] == 1) {
|
||||||
|
// send file
|
||||||
|
this.sendNextPacket();
|
||||||
|
progressCallback(`MTU: ${this._packetSize}; Sending (${Math.floor((this._fileProgress *100)/this._fileSize)}%), ${this._fileProgress} / ${this._fileSize}`);
|
||||||
|
} else if (dataView[0] == 2) {
|
||||||
|
// done logic
|
||||||
|
console.log(`progress >= filesize: ${this._fileProgress} vs ${this._fileSize}`);
|
||||||
|
if (this._fileProgress >= (this._fileSize - this._packetSize)) {
|
||||||
|
console.log("true");
|
||||||
|
// send agree
|
||||||
|
this.sendEndCmd(true);
|
||||||
|
progressCallback(`Complete!`);
|
||||||
|
this.bleObject.stopNotification(this.bleDeviceId,
|
||||||
|
this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID,
|
||||||
|
() => {
|
||||||
|
// success
|
||||||
|
resolve(true);
|
||||||
|
},
|
||||||
|
(error) => {
|
||||||
|
reject("Error: Failed to stop notify");
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
console.log("False");
|
||||||
|
// send disagree
|
||||||
|
this.sendEndCmd(false);
|
||||||
|
progressCallback(`Error, starting over: ${this._fileProgress} / ${this._fileSize}`);
|
||||||
|
this._fileProgress = 0;
|
||||||
|
}
|
||||||
|
} else if (dataView[0] == 15) {
|
||||||
|
// error cmd
|
||||||
|
progressCallback(`Error on remote: ${this._fileProgress} / ${this._fileSize}`);
|
||||||
|
reject("Error on remote");
|
||||||
|
} else if (dataView[0] == 0) {
|
||||||
|
// ignore no command
|
||||||
|
progressCallback(`Board is on`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// should be the file progress
|
||||||
|
let fileProgressView = new Uint32Array(rawData);
|
||||||
|
this._fileProgress += fileProgressView[0];
|
||||||
|
this.sendNextPacket();
|
||||||
|
}
|
||||||
|
}, (error) => { reject("Error: Failed to start notify");
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
const buffer = new ArrayBuffer(4)
|
||||||
|
let view = new Int32Array(buffer);
|
||||||
|
view[0] = this._fileSize;
|
||||||
|
this.bleObject.withPromises.write(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, buffer);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user