Compare commits
64 Commits
eac14571e2
...
1.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
f85c89e2c4
|
|||
|
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
|
|||
|
b978ffa313
|
|||
|
5762bcb549
|
|||
|
836e0f70aa
|
|||
|
b0813ed082
|
|||
|
6ffc5c1e8a
|
|||
|
fdcf232f8e
|
|||
|
c65ec8dcd2
|
|||
|
3e7c9f358e
|
|||
|
6f77c15088
|
|||
|
403f01d52f
|
|||
|
0125bae4e5
|
|||
|
0a37ee00bf
|
|||
|
03038eaa00
|
|||
|
76a6d725f5
|
|||
|
1452d6eaad
|
|||
|
b800542afa
|
|||
|
678d7e89d2
|
|||
|
387f75a4d3
|
180
dist/index.cjs.js
vendored
180
dist/index.cjs.js
vendored
@@ -2,16 +2,34 @@
|
||||
|
||||
var parseXml = require('@rgrove/parse-xml');
|
||||
|
||||
/// <reference types="cordova-plugin-ble-central" />
|
||||
class Updater {
|
||||
archiveURL;
|
||||
feedType;
|
||||
constructor(archiveURL = "/", feedType = "atom") {
|
||||
bleObject;
|
||||
bleDeviceId;
|
||||
_updaterServiceUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561900";
|
||||
_updaterVersionCharactersiticUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561910";
|
||||
_updaterCommandCharacterisitcUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561920";
|
||||
_updateFileCharacteristicUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561930";
|
||||
file;
|
||||
_fileSize;
|
||||
_fileProgress = 0;
|
||||
_packetSize;
|
||||
constructor(archiveURL = "/", feedType = "atom", bleObject, packetSize = 512) {
|
||||
this.archiveURL = archiveURL;
|
||||
this.feedType = feedType;
|
||||
if (bleObject) {
|
||||
this.bleObject = bleObject;
|
||||
}
|
||||
this._packetSize = packetSize;
|
||||
}
|
||||
/*
|
||||
FEEDS
|
||||
*/
|
||||
async getRawArchive() {
|
||||
const res = await fetch(`https://cors.emaker.limited/?url=${this.archiveURL}.${this.feedType}`, {
|
||||
"mode": "cors"
|
||||
const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}.${this.feedType}`, {
|
||||
// "mode": "cors"
|
||||
});
|
||||
const text = await res.text();
|
||||
return text;
|
||||
@@ -24,7 +42,7 @@ class Updater {
|
||||
if (element.name == "title") {
|
||||
outEntry.title = element.children[0].text;
|
||||
}
|
||||
else if (element.name == "date") {
|
||||
else if (element.name == "updated") {
|
||||
outEntry.date = new Date(element.children[0].text);
|
||||
}
|
||||
else if (element.name == "link") {
|
||||
@@ -79,7 +97,7 @@ class Updater {
|
||||
if (elm.type == "element") {
|
||||
const element = elm;
|
||||
if (element.name == "item") {
|
||||
output.push(this.atomGetVersionDetails(element));
|
||||
output.push(this.rssGetVersionDetails(element));
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -93,6 +111,158 @@ class Updater {
|
||||
return this.rssGetArchive();
|
||||
}
|
||||
}
|
||||
/*
|
||||
BLUETOOTH
|
||||
*/
|
||||
setDeviceId(id) {
|
||||
this.bleDeviceId = id;
|
||||
}
|
||||
bytesToString(buffer) {
|
||||
return String.fromCharCode.apply(null, new Uint8Array(buffer));
|
||||
}
|
||||
async readVersionNumber() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.bleObject.read(this.bleDeviceId, this._updaterServiceUUID, this._updaterVersionCharactersiticUUID, (rawData) => {
|
||||
resolve(this.bytesToString(rawData));
|
||||
}, (error) => {
|
||||
reject(`Error: ${error}`);
|
||||
});
|
||||
});
|
||||
}
|
||||
async getLatestVersion() {
|
||||
let feed = await this.getArchive();
|
||||
let newestDate = feed[0].date;
|
||||
let i = 0;
|
||||
feed.forEach((item, index) => {
|
||||
if (item.date > newestDate) {
|
||||
newestDate = item.date;
|
||||
i = index;
|
||||
}
|
||||
});
|
||||
return feed[i].title;
|
||||
}
|
||||
async checkForUpdate() {
|
||||
// read device value
|
||||
const deviceVersion = await this.readVersionNumber();
|
||||
// compare with latest version
|
||||
const latestVersion = await this.getLatestVersion();
|
||||
if (deviceVersion != latestVersion) {
|
||||
return true;
|
||||
}
|
||||
// update
|
||||
return false;
|
||||
}
|
||||
async getBoardVersion() {
|
||||
return await this.readVersionNumber();
|
||||
}
|
||||
/*
|
||||
FILE FLASHING
|
||||
*/
|
||||
async getFirmware(version) {
|
||||
try {
|
||||
const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}/download/${version.title}/firmware.bin`);
|
||||
let buf = await res.arrayBuffer();
|
||||
this.file = new Int8Array(buf);
|
||||
this._fileSize = this.file.byteLength;
|
||||
return true;
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
getFileSize() {
|
||||
return this._fileSize;
|
||||
}
|
||||
async sendNextPacket() {
|
||||
let packet = this.file.slice(this._fileProgress, this._fileProgress + this._packetSize);
|
||||
// this._fileProgress += this._packetSize;
|
||||
return new Promise((resolve, reject) => {
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
async sendEndCmd(agree) {
|
||||
const buffer = new ArrayBuffer(1);
|
||||
let view = new Int8Array(buffer);
|
||||
view[0] = agree ? 3 : 4;
|
||||
await this.bleObject.withPromises.write(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, buffer);
|
||||
return;
|
||||
}
|
||||
// start and autoconnect before you run this function, so that you can have an "uninterrupted" connection when the board reboots
|
||||
async flashFirmware(progressCallback) {
|
||||
// write filesize to board
|
||||
// await notify from cmd - ready
|
||||
// send a packet
|
||||
// check for error
|
||||
// write file length
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// set mtu
|
||||
this._packetSize = await this.bleObject.withPromises.requestMtu(this.bleDeviceId, this._packetSize);
|
||||
// start notify
|
||||
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
|
||||
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
|
||||
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 && dataView.length == 1) {
|
||||
// error cmd
|
||||
progressCallback(`Error on remote: ${this._fileProgress} / ${this._fileSize}`);
|
||||
reject("Error on remote");
|
||||
}
|
||||
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}`);
|
||||
}
|
||||
}, (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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Updater;
|
||||
|
||||
23
dist/index.d.ts
vendored
23
dist/index.d.ts
vendored
@@ -7,11 +7,32 @@ export type versionNotes = {
|
||||
export default class Updater {
|
||||
archiveURL: string;
|
||||
feedType: string;
|
||||
constructor(archiveURL?: string, feedType?: string);
|
||||
bleObject: BLECentralPlugin.BLECentralPluginStatic;
|
||||
protected bleDeviceId: string;
|
||||
private readonly _updaterServiceUUID;
|
||||
private readonly _updaterVersionCharactersiticUUID;
|
||||
private readonly _updaterCommandCharacterisitcUUID;
|
||||
private readonly _updateFileCharacteristicUUID;
|
||||
file: Int8Array;
|
||||
private _fileSize;
|
||||
private _fileProgress;
|
||||
private _packetSize;
|
||||
constructor(archiveURL?: string, feedType?: string, bleObject?: BLECentralPlugin.BLECentralPluginStatic, packetSize?: number);
|
||||
private getRawArchive;
|
||||
private atomGetVersionDetails;
|
||||
private atomGetArchive;
|
||||
private rssGetVersionDetails;
|
||||
private rssGetArchive;
|
||||
getArchive(): Promise<versionNotes[]>;
|
||||
setDeviceId(id: string): void;
|
||||
private bytesToString;
|
||||
private readVersionNumber;
|
||||
private getLatestVersion;
|
||||
checkForUpdate(): Promise<boolean>;
|
||||
getBoardVersion(): Promise<string>;
|
||||
getFirmware(version: versionNotes): Promise<boolean>;
|
||||
getFileSize(): number;
|
||||
private sendNextPacket;
|
||||
private sendEndCmd;
|
||||
flashFirmware(progressCallback: (message: string) => void): Promise<boolean>;
|
||||
}
|
||||
|
||||
180
dist/index.es.js
vendored
180
dist/index.es.js
vendored
@@ -1,15 +1,33 @@
|
||||
import { parseXml } from '@rgrove/parse-xml';
|
||||
|
||||
/// <reference types="cordova-plugin-ble-central" />
|
||||
class Updater {
|
||||
archiveURL;
|
||||
feedType;
|
||||
constructor(archiveURL = "/", feedType = "atom") {
|
||||
bleObject;
|
||||
bleDeviceId;
|
||||
_updaterServiceUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561900";
|
||||
_updaterVersionCharactersiticUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561910";
|
||||
_updaterCommandCharacterisitcUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561920";
|
||||
_updateFileCharacteristicUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561930";
|
||||
file;
|
||||
_fileSize;
|
||||
_fileProgress = 0;
|
||||
_packetSize;
|
||||
constructor(archiveURL = "/", feedType = "atom", bleObject, packetSize = 512) {
|
||||
this.archiveURL = archiveURL;
|
||||
this.feedType = feedType;
|
||||
if (bleObject) {
|
||||
this.bleObject = bleObject;
|
||||
}
|
||||
this._packetSize = packetSize;
|
||||
}
|
||||
/*
|
||||
FEEDS
|
||||
*/
|
||||
async getRawArchive() {
|
||||
const res = await fetch(`https://cors.emaker.limited/?url=${this.archiveURL}.${this.feedType}`, {
|
||||
"mode": "cors"
|
||||
const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}.${this.feedType}`, {
|
||||
// "mode": "cors"
|
||||
});
|
||||
const text = await res.text();
|
||||
return text;
|
||||
@@ -22,7 +40,7 @@ class Updater {
|
||||
if (element.name == "title") {
|
||||
outEntry.title = element.children[0].text;
|
||||
}
|
||||
else if (element.name == "date") {
|
||||
else if (element.name == "updated") {
|
||||
outEntry.date = new Date(element.children[0].text);
|
||||
}
|
||||
else if (element.name == "link") {
|
||||
@@ -77,7 +95,7 @@ class Updater {
|
||||
if (elm.type == "element") {
|
||||
const element = elm;
|
||||
if (element.name == "item") {
|
||||
output.push(this.atomGetVersionDetails(element));
|
||||
output.push(this.rssGetVersionDetails(element));
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -91,6 +109,158 @@ class Updater {
|
||||
return this.rssGetArchive();
|
||||
}
|
||||
}
|
||||
/*
|
||||
BLUETOOTH
|
||||
*/
|
||||
setDeviceId(id) {
|
||||
this.bleDeviceId = id;
|
||||
}
|
||||
bytesToString(buffer) {
|
||||
return String.fromCharCode.apply(null, new Uint8Array(buffer));
|
||||
}
|
||||
async readVersionNumber() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.bleObject.read(this.bleDeviceId, this._updaterServiceUUID, this._updaterVersionCharactersiticUUID, (rawData) => {
|
||||
resolve(this.bytesToString(rawData));
|
||||
}, (error) => {
|
||||
reject(`Error: ${error}`);
|
||||
});
|
||||
});
|
||||
}
|
||||
async getLatestVersion() {
|
||||
let feed = await this.getArchive();
|
||||
let newestDate = feed[0].date;
|
||||
let i = 0;
|
||||
feed.forEach((item, index) => {
|
||||
if (item.date > newestDate) {
|
||||
newestDate = item.date;
|
||||
i = index;
|
||||
}
|
||||
});
|
||||
return feed[i].title;
|
||||
}
|
||||
async checkForUpdate() {
|
||||
// read device value
|
||||
const deviceVersion = await this.readVersionNumber();
|
||||
// compare with latest version
|
||||
const latestVersion = await this.getLatestVersion();
|
||||
if (deviceVersion != latestVersion) {
|
||||
return true;
|
||||
}
|
||||
// update
|
||||
return false;
|
||||
}
|
||||
async getBoardVersion() {
|
||||
return await this.readVersionNumber();
|
||||
}
|
||||
/*
|
||||
FILE FLASHING
|
||||
*/
|
||||
async getFirmware(version) {
|
||||
try {
|
||||
const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}/download/${version.title}/firmware.bin`);
|
||||
let buf = await res.arrayBuffer();
|
||||
this.file = new Int8Array(buf);
|
||||
this._fileSize = this.file.byteLength;
|
||||
return true;
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
getFileSize() {
|
||||
return this._fileSize;
|
||||
}
|
||||
async sendNextPacket() {
|
||||
let packet = this.file.slice(this._fileProgress, this._fileProgress + this._packetSize);
|
||||
// this._fileProgress += this._packetSize;
|
||||
return new Promise((resolve, reject) => {
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
async sendEndCmd(agree) {
|
||||
const buffer = new ArrayBuffer(1);
|
||||
let view = new Int8Array(buffer);
|
||||
view[0] = agree ? 3 : 4;
|
||||
await this.bleObject.withPromises.write(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, buffer);
|
||||
return;
|
||||
}
|
||||
// start and autoconnect before you run this function, so that you can have an "uninterrupted" connection when the board reboots
|
||||
async flashFirmware(progressCallback) {
|
||||
// write filesize to board
|
||||
// await notify from cmd - ready
|
||||
// send a packet
|
||||
// check for error
|
||||
// write file length
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// set mtu
|
||||
this._packetSize = await this.bleObject.withPromises.requestMtu(this.bleDeviceId, this._packetSize);
|
||||
// start notify
|
||||
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
|
||||
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
|
||||
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 && dataView.length == 1) {
|
||||
// error cmd
|
||||
progressCallback(`Error on remote: ${this._fileProgress} / ${this._fileSize}`);
|
||||
reject("Error on remote");
|
||||
}
|
||||
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}`);
|
||||
}
|
||||
}, (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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export { Updater as default };
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "updaterweblibrary",
|
||||
"version": "1.0.6",
|
||||
"version": "1.1.0",
|
||||
"description": "OTA Updater App frontend library",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -24,7 +24,8 @@
|
||||
"scripts": {
|
||||
"build:types": "tsc -p tsconfig.json --emitDeclarationOnly",
|
||||
"build": "rm -rf ./dist && npm run build:types && rollup -c",
|
||||
"test": "vitest"
|
||||
"test": "vitest",
|
||||
"version": "npm run build && git add -A ./dist"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-typescript": "^12.1.4",
|
||||
@@ -34,6 +35,7 @@
|
||||
"vitest": "^3.2.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@rgrove/parse-xml": "^4.2.0"
|
||||
"@rgrove/parse-xml": "^4.2.0",
|
||||
"cordova-plugin-ble-central": "^2.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
233
src/index.ts
233
src/index.ts
@@ -1,23 +1,47 @@
|
||||
/// <reference types="cordova-plugin-ble-central" />
|
||||
import { parseXml, XmlDocument, XmlElement, XmlText } from "@rgrove/parse-xml";
|
||||
|
||||
export type versionNotes = {
|
||||
title: string;
|
||||
date: Date | string;
|
||||
link: string;
|
||||
html: string;
|
||||
title: string;
|
||||
date: Date | string;
|
||||
link: string;
|
||||
html: string;
|
||||
}
|
||||
|
||||
export default class Updater {
|
||||
public archiveURL: string;
|
||||
public feedType: string;
|
||||
constructor(archiveURL: string = "/", feedType: string = "atom") {
|
||||
|
||||
public bleObject: BLECentralPlugin.BLECentralPluginStatic;
|
||||
protected bleDeviceId: string;
|
||||
|
||||
private readonly _updaterServiceUUID: string = "71a4438e-fd52-4b15-b3d2-ec0e3e561900";
|
||||
private readonly _updaterVersionCharactersiticUUID: string = "71a4438e-fd52-4b15-b3d2-ec0e3e561910";
|
||||
private readonly _updaterCommandCharacterisitcUUID: string = "71a4438e-fd52-4b15-b3d2-ec0e3e561920";
|
||||
private readonly _updateFileCharacteristicUUID: string = "71a4438e-fd52-4b15-b3d2-ec0e3e561930";
|
||||
|
||||
public file: Int8Array;
|
||||
private _fileSize: number;
|
||||
private _fileProgress: number = 0;
|
||||
|
||||
private _packetSize: number;
|
||||
|
||||
constructor(archiveURL: string = "/", feedType: string = "atom", bleObject?: BLECentralPlugin.BLECentralPluginStatic, packetSize: number = 512) {
|
||||
this.archiveURL = archiveURL;
|
||||
this.feedType = feedType;
|
||||
if (bleObject) {
|
||||
this.bleObject = bleObject;
|
||||
}
|
||||
this._packetSize = packetSize;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
FEEDS
|
||||
*/
|
||||
|
||||
private async getRawArchive(): Promise<string> {
|
||||
const res = await fetch(`https://cors.emaker.limited/?url=${this.archiveURL}.${this.feedType}`, {
|
||||
"mode": "cors"
|
||||
const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}.${this.feedType}`, {
|
||||
// "mode": "cors"
|
||||
});
|
||||
const text = await res.text();
|
||||
return text;
|
||||
@@ -25,19 +49,19 @@ export default class Updater {
|
||||
|
||||
// atom feeds
|
||||
private atomGetVersionDetails(entry: XmlElement): versionNotes {
|
||||
let outEntry: versionNotes = {title: "", date: new Date, link: "", html: ""};
|
||||
let outEntry: versionNotes = { title: "", date: new Date, link: "", html: "" };
|
||||
entry.children.forEach((elm) => {
|
||||
let element = elm as XmlElement;
|
||||
if (element.name == "title") {
|
||||
outEntry.title = (element.children[0] as XmlText).text;
|
||||
}
|
||||
else if (element.name == "date") {
|
||||
else if (element.name == "updated") {
|
||||
outEntry.date = new Date((element.children[0] as XmlText).text);
|
||||
}
|
||||
else if (element.name == "link") {
|
||||
outEntry.link = element.attributes["href"];
|
||||
} else if (element.name == "content") {
|
||||
outEntry.html = (element.children[0] as XmlText).text;
|
||||
outEntry.html = (element.children[0] as XmlText).text;
|
||||
}
|
||||
})
|
||||
|
||||
@@ -63,7 +87,7 @@ export default class Updater {
|
||||
|
||||
// rss feeds
|
||||
private rssGetVersionDetails(entry: XmlElement): versionNotes {
|
||||
let outEntry: versionNotes = {title: "", date: new Date, link: "", html: ""};
|
||||
let outEntry: versionNotes = { title: "", date: new Date, link: "", html: "" };
|
||||
entry.children.forEach((elm) => {
|
||||
let element = elm as XmlElement;
|
||||
if (element.name == "title") {
|
||||
@@ -75,7 +99,7 @@ export default class Updater {
|
||||
else if (element.name == "link") {
|
||||
outEntry.link = (element.children[0] as XmlText).text;
|
||||
} else if (element.name == "content") {
|
||||
outEntry.html = (element.children[0] as XmlText).text;
|
||||
outEntry.html = (element.children[0] as XmlText).text;
|
||||
}
|
||||
})
|
||||
|
||||
@@ -91,19 +115,194 @@ export default class Updater {
|
||||
if (elm.type == "element") {
|
||||
const element = elm as XmlElement;
|
||||
if (element.name == "item") {
|
||||
output.push(this.atomGetVersionDetails(element))
|
||||
output.push(this.rssGetVersionDetails(element))
|
||||
}
|
||||
}
|
||||
})
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
public async getArchive(): Promise<versionNotes[]> {
|
||||
if (this.feedType == "atom") {
|
||||
return this.atomGetArchive()
|
||||
} else if (this.feedType == "rss"){
|
||||
} else if (this.feedType == "rss") {
|
||||
return this.rssGetArchive()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
BLUETOOTH
|
||||
*/
|
||||
|
||||
public setDeviceId(id: string): void {
|
||||
this.bleDeviceId = id;
|
||||
}
|
||||
|
||||
private bytesToString(buffer: ArrayBuffer): string {
|
||||
return String.fromCharCode.apply(null, new Uint8Array(buffer));
|
||||
}
|
||||
|
||||
private async readVersionNumber(): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.bleObject.read(
|
||||
this.bleDeviceId,
|
||||
this._updaterServiceUUID,
|
||||
this._updaterVersionCharactersiticUUID,
|
||||
(rawData: ArrayBuffer) => {
|
||||
resolve(this.bytesToString(rawData));
|
||||
},
|
||||
(error: string) => {
|
||||
reject(`Error: ${error}`);
|
||||
}
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
private async getLatestVersion(): Promise<string> {
|
||||
let feed: versionNotes[] = await this.getArchive();
|
||||
let newestDate: Date = feed[0].date as Date;
|
||||
let i: number = 0;
|
||||
feed.forEach((item: versionNotes, index: number) => {
|
||||
if (item.date > newestDate) {
|
||||
newestDate = item.date as Date;
|
||||
i = index;
|
||||
}
|
||||
});
|
||||
return feed[i].title;
|
||||
}
|
||||
|
||||
public async checkForUpdate(): Promise<boolean> {
|
||||
// read device value
|
||||
const deviceVersion = await this.readVersionNumber();
|
||||
// compare with latest version
|
||||
const latestVersion = await this.getLatestVersion();
|
||||
if (deviceVersion != latestVersion) {
|
||||
return true;
|
||||
}
|
||||
// update
|
||||
return false;
|
||||
}
|
||||
|
||||
public async getBoardVersion(): Promise<string> {
|
||||
return await this.readVersionNumber();
|
||||
}
|
||||
|
||||
/*
|
||||
FILE FLASHING
|
||||
*/
|
||||
|
||||
public async getFirmware(version: versionNotes): Promise<boolean> {
|
||||
try {
|
||||
const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}/download/${version.title}/firmware.bin`);
|
||||
let buf = await res.arrayBuffer();
|
||||
this.file = new Int8Array(buf);
|
||||
this._fileSize = this.file.byteLength;
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public getFileSize(): number {
|
||||
return this._fileSize;
|
||||
}
|
||||
|
||||
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.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);
|
||||
}
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
private async sendEndCmd(agree: boolean): Promise<void> {
|
||||
const buffer = new ArrayBuffer(1);
|
||||
let view = new Int8Array(buffer);
|
||||
view[0] = agree ? 3 : 4;
|
||||
await this.bleObject.withPromises.write(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, buffer);
|
||||
return;
|
||||
}
|
||||
|
||||
// start and autoconnect before you run this function, so that you can have an "uninterrupted" connection when the board reboots
|
||||
public async flashFirmware(progressCallback: (message: string) => void): Promise<boolean> {
|
||||
// write filesize to board
|
||||
// await notify from cmd - ready
|
||||
// send a packet
|
||||
// check for error
|
||||
|
||||
// write file length
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// set mtu
|
||||
this._packetSize = await this.bleObject.withPromises.requestMtu(this.bleDeviceId, this._packetSize);
|
||||
// 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 && dataView.length == 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 && dataView.length == 1) {
|
||||
// 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 && dataView.length == 1) {
|
||||
// error cmd
|
||||
progressCallback(`Error on remote: ${this._fileProgress} / ${this._fileSize}`);
|
||||
reject("Error on remote");
|
||||
} 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}`);
|
||||
}
|
||||
}, (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,23 +1,37 @@
|
||||
import { expect, expectTypeOf, test } from "vitest";
|
||||
import Updater from "../src/index.ts";
|
||||
import { describe, expect, test } from "vitest";
|
||||
import Updater, { versionNotes } from "../src/index.ts";
|
||||
|
||||
test("get an atom feed from gitea", async (): Promise<void> => {
|
||||
describe("retrieving data", () => {
|
||||
test("get an atom feed from gitea", async (): Promise<void> => {
|
||||
const updater = new Updater("https://git.emaker.limited/MicrocontrollerCD/SoftwareRelease/releases", "atom");
|
||||
const res = await updater.getArchive();
|
||||
expect(res).not.toBe(null);
|
||||
expect(res.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("get an rss feed from gitea", async (): Promise<void> => {
|
||||
const updater = new Updater("https://git.emaker.limited/MicrocontrollerCD/SoftwareRelease/releases", "rss");
|
||||
const res = await updater.getArchive();
|
||||
expect(res).not.toBe(null);
|
||||
expect(res.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("get an atom feed from github", async (): Promise<void> => {
|
||||
const updater = new Updater("https://github.com/chopster44/Phaser_3_pong/releases", "atom");
|
||||
const res = await updater.getArchive();
|
||||
expect(res).not.toBe(null);
|
||||
expect(res.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("data validation", () => {
|
||||
const updater = new Updater("https://git.emaker.limited/MicrocontrollerCD/SoftwareRelease/releases", "atom");
|
||||
const res = await updater.getArchive();
|
||||
expect(res).not.toBe(null);
|
||||
expect(res.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("get an rss feed from gitea", async(): Promise<void> => {
|
||||
const updater = new Updater("https://git.emaker.limited/MicrocontrollerCD/SoftwareRelease/releases", "rss");
|
||||
const res = await updater.getArchive();
|
||||
expect(res).not.toBe(null);
|
||||
expect(res.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("get an atom feed from github", async (): Promise<void> => {
|
||||
const updater = new Updater("https://github.com/chopster44/Phaser_3_pong/releases", "atom");
|
||||
const res = await updater.getArchive();
|
||||
expect(res).not.toBe(null);
|
||||
expect(res.length).toBeGreaterThan(0);
|
||||
});
|
||||
test("date checked is not current date", async () => {
|
||||
const res: versionNotes[] = await updater.getArchive();
|
||||
const date: Date = res[0].date as Date;
|
||||
const today: Date = new Date;
|
||||
console.log(date);
|
||||
console.log(today)
|
||||
expect(date.getUTCSeconds()).not.toBe(today.getUTCSeconds())
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user