package v1 import ( "context" "log/slog" "forge.cadoles.com/cadoles/go-emlid/reach/client/logger" "forge.cadoles.com/cadoles/go-emlid/reach/client/protocol" "github.com/Masterminds/semver/v3" "github.com/pkg/errors" ) const Identifier protocol.Identifier = "v1" const compatibleVersionConstraint = "^2.24" type Protocol struct { logger logger.Logger dial protocol.DialFunc } // Available implements protocol.Protocol. func (p *Protocol) Available(ctx context.Context, addr string) (bool, error) { 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 } 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} } var _ protocol.Protocol = &Protocol{}