'API request to the server to DELETE records
Public Sub API_DELETE(table As String, filter As Map)
If others.Internet_Connected = False Then
CallSubDelayed2(Me, "API_DELETE_DELETED", False)
Return
End If
If table = "" Then
CallSubDelayed2(Me, "API_DELETE_DELETED", False)
Return
End If
If Main.BusySending Then Return
Main.BusySending = True
Log("API_DELETE: " & table)
Dim m As Map
m.Initialize
m.Put("action", "delete")
m.Put("scanner", others.Get_DeviceID)
m.Put("key", API_KEY.ToLowerCase)
Dim timestamp As Long = DateTime.Now / 1000
m.Put("timestamp", timestamp)
m.Put("dbtable", table)
If filter <> Null Then
If filter.IsInitialized Then
If filter.Size > 0 Then
m.Put("filter", filter)
End If
End If
End If
Dim jg As JSONGenerator
jg.Initialize(m)
Dim job As HttpJob
job.Initialize("API_DELETE_" & table, Me)
Dim text As String = jg.ToString
#if debug
Log(text)
#End If
job.PostString(URL, text)
job.GetRequest.SetHeader("Content-Type", "application/json;charset=UTF-8")
job.GetRequest.Timeout = 60000
Wait For (job) JobDone(job As HttpJob)
Dim jp As JSONParser
If job.Success = False Then 'error
Dim a As String = job.JobName & ": requesting error: " & job.ErrorMessage
others.Add_ToLog(a, 3)
Log(a)
Main.BusySending = False 'allow next send
job.Release
others.Save_Blackbox_Logs
CallSubDelayed2(Me, "API_DELETE_DELETED", False)
Return
End If
#if debug
Dim resString As String = job.GetString
Log(resString)
#End If
'parsing
Try
jp.Initialize(job.GetString)
Dim res As Map = jp.NextObject
If res.IsInitialized = False Or (res.IsInitialized And res.Size = 0) Then
API_Error(job)
CallSubDelayed2(Me, "API_DELETE_DELETED", False)
Return
End If
Catch
API_Error(job)
CallSubDelayed2(Me, "API_DELETE_DELETED", False)
Return
End Try
Dim ret As String = res.Get("ret")
Dim msg As String = res.Get("msg")
If ret <> "ok" Then
Dim msg As String = job.JobName & ": " & res.Get("msg")
others.Add_ToLog(msg, 2)
Log(a)
Main.BusySending = False 'allow next send
job.Release
others.Save_Blackbox_Logs
CallSubDelayed2(Me, "API_DELETE_DELETED", False)
Return
End If
Main.BusySending = False 'allow next send
job.Release 'OK parsed finally
CallSubDelayed2(Me, "API_DELETE_DELETED", True)
End Sub
Sub API_Error(j As HttpJob)
Try
Dim resString As String = j.GetString
Dim a As String = j.JobName & ": server error reply: " & resString
others.Add_ToLog(a, 3)
Log(a)
j.Release
Catch
Log("API_Error.error: " & LastException.Message)
End Try
Main.BusySending = False 'allow next send
End Sub
'usage
Sub Several_HTTP_requests
If others.Internet_Connected = False Then Return
Dim f As Map
f.Initialize
f.Put("scanner_name", others.Get_DeviceID)
API_DELETE("loops", f)
Wait for API_DELETE_DELETED (Success As Boolean)
If Success = False Then
Log("Send_loop deleted: " & Success)
Return
End If
'if deleted OK - next requests to CREATE records
....
End sub