Skip to content

Commit

Permalink
refactoring and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zsco committed Mar 26, 2024
1 parent 1b56b10 commit 252ae31
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
9 changes: 6 additions & 3 deletions lib/services/device_manager_old.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@ class DeviceManagerOld {


Future<Response<Map<String, dynamic>>> getDevices() async {
final devicesUrl = basePath + "/devices";
final devicesUrl = "$basePath/devices";
final Response<Map<String, dynamic>> resp;
try {
_logger.d(LOG_PREFIX + ": Try to load devices from: " + devicesUrl);
_logger.d("$LOG_PREFIX: Try to load devices from: $devicesUrl");
resp = await dio.get<Map<String, dynamic>>(devicesUrl);
return resp;
} on DioError catch (e) {
_logger.d(LOG_PREFIX + ": Could not load devices: " + e.message);
if (e.type == DioErrorType.connectTimeout) {
throw ApiUnavailableException();
}
_logger.d("$LOG_PREFIX: Could not load devices: ${e.message}");
if (e.response?.statusCode == null || e.response!.statusCode! > 304) {
throw UnexpectedStatusCodeException(e.response?.statusCode, "$devicesUrl ${e.message}");
}
Expand Down
16 changes: 8 additions & 8 deletions lib/services/mgw/restricted.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class MgwService {

Future<String> GetSessionToken() async {
// TODO dont load credentials + request token at every request
_logger.d(LOG_PREFIX + ": Load device credentials from storage");
_logger.d("$LOG_PREFIX: Load device credentials from storage");
await LoadCredentialsFromStorage();
_logger.d(LOG_PREFIX + ": Perform Login");
_logger.d("$LOG_PREFIX: Perform Login");
var loginResponse = await mgwAuthService.Login(deviceCredentials.login, deviceCredentials.secret);
return loginResponse.token;
}
Expand Down Expand Up @@ -90,29 +90,29 @@ class MgwService {

Future<Response<dynamic>> Post(String path, dynamic data, Options options) async {
var url = baseUrl + path;
_logger.d(LOG_PREFIX + ": POST to: " + url);
_logger.d("$LOG_PREFIX: POST to: $url");
Response resp;
try {
resp = await dio.post(url, data: data, options: options);
return resp;
} on DioError catch (e) {
_logger.e(LOG_PREFIX + ": Request error");
_logger.e("$LOG_PREFIX: Request error");
var failure = handleDioError(e);
throw(failure);
};
}
}

Future<Response<dynamic>> Get(String path, Options options) async {
var url = baseUrl + path;
_logger.d(LOG_PREFIX + ": GET from: " + url);
_logger.d("$LOG_PREFIX: GET from: $url");
Response resp;
try {
resp = await dio.get(url, options: options);
return resp;
} on DioError catch (e) {
_logger.e(LOG_PREFIX + ": Request error");
_logger.e("$LOG_PREFIX: Request error");
var failure = handleDioError(e);
throw(failure);
};
}
}
}
30 changes: 19 additions & 11 deletions lib/services/mgw_device_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import 'package:dio/dio.dart';
import 'package:logger/logger.dart';
import 'package:mobile_app/exceptions/argument_exception.dart';
import 'package:mobile_app/exceptions/api_unavailable_exception.dart';
import 'package:mobile_app/models/device_search_filter.dart';

Expand Down Expand Up @@ -45,7 +44,7 @@ class MgwDeviceManager {
if (network?.localService != null) {
futures.add(_updateFromMgw(network!, devices)
.onError((error, stackTrace) async {
_logger.e(error);
_logger.e(error);
final deviceIds = devices.map((e) => e.id).toList();
try {
await DevicesService.getDevices(devices.length, 0,
Expand All @@ -71,10 +70,12 @@ class MgwDeviceManager {

static Future<void> _setupDeviceManager(String host) async {
// TODO: remove this check when the old port based deployment of device manager is not running anymore
_logger.d("Find out which device manager to use by checking endpoints");
_logger.d(
"MGW-DEVICE-MANAGER: Find out which device manager to use by checking endpoints");
var deviceManagerEndpoints = [];
try {
deviceManagerEndpoints = await DeviceManagerNew(host).getDeviceManagerEndpoints();
deviceManagerEndpoints =
await DeviceManagerNew(host).getDeviceManagerEndpoints();
} on Failure catch (e) {
_logger.e("Cant check device manager endpoints: " + e.detailedMessage);
return;
Expand All @@ -83,12 +84,14 @@ class MgwDeviceManager {
return;
}

if(deviceManagerEndpoints.isEmpty) {
if (deviceManagerEndpoints.isEmpty) {
useNewDeviceManager = false;
_logger.d("No endpoints found for device manager -> use port based device manager");
_logger.d(
"No endpoints found for device manager -> use port based device manager");
return;
}
_logger.d("Endpoints found for device manager -> use new path based device manager");
_logger.d(
"Endpoints found for device manager -> use new path based device manager");
useNewDeviceManager = true;
}

Expand All @@ -103,14 +106,19 @@ class MgwDeviceManager {

await _setupDeviceManager(ip);
Response<dynamic> devicesFromMgw;
_logger.d("Load devices from new device manager: " + useNewDeviceManager.toString());
_logger.d(
"MGW-DEVICE-MANAGER: Load devices from new device manager: $useNewDeviceManager");
// TODO remove this part when port based device manager are not used anymore in the future
if(useNewDeviceManager) {
if (useNewDeviceManager) {
devicesFromMgw = await DeviceManagerNew(ip).getDevices();
} else {
devicesFromMgw = await DeviceManagerOld(ip).getDevices();
try {
devicesFromMgw = await DeviceManagerOld(ip).getDevices();
} catch (e) {
rethrow;
}
}
_logger.d("Loaded devices: "+devicesFromMgw.toString());
_logger.d("MGW-DEVICE-MANAGER: Loaded devices: $devicesFromMgw");
for (final device in devices) {
if (devicesFromMgw.data?.containsKey(device.local_id) != true) {
device.connectionStatus = DeviceConnectionStatus.unknown;
Expand Down

0 comments on commit 252ae31

Please sign in to comment.