diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..dfa1771 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + mariadb + true + org.mariadb.jdbc.Driver + jdbc:mariadb://localhost:3306 + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/dataSources/f334e98a-3c30-4412-8c71-35fe124ed605.xml b/.idea/dataSources/f334e98a-3c30-4412-8c71-35fe124ed605.xml new file mode 100644 index 0000000..51b344b --- /dev/null +++ b/.idea/dataSources/f334e98a-3c30-4412-8c71-35fe124ed605.xml @@ -0,0 +1,16 @@ + + + + + lower/lower + 10.4.25 + + + + + + + + + + \ No newline at end of file diff --git a/.idea/dataSources/f334e98a-3c30-4412-8c71-35fe124ed605/storage_v2/_src_/schema/information_schema.FNRwLQ.meta b/.idea/dataSources/f334e98a-3c30-4412-8c71-35fe124ed605/storage_v2/_src_/schema/information_schema.FNRwLQ.meta new file mode 100644 index 0000000..1ff3db2 --- /dev/null +++ b/.idea/dataSources/f334e98a-3c30-4412-8c71-35fe124ed605/storage_v2/_src_/schema/information_schema.FNRwLQ.meta @@ -0,0 +1,2 @@ +#n:information_schema +! [null, 0, null, null, -2147483648, -2147483648] diff --git a/.idea/dataSources/f334e98a-3c30-4412-8c71-35fe124ed605/storage_v2/_src_/schema/mysql.osA4Bg.meta b/.idea/dataSources/f334e98a-3c30-4412-8c71-35fe124ed605/storage_v2/_src_/schema/mysql.osA4Bg.meta new file mode 100644 index 0000000..86a53f1 --- /dev/null +++ b/.idea/dataSources/f334e98a-3c30-4412-8c71-35fe124ed605/storage_v2/_src_/schema/mysql.osA4Bg.meta @@ -0,0 +1,2 @@ +#n:mysql +! [null, 0, null, null, -2147483648, -2147483648] diff --git a/.idea/dataSources/f334e98a-3c30-4412-8c71-35fe124ed605/storage_v2/_src_/schema/performance_schema.kIw0nw.meta b/.idea/dataSources/f334e98a-3c30-4412-8c71-35fe124ed605/storage_v2/_src_/schema/performance_schema.kIw0nw.meta new file mode 100644 index 0000000..9394db1 --- /dev/null +++ b/.idea/dataSources/f334e98a-3c30-4412-8c71-35fe124ed605/storage_v2/_src_/schema/performance_schema.kIw0nw.meta @@ -0,0 +1,2 @@ +#n:performance_schema +! [null, 0, null, null, -2147483648, -2147483648] diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..8485c62 --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Sources/dafl_project_flutter/lib/position/location.dart b/Sources/dafl_project_flutter/lib/position/location.dart index 3be754f..6fc1f55 100644 --- a/Sources/dafl_project_flutter/lib/position/location.dart +++ b/Sources/dafl_project_flutter/lib/position/location.dart @@ -6,48 +6,52 @@ import '../main.dart'; class Location { static Future> sendCurrentLocation() async { - Uri uri = Uri.parse("https://codefirst.iut.uca.fr/containers/php_script-dorianhodin/insert.php"); + + Uri uri = Uri.parse("https://codefirst.iut.uca.fr/containers/php_script-dorianhodin/insertAndMakeListUser.php"); + Map spot = {}; LocationPermission permission; + permission = await Geolocator.checkPermission(); + if (permission == LocationPermission.denied) { + permission = await Geolocator.requestPermission(); + if (permission == LocationPermission.deniedForever) { - //faire l'interface gra pour gérer ça + return Future.error('Location Not Available'); + } } + String actualUser = MyApp.controller.currentUser.usernameDafl; String actualSong = await MyApp.api.getCurrentlyPlayingTrack(); Position current = await Geolocator.getCurrentPosition(); - await http.post(uri, body: { + + http.Response response = await http.post(uri, body: { "id": actualUser.toString(), "latitude": current.latitude.toString(), "longitude": current.longitude.toString(), "idMusic": actualSong.toString(), }); - return getData(); - } - static Future> getData() async { - Map spot = {}; - String actualUser = MyApp.controller.currentUser.usernameDafl; - Uri uri = Uri.parse("https://codefirst.iut.uca.fr/containers/php_script-dorianhodin/distance.php"); - http.Response response = await http.post(uri, body: { - "id": actualUser, - }); var data = jsonDecode(response.body); - if (data.runtimeType==String) { - if (data=="Failed to connect to MySQL, connection timeout"){ - return Future.error(data); - }else if (data=="The POST didn't return any values"){ - return Future.error(data); - }else{ - return Future.error(data); //ERROR No user found in the database - } - }else { + + if (data == 2){ + + return Future.error("Failed to connect, connection timeout"); + + }else if (data == 3) { + + return Future.error("POST method failed"); + + }else{ + data.forEach((s) => spot.putIfAbsent(s['user'], () => s['music'])); return spot; + } } } + diff --git a/Sources/php_script/script/config.php b/Sources/php_script/script/config.php index c604963..c4ed787 100644 --- a/Sources/php_script/script/config.php +++ b/Sources/php_script/script/config.php @@ -1,13 +1,22 @@ fetch_row()){ //For all the row in the database - if (strcmp($row[0], $id) == 0) { //If the user is found in the database - $lat1 = $row[1]; //Set $lat1 to the latitude of the current user - $lng1 = $row[2]; //Set $lng1 to the longitude of the current user - } - } - if ($lat1 == 0 && $lng1 == 0) { //Check if the user get found in the database - print(json_encode("ERROR No user found in the database")); //Return a json string, so the dart script can interpret the error - exit(1); //Exit the actual script - } - $results = mysqli_query($connect, $query); //Execute again the SQL command to restart the fetch_row() - while ($row = $results->fetch_row()) { //For all the row in the database - if (strcmp($row[0], $id) != 0) { //If the row is not the row of the current user - $lat2 = $row[1]; //Set $lat2 to the latitude of the user who is in the actual row - $lng2 = $row[2]; //Set $lng2 to the latitude of the user who is in the actual row - $userID = $row[0]; //Set $userID to the username of the user who is in the actual row - $idMusic = $row[3]; //Set $idMusic to the id of the actual song of the user who is in the actual row - $dist = meters($lat1, $lng1, $lat2, $lng2); //With the function meters, calcul of the distance between the current user and the user who is in the actual row - if ($dist <= 100) { //If the user in the actual row is less than 100 meters away of the current user - $listUser[] = ['user' => $userID, 'music' => $idMusic]; //Add the username and the ID of the song that user who is in the actual row is listening - } - } - } - print(json_encode($listUser)); //Return a json string of the list listUser -}else{ //If the method POST return nothing - print (json_encode("The POST didn't return any values")); //Return a json string, so the dart script can interpret the error -} diff --git a/Sources/php_script/script/index.php b/Sources/php_script/script/index.php deleted file mode 100644 index f6595e4..0000000 --- a/Sources/php_script/script/index.php +++ /dev/null @@ -1,5 +0,0 @@ - \ No newline at end of file diff --git a/Sources/php_script/script/insert.php b/Sources/php_script/script/insert.php deleted file mode 100644 index 84ffd22..0000000 --- a/Sources/php_script/script/insert.php +++ /dev/null @@ -1,26 +0,0 @@ -10;"; - $results = mysqli_query($connect, $query); - */ - $query = "DELETE FROM gps WHERE id='$id';"; //Delete the actual line and replace this line with the next lines - $results = mysqli_query($connect, $query); //Execute the SQL command - - $query = "INSERT INTO gps (id,latitude,longitude,idMusic,dateLog) - VALUES('$id','$latitude','$longitude','$idMusic',CURRENT_TIMESTAMP);"; //Insert into the database the new data and new information about this user - $results = mysqli_query($connect, $query); //Execute the SQL command -}else{ //If the method POST return nothing - print (json_encode("The POST didn't return any values")); //Return a json string, so the dart script can interpret the error -} \ No newline at end of file diff --git a/Sources/php_script/script/insertAndMakeListUser.php b/Sources/php_script/script/insertAndMakeListUser.php new file mode 100644 index 0000000..f81f726 --- /dev/null +++ b/Sources/php_script/script/insertAndMakeListUser.php @@ -0,0 +1,68 @@ +10;"; + $results = mysqli_query($connect, $query); + */ + + $query = "SELECT id FROM gps WHERE id='$id'"; + $results = mysqli_query($res, $query); //Execute the SQL command + + if (empty($results)){ + + $query = "INSERT INTO gps(id,latitude,longitude,idMusic,dateLog) VALUES('$id','$latitude','$longitude','$idMusic',CURRENT_TIMESTAMP);"; //Insert into the database the new data and new information about this user + + }else{ + + $query = "UPDATE gps SET latitude='$latitude', longitude='$longitude', idMusic='$idMusic' WHERE id='$id'"; //Delete the actual line and replace this line with the next lines + + } + + mysqli_query($res, $query); + + $query = "SELECT * FROM gps WHERE id != '$id'"; //Browse all the database + $results = mysqli_query($res, $query); //Execute the SQL command + $listUser = []; //Set the listUser to an empty list + + while ($row = $results->fetch_row()) { //For all the row in the database + + $lat2 = $row[1]; //Set $lat2 to the latitude of the user who is in the actual row + $lng2 = $row[2]; //Set $lng2 to the latitude of the user who is in the actual row + $userID = $row[0]; //Set $userID to the username of the user who is in the actual row + $idMusic = $row[3]; //Set $idMusic to the id of the actual song of the user who is in the actual row + + $dist = metersBetweenTwoUser($latitude, $longitude, $lat2, $lng2); //With the function meters, determinate the distance between the current user and the user who is in the actual row + + if ($dist <= 100) { //If the user in the actual row is less than 100 meters away of the current user + + $listUser[] = ['user' => $userID, 'music' => $idMusic]; //Add the username and the ID of the song that user who is in the actual row is listening + + } + } + return (json_encode($listUser)); //Return an encode array in json, so flutter ca=n interpret it + + } else { //If the method POST return nothing + + return 3; //Return a code error + + } +} \ No newline at end of file