forked from tkeel-io/device-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelloworld.go
78 lines (61 loc) · 1.6 KB
/
helloworld.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"math/rand"
"time"
"github.com/pkg/errors"
"github.com/tkeel-io/device-sdk-go/client"
"github.com/tkeel-io/device-sdk-go/util/wait"
)
const (
_brokerAddr = "tcp://192.168.123.9:31883"
_username = "iotd-43b5b654-5d29-4464-9a87-822d3aa0957d"
_pwd = "ZjI1M2IyNGMtNjNjZi0zMzM5LWFlMDMtYjBkOWVlYTQ4ZDNh"
)
func main() {
log.SetFlags(log.Lshortfile)
cli := client.NewClient(_brokerAddr, _username, _pwd)()
err := cli.Connect()
if err != nil {
log.Fatalln(err)
}
_ = cli.SubscribeRaw(context.TODO(), rawTopicHandler)
_ = cli.SubscribeAttribute(context.TODO(), attributesTopicHandler)
_ = cli.SubscribeCommand(context.TODO(), commandsTopicHandler)
tm := time.Second * 1
wait.EveryWithContext(context.Background(), func(ctx context.Context) {
v, _ := deviceValue()
// telemetry.
_ = cli.PublishTelemetry(ctx, v)
}, tm)
select {}
}
//
func attributesTopicHandler(message client.Message) (interface{}, error) {
fmt.Printf("attributes=%s\n", string(message.Payload()))
return nil, nil
}
//
func commandsTopicHandler(message client.Message) (interface{}, error) {
fmt.Printf("commands=%s\n", string(message.Payload()))
return "success", nil
}
//
func rawTopicHandler(message client.Message) (interface{}, error) {
fmt.Printf("rawTopic=%s\n", string(message.Payload()))
return nil, nil
}
func deviceValue() ([]byte, error) {
mv := map[string]interface{}{
"humidity": rand.Intn(20),
"temperature": rand.Intn(20),
}
bys, err := json.Marshal(mv)
if err != nil {
err = errors.Wrap(err, "deviceValue")
}
return bys, err
}