-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_service_managed_counter_test.go
138 lines (120 loc) · 3.99 KB
/
example_service_managed_counter_test.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package framework_test
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/openchirp/framework"
)
const (
// The subscription key used to identify a messages types
rawRxKey = 0
rawTxKey = 1
)
// CDevice holds any data you want to keep around for a specific
// device that has linked your service.
//
// In this example, we will keep track of the rawrx and rawtx message counts
type CDevice struct {
rawRxCount int
rawTxCount int
}
// NewCDevice is called by the framework when a new device has been linked.
func NewCDevice() framework.Device {
d := new(CDevice)
// The following initialization is redundant in Go
d.rawRxCount = 0
d.rawTxCount = 0
// Change type to the Device interface
return framework.Device(d)
}
// ProcessLink is called once, during the initial setup of a
// device, and is provided the service config for the linking device.
func (d *CDevice) ProcessLink(ctrl *framework.DeviceControl) string {
// Subscribe to subtopic "rawrx"
ctrl.Subscribe("rawrx", rawRxKey)
// Subscribe to subtopic "rawtx"
ctrl.Subscribe("rawtx", rawTxKey)
// This message is sent to the service status for the linking device
return "Success"
}
// ProcessUnlink is called once, when the service has been unlinked from
// the device.
func (d *CDevice) ProcessUnlink(ctrl *framework.DeviceControl) {
// The framework already handles unsubscribing from all
// Device associted subtopics, so we don't need to call
// ctrl.Unsubscribe.
}
// ProcessConfigChange is intended to handle a service config updates.
// If your program does not need to handle incremental config changes,
// simply return false, to indicate the config update was unhandled.
// The framework will then automatically issue a ProcessUnlink and then a
// ProcessLink, instead. Note, NewCDevice is not called.
//
// For more information about this or other Device interface functions,
// please see https://godoc.org/github.com/OpenChirp/framework#Device .
func (d *CDevice) ProcessConfigChange(ctrl *framework.DeviceControl, cchanges, coriginal map[string]string) (string, bool) {
return "", false
// If we have processed this config change, we should return the
// new service status message and true.
//
//return "Sucessfully updated", true
}
// ProcessMessage is called upon receiving a pubsub message destined for
// this CDevice.
// Along with the standard DeviceControl object, the handler is provided
// a Message object, which contains the received message's payload,
// subtopic, and the provided Subscribe key.
func (d *CDevice) ProcessMessage(ctrl *framework.DeviceControl, msg framework.Message) {
if msg.Key().(int) == rawRxKey {
d.rawRxCount++
subtopic := "rawrxcount"
ctrl.Publish(subtopic, fmt.Sprint(d.rawRxCount))
} else if msg.Key().(int) == rawTxKey {
d.rawTxCount++
subtopic := "rawtxcount"
ctrl.Publish(subtopic, fmt.Sprint(d.rawTxCount))
} else {
log.Fatalln("Received unassociated message")
}
}
func ExampleStartServiceClientManaged_counter() {
// Parse parameters from command line or environment variables
frameworkServer := "http://localhost:7000"
mqttServer := "localhost:1883"
serviceId := "5a1ea73df76abe01c57abfb8"
serviceToken := "DJpHxwmExGbcYwsEHgQezDVeKS4N"
c, err := framework.StartServiceClientManaged(
frameworkServer,
mqttServer,
serviceId,
serviceToken,
"Unexpected disconnect!",
NewCDevice)
if err != nil {
log.Fatalln("Failed to StartServiceClient: ", err)
}
defer c.StopClient()
log.Println("Started service")
/* Post service's global status */
err = c.SetStatus("Started")
if err != nil {
log.Fatalln("Failed to publish service status: ", err)
return
}
log.Println("Published Service Status")
/* Setup signal channel */
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
/* Wait on a signal */
<-signals
log.Println("Shutting down")
/* Post service's global status */
err = c.SetStatus("Shutting down")
if err != nil {
log.Fatalln("Failed to publish service status: ", err)
return
}
log.Println("Published Service Status")
}