mqtt

package
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MqttVersion5 = "v5"
	MqttVersion3 = "v3"
)

Variables

View Source
var (
	ErrNotValidSubscriptionTopic = fmt.Errorf("not a valid subscription topic")
	ErrNotSameTopic              = fmt.Errorf("not the same topic")
	ErrNotSubscribedTopic        = fmt.Errorf("not the subscribed topic")
	ErrInvalidArgs               = fmt.Errorf("invalid number of arguments")
	ErrInvalidIds                = fmt.Errorf("ids cannot be an empty string")
)
View Source
var ErrInvalidVersion = errors.New("invalid MQTT version")

Functions

func CreateTopicWithIds

func CreateTopicWithIds(logger *otelzap.Logger, topicTemplate Topic, ids ...string) (string, error)

CreateTopicWithIds replaces all the + sign in a topic used for subscription with ids. Works only if the number of pluses is matches the number of ids.

func GetIdsFromTopic

func GetIdsFromTopic(logger *otelzap.Logger, actualTopic string, subTopic Topic) ([]string, error)

GetIdsFromTopic parses the topic received from the MQTT client and returns the ids based on the original subscription topic. For example: actual topic = some/exampleId1/subscription/exampleId2/topic subscription topic = some/+/subscription/+/topic should return ["exampleId1", "exampleId2"] If the topic are not the same length or don't contain the same words, it will return an error

Types

type Client

type Client interface {
	Connect(ctx context.Context) error
	Disconnect(ctx context.Context) error
	Publish(ctx context.Context, topic Topic, message interface{}) error
	// PublishRPC(ctx context.Context, topic Topic, message interface{}) error
	SubscribeWithId(ctx context.Context, topic Topic, handler Handler)
	Subscribe(ctx context.Context, topic Topic, handler Handler) error
	GetId() string
	checks.Check
}

Client is an interface wrapper for a simple MQTT client.

func NewClientFromConfig added in v0.2.0

func NewClientFromConfig(obs observability.Observability, cfg Configuration) (Client, error)

NewClientFromConfig Creates a new MQTT client from the configuration based on the supported version.

func NewV3Client added in v0.2.0

func NewV3Client(clientSettings Configuration, obs observability.Observability) (Client, error)

NewV3Client creates a wrapped mqtt Client with specific settings.

func NewV5Client added in v0.2.0

func NewV5Client(clientSettings Configuration, obs observability.Observability) (Client, error)

NewV5Client creates a wrapped mqtt ClientV5 with specific settings.

type Configuration

type Configuration struct {
	Version  string  `yaml:"version"`
	Address  string  `validate:"required,url" json:"address" yaml:"address"`
	Username string  `yaml:"username"`
	Password string  `yaml:"password"`
	ClientId string  `validate:"required" yaml:"clientId"`
	TLS      tls.TLS `validate:"required" yaml:"tls"`
}

Configuration is the configuration for the MQTT client

type Handler added in v0.2.0

type Handler func(client Client, topicIds []string, payloadId uint16, payload interface{}, err error)

type Topic

type Topic string

func (Topic) String

func (t Topic) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL