alpaca

package module
v0.0.0-...-4bda072 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2018 License: BSD-3-Clause Imports: 8 Imported by: 0

README

Alpaca

An opinionated library for writing services on a MQTT message bus.

Why?

Something along the lines of: After implementing DaliQTT I wanted something more generalized to create new services more easily.

I guess this will later need a python port aswell.

How to use

Creating a new service in your network is now as easy as:

dispatch, actions := alpaca.DialMqtt(
    "tcp://user:pass@localhost:1889",
    alpaca.Routes{
        "lights": "v1/upstairs/lights",
        "meta": "v1/_meta/",
    })

handle(actions, dispatch)

With a service handler like:

const GET_LIGHT_VALUE_REQUEST = "@lights/GET_LIGHT_VALUE_REQUEST"

type Light struct {
    Id     int `json:"id"`
    Value  int `json:"value"`
}

func handle(actions alpaca.Actions, dispatch alpaca.Dispatch) {
    // Do some more setup stuff...

    // Handle incoming actions
    for action := range actions {
        switch action.Type {
        case SET_LIGHT_VALUE_REQUEST:
            var light Light
            action.DecodePayload(&light)
            setLightValue(light.Id, light.Value) 
            dispatch(SetLightValueSuccess(light))
        }
    }
}

For a simple working example please checkout the examples/simple/simple.go string reversal service.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(opts *mqtt.ClientOptions, routes Routes) (Actions, Dispatch)

Connect to MQTT broker like DialMqtt, but give the user more control over the client options.

func DialMqtt

func DialMqtt(brokerUri string, routes Routes) (Actions, Dispatch)

Connect to MQTT broker and create action channel and dispatch function.

Types

type Action

type Action struct {
	Type    string  `json:"type"`
	Payload Payload `json:"payload"`
}

func (Action) DecodePayload

func (a Action) DecodePayload(v interface{}) error

Decode the payload. If an action is received from MQTT, the payload should be raw bytes. We decode this then on demand, as the handler function should know what kind of payload it requires.

type Actions

type Actions chan Action

type Dispatch

type Dispatch func(Action) error

type Payload

type Payload interface{}

type Routes

type Routes map[string]string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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