annikahannig / alpaca-go

A friendly library to implement MQTT services. In go.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Alpaca

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

IMPORTANT: I deceided to move this into an org: CamelIoT

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.

About

A friendly library to implement MQTT services. In go.

License:Other


Languages

Language:Go 100.0%