basic bluetooth code
This commit is contained in:
69
src/index.ts
69
src/index.ts
@@ -1,3 +1,4 @@
|
|||||||
|
/// <reference types="cordova-plugin-ble-central" />
|
||||||
import { parseXml, XmlDocument, XmlElement, XmlText } from "@rgrove/parse-xml";
|
import { parseXml, XmlDocument, XmlElement, XmlText } from "@rgrove/parse-xml";
|
||||||
|
|
||||||
export type versionNotes = {
|
export type versionNotes = {
|
||||||
@@ -10,11 +11,25 @@ export type versionNotes = {
|
|||||||
export default class Updater {
|
export default class Updater {
|
||||||
public archiveURL: string;
|
public archiveURL: string;
|
||||||
public feedType: 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";
|
||||||
|
|
||||||
|
constructor(archiveURL: string = "/", feedType: string = "atom", bleObject?: BLECentralPlugin.BLECentralPluginStatic) {
|
||||||
this.archiveURL = archiveURL;
|
this.archiveURL = archiveURL;
|
||||||
this.feedType = feedType;
|
this.feedType = feedType;
|
||||||
|
if (bleObject) {
|
||||||
|
this.bleObject = bleObject;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
FEEDS
|
||||||
|
*/
|
||||||
|
|
||||||
private async getRawArchive(): Promise<string> {
|
private async getRawArchive(): Promise<string> {
|
||||||
const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}.${this.feedType}`, {
|
const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}.${this.feedType}`, {
|
||||||
// "mode": "cors"
|
// "mode": "cors"
|
||||||
@@ -105,5 +120,57 @@ export default class Updater {
|
|||||||
return this.rssGetArchive()
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user