From 2db934e0facfdebdf7721563b401636b3a0b5403 Mon Sep 17 00:00:00 2001 From: andig Date: Wed, 2 Apr 2025 08:31:08 +0200 Subject: [PATCH] hardybarth-salia: add rfid identifier (#20321) --- charger/echarge/salia/types.go | 41 ++++++++++++++++++++++++++++ charger/echarge/salia/types_test.go | 42 +++++++++++++++++++++++++++++ charger/hardybarth-salia.go | 13 ++++----- 3 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 charger/echarge/salia/types_test.go diff --git a/charger/echarge/salia/types.go b/charger/echarge/salia/types.go index 10b315906..729029ca2 100644 --- a/charger/echarge/salia/types.go +++ b/charger/echarge/salia/types.go @@ -1,5 +1,10 @@ package salia +import ( + "encoding/json" + "strconv" +) + const ( HeartBeat = "salia/heartbeat" ChargeMode = "salia/chargemode" @@ -87,4 +92,40 @@ type Port struct { EvPresent int `json:"ev_present,string"` Charging int `json:",string"` GridCurrentLimit float64 `json:"grid_current_limit,string"` + Session struct { + AuthorizationStatus string `json:"authorization_status"` + AuthorizationMethod string `json:"authorization_method"` + } `json:"session"` + RFID struct { + Type string `json:"type"` + Available string `json:"available"` + Authorizereq string `json:"authorizereq"` + AuthorizationRequest AuthorizationRequest `json:"authorization_request"` + } `json:"rfid"` +} + +type AuthorizationRequest struct { + Protocol string + Key string +} + +func (a *AuthorizationRequest) UnmarshalJSON(data []byte) error { + s, err := strconv.Unquote(string(data)) + if err != nil { + return nil + } + + var arr []string + if err := json.Unmarshal([]byte(s), &arr); err != nil { + return nil + } + + if len(arr) == 2 { + *a = AuthorizationRequest{ + Protocol: arr[0], + Key: arr[1], + } + } + + return nil } diff --git a/charger/echarge/salia/types_test.go b/charger/echarge/salia/types_test.go new file mode 100644 index 000000000..0c0ff0f09 --- /dev/null +++ b/charger/echarge/salia/types_test.go @@ -0,0 +1,42 @@ +package salia + +import ( + "encoding/json" + "testing" +) + +func TestAuthorizationRequestUnmarshalJSON(t *testing.T) { + tests := []struct { + name string + jsonInput string + expected AuthorizationRequest + }{ + { + name: "Leerer JSON-String", + // Dieser Input repräsentiert ein leeres Stringliteral. + jsonInput: "\"\"", + expected: AuthorizationRequest{}, + }, + { + name: "Korrekte Authentifizierungsanfrage (als JSON-String)", + // Realer Input: ein JSON-String, der ein Array repräsentiert. + // Das Ergebnis soll durch Unquote den Inhalt ["ISO14443","9af18400"] liefern. + jsonInput: "\"[\\\"ISO14443\\\",\\\"9af18400\\\"]\"", + expected: AuthorizationRequest{Protocol: "ISO14443", Key: "9af18400"}, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + var ar AuthorizationRequest + err := json.Unmarshal([]byte(tc.jsonInput), &ar) + if err != nil { + t.Errorf("Unerwarteter Fehler: %v", err) + return + } + if ar.Protocol != tc.expected.Protocol || ar.Key != tc.expected.Key { + t.Errorf("Falsches Ergebnis: got %+v, expected %+v", ar, tc.expected) + } + }) + } +} diff --git a/charger/hardybarth-salia.go b/charger/hardybarth-salia.go index 353a04c65..4c9d50632 100644 --- a/charger/hardybarth-salia.go +++ b/charger/hardybarth-salia.go @@ -266,12 +266,13 @@ func (wb *Salia) currents() (float64, float64, float64, error) { return i.L1.Actual / 1e3, i.L2.Actual / 1e3, i.L3.Actual / 1e3, err } -// var _ api.Identifier = (*Salia)(nil) - -// // Identify implements the api.Identifier interface -// func (wb *Salia) Identify() (string, error) { -// return "", api.ErrNotAvailable -// } +func (wb *Salia) Identify() (string, error) { + res, err := wb.apiG.Get() + if err != nil { + return "", err + } + return res.Secc.Port0.RFID.AuthorizationRequest.Key, nil +} func (wb *Salia) getPhases() (int, error) { res, err := wb.apiG.Get()