hardybarth-salia: add rfid identifier (#20321)

This commit is contained in:
andig 2025-04-02 08:31:08 +02:00 • committed by GitHub
parent 9a08324d8b
commit 2db934e0fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 90 additions and 6 deletions

View file

@ -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
}

View file

@ -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)
}
})
}
}

View file

@ -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()