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.
Once you have installed Go, run these commands to install the gotag
:
go get github.com/CPtung/gotag
import (
gotag github.com/CPtung/gotag
)
func main() {
client, err := gotag.NewClient()
if err != nil {
log.Fatal(err)
}
defer client.Delete()
//...
}
As mentioned, gotag use a mqtt client to do the transmission which means it has to pub/sub topics to send/receive data.
func PUB(client *gotag.Tagf) {
value := gotag.NewValue(1.414)
client.Publish(
"gotag",
"test",
value,
t.TAG_VALUE_TYPE_DOUBLE,
1546920188000,
"unit")
}
func SUB(client *gotag.Tagf) {
client.SubscribeCallback(Handler)
client.Subscribe("gotag", "test")
}
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)
}