go-emlid/reach/client/protocol/v2/protocol.go

62 lines
1.3 KiB
Go
Raw Normal View History

2024-07-30 14:28:39 +02:00
package v2
import (
"context"
2024-08-02 12:57:07 +02:00
"forge.cadoles.com/cadoles/go-emlid/reach/client/logger"
2024-07-30 14:28:39 +02:00
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol"
"github.com/Masterminds/semver/v3"
"github.com/pkg/errors"
)
const Identifier protocol.Identifier = "v2"
const compatibleVersionConstraint = ">= 32"
type Protocol struct {
2024-08-02 12:57:07 +02:00
logger logger.Logger
dial protocol.DialFunc
2024-07-30 14:28:39 +02:00
}
// Available implements protocol.Protocol.
func (p *Protocol) Available(ctx context.Context, addr string) (bool, error) {
ops := p.Operations(addr).(*Operations)
info, err := ops.GetInfo(ctx)
if err != nil {
return false, errors.WithStack(err)
}
versionConstraint, err := semver.NewConstraint(compatibleVersionConstraint)
if err != nil {
return false, errors.WithStack(err)
}
version, err := semver.NewVersion(info.Reachview.Version)
if err != nil {
return false, errors.WithStack(err)
}
if !versionConstraint.Check(version) {
2024-08-02 12:57:07 +02:00
return false, nil
2024-07-30 14:28:39 +02:00
}
return true, 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{
dial: p.dial,
addr: addr,
logger: p.logger,
}
2024-07-30 14:28:39 +02:00
}
var _ protocol.Protocol = &Protocol{}