@ -1,7 +1,6 @@
package com.speculos.myapplicationoc ;
package com.speculos.myapplicationoc ;
import android.app.Activity ;
import android.app.Activity ;
import android.content.Intent ;
import android.os.Bundle ;
import android.os.Bundle ;
import android.util.Log ;
import android.util.Log ;
import android.view.View ;
import android.view.View ;
@ -13,19 +12,26 @@ import android.widget.Toast;
import androidx.annotation.Nullable ;
import androidx.annotation.Nullable ;
import java.io.IOException ;
import java.io.IOException ;
import java.util.regex.Pattern ;
public class TagDetectedActivityAdmin extends Activity {
public class TagDetectedActivityAdmin extends Activity {
String hexa_id , mail ;
String hexa_id , mail , username ;
// recuperation de MyApplication contenant les variables globales
// MyApplication myApplication = (MyApplication) getApplication();
String srvURL ;
// Temporaire
// Temporaire
String srvURL = "http://192.168.1.21" ;
// String nfcId = "azerty";
String nfcId = "azerty" ;
@Override
@Override
protected void onCreate ( @Nullable Bundle savedInstanceState ) {
protected void onCreate ( @Nullable Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
super . onCreate ( savedInstanceState ) ;
MyApplication myApplication = ( MyApplication ) getApplication ( ) ;
srvURL = myApplication . getServerURL ( ) ;
setContentView ( R . layout . activity_tag_detected_admin ) ;
setContentView ( R . layout . activity_tag_detected_admin ) ;
hexa_id = getIntent ( ) . getStringExtra ( "hexa_id" ) ;
hexa_id = getIntent ( ) . getStringExtra ( "hexa_id" ) ;
@ -47,7 +53,79 @@ public class TagDetectedActivityAdmin extends Activity {
// Vérifications et Call API créer le compte
// Vérifications et Call API créer le compte
EditText value_mail = findViewById ( R . id . value_mail ) ;
EditText value_mail = findViewById ( R . id . value_mail ) ;
mail = value_mail . getText ( ) . toString ( ) ;
mail = value_mail . getText ( ) . toString ( ) ;
Toast . makeText ( TagDetectedActivityAdmin . this , mail , Toast . LENGTH_LONG ) . show ( ) ;
username = mail . subSequence ( 0 , mail . indexOf ( "@" ) ) . toString ( ) ;
// Validation du champ email
String regex = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$" ;
Pattern pattern = Pattern . compile ( regex ) ;
if ( pattern . matcher ( mail ) . matches ( ) ) {
// L'email est validé par la regex on va pouvoir préparer la requête de verif si compte existe
// Call l'API pour voir si un client avec ce mail existe
String apiResponse = "" ;
try {
apiResponse = APICommandes . getMethod ( srvURL + "/api/clients.json?email=" + mail ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
if ( apiResponse . length ( ) < = 3 ) {
// Le mail n'est pas présent créer un nouveau compte.
Toast . makeText ( TagDetectedActivityAdmin . this , "Création de compte NFC Cashless, mail:" + mail + " name:" + username + " bracelet:" + hexa_id , Toast . LENGTH_LONG ) . show ( ) ;
String response = "" ;
try {
response = APICommandes . postMethod ( srvURL + "/api/clients" , "{\"name\": \"" + username + "\", \"email\": \"" + mail + "\", \"solde\" :" + 0 + "}" ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
Toast . makeText ( TagDetectedActivityAdmin . this , "Error" , Toast . LENGTH_LONG ) . show ( ) ;
}
if ( response . length ( ) < = 3 ) {
// Info sur le retour du POST Creation client
Toast . makeText ( TagDetectedActivityAdmin . this , "Server not responding: " + response , Toast . LENGTH_LONG ) . show ( ) ;
} else {
// Creation du compte réussie, link le bracelet au compte client (TODO: penser à faire les verifs si bracelet existe avant de créer un compte client)
// Une fois qu'on a vérifié que le bracelet n'existe pas dans la base on l'ajoute au client
// On commence par parser la reponse de l'appel POST précédent pour avoir l'idClient sans refaire de requête
JsonTraductor maListeJson = new JsonTraductor ( response ) ;
int idClient = Integer . parseInt ( ( maListeJson . getJsonData ( "id" , 0 ) ) ) ;
// On peut maintenant link le bracelet grace à l'hexa_id et l'idClient
try {
response = APICommandes . postMethod ( srvURL + "/api/bracelets" , "{\"nfcId\": \"" + hexa_id + "\", \"client\": \"/api/clients/" + idClient + "\", \"state\" :" + 0 + "}" ) ;
Toast . makeText ( TagDetectedActivityAdmin . this , "Message: " + response , Toast . LENGTH_LONG ) . show ( ) ;
Log . d ( "DEBUG-CLIENT" , response ) ;
try {
maListeJson = new JsonTraductor ( response ) ;
String violationMessage = ( ( maListeJson . getJsonData ( "violations" , 0 ) ) ) ;
Toast . makeText ( TagDetectedActivityAdmin . this , "Message: " + violationMessage , Toast . LENGTH_LONG ) . show ( ) ;
} catch ( Exception e ) { }
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
} else {
// Le mail est déjà present en base
Toast . makeText ( TagDetectedActivityAdmin . this , "Le mail est déjà présent en base. Souhaitez vous ajouter ce bracelet au compte: " + mail , Toast . LENGTH_LONG ) . show ( ) ;
}
} else {
// Le mail ne match pas la regex mail
Toast . makeText ( TagDetectedActivityAdmin . this , "Message:" + mail + " non valide" , Toast . LENGTH_LONG ) . show ( ) ;
}
}
}
} ) ;
} ) ;