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

67 lines
1.5 KiB
Go
Raw Normal View History

2024-07-30 14:28:39 +02:00
package v1
import (
"context"
2024-08-02 12:57:07 +02:00
"log/slog"
2024-07-30 14:28:39 +02:00
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"
2024-08-02 12:57:07 +02:00
"github.com/Masterminds/semver/v3"
"github.com/pkg/errors"
2024-07-30 14:28:39 +02:00
)
const Identifier protocol.Identifier = "v1"
2024-08-02 12:57:07 +02:00
const compatibleVersionConstraint = "^2.24"
2024-07-30 14:28:39 +02:00
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) {
2024-08-02 12:57:07 +02:00
ops := p.Operations(addr).(*Operations)
if err := ops.Connect(ctx); err != nil {
return false, nil
}
defer ops.Close(ctx)
rawVersion, _, err := ops.Version(ctx)
if err != nil {
return false, nil
}
versionConstraint, err := semver.NewConstraint(compatibleVersionConstraint)
if err != nil {
return false, errors.WithStack(err)
}
version, err := semver.NewVersion(rawVersion)
if err != nil {
return false, errors.WithStack(err)
}
p.logger.Debug("checking version", slog.Any("version", rawVersion), slog.Any("constraint", compatibleVersionConstraint))
if !versionConstraint.Check(version) {
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{addr: addr, logger: p.logger, dial: p.dial}
2024-07-30 14:28:39 +02:00
}
var _ protocol.Protocol = &Protocol{}