Compare commits
8 Commits
76a6d725f5
...
fdcf232f8e
| Author | SHA1 | Date | |
|---|---|---|---|
|
fdcf232f8e
|
|||
|
c65ec8dcd2
|
|||
|
3e7c9f358e
|
|||
|
6f77c15088
|
|||
|
403f01d52f
|
|||
|
0125bae4e5
|
|||
|
0a37ee00bf
|
|||
|
03038eaa00
|
77
dist/index.cjs.js
vendored
77
dist/index.cjs.js
vendored
@@ -2,13 +2,25 @@
|
||||
|
||||
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-ec0e3e56193b";
|
||||
_updaterVersionCharactersiticUUID = "1978a3df-c009-4837-b295-57ef429dde8c";
|
||||
file;
|
||||
constructor(archiveURL = "/", feedType = "atom", bleObject) {
|
||||
this.archiveURL = archiveURL;
|
||||
this.feedType = feedType;
|
||||
if (bleObject) {
|
||||
this.bleObject = bleObject;
|
||||
}
|
||||
}
|
||||
/*
|
||||
FEEDS
|
||||
*/
|
||||
async getRawArchive() {
|
||||
const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}.${this.feedType}`, {
|
||||
// "mode": "cors"
|
||||
@@ -79,7 +91,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 +105,67 @@ 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(`${this.archiveURL}/download/${version.title}/firmware.bin`);
|
||||
let buf = await res.arrayBuffer();
|
||||
this.file = new Int8Array(buf);
|
||||
return true;
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
getFileSize() {
|
||||
return this.file.byteLength;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Updater;
|
||||
|
||||
15
dist/index.d.ts
vendored
15
dist/index.d.ts
vendored
@@ -7,11 +7,24 @@ 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;
|
||||
file: Int8Array;
|
||||
constructor(archiveURL?: string, feedType?: string, bleObject?: BLECentralPlugin.BLECentralPluginStatic);
|
||||
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;
|
||||
}
|
||||
|
||||
77
dist/index.es.js
vendored
77
dist/index.es.js
vendored
@@ -1,12 +1,24 @@
|
||||
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-ec0e3e56193b";
|
||||
_updaterVersionCharactersiticUUID = "1978a3df-c009-4837-b295-57ef429dde8c";
|
||||
file;
|
||||
constructor(archiveURL = "/", feedType = "atom", bleObject) {
|
||||
this.archiveURL = archiveURL;
|
||||
this.feedType = feedType;
|
||||
if (bleObject) {
|
||||
this.bleObject = bleObject;
|
||||
}
|
||||
}
|
||||
/*
|
||||
FEEDS
|
||||
*/
|
||||
async getRawArchive() {
|
||||
const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}.${this.feedType}`, {
|
||||
// "mode": "cors"
|
||||
@@ -77,7 +89,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 +103,67 @@ 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(`${this.archiveURL}/download/${version.title}/firmware.bin`);
|
||||
let buf = await res.arrayBuffer();
|
||||
this.file = new Int8Array(buf);
|
||||
return true;
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
getFileSize() {
|
||||
return this.file.byteLength;
|
||||
}
|
||||
}
|
||||
|
||||
export { Updater as default };
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "updaterweblibrary",
|
||||
"version": "1.0.9",
|
||||
"version": "1.0.12",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
||||
98
src/index.ts
98
src/index.ts
@@ -1,3 +1,4 @@
|
||||
/// <reference types="cordova-plugin-ble-central" />
|
||||
import { parseXml, XmlDocument, XmlElement, XmlText } from "@rgrove/parse-xml";
|
||||
|
||||
export type versionNotes = {
|
||||
@@ -10,11 +11,27 @@ export type versionNotes = {
|
||||
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-ec0e3e56193b";
|
||||
private readonly _updaterVersionCharactersiticUUID: string = "1978a3df-c009-4837-b295-57ef429dde8c";
|
||||
|
||||
public file: Int8Array;
|
||||
|
||||
constructor(archiveURL: string = "/", feedType: string = "atom", bleObject?: BLECentralPlugin.BLECentralPluginStatic) {
|
||||
this.archiveURL = archiveURL;
|
||||
this.feedType = feedType;
|
||||
if (bleObject) {
|
||||
this.bleObject = bleObject;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
FEEDS
|
||||
*/
|
||||
|
||||
private async getRawArchive(): Promise<string> {
|
||||
const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}.${this.feedType}`, {
|
||||
// "mode": "cors"
|
||||
@@ -105,5 +122,84 @@ export default class Updater {
|
||||
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(`${this.archiveURL}/download/${version.title}/firmware.bin`);
|
||||
let buf = await res.arrayBuffer();
|
||||
this.file = new Int8Array(buf);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public getFileSize(): number {
|
||||
return this.file.byteLength;
|
||||
}
|
||||
|
||||
// public async flashFirmware(): Promise<boolean> {
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user