CBAM Rechner
This commit is contained in:
@@ -0,0 +1,292 @@
|
||||
' ============================================================================
|
||||
' GreenPulse – CBAM Carbon Cost API
|
||||
' Single-file VB.NET implementation using Chilkat
|
||||
' --------------------------------------------------------------------------
|
||||
' Contains:
|
||||
' - cATEZ_Greenpulse_CBAM_CostCalculation (main API class)
|
||||
' - Request / Response DTO classes
|
||||
' - Error handling & JSON parsing
|
||||
'
|
||||
' Requirements:
|
||||
' - Chilkat.Http
|
||||
' - Chilkat.JsonObject
|
||||
' ============================================================================
|
||||
|
||||
Imports Chilkat
|
||||
Imports System.Globalization
|
||||
|
||||
' ============================================================================
|
||||
' MAIN API CLASS
|
||||
' ============================================================================
|
||||
Public Class cATEZ_Greenpulse_CBAM_CostCalculation
|
||||
|
||||
Private ReadOnly _baseUrl As String = "https://test-greenpulse-api.singlewindow.io/api/v1-0/public"
|
||||
' Private ReadOnly _baseUrl As String = "https://greenpulse-api.singlewindow.io/api/v1-0/public"
|
||||
Private ReadOnly _http As Http
|
||||
|
||||
Public Sub New()
|
||||
'_baseUrl = baseUrl.TrimEnd("/"c)
|
||||
_http = New Http()
|
||||
_http.SetRequestHeader("Content-Type", "application/json")
|
||||
End Sub
|
||||
|
||||
' ------------------------------------------------------------------------
|
||||
' GET /carbon-cost/cn-codes
|
||||
' Returns raw JSON (ideal for dropdowns / caching)
|
||||
' ------------------------------------------------------------------------
|
||||
Public Function GetCnCodesRaw() As String
|
||||
Dim url As String = _baseUrl & "/carbon-cost/cn-codes"
|
||||
|
||||
Dim resp As HttpResponse = _http.QuickGetObj(url)
|
||||
If resp Is Nothing Then
|
||||
Throw New Exception(_http.LastErrorText)
|
||||
End If
|
||||
|
||||
Return resp.BodyStr
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------
|
||||
' POST /carbon-cost/calculate
|
||||
' ------------------------------------------------------------------------
|
||||
Public Function CalculateCost(req As cCBAM_CostCalculation_Request) As cCBAM_CostCalculation_Response
|
||||
|
||||
VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat()
|
||||
|
||||
|
||||
Dim url As String = _baseUrl & "/carbon-cost/calculate"
|
||||
|
||||
Dim resp As HttpResponse = _http.PostJson2(
|
||||
url,
|
||||
"application/json",
|
||||
req.ToJson()
|
||||
)
|
||||
|
||||
If resp Is Nothing Then
|
||||
Throw New Exception(_http.LastErrorText)
|
||||
End If
|
||||
|
||||
Return ParseResponse(resp.BodyStr)
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------
|
||||
' JSON → Object mapping
|
||||
' ------------------------------------------------------------------------
|
||||
Private Function ParseResponse(jsonStr As String) _
|
||||
As cCBAM_CostCalculation_Response
|
||||
|
||||
Dim json As New JsonObject()
|
||||
json.Load(jsonStr)
|
||||
|
||||
Dim result As New cCBAM_CostCalculation_Response()
|
||||
result.success = json.BoolOf("success")
|
||||
|
||||
If result.success Then
|
||||
Dim d = json.ObjectOf("data")
|
||||
|
||||
result.data = New cCBAM_CostCalculation_Data With {
|
||||
.cost = GetDec(d, "cost"),
|
||||
.cbam_emission = GetDec(d, "cbam_emission"),
|
||||
.benchmark = GetDec(d, "benchmark"),
|
||||
.phase_factor = GetDec(d, "phase_factor"),
|
||||
.carbon_price = GetDec(d, "carbon_price"),
|
||||
.currency = d.StringOf("currency")
|
||||
}
|
||||
|
||||
Dim det = d.ObjectOf("calculation_details")
|
||||
result.data.calculation_details = New cCBAM_CostCalculation_Details With {
|
||||
.cbam_emission = GetDec(det, "cbam_emission"),
|
||||
.benchmark = GetDec(det, "benchmark"),
|
||||
.adjusted_benchmark = GetDec(det, "adjusted_benchmark"),
|
||||
.emission_difference = GetDec(det, "emission_difference"),
|
||||
.weight = GetDec(det, "weight"),
|
||||
.carbon_price = GetDec(det, "carbon_price")
|
||||
}
|
||||
Else
|
||||
Dim e = json.ObjectOf("error")
|
||||
result.error = New cCBAM_Error With {
|
||||
.code = e.StringOf("code"),
|
||||
.message = e.StringOf("message"),
|
||||
.statusCode = e.IntOf("statusCode")
|
||||
}
|
||||
End If
|
||||
|
||||
Return result
|
||||
End Function
|
||||
|
||||
' ------------------------------------------------------------------------
|
||||
' Robust numeric parsing across Chilkat versions:
|
||||
' Many Chilkat builds don't expose DoubleOf/NumberOf in the .NET wrapper.
|
||||
' Any JSON value (incl. numbers) can be read via StringOf and converted.
|
||||
' ------------------------------------------------------------------------
|
||||
Private Shared Function GetDec(obj As JsonObject, name As String) As Decimal
|
||||
Dim s As String = obj.StringOf(name)
|
||||
If String.IsNullOrWhiteSpace(s) Then Return 0D
|
||||
|
||||
' Ensure dot decimal separator
|
||||
s = s.Trim().Replace(",", ".")
|
||||
|
||||
Dim v As Decimal
|
||||
If Decimal.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, v) Then
|
||||
Return v
|
||||
End If
|
||||
|
||||
' Fallback: try current culture (last resort)
|
||||
If Decimal.TryParse(s, NumberStyles.Any, CultureInfo.CurrentCulture, v) Then
|
||||
Return v
|
||||
End If
|
||||
|
||||
Return 0D
|
||||
End Function
|
||||
|
||||
Shared Function calcCBAM(cn_code, weight, Optional see_total = "", Optional year = "", Optional country = "") As String
|
||||
|
||||
If cn_code = "" Or Not IsNumeric(cn_code) Then
|
||||
Return "Fehler: ungültiger CN-Code"
|
||||
End If
|
||||
|
||||
If weight = "" Or Not IsNumeric(weight) Then
|
||||
Return "Fehler: ungültiges Gewicht"
|
||||
End If
|
||||
|
||||
If see_total <> "" AndAlso Not IsNumeric(see_total) Then
|
||||
Return "Fehler: ungültige echten Emissionen"
|
||||
End If
|
||||
|
||||
If year <> "" AndAlso Not IsNumeric(year) Then
|
||||
If Not IsNumeric(year) Then Return "Fehler: ungültiges Phase-in Jahr"
|
||||
If year.length <> 4 Then Return "Fehler: ungültiges Phase-in Jahr"
|
||||
End If
|
||||
|
||||
If country <> "" AndAlso country.length > 2 Then
|
||||
' Derzeit nicht unterstützt
|
||||
Return "Fehler: Land wird derzeit nicht unterstützt"
|
||||
End If
|
||||
' ------------------------------------------------------------
|
||||
' 1) API initialisieren
|
||||
' ------------------------------------------------------------
|
||||
Dim api As New cATEZ_Greenpulse_CBAM_CostCalculation()
|
||||
|
||||
|
||||
' ------------------------------------------------------------
|
||||
' 2) Request aufbauen
|
||||
' ------------------------------------------------------------
|
||||
Dim req As New cCBAM_CostCalculation_Request With {
|
||||
.cn_code = cn_code,
|
||||
.weight = CDbl(weight), ' Tonnen
|
||||
.see_total = If(see_total = "", Nothing, see_total), ' optionale echte Emissionen
|
||||
.year = If(year = "", Nothing, CInt(year)) ' Phase-in Jahr
|
||||
}
|
||||
|
||||
|
||||
' ------------------------------------------------------------
|
||||
' 3) Request ausführen
|
||||
' ------------------------------------------------------------
|
||||
Dim resp As cCBAM_CostCalculation_Response = api.CalculateCost(req)
|
||||
|
||||
Dim erg = ""
|
||||
' ------------------------------------------------------------
|
||||
' 4) Ergebnis auswerten
|
||||
' ------------------------------------------------------------
|
||||
If resp.success Then
|
||||
|
||||
erg &= "CBAM Kostenberechnung erfolgreich" & vbNewLine
|
||||
erg &= "--------------------------------" & vbNewLine
|
||||
erg &= $"Kosten: {CDbl(resp.data.cost).ToString("N2")} {resp.data.currency}" & vbNewLine
|
||||
erg &= $"CBAM Emission: {resp.data.cbam_emission}" & vbNewLine
|
||||
erg &= $"Benchmark: {resp.data.benchmark}" & vbNewLine
|
||||
erg &= $"Phase-Faktor: {resp.data.phase_factor}" & vbNewLine
|
||||
erg &= $"CO2 Preis: {resp.data.carbon_price}" & vbNewLine
|
||||
|
||||
erg &= "" & vbNewLine
|
||||
erg &= "Details:" & vbNewLine
|
||||
erg &= $" Adjusted Benchmark: {resp.data.calculation_details.adjusted_benchmark}" & vbNewLine
|
||||
erg &= $" Emission Difference: {resp.data.calculation_details.emission_difference}" & vbNewLine
|
||||
erg &= $" Gewicht: {CDbl(resp.data.calculation_details.weight).ToString("N2")} t" & vbNewLine
|
||||
|
||||
Else
|
||||
|
||||
erg &= "CBAM Kostenberechnung FEHLER" & vbNewLine
|
||||
erg &= "--------------------------------" & vbNewLine
|
||||
erg &= $"Code: {resp.error.code}" & vbNewLine
|
||||
erg &= $"Message: {resp.error.message}" & vbNewLine
|
||||
erg &= $"HTTP Status: {resp.error.statusCode}" & vbNewLine
|
||||
|
||||
End If
|
||||
|
||||
' Console.ReadKey()
|
||||
Return erg
|
||||
End Function
|
||||
|
||||
End Class
|
||||
|
||||
' ============================================================================
|
||||
' REQUEST DTO
|
||||
' ============================================================================
|
||||
Public Class cCBAM_CostCalculation_Request
|
||||
|
||||
Public Property cn_code As String
|
||||
Public Property weight As Decimal
|
||||
Public Property see_total As Nullable(Of Decimal)
|
||||
Public Property year As Nullable(Of Integer)
|
||||
|
||||
Public Function ToJson() As String
|
||||
Dim json As New JsonObject()
|
||||
|
||||
json.UpdateString("cn_code", cn_code)
|
||||
json.UpdateNumber("weight", weight.ToString(CultureInfo.InvariantCulture))
|
||||
|
||||
If see_total.HasValue Then
|
||||
json.UpdateNumber("see_total", see_total.Value.ToString(CultureInfo.InvariantCulture))
|
||||
End If
|
||||
|
||||
If year.HasValue Then
|
||||
json.UpdateInt("year", year.Value)
|
||||
End If
|
||||
|
||||
Return json.Emit()
|
||||
End Function
|
||||
|
||||
End Class
|
||||
|
||||
' ============================================================================
|
||||
' RESPONSE ROOT
|
||||
' ============================================================================
|
||||
Public Class cCBAM_CostCalculation_Response
|
||||
Public Property success As Boolean
|
||||
Public Property data As cCBAM_CostCalculation_Data
|
||||
Public Property [error] As cCBAM_Error
|
||||
End Class
|
||||
|
||||
' ============================================================================
|
||||
' RESPONSE DATA
|
||||
' ============================================================================
|
||||
Public Class cCBAM_CostCalculation_Data
|
||||
Public Property cost As Decimal
|
||||
Public Property cbam_emission As Decimal
|
||||
Public Property benchmark As Decimal
|
||||
Public Property phase_factor As Decimal
|
||||
Public Property carbon_price As Decimal
|
||||
Public Property currency As String
|
||||
Public Property calculation_details As cCBAM_CostCalculation_Details
|
||||
End Class
|
||||
|
||||
' ============================================================================
|
||||
' RESPONSE DETAILS
|
||||
' ============================================================================
|
||||
Public Class cCBAM_CostCalculation_Details
|
||||
Public Property cbam_emission As Decimal
|
||||
Public Property benchmark As Decimal
|
||||
Public Property adjusted_benchmark As Decimal
|
||||
Public Property emission_difference As Decimal
|
||||
Public Property weight As Decimal
|
||||
Public Property carbon_price As Decimal
|
||||
End Class
|
||||
|
||||
' ============================================================================
|
||||
' ERROR OBJECT
|
||||
' ============================================================================
|
||||
Public Class cCBAM_Error
|
||||
Public Property code As String
|
||||
Public Property message As String
|
||||
Public Property statusCode As Integer
|
||||
End Class
|
||||
Reference in New Issue
Block a user