From c9ddb63ecbfddbc5566f7b7ad830ebac6e63a6af Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Mon, 29 Oct 2018 19:24:37 +0100 Subject: [PATCH] add initial OtaWifiProvider implementation --- src/app/app.module.ts | 4 +- src/providers/ota-wifi/ota-wifi.ts | 73 ++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/providers/ota-wifi/ota-wifi.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index de3d9a5..8617043 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -8,6 +8,7 @@ import { openSenseApp } from './app.component'; import { HomePage } from '../pages/home/home'; import { ApiProvider } from '../providers/api/api'; import { HttpClientModule } from '@angular/common/http'; +import { OtaWifiProvider } from '../providers/ota-wifi/ota-wifi'; @NgModule({ declarations: [ @@ -28,7 +29,8 @@ import { HttpClientModule } from '@angular/common/http'; StatusBar, SplashScreen, {provide: ErrorHandler, useClass: IonicErrorHandler}, - ApiProvider + ApiProvider, + OtaWifiProvider ] }) export class AppModule {} diff --git a/src/providers/ota-wifi/ota-wifi.ts b/src/providers/ota-wifi/ota-wifi.ts new file mode 100644 index 0000000..1ff2084 --- /dev/null +++ b/src/providers/ota-wifi/ota-wifi.ts @@ -0,0 +1,73 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { Platform } from 'ionic-angular'; + +// use global var as no @ionic-native/wifiwizard2 package is available yet +declare var WifiWizard2: any; + +// corresponding to the initial MCU firmware +const SSID_PREFIX = 'sensebox'; +const SENSEBOX_API = 'http://192.168.0.1:80'; + +/* + Interface for uploading a binary to a senseBox MCU. +*/ +@Injectable() +export class OtaWifiProvider { + public strategy: WifiStrategy + + constructor(private platform: Platform, private http: HttpClient) { + this.selectStrategy() + } + + private selectStrategy (): WifiStrategy { + try { + // check if plugin is available (e.g. not in browser builds) + WifiWizard2 + + if ( + this.platform.is('android') || + this.platform.is('ios') && this.platform.version().major >= 11 + ) { + this.strategy = WifiStrategy.Automatic + } else { + this.strategy = WifiStrategy.Manual + } + } catch (err) { + this.strategy = WifiStrategy.Unavailable + } + + return this.strategy; + } + + async findSenseboxes (): Promise { + if (this.strategy != WifiStrategy.Automatic) + throw Error('can not search for WiFi networks on this platform') + + return WifiWizard2.scan() + .then(n => n.filter(n.SSID.includes(SSID_PREFIX))) + .then(n => n.map(n => n.SSID)) + } + + async connectToSensebox (ssid: string): Promise { + if (this.strategy != WifiStrategy.Automatic) + throw Error('can not connect to WiFi network on this platform') + + return this.platform.is('ios') + ? WifiWizard2.iOSConnectNetwork(ssid) + : WifiWizard2.connect(ssid, true) + } + + async uploadFirmware (blob: ArrayBufferLike): Promise { + return this.http.post(SENSEBOX_API, blob) + } + +} + +// TODO: replace with "WifiCapabilities". +// makes it easier to check in each functions if required functionality is available +enum WifiStrategy { + Automatic, // android, iOS 11+ + Manual, // older iOS + Unavailable, // browser +}