Skip to content

CPtung/gotag

Repository files navigation

Gotag CircleCI

GoTag is a Go pakcage for ThingsPro. It integrates the mqtt client and the protobuffer which makes data exchanging become easily and narrow down the transmission bandwidth.

Installation

Once you have installed Go, run these commands to install the gotag:

    go get github.com/CPtung/gotag

Build a gotag client

import (
    gotag github.com/CPtung/gotag
)

func main() {   
    client, err := gotag.NewClient()
    if err != nil {
        log.Fatal(err)
    }
    defer client.Delete()

    //...
}

Run data pub/sub

As mentioned, gotag use a mqtt client to do the transmission which means it has to pub/sub topics to send/receive data.

Publish data

func PUB(client *gotag.Tagf) {
    value := gotag.NewValue(1.414)
    client.Publish(
        "gotag",
        "test",
        value,
        t.TAG_VALUE_TYPE_DOUBLE,
        1546920188000,
        "unit")
}

Subscribe data

func SUB(client *gotag.Tagf) {
    client.SubscribeCallback(Handler)
    client.Subscribe("gotag", "test")
}

SubscribeCallback

Gotag needs to register a callback function for subscribed topics.

func Handler(source string, tag string, val *t.Value, valtype int32, ts uint64, unit string) {
    fmt.Printf("Source: %v,", source)
    fmt.Printf("Tag: %v,", tag)
    fmt.Printf("Value: %v,", val.GetDouble())
    fmt.Printf("ValueType: %v,", valtype)
    fmt.Printf("At: %v,", ts)
    fmt.Printf("Unit: %v\n", unit)
}