From 4d9a8d347d43e1d8cc37c84f29e3fbe4c137136c Mon Sep 17 00:00:00 2001 From: Enzo Date: Thu, 9 Nov 2023 15:52:12 +0100 Subject: [PATCH 1/3] getSpeedWithTime finie --- lib/Modele/manager_file.dart | 28 ++++++++++++++++++---------- lib/View/page_test.dart | 4 ++-- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/Modele/manager_file.dart b/lib/Modele/manager_file.dart index fa13c6e..dd3ea85 100644 --- a/lib/Modele/manager_file.dart +++ b/lib/Modele/manager_file.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'dart:ffi'; import 'dart:io'; import 'package:csv/csv.dart'; import 'package:fit_tool/fit_tool.dart'; @@ -102,7 +103,8 @@ class ManagerFile { firtTimeStamp = ligne[4]; } //result.add([(ligne[4] - firtTimeStamp) ~/ 100, ligne[7].toInt()]); - result.add(FlSpot((ligne[4] - firtTimeStamp) ~/ 100, ligne[7])); + result + .add(FlSpot((ligne[4] - firtTimeStamp) / 100, ligne[7].toDouble())); } } return result; @@ -118,27 +120,27 @@ class ManagerFile { firtTimeStamp = ligne[4]; } //result.add([(ligne[4] - firtTimeStamp) ~/ 100, ligne[13].toInt()]); - result.add(FlSpot((ligne[4] - firtTimeStamp) ~/ 100, ligne[13])); + result.add( + FlSpot((ligne[4] - firtTimeStamp) / 100, ligne[13].toDouble())); } } return result; } - int getDistance(ActivityOfUser activity) { - int result = 0; + double getDistance(ActivityOfUser activity) { + double result = 0.0; for (int i = activity.contentActivity.length - 1; i >= 0; i--) { if (activity.contentActivity[i].length >= 8 && activity.contentActivity[i][0] == "Data" && activity.contentActivity[i][6] == "distance") { if (activity.contentActivity[i][7] > result) { - result = activity.contentActivity[i][7].toInt(); + result = activity.contentActivity[i][7].toDouble(); } } } return result; } - /* En Cours List getSpeedWithTime(ActivityOfUser activityOfUser) { List result = List.empty(growable: true); int firtTimeStamp = 0; @@ -148,11 +150,17 @@ class ManagerFile { if (firtTimeStamp == 0) { firtTimeStamp = ligne[4]; } - result - .add(FlSpot((ligne[4] - firtTimeStamp) ~/ 100, ligne[19].toInt())); - //result.add([(ligne[4] - firtTimeStamp) ~/ 100, ligne[19].toInt()]); + result.add( + FlSpot((ligne[4] - firtTimeStamp) / 100, ligne[19].toDouble())); + } + if (ligne[0] == "Data" && ligne[1] == 2) { + if (firtTimeStamp == 0) { + firtTimeStamp = ligne[4]; + } + result.add( + FlSpot((ligne[4] - firtTimeStamp) / 100, ligne[25].toDouble())); } } return result; - }*/ + } } diff --git a/lib/View/page_test.dart b/lib/View/page_test.dart index b718819..c42bae4 100644 --- a/lib/View/page_test.dart +++ b/lib/View/page_test.dart @@ -66,11 +66,11 @@ class _TestPage extends State { print("t"); } else { List result = await x.readFitFile(y!); - print(result); - print(x.getHeartRateWithTime(ActivityOfUser(result))); + //print(x.getHeartRateWithTime(ActivityOfUser(result))); //print(x.getDistanceWithTime(ActivityOfUser(result))); //print(x.getDistance(ActivityOfUser(result))); //print(x.getAltitudeWithTime(ActivityOfUser(result))); + print(x.getSpeedWithTime(ActivityOfUser(result))); } } From 5cb64e5682bb62eb85eceffb7ed903ef32279721 Mon Sep 17 00:00:00 2001 From: Enzo Date: Thu, 9 Nov 2023 16:24:25 +0100 Subject: [PATCH 2/3] modification de post User --- lib/Modele/Api/i_data_strategy.dart | 4 +--- lib/Modele/Api/request_api.dart | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/Modele/Api/i_data_strategy.dart b/lib/Modele/Api/i_data_strategy.dart index aff5893..4495306 100644 --- a/lib/Modele/Api/i_data_strategy.dart +++ b/lib/Modele/Api/i_data_strategy.dart @@ -1,10 +1,8 @@ import 'dart:io'; -import 'package:smartfit_app_mobile/Modele/user.dart'; - abstract class IDataStrategy { // Create user - Future postUser(User user); + Future postUser(String email, String hash, String username); // Delete user Future deleteUser(String token); diff --git a/lib/Modele/Api/request_api.dart b/lib/Modele/Api/request_api.dart index f71aece..55abff7 100644 --- a/lib/Modele/Api/request_api.dart +++ b/lib/Modele/Api/request_api.dart @@ -3,11 +3,11 @@ import 'dart:io'; import 'package:smartfit_app_mobile/Modele/Api/i_data_strategy.dart'; import 'package:http/http.dart' as http; -import 'package:smartfit_app_mobile/Modele/user.dart'; class RequestApi extends IDataStrategy { // Faire attention au URL - String urlApi = ""; + String urlApi = + "https://codefirst.iut.uca.fr/containers/SmartFit-smartfit_api"; @override Future getFile(String token, String idFile) async { @@ -33,7 +33,8 @@ class RequestApi extends IDataStrategy { @override Future deleteUser(String token) async { - final response = await http.delete(Uri.parse('$urlApi/$token')); + final response = await http.delete(Uri.parse('$urlApi/user'), + headers: {'Authorization': token}); if (response.statusCode == 200) { return true; } else { @@ -65,17 +66,23 @@ class RequestApi extends IDataStrategy { } @override - Future postUser(User user) async { - final response = await http.post(Uri.parse(urlApi), - headers: { - 'Content-Type': 'application/json; charset=UTF-8', - }, - body: jsonEncode({'user': user})); + Future postUser(String email, String hash, String username) async { + final response = + await http.post(Uri.parse('$urlApi/user'), headers: { + 'Content-Type': 'application/json; charset=UTF-8', + }, body: { + "email": "'$email'", + "hash": "'$hash'", + "username": "'$username'" + }); if (response.statusCode == 200) { return true; - } else { - return false; } + if (response.statusCode == 400) { + print("Json mal formaté"); + } + // Erreur possible 409 (Déja un compte avec cet email) + return false; } @override From 4778f2b5fbf73e8028587fbce1df53bb526ed7c1 Mon Sep 17 00:00:00 2001 From: Enzo Date: Fri, 10 Nov 2023 09:22:34 +0100 Subject: [PATCH 3/3] rapide commit --- lib/Modele/Api/i_data_strategy.dart | 17 +++-- lib/Modele/Api/request_api.dart | 108 +++++++++++++++++++++------- pubspec.yaml | 1 + 3 files changed, 97 insertions(+), 29 deletions(-) diff --git a/lib/Modele/Api/i_data_strategy.dart b/lib/Modele/Api/i_data_strategy.dart index 4495306..e7c7286 100644 --- a/lib/Modele/Api/i_data_strategy.dart +++ b/lib/Modele/Api/i_data_strategy.dart @@ -1,5 +1,7 @@ import 'dart:io'; +import 'package:tuple/tuple.dart'; + abstract class IDataStrategy { // Create user Future postUser(String email, String hash, String username); @@ -8,23 +10,28 @@ abstract class IDataStrategy { Future deleteUser(String token); // Get Token validate - Future getToken(String uuid, String passwordHash); + Future connexion(String email, String hash); // Get all files for user - Future getFiles(String token); + //Future getFiles(String token); // Upload file on BDD - Future uploadFile(String token, File file); + Future> uploadFile(String token, File file); // Get one file by id - Future getFile(String token, String idFile); + Future> getFile(String token, String fileUuid); // Delete one file on BDD - Future deleteFile(String token, String idFile); + //Future deleteFile(String token, String idFile); + /* -> Modification attribut // Update email Future updateEmail(String token, String email); // Update username Future updateUsername(String token, String username); + */ + + Future modifAttribut( + String token, String nameAttribut, String newValue); } diff --git a/lib/Modele/Api/request_api.dart b/lib/Modele/Api/request_api.dart index 55abff7..b35bfb5 100644 --- a/lib/Modele/Api/request_api.dart +++ b/lib/Modele/Api/request_api.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:smartfit_app_mobile/Modele/Api/i_data_strategy.dart'; import 'package:http/http.dart' as http; +import 'package:tuple/tuple.dart'; class RequestApi extends IDataStrategy { // Faire attention au URL @@ -10,8 +11,10 @@ class RequestApi extends IDataStrategy { "https://codefirst.iut.uca.fr/containers/SmartFit-smartfit_api"; @override - Future getFile(String token, String idFile) async { - final response = await http.get(Uri.parse('$urlApi/$token/files/$idFile')); + Future> getFile(String token, String fileUuid) async { + final response = await http.get(Uri.parse('$urlApi/user/files/$fileUuid'), + headers: {'Authorization': token}); + if (response.statusCode == 200) { /*return Classe.fromJson(jsonDecode(response.body) as Map);*/ throw UnimplementedError(); @@ -32,19 +35,25 @@ class RequestApi extends IDataStrategy { } @override - Future deleteUser(String token) async { + Future deleteUser(String token) async { final response = await http.delete(Uri.parse('$urlApi/user'), headers: {'Authorization': token}); if (response.statusCode == 200) { - return true; - } else { - return false; + return const Tuple2(true, "Successful"); + } else if (response.statusCode == 401) { + return const Tuple2( + false, "401 UNAUTHORIZED - Mauvais ou pas de token"); + } else if (response.statusCode == 404) { + return const Tuple2( + false, "404 NOT FOUND - Pas de compte lié"); } + return const Tuple2(false, "Fail"); } @override Future getFiles(String token) async { final response = await http.get(Uri.parse('$urlApi/$token/files')); + if (response.statusCode == 200) { /*return Classe.fromJson(jsonDecode(response.body) as Map);*/ throw UnimplementedError(); @@ -54,19 +63,22 @@ class RequestApi extends IDataStrategy { } @override - Future getToken(String uuid, String passwordHash) async { + Future connexion(String email, String hash) async { final response = - await http.get(Uri.parse('$urlApi/$uuid/$passwordHash/token')); + await http.get(Uri.parse('$urlApi/user/login/$email/$hash')); if (response.statusCode == 200) { - /*return Classe.fromJson(jsonDecode(response.body) as Map);*/ - throw UnimplementedError(); - } else { - throw UnimplementedError(); + Map json = + jsonDecode(response.body) as Map; + return Tuple2(true, json['token']!); + } else if (response.statusCode == 401) { + return const Tuple2(false, "UNAUTHORIZED"); } + return const Tuple2(false, "Fail"); } + /* @override - Future postUser(String email, String hash, String username) async { + Future postUser(String email, String hash, String username) async { final response = await http.post(Uri.parse('$urlApi/user'), headers: { 'Content-Type': 'application/json; charset=UTF-8', @@ -76,15 +88,19 @@ class RequestApi extends IDataStrategy { "username": "'$username'" }); if (response.statusCode == 200) { - return true; + return const Tuple2(true, "Successful"); } if (response.statusCode == 400) { - print("Json mal formaté"); + return const Tuple2(false, "400 BAD REQUEST - Json mal formaté"); } - // Erreur possible 409 (Déja un compte avec cet email) - return false; - } + if (response.statusCode == 409) { + return const Tuple2( + false, "409 CONFLICT - Déja un compte avec cet email"); + } + return const Tuple2(false, "Fail"); + }*/ + /* @override Future updateEmail(String token, String email) async { final response = await http.put(Uri.parse('$urlApi/$token/email'), @@ -113,19 +129,63 @@ class RequestApi extends IDataStrategy { } else { return false; } - } + }*/ @override - Future uploadFile(String token, File file) async { - final response = await http.post(Uri.parse('$urlApi/$token/UploadFile'), + Future modifAttribut( + String token, String nameAttribut, String newValue) async { + final response = await http.put(Uri.parse('$urlApi/user/$nameAttribut'), headers: { 'Content-Type': 'application/json; charset=UTF-8', + 'Authorization': token }, - body: jsonEncode({'file': file})); + body: jsonEncode({nameAttribut: newValue})); + if (response.statusCode == 200) { - return true; + return const Tuple2(true, "Successful"); + } + if (response.statusCode == 400) { + return const Tuple2(false, "400 - BAD REQUEST"); + } + if (response.statusCode == 401) { + return const Tuple2(false, "400 - UNAUTHORIZED"); } else { - return false; + return const Tuple2(false, "Fail"); } } + + @override + Future> uploadFile(String token, File file) async { + final uri = Uri.parse('$urlApi/user/files'); + Map headers = {'Authorization': token}; + + var request = http.MultipartRequest('POST', uri); + final httpImage = http.MultipartFile.fromBytes( + 'file_FIT', await file.readAsBytes(), + filename: file.path.split('/').last); + request.files.add(httpImage); + request.headers.addAll(headers); + + final response = await request.send(); + + if (response.statusCode == 200) { + return const Tuple2(true, "Successful"); + } + if (response.statusCode == 400) { + return const Tuple2(false, "400 - BAD REQUEST"); + } + if (response.statusCode == 401) { + return const Tuple2(false, "401 - UNAUTHORIZED"); + } + if (response.statusCode == 409) { + return const Tuple2(false, "409 - CONFLICT"); + } + return const Tuple2(false, "Fail "); + } + + @override + Future postUser(String email, String hash, String username) { + // TODO: implement postUser + throw UnimplementedError(); + } } diff --git a/pubspec.yaml b/pubspec.yaml index 4cb6e21..affc2bd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -52,6 +52,7 @@ dependencies: flutter_form_builder: ^9.1.1 http: ^1.1.0 provider: ^6.0.5 + tuple: ^2.0.2 dev_dependencies: flutter_test: