Support Pulsares Energy Manager (#10755)

This commit is contained in:
andig 2023-11-12 12:45:19 +01:00 • committed by GitHub
parent c8c6f7d95b
commit 1951e39b63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 1 deletions

View file

@ -368,7 +368,7 @@ func configureEnvironment(cmd *cobra.Command, conf globalConfig) (err error) {
}
// setup sponsorship (allow env override)
if err == nil && conf.SponsorToken != "" {
if err == nil {
err = sponsor.ConfigureSponsorship(conf.SponsorToken)
}

View file

@ -29,6 +29,13 @@ func IsAuthorizedForApi() bool {
// check and set sponsorship token
func ConfigureSponsorship(token string) error {
if token == "" {
var err error
if token, err = readSerial(); token == "" || err != nil {
return err
}
}
host := util.Getenv("GRPC_URI", cloud.Host)
conn, err := cloud.Connection(host)
if err != nil {

27
util/sponsor/pulsares.go Normal file
View file

@ -0,0 +1,27 @@
package sponsor
import (
"os"
"time"
)
func readSerial() (string, error) {
f, err := os.OpenFile("/dev/PulsaresSerial", os.O_RDWR, 0o644)
if err != nil {
return "", nil
}
if _, err := f.Write([]byte{0x0E, 0x00, 0xD0, 0x05}); err != nil {
return "", err
}
// serial timeout
time.AfterFunc(3*time.Second, func() {
_ = f.Close()
})
b := make([]byte, 512)
n, err := f.Read(b)
return string(b[:n]), err
}