Skip to content

Commit

Permalink
#175 improve traffic
Browse files Browse the repository at this point in the history
  • Loading branch information
DevKevYT committed May 12, 2022
1 parent 0c10ba1 commit 6a3ddfb
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 30 deletions.
3 changes: 2 additions & 1 deletion lib/core/excel/models/phasestatus.dart
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
43 changes: 36 additions & 7 deletions lib/core/excel/solc_api_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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<List<PhaseStatus>> getSchoolClassInfos({required List<int> schoolClassIds}) async {

String args = "";
for(int id in schoolClassIds) {
args += " <" + id.toString() + ">";
}

SOLCResponse? response = await _querySOLC(command: "phase-statuses$args");

if (response != null) {
List<PhaseStatus> 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<PhaseStatus?> 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;
}
Expand Down Expand Up @@ -115,6 +141,7 @@ class SOLCApiManager {
Future<SOLCResponse?> _querySOLC({required String command, File? uploadFileSource, List<int>? downloadBytes}) async {
SOLCResponse? returnValue;
dynamic exception;
bool hasException = false;

try {
_activeSockets = _activeSockets + 1;
Expand All @@ -131,6 +158,7 @@ class SOLCApiManager {

void throwException(Exception e) {
exception = e;
hasException = true;
socket.close();
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions lib/core/excel/solcresponse.dart
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/service/time_table.service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
74 changes: 56 additions & 18 deletions lib/ui/screens/teacher_classes/teacher_classes.screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -67,7 +68,7 @@ class _TeacherClassesScreenState extends ConsumerState<TeacherClassesScreen> {
final theme = ref.watch(themeService).theme;

List<Widget> list = [];
//_timeTableService.session.setTimetableBehaviour(308, PersonTypes.teacher);
_timeTableService.session.setTimetableBehaviour(308, PersonTypes.teacher);
List<SchoolClass> allClassesAsTeacher = await _timeTableService.session.getClassesAsTeacher(checkRange: 2);
List<SchoolClass> ownClassesAsTeacher =
await _timeTableService.session.getOwnClassesAsClassteacher(simulateTeacher: "CAG");
Expand Down Expand Up @@ -113,18 +114,36 @@ class _TeacherClassesScreenState extends ConsumerState<TeacherClassesScreen> {
),
),
);
}

try {
List<PhaseStatus> 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) {
Expand All @@ -150,16 +169,35 @@ class _TeacherClassesScreenState extends ConsumerState<TeacherClassesScreen> {
),
);

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<PhaseStatus> 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),
),
),
),
);
}
}

Expand Down

0 comments on commit 6a3ddfb

Please sign in to comment.