-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (54 loc) · 1.64 KB
/
main.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
/*
In this example, the device sends an "echo" event whenever it receives a "doEcho"
command.
*/
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/moderepo/device-sdk-go"
)
func doEcho(_ *mode.DeviceContext, cmd *mode.DeviceCommand) {
// Command parameters are JSON encoded and can be retrieved as follows.
var params struct {
Msg string `json:"msg"`
}
if err := cmd.BindParameters(¶ms); err != nil {
fmt.Printf("Failed to bind command parameters: %s\n", err.Error())
return
}
eventData := map[string]interface{}{"msg": params.Msg}
if err := mode.SendEvent("echo", eventData, mode.QOSAtLeastOnce); err != nil {
fmt.Printf("Failed to send event: %s\n", err.Error())
}
}
func main() {
dc := &mode.DeviceContext{
DeviceID: 0, // change this to real device ID
AuthToken: "XXXXXXXXXXX", // change this to real API key assigned to device
}
mode.SetCommandHandler("doEcho", doEcho)
mode.SetDefaultCommandHandler(func(_ *mode.DeviceContext, cmd *mode.DeviceCommand) {
fmt.Printf("Received unknown command %s\n", cmd.Action)
})
mode.SetSessionStateCallback(func(state mode.SessionState) {
fmt.Printf("Session state changed to %v\n", state)
})
if d, err := dc.GetInfo(); err == nil {
fmt.Printf("Running as device %v\n", d)
} else {
fmt.Printf("Failed to get device info: %s\n", err.Error())
os.Exit(1)
}
if err := mode.StartSession(dc); err != nil {
fmt.Printf("Failed to start session: %s\n", err.Error())
os.Exit(1)
}
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT)
sig := <-c
fmt.Printf("Received signal [%v]; shutting down...\n", sig)
mode.StopSession()
}