Page 1 of 1

howto add parameter into callback ?

Posted: Sat Apr 03, 2021 11:30 am
by killer128k
ENG: Hello,
I want to add a parameter to a callback and I don't know how?
I thank you in advance for your answers.
(error line #21)

FR: Bonjour,
Je souhaite ajouter un paramètre à un callback et je ne sais pas comment faire ?
Je vous remerci d'avance pour vos réponses.
(erreur ligne #21)

Code: Select all

Procedure CreateFileCallback(Status, Filename$, File, SizeRead)
  Select Status
    Case #PB_Status_Loaded
      Debug "Database file found and loaded: " + Filename$
      *DatabaseBuffer = ExportFileMemory(File)    
      If OpenDatabase(0, *DatabaseBuffer)
        ExportFile(0, "application/octet-stream")
        CloseFile(0)
        Debug "export du fichier sqlite: réussit."
      Else
        Debug "export du fichier sqlite: échec."
      EndIf      
    Case #PB_Status_Saved
      Debug "Database file saved: " + Filename$
      Debug "Please relaunch the program to test the persitency."     
    Case #PB_Status_Error : Debug "Can't save the database: " + Filename$
  EndSelect
EndProcedure

Procedure ReadFileCallbackMAJcompta(Status, Filename$, File, SizeRead)  ;<- ,Result$
  Debug "rrrr "+Result$ ;<- error I not have ;     Result$ = "[{"+Chr(34)+"comptasql"+Chr(34)+":[{"+Chr(34)+"date"+Chr(34)+":"+Chr(34)+"01\/01\/2021"+Chr(34)+","+Chr(34)+"libelle"+Chr(34)+":"+Chr(34)+"JANVIER   21"+Chr(34)+","+Chr(34)+"debit"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+","+Chr(34)+"credit"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+","+Chr(34)+"solde"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+"},{"+Chr(34)+"date"+Chr(34)+":"+Chr(34)+"15\/01\/2021"+Chr(34)+","+Chr(34)+"libelle"+Chr(34)+":"+Chr(34)+"Règlement poptrick      5117724"+Chr(34)+","+Chr(34)+"debit"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+","+Chr(34)+"credit"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+","+Chr(34)+"solde"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+"},{"+Chr(34)+"date"+Chr(34)+":"+Chr(34)+"01\/02\/2021"+Chr(34)+","+Chr(34)+"libelle"+Chr(34)+":"+Chr(34)+"FEVRIER   21"+Chr(34)+","+Chr(34)+"debit"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+","+Chr(34)+"credit"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+","+Chr(34)+"solde"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+"},{"+Chr(34)+"date"+Chr(34)+":"+Chr(34)+"17\/02\/2021"+Chr(34)+","+Chr(34)+"libelle"+Chr(34)+":"+Chr(34)+"Règlement popick      5117725"+Chr(34)+","+Chr(34)+"debit"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+","+Chr(34)+"credit"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+","+Chr(34)+"solde"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+"},{"+Chr(34)+"date"+Chr(34)+":"+Chr(34)+"01\/03\/2021"+Chr(34)+","+Chr(34)+"libelle"+Chr(34)+":"+Chr(34)+"MARS      21"+Chr(34)+","+Chr(34)+"debit"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+","+Chr(34)+"credit"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+","+Chr(34)+"solde"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+"},{"+Chr(34)+"date"+Chr(34)+":"+Chr(34)+"16\/03\/2021"+Chr(34)+","+Chr(34)+"libelle"+Chr(34)+":"+Chr(34)+"Règlement popck      5117728"+Chr(34)+","+Chr(34)+"debit"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+","+Chr(34)+"credit"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+","+Chr(34)+"solde"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+"}]}]"
  Select Status
    Case #PB_Status_Loaded
      Debug "fichier sql trouvé et chargement de " + Filename$
      *DatabaseBuffer = ExportFileMemory(File)    
      If OpenDatabase(0, *DatabaseBuffer)           
        If DatabaseQuery(0, "TRUNCATE TABLE `compta`")    
          FinishDatabaseQuery(0)
  Success.i=1  
  If Success
      mpo.s ="" : mkey.s= ""  : valu.s=""
      If ParseJSON(0, Result$ )
        For i = 0 To JSONArraySize(JSONValue(0)) - 1    
          Val = GetJSONElement(JSONValue(0), i)        
          !var v_ll= Object.keys(v_val.json[v_i]);  
          !v_k2= v_ll.length;
          key.s=""    :   valu.s=""
          For k = 0 To k2-1
          !v_key= v_ll[v_k];
          !pp=v_val.json[v_i];
          !v_valu= JSON.stringify( pp[v_key] );
          If (key="comptasql")
            If ParseJSON(1, valu )
              pop.s=""
        For i2 = 0 To JSONArraySize(JSONValue(1)) - 1    
          Valr = GetJSONElement(JSONValue(1), i2)
            !var v_llr= Object.keys(v_valr.json[v_i2]);  
            !v_k2r= v_llr.length;
            keyr.s=""   :   valur.s=""
            lng.s=""
            For kr = 0 To k2r-1
            !v_keyr= v_llr[v_kr];
            !ppr=v_valr.json[v_i2];
            !v_valur= ppr[v_keyr];
            If keyr="clo"
              k_clo.s = valur
            EndIf
            If keyr="date"
              k_date.s = valur
            EndIf            
            If keyr="jnal"
              k_jnal.s = valur
            EndIf
            If keyr="libelle" : k_lib.s=valur  : EndIf
            If keyr="debit"
              k_deb.s =valur
            EndIf
            If keyr="credit"
              k_cre.s=valur
            EndIf
            If keyr="solde"
              k_sol.s= valur
            EndIf            
          Next
        DatabaseUpdate(0, "INSERT INTO `compta` (`id`, `Clo`, `date`, `jnal`, `libelle`, `debit`, `credit`, `solde`) VALUES (NULL, '"+k_clo+"', '"+k_date+"', '"+k_jnal+"', '"+k_lib+"', '"+k_deb+"', '"+k_cre+"', '"+k_sol+"');")
          
            Next
          EndIf
        EndIf
        Next
      Next i
    Else
      JSONErrorMessage()
    EndIf    
  Else
    Debug "HTTPRequest(): Error"
  EndIf             
          If DatabaseQuery(0, "SELECT * FROM compta order by id asc limit 0,100")
            Debug "aff les od compta"
ClearGadgetItems(lst_hisloc)            
          While NextDatabaseRow(0)
;            Debug "lng("+GetDatabaseString(0, 0)+"): " + GetDatabaseString(0, 1)+" : " + GetDatabaseString(0, 2)+" : "+GetDatabaseString(0, 3)
            If GetDatabaseString(0, 2) ;"date"
              If valur=""
                lng=""                
                lastDate.s= ""
              Else           
                concatdate.i= ParseDate("%dd/%mm/%yyyy", GetDatabaseString(0, 2))
                lng = FormatDate("%dd/%mm/%yy", concatdate)     
                Debug "date: "+GetDatabaseString(0, 2)
                lastDate.s= GetDatabaseString(0, 2)
              EndIf
            EndIf
            lng= lng+Chr(10)+GetDatabaseString(0, 4) ;libelle
            If GetDatabaseString(0, 5)="0.00" ;"debit"
              lng= lng+Chr(10)                
            Else
              lng= lng+Chr(10)+valur
            EndIf
            If GetDatabaseString(0, 6)="0.00" ;"credit"
              lng= lng+Chr(10)                
            Else
              lng= lng+Chr(10)+valur
            EndIf
            lastSolde.s=GetDatabaseString(0, 7)
            AddGadgetItem(lst_hisloc, -1, lng) 
          Wend
          FinishDatabaseQuery(0)    
        Else
          Debug "impossible de vider la table compta: " + DatabaseError()
        EndIf       
        *DatabaseBuffer = ExportDatabaseMemory(DB)
        If CreateFile(0, "testdbv3.sqlite", @CreateFileCallback(), #PB_LocalStorage)
          WriteData(0, *DatabaseBuffer, 0, MemorySize(*DatabaseBuffer))
          CloseFile(0)
        EndIf
        CloseDatabase(0)
      Else : Debug "vider la table compta impossible "
      EndIf        
      Else : Debug "impossible d'ouvrir le fichier sql "+Filename$
      EndIf
    Case #PB_Status_Error
      Debug "fichier sql introuvable: " + Filename$
      Debug "creation d'un fichier sql ... "+Filename$
      If OpenDatabase(0)
        DatabaseUpdate(0, "CREATE TABLE `compta` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT , `Clo` TEXT, `date` DATE, `jnal` TEXT, `libelle` TEXT, `debit` DECIMAL(10,2) NOT NULL DEFAULT '0.00' ,`credit` DECIMAL(10,2) NOT NULL DEFAULT '0.00' ,`solde` DECIMAL(10,2) NOT NULL DEFAULT '0.00')")
        Debug "0erreur? : "+DatabaseError()
        DatabaseUpdate(0, "INSERT INTO `compta` (`id`, `Clo`, `date`, `jnal`, `libelle`, `debit`, `credit`, `solde`) VALUES (NULL, '0', '2021-04-02', 'quit', 'loyer avril', '123.45', '0.00', '-123.45');")
        DatabaseUpdate(0, "INSERT INTO `compta` (`id`, `Clo`, `date`, `jnal`, `libelle`, `debit`, `credit`, `solde`) VALUES (NULL, '0', '2021-04-02', 'quit', 'rglt loyer', '0.00', '100.45', '-23.00'); ")
        *DatabaseBuffer = ExportDatabaseMemory(DB)
        If CreateFile(0, "testdbv3.sqlite", @CreateFileCallback(), #PB_LocalStorage)
          WriteData(0, *DatabaseBuffer, 0, MemorySize(*DatabaseBuffer))
          CloseFile(0)
        EndIf
        CloseDatabase(0)
EndIf      
  EndSelect
EndProcedure

Procedure HttpGetEventHistloc(Success, Result$, UserData) 
    ;fake
    Result$ = "[{"+Chr(34)+"comptasql"+Chr(34)+":[{"+Chr(34)+"date"+Chr(34)+":"+Chr(34)+"01\/01\/2021"+Chr(34)+","+Chr(34)+"libelle"+Chr(34)+":"+Chr(34)+"JANVIER   21"+Chr(34)+","+Chr(34)+"debit"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+","+Chr(34)+"credit"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+","+Chr(34)+"solde"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+"},{"+Chr(34)+"date"+Chr(34)+":"+Chr(34)+"15\/01\/2021"+Chr(34)+","+Chr(34)+"libelle"+Chr(34)+":"+Chr(34)+"Règlement poptrick      5117724"+Chr(34)+","+Chr(34)+"debit"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+","+Chr(34)+"credit"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+","+Chr(34)+"solde"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+"},{"+Chr(34)+"date"+Chr(34)+":"+Chr(34)+"01\/02\/2021"+Chr(34)+","+Chr(34)+"libelle"+Chr(34)+":"+Chr(34)+"FEVRIER   21"+Chr(34)+","+Chr(34)+"debit"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+","+Chr(34)+"credit"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+","+Chr(34)+"solde"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+"},{"+Chr(34)+"date"+Chr(34)+":"+Chr(34)+"17\/02\/2021"+Chr(34)+","+Chr(34)+"libelle"+Chr(34)+":"+Chr(34)+"Règlement popick      5117725"+Chr(34)+","+Chr(34)+"debit"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+","+Chr(34)+"credit"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+","+Chr(34)+"solde"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+"},{"+Chr(34)+"date"+Chr(34)+":"+Chr(34)+"01\/03\/2021"+Chr(34)+","+Chr(34)+"libelle"+Chr(34)+":"+Chr(34)+"MARS      21"+Chr(34)+","+Chr(34)+"debit"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+","+Chr(34)+"credit"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+","+Chr(34)+"solde"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+"},{"+Chr(34)+"date"+Chr(34)+":"+Chr(34)+"16\/03\/2021"+Chr(34)+","+Chr(34)+"libelle"+Chr(34)+":"+Chr(34)+"Règlement popck      5117728"+Chr(34)+","+Chr(34)+"debit"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+","+Chr(34)+"credit"+Chr(34)+":"+Chr(34)+"72.05"+Chr(34)+","+Chr(34)+"solde"+Chr(34)+":"+Chr(34)+"0.00"+Chr(34)+"}]}]"
        
;    If Success
    Debug Result$ 
    ReadFile(0, "testdbv3.sqlite", @ReadFileCallbackMAJcompta(), #PB_LocalStorage)       ;Result$)  <-- howto add this parametre into @ReadFileCallbackMAJcompta()
;    Else
;      Debug "HTTPRequest(): Error"
;    EndIf
EndProcedure


  HTTPRequest(#PB_HTTP_Get,"https://*********.php?opt=******", "", @HttpGetEventHistloc())   


Re: howto add parameter into callback ?

Posted: Sat Apr 03, 2021 2:13 pm
by Peter
you can use a global variable (here: HttpGetEventHistlocResult)

Code: Select all

Global HttpGetEventHistlocResult.s

Procedure ReadFileCallbackMAJcompta(Status, Filename$, File, SizeRead)
  
  Select Status
    Case #PB_Status_Loaded
      ; ...
      If ParseJSON(0, HttpGetEventHistlocResult )  
        ; ...
      EndIf
      
  EndSelect
  
EndProcedure

Procedure HttpGetEventHistloc(Success, Result$, UserData)
  
  If Success
    HttpGetEventHistlocResult = Result$
    ReadFile(0, "testdbv3.sqlite", @ReadFileCallbackMAJcompta(), #PB_LocalStorage)       
  Else
    Debug "HTTPRequest(): Error"
  EndIf
EndProcedure


HTTPRequest(#PB_HTTP_Get,"https://*********.php?opt=******", "", @HttpGetEventHistloc()) 

Re: howto add parameter into callback ?

Posted: Mon Apr 05, 2021 7:33 pm
by killer128k
Eng: This is not a good answer. Wrong path.

Fr: Ce n'est pas une bonne réponse. Mauvais chemin.

Re: howto add parameter into callback ?

Posted: Mon Apr 05, 2021 8:41 pm
by Peter
killer128k wrote:Eng: This is not a good answer. Wrong path.

Fr: Ce n'est pas une bonne réponse. Mauvais chemin.
i beg your pardon.

Re: howto add parameter into callback ?

Posted: Mon Apr 05, 2021 10:04 pm
by the.weavster
killer128k wrote:Eng: This is not a good answer. Wrong path.

Fr: Ce n'est pas une bonne réponse. Mauvais chemin.
I'm guessing you don't like Peter's answer because you want to be able to match responses to requests so here's a slight variation (I'm not in a position to test at the moment but if I've slipped up you should still get the idea):

Code: Select all

Global NewMap ResponseTable.s()

Procedure.i GetUniqueId()
  Static UniqueId
  UniqueId + 1
  ProcedureReturn UniqueId
EndProcedure

Procedure ReadFileCallbackMAJcompta(Status, Filename$, FileId, SizeRead) 
  Select Status
    Case #PB_Status_Loaded
      ; use file id to pick up the response text
      If ParseJSON(0, ResponseTable(Str(FileId))) 
        ; ...
      EndIf
      DeleteMapElement(ResponseTable(), Str(FileId))
  EndSelect 
EndProcedure

Procedure HttpGetEventHistloc(Success, Result$, UserData) 
  If Success
    ; get our unique id to link request and response
    FileID = GetUniqueId()
    ; unique id is used as map key and file id
    ResponseTable(Str(FileID)) = Result$
    ReadFile(FileID, "testdbv3.sqlite", @ReadFileCallbackMAJcompta(), #PB_LocalStorage)       
  Else
    Debug "HTTPRequest(): Error"
  EndIf
EndProcedure

HTTPRequest(#PB_HTTP_Get,"https://*********.php?opt=******", "", @HttpGetEventHistloc())

Re: howto add parameter into callback ?

Posted: Tue Apr 06, 2021 8:36 am
by killer128k
Eng: It's my bad
Thank you for your answers. Thank you the.weavster, thank you Peter
My error resided in the definition of the variable
do not specify HttpGetEventHistlocResult.s each time
at the risk of having a variable that behaves like an integer and not a string
HttpGetEventHistlocResult = "undefind"
But
HttpGetEventHistlocResult.s = "roch'n roll"
I will use Peter's solution but maybe later use this path

Fr: It's my bad
Merci pour vos réponses. Merci the.weavster, merci Peter
Mon erreur résidé dans la définition de la varriable
n'oubliez pas de préciser HttpGetEventHistlocResult.s à chaques fois
au risque d'avoir une varriable qui se comporte comme un entier et non un string
HttpGetEventHistlocResult = "undefind"
but
HttpGetEventHistlocResult.s = "roch'n roll"
Je vais utiliser la solution de Peter mais peut être que par la suite utiliser ce chemin

Code: Select all

Global NewMap ResponseTable.s()

Code: Select all

Global HttpGetEventHistlocResult.s
Procedure ReadFileCallbackMAJcompta(Status, Filename$, File, SizeRead)
  Select Status
    Case #PB_Status_Loaded
      ; ...
      If ParseJSON(0, HttpGetEventHistlocResult.s ) 
        ; ...
      EndIf
  EndSelect
EndProcedure
Procedure HttpGetEventHistloc(Success, Result$, UserData)
  If Success
    HttpGetEventHistlocResult.s = Result$
    ReadFile(0, "testdbv3.sqlite", @ReadFileCallbackMAJcompta(), #PB_LocalStorage)       
  Else
    Debug "HTTPRequest(): Error"
  EndIf
EndProcedure
HTTPRequest(#PB_HTTP_Get,"https://*********.php?opt=******", "", @HttpGetEventHistloc())