Imports System.Net Public Class cTimasAPI Dim API_STRING = "https://zeit.verag.ag" Dim rest As New Chilkat.Rest Sub New() VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat() End Sub Public Function checkConnectionTImas(ByRef failureText As String) As Boolean Try Dim success As Boolean rest.VerboseLogging = False Dim bTls As Boolean = True Dim port As Integer = 443 Dim bAutoReconnect As Boolean = True success = rest.Connect(API_STRING, port, bTls, bAutoReconnect) If (success <> True) Then Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason) Debug.WriteLine(rest.LastErrorText) failureText = rest.LastErrorText Return False End If success = rest.SetAuthBasic("admin", "BmWr501956") If (success <> True) Then Debug.WriteLine("BAFailReason: " & rest.ConnectFailReason) Debug.WriteLine(rest.LastErrorText) failureText = rest.LastErrorText Return False End If Return True Catch ex As WebException VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Function Public Sub getTimeSaldo(ByVal maid As Integer, ByRef info As String, Optional ByRef requestDone As Boolean = False) Try Dim returnText As String = "" Dim failureText As String = "" If maid < 1 Then Exit Sub End If If Not checkConnectionTImas(failureText) Then Exit Sub End If Dim success As Boolean Dim responseJson As String = rest.FullRequestNoBody("GET", "/rest/web-api/employees/" & maid & "/balance") If (rest.LastMethodSuccess <> True) Then Debug.WriteLine(rest.LastErrorText) Exit Sub End If If (rest.ResponseStatusCode <> 200) Then Debug.WriteLine(rest.ResponseHeader) 'lblTimas.Text = rest.ResponseStatusCode & " " & rest.ResponseStatusText Exit Sub End If Debug.WriteLine(responseJson) Dim json As New Chilkat.JsonObject success = json.Load(responseJson) If (success <> True) Then Debug.WriteLine(json.LastErrorText) End If Debug.WriteLine(json) '"overallBalance" 101580, '"dailyBalance": -17340, '"monthlyBalance": -328380, '"actualTimeOfDay": 4260, Dim saldo = json.StringOf("dailyBalanceYesterday") 'Tagessaldo (gestern) in Sekunden If saldo IsNot Nothing Then Dim saldoAsDouble = CDbl(saldo) saldoAsDouble = Math.Round(saldoAsDouble / 3600, 2) info = "Saldo Vortag: " & saldoAsDouble & "h" End If Catch ex As WebException VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Sub Public Sub getTimeEntries(ByVal maid As Integer, ByRef info As String, Optional ByRef entryExistingToday As Boolean = True, Optional ByRef requestDone As Boolean = False) Try Dim returnText As String = "" Dim failureText As String = "" If maid < 1 Then Exit Sub End If If Not checkConnectionTImas(failureText) Then Exit Sub End If Dim success As Boolean rest.AddQueryParam("id", maid) rest.AddQueryParam("from", Today().AddDays(-1).ToString("yyyy-MM-ddTHH:mm:ss")) rest.AddQueryParam("to", Now().ToString("yyyy-MM-ddTHH:mm:ss")) Dim Response As String Response = rest.FullRequestNoBody("GET", "/rest/web-api/employees/" & maid & "/bookings") If (rest.LastMethodSuccess <> True) Then Debug.WriteLine(rest.LastErrorText) Exit Sub End If If (rest.ResponseStatusCode <> 200) Then Debug.WriteLine(rest.ResponseHeader) 'rest.ResponseStatusCode & " " & rest.ResponseStatusText Exit Sub End If Debug.WriteLine(Response) Dim jsonArray As New Chilkat.JsonArray success = jsonArray.Load(Response) If (success <> True) Then Debug.WriteLine(jsonArray.LastErrorText) Exit Sub End If If jsonArray.Size = -1 Then Exit Sub End If Dim dt As New DataTable dt.Columns.Add("statusid", GetType(Integer)) dt.Columns.Add("stamp", GetType(DateTime)) dt.Columns.Add("type", GetType(String)) entryExistingToday = False Dim j As Integer = 0 While j < jsonArray.Size Dim timeEntry As Chilkat.JsonObject = jsonArray.ObjectAt(j) If IsDate(timeEntry.StringOf("stamp")) Then If CDate(timeEntry.StringOf("stamp")) >= CDate(Today()) Then Dim R As DataRow = dt.NewRow R("statusid") = timeEntry.StringOf("statusid") R("stamp") = timeEntry.StringOf("stamp") R("type") = timeEntry.StringOf("type") dt.Rows.Add(R) entryExistingToday = True info &= " " & IIf(timeEntry.StringOf("type") = "in", "+", " - ") & CDate(timeEntry.StringOf("stamp")).ToString("HH:mm:ss") If jsonArray.Size - 1 = j Then info &= " Status: " & IIf(timeEntry.StringOf("type") = "in", "Anwesend", "Abwesend") End If End If End If j = j + 1 End While requestDone = True rest.ClearAllQueryParams() Catch ex As WebException VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Sub Public Function createMA(ByVal mitarbeiter As cMitarbeiter, ByRef info As String, ByRef datatableTimas As DataTable, Optional ByRef requestDone As Boolean = False) Try rest.ClearAllHeaders() rest.ClearAllParts() Dim timasEmployeeCreated As Boolean = False Dim returnText As String = "" Dim failureText As String = "" If Not checkConnectionTImas(failureText) Then Return timasEmployeeCreated End If rest.AddHeader("Content-Type", "application/json") Dim success As Boolean Dim json As New Chilkat.JsonObject success = json.UpdateString("externid", mitarbeiter.mit_id) success = json.UpdateString("markingColor", "#3acc2d") success = json.UpdateString("pnr1", mitarbeiter.mit_PersonalNr) 'success = json.UpdateString("pnr2", "ZZ-3A-Q") success = json.UpdateString("firstname", mitarbeiter.mit_vname) success = json.UpdateString("lastname", mitarbeiter.mit_nname) success = json.UpdateString("gender", IIf(mitarbeiter.mit_geschlecht = "m", "male", "female")) 'success = json.UpdateInt("card", 42) success = json.UpdateString("info", mitarbeiter.mit_firma & " - " & mitarbeiter.mit_abteilung) 'success = json.UpdateString("clientNumber", "5600-02") 'success = json.UpdateInt("rfid", 178230359) If mitarbeiter.mit_gebdat <> "" And IsDate(mitarbeiter.mit_gebdat) Then success = json.UpdateString("birthday", CDate(mitarbeiter.mit_gebdat).ToString("yyyy-MM-dd")) End If success = json.UpdateString("entry", CDate(mitarbeiter.mit_einstiegsdatum).ToString("yyyy-MM-dd")) success = json.UpdateNull("exit") success = json.UpdateString("importSign", "Iimport from Aviso") success = json.UpdateString("login", mitarbeiter.mit_AliasAD_Username) success = json.UpdateBool("loginActive", 1) success = json.UpdateString("email", mitarbeiter.mit_email) success = json.UpdateString("street", mitarbeiter.mit_strasse) success = json.UpdateString("city", mitarbeiter.mit_ort) success = json.UpdateString("zipcode", mitarbeiter.mit_plz) success = json.UpdateString("phone1", mitarbeiter.mit_telefonnr & " " & mitarbeiter.mit_durchwahl) success = json.UpdateString("phone2", mitarbeiter.mit_mobiltel) success = json.UpdateString("password", mitarbeiter.mit_pwd) success = json.UpdateBool("resetPassword", 0) Dim i As Integer = 0 For Each r As DataRow In datatableTimas.Rows If r("set") = True Then success = json.UpdateInt("groups[" & i & "]", r("id")) i = i + 1 End If Next Debug.WriteLine(json.Emit()) Dim sbRequestBody As New Chilkat.StringBuilder json.EmitSb(sbRequestBody) Dim sbResponseBody As New Chilkat.StringBuilder success = rest.FullRequestSb("POST", "/rest/web-api/employees", sbRequestBody, sbResponseBody) If (success <> True) Then Debug.WriteLine(rest.LastErrorText) Return timasEmployeeCreated End If If (rest.LastMethodSuccess <> True) Then Debug.WriteLine(rest.LastErrorText) info = rest.LastErrorText Return timasEmployeeCreated End If If (rest.ResponseStatusCode <> 201) Then Debug.WriteLine(rest.ResponseHeader) info = rest.ResponseStatusCode & " " & rest.ResponseStatusText If sbResponseBody.GetAsString <> "" Then info &= vbNewLine & sbResponseBody.GetAsString End If Return timasEmployeeCreated Else info = "Mitarbeiter angelegt!" & vbNewLine Dim jsonResult As New Chilkat.JsonObject success = jsonResult.LoadSb(sbResponseBody) jsonResult.Emit() info &= "Timas-ID: " & jsonResult.StringOf("id") & vbNewLine info &= "MA-ID: " & jsonResult.StringOf("externid") & vbNewLine info &= "Name: " & jsonResult.StringOf("firstname") & " " & jsonResult.StringOf("lastname") mitarbeiter.mit_timasId = jsonResult.StringOf("id") mitarbeiter.SAVE() timasEmployeeCreated = True Return timasEmployeeCreated End If Return timasEmployeeCreated Catch ex As WebException VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Function Public Sub getInfo(ByVal mitarbeiter As cMitarbeiter, ByRef info As String) Dim failureText As String = "" 'If Not cbxTimasAngelegt.Checked Then ' info = "Mitarbeiter nicht im Timas angelegt!" ' Exit Sub 'End If Try If Not checkConnectionTImas(failureText) Then Exit Sub End If Dim responseJson As String = rest.FullRequestNoBody("GET", "/rest/web-api/employees/" & mitarbeiter.mit_timasId) If (rest.LastMethodSuccess <> True) Then Debug.WriteLine(rest.LastErrorText) Exit Sub End If If (rest.ResponseStatusCode <> 200) Then Debug.WriteLine(rest.ResponseHeader) info = rest.ResponseStatusCode & " " & rest.ResponseStatusText Exit Sub End If Debug.WriteLine(responseJson) Dim json As New Chilkat.JsonObject Dim success = json.Load(responseJson) If (success <> True) Then Debug.WriteLine(json.LastErrorText) End If Debug.WriteLine(json) info &= IIf(json.StringOf("entry") <> "", "Startdatum: " & json.StringOf("entry") & vbNewLine, "") info &= IIf(json.StringOf("externid") <> "", "MA-ID: " & json.StringOf("externid") & vbNewLine, "") info &= IIf(json.StringOf("pnr1") <> "", "PersonalNr: " & json.StringOf("pnr1") & vbNewLine, "") info &= IIf(json.StringOf("firstname") <> "", "Vorame: " & json.StringOf("firstname") & vbNewLine, "") info &= IIf(json.StringOf("lastname") <> "", "Nachname: " & json.StringOf("lastname") & vbNewLine, "") info &= IIf(json.StringOf("info") <> "", "Info: " & json.StringOf("info") & vbNewLine, "") info &= IIf(json.StringOf("id") <> "", "Timas-ID: " & json.StringOf("id") & vbNewLine, "") If json.BoolOf("loginActive") Then info &= IIf(json.StringOf("login") <> "", "Login: " & json.StringOf("login") & vbNewLine, "") info &= IIf(json.StringOf("password") <> "", "PW: " & json.StringOf("password"), "") End If Catch ex As WebException VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Sub Public Sub getGroups(ByRef dt As DataTable) Try Dim failureText As String = "" If Not checkConnectionTImas(failureText) Then Exit Sub End If Dim responseBody As String = rest.FullRequestNoBody("GET", API_STRING & "/rest/web-api/groups3") If (rest.LastMethodSuccess <> True) Then Debug.WriteLine(rest.LastErrorText) Exit Sub End If If (rest.ResponseStatusCode <> 200) Then Debug.WriteLine(rest.ResponseStatusCode & " " & rest.ResponseStatusText) End If Dim jsonArray As New Chilkat.JsonArray Dim success As Boolean = jsonArray.Load(responseBody) If (success <> True) Then Debug.WriteLine(jsonArray.LastErrorText) Exit Sub End If Dim num As Integer = jsonArray.Size If num = -1 Then Exit Sub End If Dim j As Integer = 0 If dt IsNot Nothing And Not dt.Columns.Contains("id") Then dt.Columns.Add("id", GetType(Integer)) dt.Columns.Add("name", GetType(String)) dt.Columns.Add("groupType", GetType(String)) dt.Columns.Add("info", GetType(String)) dt.Columns.Add("set", GetType(Boolean)) While j < num Dim groups As Chilkat.JsonObject = jsonArray.ObjectAt(j) Dim R As DataRow = dt.NewRow R("id") = groups.IntOf("id") R("name") = groups.StringOf("name") R("groupType") = groups.StringOf("groupType") R("info") = groups.StringOf("info") If R("id") = "19" Or R("id") = "129" Then 'Typ Mitarbeiter und Standardgruppe für Mitarbeiter Default true R("set") = True Else R("set") = False End If dt.Rows.Add(R) j = j + 1 End While dt.DefaultView.Sort = " groupType asc, name asc" dt = dt.DefaultView.ToTable() End If Catch ex As WebException VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Sub Public Function setTimeEntry(ByVal mitarbeiter As cMitarbeiter, ByVal timeEntry As DateTime, ByRef info As String, ByRef inout As String) Dim failureText As String = "" Dim timeEntryCreated As Boolean = False If Not checkConnectionTImas(failureText) Then Return timeEntryCreated End If If Not mitarbeiter.mit_timasId > 0 Then info = "Mitarbeiter besitzt keine TImas-Zuordnung" Return timeEntryCreated End If Dim entryType As String Select Case inout Case "in" entryType = "KOMMT" Case "out" entryType = "GEHT" Case Else entryType = "falscher ErfassgsTyp: " & inout info = entryType Return timeEntryCreated End Select Dim json As New Chilkat.JsonObject json.UpdateString("stamp", timeEntry.ToString("yyyy-MM-ddTHH:mm:ss")) json.UpdateInt("statusnumber", 0) ' json.UpdateString("type", inout) json.UpdateString("employeeid", mitarbeiter.mit_timasId) Dim sbRequestBody As New Chilkat.StringBuilder json.EmitSb(sbRequestBody) Dim sbResponseBody As New Chilkat.StringBuilder Dim success = rest.FullRequestSb("POST", "/rest/web-api/bookings", sbRequestBody, sbResponseBody) If (success <> True) Then Debug.WriteLine(rest.LastErrorText) Return timeEntryCreated End If If (rest.LastMethodSuccess <> True) Then Debug.WriteLine(rest.LastErrorText) info = rest.LastErrorText Return timeEntryCreated End If If (rest.ResponseStatusCode <> 200) Then Debug.WriteLine(rest.ResponseHeader) info = rest.ResponseStatusCode & " " & rest.ResponseStatusText If sbResponseBody.GetAsString <> "" Then info &= vbNewLine & sbResponseBody.GetAsString End If Return timeEntryCreated Else info = "Zeiteintrag " & entryType & " angelegt!" & timeEntry.ToString("HH:mm:ss") 'Dim jsonResult As New Chilkat.JsonObject 'success = jsonResult.LoadSb(sbResponseBody) 'jsonResult.Emit() timeEntryCreated = True End If Return timeEntryCreated End Function Public Function getEmployeeList(ByRef dt As DataTable) As String Dim failureText As String = "" Try If Not checkConnectionTImas(failureText) Then Return failureText End If Dim responseJson As String = rest.FullRequestNoBody("GET", "/rest/web-api/employees/") If (rest.LastMethodSuccess <> True) Then Debug.WriteLine(rest.LastErrorText) Return rest.LastErrorText End If If (rest.ResponseStatusCode <> 200) Then Debug.WriteLine(rest.ResponseHeader) failureText = rest.ResponseStatusCode & " " & rest.ResponseStatusText Return rest.ResponseStatusCode & " " & rest.ResponseStatusText End If Debug.WriteLine(responseJson) Dim jsonArray As New Chilkat.JsonArray Dim success As Boolean = jsonArray.Load(responseJson) If (success <> True) Then Debug.WriteLine(jsonArray.LastErrorText) Return jsonArray.LastErrorText End If Dim num As Integer = jsonArray.Size If num = -1 Then Return "Array Failure" End If If dt IsNot Nothing And Not dt.Columns.Contains("id") Then dt.Columns.Add("id", GetType(Integer)) dt.Columns.Add("externid", GetType(String)) dt.Columns.Add("pnr1", GetType(String)) dt.Columns.Add("firstname", GetType(String)) dt.Columns.Add("lastname", GetType(String)) dt.Columns.Add("gender", GetType(String)) dt.Columns.Add("card", GetType(Integer)) dt.Columns.Add("info", GetType(String)) Dim j As Integer = 0 While j < num Dim employee As Chilkat.JsonObject = jsonArray.ObjectAt(j) Dim R As DataRow = dt.NewRow R("id") = employee.IntOf("id") R("externid") = employee.StringOf("externid") R("pnr1") = employee.StringOf("pnr1") R("firstname") = employee.StringOf("firstname") R("lastname") = employee.StringOf("lastname") R("gender") = employee.StringOf("gender") R("card") = employee.IntOf("card") R("info") = employee.StringOf("info") dt.Rows.Add(R) j = j + 1 End While End If Catch ex As WebException VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Function End Class