LGEss: support user login (#10009)
This commit is contained in:
parent
85517da4ff
commit
cd044259de
3 changed files with 38 additions and 13 deletions
|
|
@ -54,9 +54,10 @@ func init() {
|
|||
// NewLgEssFromConfig creates an LgEss Meter from generic config
|
||||
func NewLgEssFromConfig(other map[string]interface{}) (api.Meter, error) {
|
||||
cc := struct {
|
||||
capacity `mapstructure:",squash"`
|
||||
URI, Usage, Password string
|
||||
Cache time.Duration
|
||||
capacity `mapstructure:",squash"`
|
||||
URI, Usage string
|
||||
Registration, Password string
|
||||
Cache time.Duration
|
||||
}{
|
||||
Cache: time.Second,
|
||||
}
|
||||
|
|
@ -69,12 +70,12 @@ func NewLgEssFromConfig(other map[string]interface{}) (api.Meter, error) {
|
|||
return nil, errors.New("missing usage")
|
||||
}
|
||||
|
||||
return NewLgEss(cc.URI, cc.Usage, cc.Password, cc.Cache, cc.capacity.Decorator())
|
||||
return NewLgEss(cc.URI, cc.Usage, cc.Registration, cc.Password, cc.Cache, cc.capacity.Decorator())
|
||||
}
|
||||
|
||||
// NewLgEss creates an LgEss Meter
|
||||
func NewLgEss(uri, usage, password string, cache time.Duration, capacity func() float64) (api.Meter, error) {
|
||||
lp, err := lgpcs.GetInstance(uri, password, cache)
|
||||
func NewLgEss(uri, usage, registration, password string, cache time.Duration, capacity func() float64) (api.Meter, error) {
|
||||
lp, err := lgpcs.GetInstance(uri, registration, password, cache)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,8 +18,9 @@ import (
|
|||
|
||||
// URIs
|
||||
const (
|
||||
LoginURI = "/v1/login"
|
||||
MeterURI = "/v1/user/essinfo/home"
|
||||
LoginURI = "/v1/login"
|
||||
UserLoginURI = "/v1/user/setting/login"
|
||||
MeterURI = "/v1/user/essinfo/home"
|
||||
)
|
||||
|
||||
type MeterResponse struct {
|
||||
|
|
@ -42,6 +43,7 @@ type EssData struct {
|
|||
type Com struct {
|
||||
*request.Helper
|
||||
uri string // URI address of the LG ESS inverter - e.g. "https://192.168.1.28"
|
||||
authPath string
|
||||
password string // registration number of the LG ESS Inverter - e.g. "DE2001..."
|
||||
authKey string // auth_key returned during login and renewed with new login after expiration
|
||||
dataG func() (EssData, error)
|
||||
|
|
@ -53,7 +55,7 @@ var (
|
|||
)
|
||||
|
||||
// GetInstance implements the singleton pattern to handle the access via the authkey to the PCS of the LG ESS HOME system
|
||||
func GetInstance(uri, password string, cache time.Duration) (*Com, error) {
|
||||
func GetInstance(uri, registration, password string, cache time.Duration) (*Com, error) {
|
||||
uri = util.DefaultScheme(strings.TrimSuffix(uri, "/"), "https")
|
||||
|
||||
var err error
|
||||
|
|
@ -62,9 +64,15 @@ func GetInstance(uri, password string, cache time.Duration) (*Com, error) {
|
|||
instance = &Com{
|
||||
Helper: request.NewHelper(log),
|
||||
uri: uri,
|
||||
authPath: UserLoginURI,
|
||||
password: password,
|
||||
}
|
||||
|
||||
if registration != "" {
|
||||
instance.authPath = LoginURI
|
||||
instance.password = registration
|
||||
}
|
||||
|
||||
// ignore the self signed certificate
|
||||
instance.Client.Transport = request.NewTripper(log, transport.Insecure())
|
||||
|
||||
|
|
@ -78,6 +86,11 @@ func GetInstance(uri, password string, cache time.Duration) (*Com, error) {
|
|||
}
|
||||
})
|
||||
|
||||
// check if different uris are provided
|
||||
if password != "" && registration != "" {
|
||||
return nil, errors.New("cannot have registration and password")
|
||||
}
|
||||
|
||||
// check if different uris are provided
|
||||
if uri != "" && instance.uri != uri {
|
||||
return nil, fmt.Errorf("uri mismatch: %s vs %s", instance.uri, uri)
|
||||
|
|
@ -97,7 +110,7 @@ func (m *Com) Login() error {
|
|||
"password": m.password,
|
||||
}
|
||||
|
||||
req, err := request.New(http.MethodPut, m.uri+LoginURI, request.MarshalJSON(data), request.JSONEncoding)
|
||||
req, err := request.New(http.MethodPut, m.uri+m.authPath, request.MarshalJSON(data), request.JSONEncoding)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue