mqtt

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

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")
)

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()
	Disconnect()
	Publish(topic Topic, message interface{}) error
	Subscribe(topic Topic, handler MessageHandler)
	SubscribeToAny(topic Topic, handler MessageHandler)
	GetId() string
}

Client is an interface wrapper for a simple MQTT client.

func NewMqttClient

func NewMqttClient(clientSettings Configuration, obs observability.Observability) Client

NewMqttClient creates a wrapped mqtt Client with specific settings.

type ClientV5

type ClientV5 interface {
	Connect(ctx context.Context)
	Disconnect()
	Publish(ctx context.Context, topic Topic, message interface{}) error
	SubscribeWithId(ctx context.Context, topic Topic, handler HandlerV5)
	Subscribe(ctx context.Context, topic Topic, handler HandlerV5)
}

ClientV5 is an interface wrapper for a simple MQTT client.

func NewMqttV5Client

func NewMqttV5Client(clientSettings Configuration, obs observability.Observability) ClientV5

NewMqttV5Client creates a wrapped mqtt ClientV5 with specific settings.

type Configuration

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

Configuration is the configuration for the MQTT client

type HandlerV5

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

type MessageHandler

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

type Topic

type Topic string

func (Topic) String

func (t Topic) String() string

type V5Impl

type V5Impl struct {
	// contains filtered or unexported fields
}

V5Impl concrete implementation of the ClientV5, which is essentially a wrapper over the mqtt lib.

func (*V5Impl) Connect

func (c *V5Impl) Connect(ctx context.Context)

func (*V5Impl) Disconnect

func (c *V5Impl) Disconnect()

func (*V5Impl) Publish

func (c *V5Impl) Publish(ctx context.Context, topic Topic, message interface{}) error

Publish a new message to a topic

func (*V5Impl) Subscribe

func (c *V5Impl) Subscribe(ctx context.Context, topic Topic, handler HandlerV5)

Subscribe to a topic

func (*V5Impl) SubscribeWithId

func (c *V5Impl) SubscribeWithId(ctx context.Context, topic Topic, handler HandlerV5)

SubscribeWithId to a topic

Jump to

Keyboard shortcuts

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