mirror of
https://github.com/sensebox/blockly-app
synced 2025-06-08 10:05:50 +02:00
add initial OtaWifiProvider implementation
This commit is contained in:
parent
07a8fe47fb
commit
c9ddb63ecb
2 changed files with 76 additions and 1 deletions
|
@ -8,6 +8,7 @@ import { openSenseApp } from './app.component';
|
||||||
import { HomePage } from '../pages/home/home';
|
import { HomePage } from '../pages/home/home';
|
||||||
import { ApiProvider } from '../providers/api/api';
|
import { ApiProvider } from '../providers/api/api';
|
||||||
import { HttpClientModule } from '@angular/common/http';
|
import { HttpClientModule } from '@angular/common/http';
|
||||||
|
import { OtaWifiProvider } from '../providers/ota-wifi/ota-wifi';
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [
|
declarations: [
|
||||||
|
@ -28,7 +29,8 @@ import { HttpClientModule } from '@angular/common/http';
|
||||||
StatusBar,
|
StatusBar,
|
||||||
SplashScreen,
|
SplashScreen,
|
||||||
{provide: ErrorHandler, useClass: IonicErrorHandler},
|
{provide: ErrorHandler, useClass: IonicErrorHandler},
|
||||||
ApiProvider
|
ApiProvider,
|
||||||
|
OtaWifiProvider
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
|
73
src/providers/ota-wifi/ota-wifi.ts
Normal file
73
src/providers/ota-wifi/ota-wifi.ts
Normal file
|
@ -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<string> {
|
||||||
|
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<any> {
|
||||||
|
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<any> {
|
||||||
|
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
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue