mqtt

package
v0.0.0-...-f315928 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package mqtt provides the IoT broker with device twin support

A device twin is a set of JSON objects, each described with a unique key.

For working with the device twin, the broker supports the following MQTT topics:

kurbisio/{device_id}/twin/requests/{key}
kurbisio/{device_id}/twin/reports/{key}
kurbisio/{device_id}/twin/get

Requests and Reports

The service always manages two sides for any given key: the request and the report. The request is an object that gets transfered to the device. The report is the device's answer to that request.

For example, a system could request a software version by publishing a message

{"version": "2.0"}

to /twin/requests/software_version.

The device would subscribe to this channel, receive the request, update its software and then report the result by posting

{"version": "2.0"}

to /twin/reports/software_version.

The REST API for twin also contains the time stamps when a request or a report was published. Multiple equal updates from the device do not change the reported_at timestamp. The reported_at timestamp stores the time when a report was received the first time.

Retrieving Requests

A connected device will immediately receive requests whenever the device twin is updated via the REST API. In order to receive the latest requests after establishing the connection, the device should publish a message to /twin/get with a JSON list of keys the device has subscribed to. In our example the device would publish

["software_version"]

to /twin/get.

Database Requirements

The broker assumes that the database manages a resource "device". It creates an additional table " twin" to store the device twin.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broker

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

Broker is a MQTT broker for IoT.

func NewBroker

func NewBroker(bb *Builder) *Broker

NewBroker returns a new broker. The broker will not actually run until you call Run()

func (*Broker) PublishMessageQ1

func (b *Broker) PublishMessageQ1(topic string, payload []byte)

PublishMessageQ1 publishes an MQTT messsage with quality level 1

func (*Broker) Run

func (b *Broker) Run()

Run is blocking and runs the server. It listens on syscall.SIGTERM and a gracefully shutdown.

type Builder

type Builder struct {
	// DB is a postgres database. This is mandatory.
	DB *csql.DB
	// CACertFile is the file path to the X.509 certificate of the certificate authority.
	// This is mandatory
	CACertFile string
	// CertFile is the file path to the X.509 certificate file. This is mandatory.
	CertFile string
	// KeyFile is the file path to the X.509 private key file. This is mandatory.
	KeyFile string
}

Builder is a builder helper for the Broker

Jump to

Keyboard shortcuts

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