Skip to content

Commit

Permalink
Merge pull request #2 from omert08/getstatus_development
Browse files Browse the repository at this point in the history
It gives feedback in case provisioning is successful or failed.
  • Loading branch information
omert08 authored Oct 7, 2021
2 parents 2131ae0 + d94a578 commit 0e8b81a
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 7 deletions.
27 changes: 23 additions & 4 deletions example/lib/wifi_screen/wifi_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:typed_data';
import 'package:bloc/bloc.dart';
import 'package:esp_softap_provisioning/esp_softap_provisioning.dart';
import 'package:esp_softap_provisioning/src/connection_models.dart';
import 'package:logger/logger.dart';
import '../softap_service.dart';
import './wifi.dart';
Expand Down Expand Up @@ -32,10 +33,10 @@ class WiFiBlocSoftAP extends Bloc<WifiEvent, WifiState> {
try {
if (Platform.isIOS)
{
prov = await softApService.startProvisioning("wifi-prov.local", "abcd1234");
prov = await softApService.startProvisioning("wifi-prov.local", "INC032017");
}
else{
prov = await softApService.startProvisioning("192.168.4.1:80","abcd1234");
prov = await softApService.startProvisioning("192.168.4.1:80","INC032017");
}

} catch (e) {
Expand Down Expand Up @@ -63,8 +64,26 @@ class WiFiBlocSoftAP extends Bloc<WifiEvent, WifiState> {
await prov.sendReceiveCustomData(customBytes);
await prov?.sendWifiConfig(ssid: event.ssid, password: event.password);
await prov?.applyWifiConfig();
await Future.delayed(Duration(seconds: 1));
yield WifiStateProvisioned();
await Future.delayed(Duration(seconds: 10));
var connectionStatus = await prov.getStatus();

if (connectionStatus.state == WifiConnectionState.Connected) {
yield WifiStateProvisionedSuccessfully();
}
/*else if (connectionStatus.state == 1){
}*/
else if (connectionStatus.state == WifiConnectionState.Disconnected){
yield WifiStateProvisioningDisconnected();
}
else if (connectionStatus.state == WifiConnectionState.ConnectionFailed){
if (connectionStatus.failedReason == WifiConnectFailedReason.AuthError){
yield WifiStateProvisioningAuthError();
}
else if (connectionStatus.failedReason == WifiConnectFailedReason.NetworkNotFound){
yield WifiStateProvisioningNetworkNotFound();
}
}
}

@override
Expand Down
29 changes: 28 additions & 1 deletion example/lib/wifi_screen/wifi_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class _WiFiScreenSoftAPState extends State<WiFiScreenSoftAP> {
),
);
}
if (state is WifiStateProvisioned) {
if (state is WifiStateProvisionedSuccessfully) {
return Container(
child: Center(
child: MaterialButton(child: Text('Done'), color: Colors.lightBlueAccent, onPressed: () {
Expand All @@ -149,6 +149,33 @@ class _WiFiScreenSoftAPState extends State<WiFiScreenSoftAP> {
),
);
}
if (state is WifiStateProvisioningAuthError) {
return Container(
child: Center(
child: MaterialButton(child: Text('Auth Error'), color: Colors.redAccent, onPressed: () {
Navigator.of(context).pop();
},),
),
);
}
if (state is WifiStateProvisioningNetworkNotFound) {
return Container(
child: Center(
child: MaterialButton(child: Text('Network Not Found'), color: Colors.redAccent, onPressed: () {
Navigator.of(context).pop();
},),
),
);
}
if (state is WifiStateProvisioningDisconnected) {
return Container(
child: Center(
child: MaterialButton(child: Text('Subol Device Disconnected'), color: Colors.redAccent, onPressed: () {
Navigator.of(context).pop();
},),
),
);
}
return Container(
child: Center(
child: SpinKitThreeBounce(
Expand Down
5 changes: 4 additions & 1 deletion example/lib/wifi_screen/wifi_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ class WifiStateLoaded extends WifiState {
}

class WifiStateProvisioning extends WifiState {}
class WifiStateProvisioned extends WifiState {}
class WifiStateProvisioningDisconnected extends WifiState {}
class WifiStateProvisioningAuthError extends WifiState {}
class WifiStateProvisioningNetworkNotFound extends WifiState {}
class WifiStateProvisionedSuccessfully extends WifiState {}
3 changes: 2 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ dependencies:
equatable: ^2.0.0
flutter_bloc: ^7.0.0
esp_softap_provisioning:
path: ../
# When depending on this package from a real application you should use:
# esp_softap_provisioning: ^x.y.z
# See https://dart.dev/tools/pub/dependencies#version-constraints
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ../
# path: ../

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down
47 changes: 47 additions & 0 deletions lib/src/connection_models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
enum WifiConnectionState {
Connected,
Connecting,
Disconnected,
ConnectionFailed,
}

enum WifiConnectFailedReason {
AuthError,
NetworkNotFound,
}

class ConnectionStatus {
ConnectionStatus({
this.state,
this.ip,
this.failedReason,
});

final WifiConnectionState state;
final String ip;
final WifiConnectFailedReason failedReason;
}

class WifiAP {
const WifiAP({
this.ssid,
this.rssi,
this.active = false,
this.private = true,
});

final String ssid;
final int rssi;
final bool active;
final bool private;

int compareTo(WifiAP other) {
if (rssi > other.rssi) {
return -1;
}
if (rssi == other.rssi) {
return 0;
}
return 1;
}
}
40 changes: 40 additions & 0 deletions lib/src/provisioning.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'proto/dart/wifi_scan.pb.dart';
import 'proto/dart/session.pb.dart';
import 'security.dart';

import 'package:esp_softap_provisioning/src/connection_models.dart';

import 'transport.dart';

class Provisioning {
Expand Down Expand Up @@ -191,6 +193,44 @@ class Provisioning {
return (respPayload.respApplyConfig.status == Status.Success);
}

Future<ConnectionStatus> getStatus() async {
var payload = WiFiConfigPayload();
payload.msg = WiFiConfigMsgType.TypeCmdGetStatus;

var cmdGetStatus = CmdGetStatus();
payload.cmdGetStatus = cmdGetStatus;

var reqData = await security.encrypt(payload.writeToBuffer());
var respData = await transport.sendReceive('prov-config', reqData);
var respRaw = await security.decrypt(respData);
var respPayload = WiFiConfigPayload.fromBuffer(respRaw);

if (respPayload.respGetStatus.staState.value == 0) {
return ConnectionStatus(
state: WifiConnectionState.Connected,
ip: respPayload.respGetStatus.connected.ip4Addr);
} else if (respPayload.respGetStatus.staState.value == 1) {
return ConnectionStatus(state: WifiConnectionState.Connecting);
} else if (respPayload.respGetStatus.staState.value == 2) {
return ConnectionStatus(state: WifiConnectionState.Disconnected);
} else if (respPayload.respGetStatus.staState.value == 3) {
if (respPayload.respGetStatus.failReason.value == 0) {
return ConnectionStatus(
state: WifiConnectionState.ConnectionFailed,
failedReason: WifiConnectFailedReason.AuthError,
);
} else if (respPayload.respGetStatus.failReason.value == 1) {
return ConnectionStatus(
state: WifiConnectionState.ConnectionFailed,
failedReason: WifiConnectFailedReason.NetworkNotFound,
);
}
return ConnectionStatus(state: WifiConnectionState.ConnectionFailed);
}

return null;
}

Future<Uint8List> sendReceiveCustomData(Uint8List data, {int packageSize = 256}) async {
var i = data.length;
var offset = 0;
Expand Down

0 comments on commit 0e8b81a

Please sign in to comment.