Compare commits
45 Commits
b978ffa313
...
6447692d4c
| Author | SHA1 | Date | |
|---|---|---|---|
|
6447692d4c
|
|||
|
855eb07d89
|
|||
|
8e840c0966
|
|||
|
5ab862d703
|
|||
|
7c6f0e8a3e
|
|||
|
bda9dddd4f
|
|||
|
95709e0285
|
|||
|
4c4b896978
|
|||
|
c20c0b02cf
|
|||
|
46bd6f7161
|
|||
|
eeb90f630d
|
|||
|
bccbd61ad5
|
|||
|
2017847b9e
|
|||
|
b471d0e20f
|
|||
|
a7d5880057
|
|||
|
e5ce96adcf
|
|||
|
27403e0aed
|
|||
|
faf4e377d7
|
|||
|
ed4890818f
|
|||
|
0f61fd26c5
|
|||
|
d1927bc24e
|
|||
|
1b7719ac3d
|
|||
|
ebcfb38e41
|
|||
|
ad7402f04b
|
|||
|
e8e9547a20
|
|||
|
852fcf67f4
|
|||
|
e798f997d3
|
|||
|
d2f9809f52
|
|||
|
3e7c26e3ba
|
|||
|
e5912cd94e
|
|||
|
20c203ffa4
|
|||
|
ad464e1c0d
|
|||
|
5009b0c8d1
|
|||
|
b13787b704
|
|||
|
b155baed03
|
|||
|
41a2e30aca
|
|||
|
266044215d
|
|||
|
418df318ee
|
|||
|
896cccd7ff
|
|||
|
66cbb31dde
|
|||
|
90b8e08946
|
|||
|
4900cf6acd
|
|||
|
3e07911cac
|
|||
|
8c3dd4c2e3
|
|||
|
3bae392285
|
64
dist/index.cjs.js
vendored
64
dist/index.cjs.js
vendored
@@ -175,10 +175,13 @@ class Updater {
|
||||
}
|
||||
async sendNextPacket() {
|
||||
let packet = this.file.slice(this._fileProgress, this._fileProgress + this._packetSize);
|
||||
// this._fileProgress += this._packetSize;
|
||||
return new Promise((resolve, reject) => {
|
||||
this.bleObject.write(this.bleDeviceId, this._updaterServiceUUID, this._updateFileCharacteristicUUID, packet.buffer, () => {
|
||||
this.bleObject.writeWithoutResponse(this.bleDeviceId, this._updaterServiceUUID, this._updateFileCharacteristicUUID, packet.buffer, () => {
|
||||
console.log(`Sent: ${packet}, progress: ${this._fileProgress}`);
|
||||
resolve(true);
|
||||
}, (error) => {
|
||||
this._fileProgress -= this._packetSize;
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
@@ -198,23 +201,24 @@ class Updater {
|
||||
// check for error
|
||||
// write file length
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const buffer = new ArrayBuffer(4);
|
||||
let view = new Int32Array(buffer);
|
||||
view[0] = this._fileSize;
|
||||
await this.bleObject.withPromises.write(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, buffer);
|
||||
// set mtu
|
||||
this._packetSize = await this.bleObject.withPromises.requestMtu(this.bleDeviceId, this._packetSize);
|
||||
// start notify
|
||||
this.bleObject.startNotification(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, async (rawData) => {
|
||||
let dataView = new Int8Array(rawData);
|
||||
if (dataView[0] == 1) {
|
||||
this.bleObject.startNotification(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, (rawData) => {
|
||||
let dataView = new Uint8Array(rawData);
|
||||
console.log(dataView);
|
||||
if (dataView[0] == 1 && dataView.length == 1) {
|
||||
// send file
|
||||
await this.sendNextPacket();
|
||||
progressCallback(`Sending (${Math.floor((this._fileProgress * 100) / this._fileSize)})`);
|
||||
this.sendNextPacket();
|
||||
progressCallback(`MTU: ${this._packetSize}; Sending (${Math.floor((this._fileProgress * 100) / this._fileSize)}%), ${this._fileProgress} / ${this._fileSize}`);
|
||||
}
|
||||
else if (dataView[0] == 2) {
|
||||
else if (dataView[0] == 2 && dataView.length == 1) {
|
||||
// done logic
|
||||
if (this._fileProgress >= this._fileSize) {
|
||||
console.log(`progress >= filesize: ${this._fileProgress} vs ${this._fileSize}`);
|
||||
if (this._fileProgress >= (this._fileSize - this._packetSize)) {
|
||||
console.log("true");
|
||||
// send agree
|
||||
await this.sendEndCmd(true);
|
||||
this.sendEndCmd(true);
|
||||
progressCallback(`Complete!`);
|
||||
this.bleObject.stopNotification(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, () => {
|
||||
// success
|
||||
@@ -224,23 +228,39 @@ class Updater {
|
||||
});
|
||||
}
|
||||
else {
|
||||
console.log("False");
|
||||
// send disagree
|
||||
await this.sendEndCmd(false);
|
||||
progressCallback(`Error, starting over`);
|
||||
this.sendEndCmd(false);
|
||||
progressCallback(`Error, starting over: ${this._fileProgress} / ${this._fileSize}`);
|
||||
this._fileProgress = 0;
|
||||
}
|
||||
}
|
||||
else if (dataView[0] == 15) {
|
||||
else if (dataView[0] == 15 && dataView.length == 1) {
|
||||
// error cmd
|
||||
progressCallback(`Error on remote`);
|
||||
progressCallback(`Error on remote: ${this._fileProgress} / ${this._fileSize}`);
|
||||
reject("Error on remote");
|
||||
}
|
||||
else {
|
||||
// no command
|
||||
progressCallback(`Error on remote`);
|
||||
reject("Error: command does not exist");
|
||||
else if (dataView[0] == 0 && dataView.length == 1) {
|
||||
// ignore no command
|
||||
progressCallback(`Board is on`);
|
||||
}
|
||||
}, () => { reject("Error: Failed to start notify"); });
|
||||
else {
|
||||
// should be the file progress
|
||||
let fileProgressView = new Uint32Array(rawData);
|
||||
this._fileProgress = (dataView[3] << 24) |
|
||||
(dataView[2] << 16) |
|
||||
(dataView[1] << 8) | dataView[0];
|
||||
this.sendNextPacket();
|
||||
progressCallback(`Recieved progress ${fileProgressView[0]}; Sending (${Math.floor((this._fileProgress * 100) / this._fileSize)}%), ${this._fileProgress} / ${this._fileSize}`);
|
||||
}
|
||||
}, (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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
64
dist/index.es.js
vendored
64
dist/index.es.js
vendored
@@ -173,10 +173,13 @@ class Updater {
|
||||
}
|
||||
async sendNextPacket() {
|
||||
let packet = this.file.slice(this._fileProgress, this._fileProgress + this._packetSize);
|
||||
// this._fileProgress += this._packetSize;
|
||||
return new Promise((resolve, reject) => {
|
||||
this.bleObject.write(this.bleDeviceId, this._updaterServiceUUID, this._updateFileCharacteristicUUID, packet.buffer, () => {
|
||||
this.bleObject.writeWithoutResponse(this.bleDeviceId, this._updaterServiceUUID, this._updateFileCharacteristicUUID, packet.buffer, () => {
|
||||
console.log(`Sent: ${packet}, progress: ${this._fileProgress}`);
|
||||
resolve(true);
|
||||
}, (error) => {
|
||||
this._fileProgress -= this._packetSize;
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
@@ -196,23 +199,24 @@ class Updater {
|
||||
// check for error
|
||||
// write file length
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const buffer = new ArrayBuffer(4);
|
||||
let view = new Int32Array(buffer);
|
||||
view[0] = this._fileSize;
|
||||
await this.bleObject.withPromises.write(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, buffer);
|
||||
// set mtu
|
||||
this._packetSize = await this.bleObject.withPromises.requestMtu(this.bleDeviceId, this._packetSize);
|
||||
// start notify
|
||||
this.bleObject.startNotification(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, async (rawData) => {
|
||||
let dataView = new Int8Array(rawData);
|
||||
if (dataView[0] == 1) {
|
||||
this.bleObject.startNotification(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, (rawData) => {
|
||||
let dataView = new Uint8Array(rawData);
|
||||
console.log(dataView);
|
||||
if (dataView[0] == 1 && dataView.length == 1) {
|
||||
// send file
|
||||
await this.sendNextPacket();
|
||||
progressCallback(`Sending (${Math.floor((this._fileProgress * 100) / this._fileSize)})`);
|
||||
this.sendNextPacket();
|
||||
progressCallback(`MTU: ${this._packetSize}; Sending (${Math.floor((this._fileProgress * 100) / this._fileSize)}%), ${this._fileProgress} / ${this._fileSize}`);
|
||||
}
|
||||
else if (dataView[0] == 2) {
|
||||
else if (dataView[0] == 2 && dataView.length == 1) {
|
||||
// done logic
|
||||
if (this._fileProgress >= this._fileSize) {
|
||||
console.log(`progress >= filesize: ${this._fileProgress} vs ${this._fileSize}`);
|
||||
if (this._fileProgress >= (this._fileSize - this._packetSize)) {
|
||||
console.log("true");
|
||||
// send agree
|
||||
await this.sendEndCmd(true);
|
||||
this.sendEndCmd(true);
|
||||
progressCallback(`Complete!`);
|
||||
this.bleObject.stopNotification(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, () => {
|
||||
// success
|
||||
@@ -222,23 +226,39 @@ class Updater {
|
||||
});
|
||||
}
|
||||
else {
|
||||
console.log("False");
|
||||
// send disagree
|
||||
await this.sendEndCmd(false);
|
||||
progressCallback(`Error, starting over`);
|
||||
this.sendEndCmd(false);
|
||||
progressCallback(`Error, starting over: ${this._fileProgress} / ${this._fileSize}`);
|
||||
this._fileProgress = 0;
|
||||
}
|
||||
}
|
||||
else if (dataView[0] == 15) {
|
||||
else if (dataView[0] == 15 && dataView.length == 1) {
|
||||
// error cmd
|
||||
progressCallback(`Error on remote`);
|
||||
progressCallback(`Error on remote: ${this._fileProgress} / ${this._fileSize}`);
|
||||
reject("Error on remote");
|
||||
}
|
||||
else {
|
||||
// no command
|
||||
progressCallback(`Error on remote`);
|
||||
reject("Error: command does not exist");
|
||||
else if (dataView[0] == 0 && dataView.length == 1) {
|
||||
// ignore no command
|
||||
progressCallback(`Board is on`);
|
||||
}
|
||||
}, () => { reject("Error: Failed to start notify"); });
|
||||
else {
|
||||
// should be the file progress
|
||||
let fileProgressView = new Uint32Array(rawData);
|
||||
this._fileProgress = (dataView[3] << 24) |
|
||||
(dataView[2] << 16) |
|
||||
(dataView[1] << 8) | dataView[0];
|
||||
this.sendNextPacket();
|
||||
progressCallback(`Recieved progress ${fileProgressView[0]}; Sending (${Math.floor((this._fileProgress * 100) / this._fileSize)}%), ${this._fileProgress} / ${this._fileSize}`);
|
||||
}
|
||||
}, (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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "updaterweblibrary",
|
||||
"version": "1.0.14",
|
||||
"version": "1.0.37",
|
||||
"description": "OTA Updater App frontend library",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
65
src/index.ts
65
src/index.ts
@@ -209,12 +209,15 @@ export default class Updater {
|
||||
|
||||
private async sendNextPacket(): Promise<boolean> {
|
||||
let packet = this.file.slice(this._fileProgress, this._fileProgress+this._packetSize);
|
||||
// this._fileProgress += this._packetSize;
|
||||
return new Promise((resolve, reject) => {
|
||||
this.bleObject.write(this.bleDeviceId, this._updaterServiceUUID, this._updateFileCharacteristicUUID, packet.buffer,
|
||||
this.bleObject.writeWithoutResponse(this.bleDeviceId, this._updaterServiceUUID, this._updateFileCharacteristicUUID, packet.buffer,
|
||||
() => {
|
||||
console.log(`Sent: ${packet}, progress: ${this._fileProgress}`);
|
||||
resolve(true);
|
||||
},
|
||||
(error) => {
|
||||
this._fileProgress -= this._packetSize;
|
||||
reject(error);
|
||||
}
|
||||
)
|
||||
@@ -238,24 +241,24 @@ export default class Updater {
|
||||
|
||||
// write file length
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const buffer = new ArrayBuffer(4)
|
||||
let view = new Int32Array(buffer);
|
||||
view[0] = this._fileSize;
|
||||
await this.bleObject.withPromises.write(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, buffer);
|
||||
|
||||
// set mtu
|
||||
this._packetSize = await this.bleObject.withPromises.requestMtu(this.bleDeviceId, this._packetSize);
|
||||
// start notify
|
||||
this.bleObject.startNotification(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID,
|
||||
async (rawData: ArrayBuffer): Promise<void> => {
|
||||
let dataView = new Int8Array(rawData);
|
||||
if (dataView[0] == 1) {
|
||||
(rawData: ArrayBuffer): void => {
|
||||
let dataView = new Uint8Array(rawData);
|
||||
console.log(dataView);
|
||||
if (dataView[0] == 1 && dataView.length == 1) {
|
||||
// send file
|
||||
await this.sendNextPacket();
|
||||
progressCallback(`Sending (${Math.floor((this._fileProgress *100)/this._fileSize)})`);
|
||||
} else if (dataView[0] == 2) {
|
||||
this.sendNextPacket();
|
||||
progressCallback(`MTU: ${this._packetSize}; Sending (${Math.floor((this._fileProgress *100)/this._fileSize)}%), ${this._fileProgress} / ${this._fileSize}`);
|
||||
} else if (dataView[0] == 2 && dataView.length == 1) {
|
||||
// done logic
|
||||
if (this._fileProgress >= this._fileSize) {
|
||||
console.log(`progress >= filesize: ${this._fileProgress} vs ${this._fileSize}`);
|
||||
if (this._fileProgress >= (this._fileSize - this._packetSize)) {
|
||||
console.log("true");
|
||||
// send agree
|
||||
await this.sendEndCmd(true);
|
||||
this.sendEndCmd(true);
|
||||
progressCallback(`Complete!`);
|
||||
this.bleObject.stopNotification(this.bleDeviceId,
|
||||
this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID,
|
||||
@@ -268,21 +271,37 @@ export default class Updater {
|
||||
}
|
||||
);
|
||||
} else {
|
||||
console.log("False");
|
||||
// send disagree
|
||||
await this.sendEndCmd(false);
|
||||
progressCallback(`Error, starting over`);
|
||||
this.sendEndCmd(false);
|
||||
progressCallback(`Error, starting over: ${this._fileProgress} / ${this._fileSize}`);
|
||||
this._fileProgress = 0;
|
||||
}
|
||||
} else if (dataView[0] == 15) {
|
||||
} else if (dataView[0] == 15 && dataView.length == 1) {
|
||||
// error cmd
|
||||
progressCallback(`Error on remote`);
|
||||
progressCallback(`Error on remote: ${this._fileProgress} / ${this._fileSize}`);
|
||||
reject("Error on remote");
|
||||
} else {
|
||||
// no command
|
||||
progressCallback(`Error on remote`);
|
||||
reject("Error: command does not exist");
|
||||
} else if (dataView[0] == 0 && dataView.length == 1) {
|
||||
// ignore no command
|
||||
progressCallback(`Board is on`);
|
||||
}
|
||||
else {
|
||||
// should be the file progress
|
||||
let fileProgressView = new Uint32Array(rawData);
|
||||
this._fileProgress = (dataView[3] << 24) |
|
||||
(dataView[2] << 16) |
|
||||
(dataView[1] << 8) | dataView[0];
|
||||
this.sendNextPacket();
|
||||
progressCallback(`Recieved progress ${fileProgressView[0]}; Sending (${Math.floor((this._fileProgress *100)/this._fileSize)}%), ${this._fileProgress} / ${this._fileSize}`);
|
||||
}
|
||||
}, () => { reject("Error: Failed to start notify"); });
|
||||
}, (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