diff --git a/src/index.ts b/src/index.ts index bda72bf..88d67dc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +/// import { parseXml, XmlDocument, XmlElement, XmlText } from "@rgrove/parse-xml"; export type versionNotes = { @@ -10,11 +11,25 @@ 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"; + + 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 { const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}.${this.feedType}`, { // "mode": "cors" @@ -105,5 +120,57 @@ 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 { + 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 { + 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 { + // 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; + } }