58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
|
package v2
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
|
||
|
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol"
|
||
|
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol/v2/api"
|
||
|
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol/v2/http"
|
||
|
"github.com/Masterminds/semver/v3"
|
||
|
"github.com/pkg/errors"
|
||
|
)
|
||
|
|
||
|
const Identifier protocol.Identifier = "v2"
|
||
|
|
||
|
const compatibleVersionConstraint = ">= 32"
|
||
|
|
||
|
type Protocol struct {
|
||
|
}
|
||
|
|
||
|
// Available implements protocol.Protocol.
|
||
|
func (p *Protocol) Available(ctx context.Context, addr string) (protocol.Operations, error) {
|
||
|
url := fmt.Sprintf("http://%s/info", addr)
|
||
|
|
||
|
info := api.Info{}
|
||
|
if err := http.GetJSON(url, &info); err != nil {
|
||
|
return nil, errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
versionConstraint, err := semver.NewConstraint(compatibleVersionConstraint)
|
||
|
if err != nil {
|
||
|
return nil, errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
version, err := semver.NewVersion(info.Reachview.Version)
|
||
|
if err != nil {
|
||
|
return nil, errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
if !versionConstraint.Check(version) {
|
||
|
return nil, errors.Errorf("reachview version '%s' does not match constraint '%s'", info.Reachview.Version, compatibleVersionConstraint)
|
||
|
}
|
||
|
|
||
|
return &Operations{addr: addr}, nil
|
||
|
}
|
||
|
|
||
|
// Identifier implements protocol.Protocol.
|
||
|
func (p *Protocol) Identifier() protocol.Identifier {
|
||
|
return Identifier
|
||
|
}
|
||
|
|
||
|
// Operations implements protocol.Protocol.
|
||
|
func (p *Protocol) Operations(addr string) protocol.Operations {
|
||
|
return &Operations{addr: addr}
|
||
|
}
|
||
|
|
||
|
var _ protocol.Protocol = &Protocol{}
|