From 6a3ddfb436bfdf1e1a48ead9cc145ea8936e210b Mon Sep 17 00:00:00 2001 From: DevKevYT Date: Thu, 12 May 2022 13:13:48 +0200 Subject: [PATCH] #175 improve traffic --- lib/core/excel/models/phasestatus.dart | 3 +- lib/core/excel/solc_api_manager.dart | 43 +++++++++-- lib/core/excel/solcresponse.dart | 9 ++- lib/core/service/time_table.service.dart | 2 +- .../teacher_classes.screen.dart | 74 ++++++++++++++----- 5 files changed, 101 insertions(+), 30 deletions(-) diff --git a/lib/core/excel/models/phasestatus.dart b/lib/core/excel/models/phasestatus.dart index 7bbc444..0b4ace4 100644 --- a/lib/core/excel/models/phasestatus.dart +++ b/lib/core/excel/models/phasestatus.dart @@ -1,12 +1,13 @@ import 'package:sol_connect/core/api/models/utils.dart'; class PhaseStatus { + int id = 0; DateTime _startDate = DateTime(0); DateTime _endDate = DateTime(0); DateTime _uploaded = DateTime(0); int _fileOwnerId = 0; - PhaseStatus(dynamic json) { + PhaseStatus(this.id, dynamic json) { if (json == null) { return; } diff --git a/lib/core/excel/solc_api_manager.dart b/lib/core/excel/solc_api_manager.dart index c3913f3..58a2750 100644 --- a/lib/core/excel/solc_api_manager.dart +++ b/lib/core/excel/solc_api_manager.dart @@ -21,7 +21,7 @@ class SOLCApiManager { String _inetAddress; int _port; - static final Version buildRequired = Version.of("2.1.5"); + static final Version buildRequired = Version.of("2.2.1"); SOLCApiManager(this._inetAddress, this._port); @@ -45,11 +45,37 @@ class SOLCApiManager { return Version(1, 0, 0); } + //Wirft eine Exception wenn ein Fehlercode auftritt + //Ermöglicht mehrere Phasierungs Abfragen mit einer Anfrage. + //Benötigt SOLC-API Server Version > 2.2.1 + //Wenn eine ID nicht vorhanden ist ist sie nicht in der Liste + Future> getSchoolClassInfos({required List schoolClassIds}) async { + + String args = ""; + for(int id in schoolClassIds) { + args += " <" + id.toString() + ">"; + } + + SOLCResponse? response = await _querySOLC(command: "phase-statuses$args"); + + if (response != null) { + List existing = []; + for(int id in schoolClassIds) { + if(response.payload[id.toString()] != null) { + existing.add(PhaseStatus(id, response.payload[id.toString()])); + } + } + return existing; + } + + return List.empty(); + } + ///Wirft eine Exception wenn ein Fehlercode auftritt Future getSchoolClassInfo({required int schoolClassId}) async { SOLCResponse? response = await _querySOLC(command: "phase-status <" + schoolClassId.toString() + ">"); if (response != null) { - return PhaseStatus(response.payload); + return PhaseStatus(schoolClassId, response.payload); } return null; } @@ -115,6 +141,7 @@ class SOLCApiManager { Future _querySOLC({required String command, File? uploadFileSource, List? downloadBytes}) async { SOLCResponse? returnValue; dynamic exception; + bool hasException = false; try { _activeSockets = _activeSockets + 1; @@ -131,6 +158,7 @@ class SOLCApiManager { void throwException(Exception e) { exception = e; + hasException = true; socket.close(); } @@ -148,10 +176,10 @@ class SOLCApiManager { dynamic decodedMessage = ""; try { decodedMessage = jsonDecode(String.fromCharCodes(event)); - } on FormatException { - socket.close(); + } catch (e) { + throwException(Exception("Invalid response: " + e.toString())); return; - } + } SOLCResponse response = SOLCResponse.handle(decodedMessage); if (response.isError) { @@ -202,13 +230,14 @@ class SOLCApiManager { _activeSockets--; //log.d("Socket closed naturally. " + _activeSockets.toString() + " active sockets."); + } on Exception catch (error) { _activeSockets--; throw FailedToEstablishSOLCServerConnection( - "Konnte keine Verbindung zum Konvertierungsserver " + _inetAddress + " herstellen: " + error.toString()); + "Konnte keine Verbindung zum Konvertierungsserver " + _inetAddress + " herstellen: " + error.toString()); } - if (exception != null) { + if (hasException) { throw exception; } return returnValue; diff --git a/lib/core/excel/solcresponse.dart b/lib/core/excel/solcresponse.dart index 0ca651e..b9a9d23 100644 --- a/lib/core/excel/solcresponse.dart +++ b/lib/core/excel/solcresponse.dart @@ -1,5 +1,7 @@ // ignore_for_file: constant_identifier_names +import 'package:sol_connect/core/exceptions.dart'; + ///Klasse um SOLC-API Server antworten zu handlen class SOLCResponse { //Server Codes @@ -18,10 +20,11 @@ class SOLCResponse { static const int CODE_HTTP_ERROR = 106; static const int CODE_ENTRY_MISSING = 107; static const int CODE_FILE_MISSING = 108; + static const int CODE_INVALID_RESPONSE_FORMAT = 109; - int _responseCode = 0; + int _responseCode = CODE_INVALID_RESPONSE_FORMAT; dynamic _payload; - String _errorMessage = ""; + String _errorMessage = "Invalid response format"; final dynamic _body; @@ -33,13 +36,13 @@ class SOLCResponse { if (response._body['code'] != null) { response._responseCode = response._body['code']; } else { - //Invalid response return response; } if (response._responseCode < 100) { if (response._body['data'] != null) { response._payload = response._body['data']; + response._errorMessage = ""; } } else { if (response._body['error'] != null) { diff --git a/lib/core/service/time_table.service.dart b/lib/core/service/time_table.service.dart index b955fc4..3183a42 100644 --- a/lib/core/service/time_table.service.dart +++ b/lib/core/service/time_table.service.dart @@ -44,7 +44,7 @@ class TimeTableService with ChangeNotifier { notifyListeners(); //apiManager = SOLCApiManager(await getServerAddress(), 6969); - apiManager = SOLCApiManager(await getServerAddress(), 6969); + apiManager = SOLCApiManager("127.0.0.1", 6969); apiManager!.getVersion().then( (value) { diff --git a/lib/ui/screens/teacher_classes/teacher_classes.screen.dart b/lib/ui/screens/teacher_classes/teacher_classes.screen.dart index f0db847..156491c 100644 --- a/lib/ui/screens/teacher_classes/teacher_classes.screen.dart +++ b/lib/ui/screens/teacher_classes/teacher_classes.screen.dart @@ -4,6 +4,7 @@ import 'package:flutter_search_bar/flutter_search_bar.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:logger/logger.dart'; import 'package:sol_connect/core/api/models/schoolclass.dart'; +import 'package:sol_connect/core/api/usersession.dart'; import 'package:sol_connect/core/excel/models/phasestatus.dart'; import 'package:sol_connect/core/service/services.dart'; import 'package:sol_connect/ui/screens/teacher_classes/widgets/teacher_class_card.dart'; @@ -67,7 +68,7 @@ class _TeacherClassesScreenState extends ConsumerState { final theme = ref.watch(themeService).theme; List list = []; - //_timeTableService.session.setTimetableBehaviour(308, PersonTypes.teacher); + _timeTableService.session.setTimetableBehaviour(308, PersonTypes.teacher); List allClassesAsTeacher = await _timeTableService.session.getClassesAsTeacher(checkRange: 2); List ownClassesAsTeacher = await _timeTableService.session.getOwnClassesAsClassteacher(simulateTeacher: "CAG"); @@ -113,18 +114,36 @@ class _TeacherClassesScreenState extends ConsumerState { ), ), ); + } + try { + List found = await ref.read(timeTableService).apiManager!.getSchoolClassInfos(schoolClassIds: ownClassesAsTeacher.map((e) => e.id).toList()); for (var i = 0; i < ownClassesAsTeacher.length; i++) { - PhaseStatus? status; - try { - status = - await ref.read(timeTableService).apiManager!.getSchoolClassInfo(schoolClassId: ownClassesAsTeacher[i].id); - } catch (e) { - log.e(e); + bool exists = false; + for(PhaseStatus f in found) { + if(f.id == ownClassesAsTeacher[i].id) { + list.add(TeacherClassCard(schoolClass: ownClassesAsTeacher[i], phaseStatus: f)); + exists = true; + break; + } + } + if(!exists) { + list.add(TeacherClassCard(schoolClass: ownClassesAsTeacher[i], phaseStatus: null)); } - - list.add(TeacherClassCard(schoolClass: ownClassesAsTeacher[i], phaseStatus: status)); } + } catch(e) { + log.e(e); + list.add( + Padding( + padding: const EdgeInsets.fromLTRB(20, 20, 20.0, 5), + child: Center( + child: Text( + "Unbekannter Fehler: " + e.toString(), + style: TextStyle(fontSize: 15, color: theme.colors.error), + ), + ), + ), + ); } if (allClassesAsTeacher.isNotEmpty) { @@ -150,16 +169,35 @@ class _TeacherClassesScreenState extends ConsumerState { ), ); - for (var i = 0; i < allClassesAsTeacher.length; i++) { - PhaseStatus? status; - try { - status = - await ref.read(timeTableService).apiManager!.getSchoolClassInfo(schoolClassId: allClassesAsTeacher[i].id); - } catch (e) { - log.e(e); + try { + List classesAsTeacherPhases = await ref.read(timeTableService).apiManager!.getSchoolClassInfos(schoolClassIds: allClassesAsTeacher.map((e) => e.id).toList()); + for (var i = 0; i < allClassesAsTeacher.length; i++) { + bool exists = false; + for(PhaseStatus f in classesAsTeacherPhases) { + if(f.id == allClassesAsTeacher[i].id) { + list.add(TeacherClassCard(schoolClass: allClassesAsTeacher[i], phaseStatus: f)); + exists = true; + break; + } + + } + if(!exists) { + list.add(TeacherClassCard(schoolClass: allClassesAsTeacher[i], phaseStatus: null)); + } } - - list.add(TeacherClassCard(schoolClass: allClassesAsTeacher[i], phaseStatus: status)); + } catch(e) { + log.e(e); + list.add( + Padding( + padding: const EdgeInsets.fromLTRB(20, 20, 20.0, 5), + child: Center( + child: Text( + "Unbekannter Fehler: " + e.toString(), + style: TextStyle(fontSize: 15, color: theme.colors.error), + ), + ), + ), + ); } }