Support Pulsares Energy Manager (#10755)
This commit is contained in:
parent
c8c6f7d95b
commit
1951e39b63
3 changed files with 35 additions and 1 deletions
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
27
util/sponsor/pulsares.go
Normal 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue