Skip to content

Commit

Permalink
Updated mosquitto to mutablelogic org
Browse files Browse the repository at this point in the history
  • Loading branch information
djthorpe committed Oct 3, 2021
1 parent 18eef8c commit 10c27e6
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 131 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CMD_DIR := $(filter-out cmd/README.md, $(wildcard cmd/*))
PLUGIN_DIR := $(wildcard plugin/*)

# Build flags
BUILD_MODULE = "github.com/djthorpe/go-mosquitto"
BUILD_MODULE = "github.com/mutablelogic/go-mosquitto"
BUILD_LD_FLAGS += -X $(BUILD_MODULE)/pkg/config.GitSource=${BUILD_MODULE}
BUILD_LD_FLAGS += -X $(BUILD_MODULE)/pkg/config.GitTag=$(shell git describe --tags)
BUILD_LD_FLAGS += -X $(BUILD_MODULE)/pkg/config.GitBranch=$(shell git name-rev HEAD --name-only --always)
Expand Down
113 changes: 10 additions & 103 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

This repository contains a Golang [mosquitto](https://mosquitto.org/) client library, which conforms to the MQTT standard. This documentation includes the following information:

* What dependencies are needed in order to use this package
* Information about the two command-line tools, `mqttpub` and `mqttsub`
* Using the `libmosquitto` bindings
* Alternatively, using the `gopi.Unit` interface

This repository is published under the Apache license. Please use the [issues](https://github.com/djthorpe/mosquitto/issues) tab on Github to file bugs, ask for features or
* What dependencies are needed in order to use this package;
* Information about the two command-line tools, `mqttpub` and `mqttsub`;
* Using the `libmosquitto` bindings;
* Alternatively, using the higher-level package;
* Building a REST API frontend to mqtt.

This repository is published under the Apache license.
Please use the [issues](https://github.com/mutablelogic/mosquitto/issues) tab on Github to file bugs, ask for features or
for general discussion.

## Copyright Notice

> Copyright 2020 David Thorpe
> Copyright 2020, 2021 David Thorpe
>
> Licensed under the Apache License, Version 2.0 (the "License");
> you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -83,7 +85,7 @@ You can use the following `libmosquitto` bindings in your code. For informaton
about the C API, please see [here](https://mosquitto.org/api/files/mosquitto-h.html):

```go
package mosquitto // import "github.com/djthorpe/mosquitto/sys/mosquitto"
package mosquitto

const MOSQ_DEFAULT_PORT = 1883

Expand Down Expand Up @@ -169,98 +171,3 @@ func (this *Message) Len() uint
func (this *Message) Qos() int
func (this *Message) Retain() bool
```

## Using the gopi.Unit

Alternatively, the gopi.Unit interface provides an easy way to use the MQTT
client. The interface is as follows:

```go
type Client interface {
// Connect to MQTT broker with options
Connect(...Opt) error

// Disconnect from MQTT broker
Disconnect() error

// Subscribe to topic with wildcard and return request-id
Subscribe(string, ...Opt) (int, error)

// Unsubscribe from topic with wildcard and return request-id
Unsubscribe(string) (int, error)

// Publish []byte data to topic and return request-id
Publish(string, []byte, ...Opt) (int, error)

// Publish JSON data to topic and return request-id
PublishJSON(string, interface{}, ...Opt) (int, error)

// Publish measurements in influxdata line protocol format and return request-id
PublishInflux(string, string, map[string]interface{}, ...Opt) (int, error)

// Wait for a specific request-id or 0 for a connect or disconnect event
// with context (for timeout)
WaitFor(context.Context, int) (Event, error)

// Implements gopi.Unit
gopi.Unit
}
```

Publishing can be done for objects using `PublishJSON` and in InfluxDB line protocol format for measurements. You need to use the `WaitFor` function to wait for acknowledgement of operatons. For example, the following function connects
to a broker, waits for the connection to be acknowledged, publishes measurements
and then disconnects from the broker:

```go
func Publish(app gopi.App,values map[string]interface{},opts []mqtt.Opts) error {
client := app.UnitInstance("mosquitto").(mqtt.Client)
if err := client.Connect(); err != nil {
return err
} else if _,err := client.WaitFor(context.Background(),0); err != nil {
return err
} else if id,err := client.PublishInflux("topic","measurement",values,opts...); err != nil {
return err
} else if _,err := client.WaitFor(context.Background(),id); err != nil {
return err
} else if err := client.Disconnect(); err != nil {
return err
} else if _,err := client.WaitFor(context.Background(),0); err != nil {
return err
} else {
return nil
}
}
```

The unit emits objects of type `mosquitto.Event` on the message bus:

```go
type Event interface {
Type() Flags
Id() int
ReturnCode() int // For CONNECT and DISCONNECT
Topic() string
Data() []byte

// Implements gopi.Event
gopi.Event
}
```

The types of events are as follows:

```go
const (
MOSQ_FLAG_EVENT_CONNECT
MOSQ_FLAG_EVENT_DISCONNECT
MOSQ_FLAG_EVENT_SUBSCRIBE
MOSQ_FLAG_EVENT_UNSUBSCRIBE
MOSQ_FLAG_EVENT_PUBLISH
MOSQ_FLAG_EVENT_MESSAGE
MOSQ_FLAG_EVENT_LOG
)
```

Please see the sample code under the `cmd` folder in the repository for
examples on using the code.

4 changes: 2 additions & 2 deletions cmd/mqttpub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"time"

// Packages
"github.com/djthorpe/go-mosquitto/pkg/app"
"github.com/djthorpe/go-mosquitto/pkg/config"
"github.com/mutablelogic/go-mosquitto/pkg/app"
"github.com/mutablelogic/go-mosquitto/pkg/config"
)

////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions cmd/mqttsub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"time"

// Packages
"github.com/djthorpe/go-mosquitto/pkg/app"
"github.com/djthorpe/go-mosquitto/pkg/config"
"github.com/mutablelogic/go-mosquitto/pkg/app"
"github.com/mutablelogic/go-mosquitto/pkg/config"
)

////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion etc/nfpm/go-server-mqtt/nfpm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ maintainer: "David Thorpe <[email protected]>"
description: |
Plugggable go-server monolith: mqtt
vendor: "mutablelogic.com"
homepage: "https://github.com/djthorpe/go-mosquitto"
homepage: "https://github.com/mutablelogic/go-mosquitto"
depends:
- go-server-httpserver
- go-server-sqlite3
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/djthorpe/go-mosquitto
module github.com/mutablelogic/go-mosquitto

go 1.13

Expand Down
2 changes: 1 addition & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

// Packages
"github.com/djthorpe/go-mosquitto/pkg/mosquitto"
"github.com/mutablelogic/go-mosquitto/pkg/mosquitto"
)

////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"runtime"

// Packages
mosq "github.com/djthorpe/go-mosquitto/sys/mosquitto"
mosq "github.com/mutablelogic/go-mosquitto/sys/mosquitto"
)

///////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion pkg/mosquitto/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

// Namespace imports
. "github.com/djthorpe/go-mosquitto"
. "github.com/mutablelogic/go-mosquitto"
)

////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions pkg/mosquitto/mosquitto.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"time"

// Packages
mosq "github.com/djthorpe/go-mosquitto/sys/mosquitto"
mosq "github.com/mutablelogic/go-mosquitto/sys/mosquitto"
multierror "github.com/hashicorp/go-multierror"

// Namespace imports
. "github.com/djthorpe/go-errors"
. "github.com/djthorpe/go-mosquitto"
. "github.com/mutablelogic/go-mosquitto"
)

////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion pkg/mosquitto/mosquitto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

// Namespace imports
. "github.com/djthorpe/go-mosquitto/pkg/mosquitto"
. "github.com/mutablelogic/go-mosquitto/pkg/mosquitto"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions plugin/mqtt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"time"

// Packages
"github.com/djthorpe/go-mosquitto/pkg/mosquitto"
"github.com/mutablelogic/go-mosquitto/pkg/mosquitto"
"github.com/hashicorp/go-multierror"

// Namespace imports
. "github.com/djthorpe/go-errors"
. "github.com/djthorpe/go-mosquitto"
. "github.com/mutablelogic/go-mosquitto"
. "github.com/mutablelogic/go-server"
. "github.com/mutablelogic/go-sqlite"

Expand Down
2 changes: 1 addition & 1 deletion plugin/mqtt/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"unicode/utf8"

// Package imports
"github.com/djthorpe/go-mosquitto/pkg/mosquitto"
"github.com/mutablelogic/go-mosquitto/pkg/mosquitto"

// Namespace imports
. "github.com/djthorpe/go-errors"
Expand Down
2 changes: 1 addition & 1 deletion sys/mosquitto/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
MQTT client bindings for the Go programming language which
bind to https://mosquitto.org/api/files/mosquitto-h.html
For more information please see
https://github.com/djthorpe/go-mosquitto/blob/master/README.md
https://github.com/mutablelogic/go-mosquitto/blob/master/README.md
*/
package mosquitto
22 changes: 11 additions & 11 deletions sys/mosquitto/mosquitto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"time"

// Namespace imports
"github.com/djthorpe/go-mosquitto/sys/mosquitto"
. "github.com/djthorpe/go-mosquitto/sys/mosquitto"

. "github.com/mutablelogic/go-mosquitto/sys/mosquitto"
)

const (
Expand Down Expand Up @@ -58,12 +58,12 @@ func Test_Mosquitto_003(t *testing.T) {
}

func Test_Mosquitto_004(t *testing.T) {
if err := mosquitto.Init(); err != nil {
if err := Init(); err != nil {
t.Fatal(err)
}
defer mosquitto.Cleanup()
defer Cleanup()

client, err := mosquitto.NewEx("id", true)
client, err := NewEx("id", true)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -117,12 +117,12 @@ func Test_Mosquitto_005(t *testing.T) {
}

func Test_Mosquitto_006(t *testing.T) {
if err := mosquitto.Init(); err != nil {
if err := Init(); err != nil {
t.Fatal(err)
}
defer mosquitto.Cleanup()
defer Cleanup()

client, err := mosquitto.NewEx("id", true)
client, err := NewEx("id", true)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -170,12 +170,12 @@ func Test_Mosquitto_006(t *testing.T) {
}

func Test_Mosquitto_007(t *testing.T) {
if err := mosquitto.Init(); err != nil {
if err := Init(); err != nil {
t.Fatal(err)
}
defer mosquitto.Cleanup()
defer Cleanup()

client, err := mosquitto.NewEx("id", true)
client, err := NewEx("id", true)
if err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 10c27e6

Please sign in to comment.