mqtt

package
v0.12.17 Latest Latest
Warning

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

Go to latest
Published: May 28, 2020 License: MIT Imports: 8 Imported by: 1

Documentation

Overview

Package mqtt implements a mqtt pub/sub interface

modules: MQTTMessage:

model: sour.is/x/toolbox/mqtt.GraphMessage

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dial

func Dial(c *ConnectOptions) error

Dial starts a shared connection to mqtt

func NewLogger

func NewLogger(topic string, level event.Level) event.Logger

NewLogger creates a new MQTT Logger

func Publish

func Publish(m Message) error

Publish sends message on shared connection

func Subscribe

func Subscribe(topic string, qos int8) (<-chan Message, func(), error)

Subscribe starts a watcher for shared connecton

func Terminate

func Terminate()

Terminate disconnects shared connection

Types

type Client

type Client struct {
	*client.Client
	Topics map[string][]chan<- Message
	// contains filtered or unexported fields
}

Client holds a threadsafe client

var Default Client

Default shared connection to mqtt

func New

func New(c *ConnectOptions) (Client, error)

New connects to MQTT

func (Client) Publish

func (c Client) Publish(m Message) error

Publish a JSON mesg to the network.

func (Client) Subscribe

func (c Client) Subscribe(
	topic string,
	qos int8,
) (
	out <-chan Message,
	unsubscribe func(),
	err error,
)

Subscribe to a topic and get messages on channel

func (Client) Terminate

func (c Client) Terminate()

Terminate closes all subsciptions and terminates connection.

type ConnectOptions

type ConnectOptions = client.ConnectOptions

ConnectOptions defines options for connecting to mqtt

type GraphMessage

type GraphMessage struct {
	Topic   string `json:"topic"`
	Message string `json:"message"`
}

GraphMessage returned to client

type GraphMqtt

type GraphMqtt struct{}

GraphMqtt implements graphql resolver

func (GraphMqtt) Mqtt

func (GraphMqtt) Mqtt(ctx context.Context, topic string, qos *int) (out <-chan *GraphMessage, err error)

Mqtt subscribes to a given topic

func (GraphMqtt) MqttPublish

func (GraphMqtt) MqttPublish(
	ctx context.Context,
	topic string,
	message string,
) (ok bool, err error)

MqttPublish publishes a message onto the queue

type Logger

type Logger struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Logger outputs an event to MQTT

func (*Logger) GetVerbose

func (l *Logger) GetVerbose() event.Level

GetVerbose gets the verbose level

func (*Logger) SetVerbose

func (l *Logger) SetVerbose(level event.Level)

SetVerbose sets the event verbose level

func (*Logger) WriteEvent

func (l *Logger) WriteEvent(e *event.Event)

WriteEvent ouputs an event to MQTT

type Message

type Message struct {
	TopicName []byte
	Message   []byte
	QoS       uint8
	Retain    bool
}

Message is a value received from mqtt

func NewMessage

func NewMessage(topic string, s interface{}) (m Message, err error)

NewMessage creates new message

func (Message) JSON

func (m Message) JSON(s interface{}) error

JSON unmarshalls content into passed struct.

func (Message) String

func (m Message) String() string

func (Message) Topic

func (m Message) Topic() string

Topic message was sent on

Jump to

Keyboard shortcuts

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