-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple.go
61 lines (49 loc) · 1.11 KB
/
simple.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
package main
import (
"log"
"github.com/mhannig/alpaca-go"
)
// Actions
const REVERSE_REQUEST = "@strings/REVERSE_REQUEST"
const REVERSE_SUCCESS = "@strings/REVERSE_SUCCESS"
const REVERSE_ERROR = "@strings/REVERSE_ERROR"
// Action Creators
func ReverseSuccess(str string) alpaca.Action {
return alpaca.Action{
Type: REVERSE_SUCCESS,
Payload: str,
}
}
func ReverseError(err error) alpaca.Action {
return alpaca.Action{
Type: REVERSE_ERROR,
Payload: err.Error(),
}
}
func handleReverse(action alpaca.Action) alpaca.Action {
var payload string
err := action.DecodePayload(&payload)
if err != nil {
log.Println("Could not decode payload:", err)
return ReverseError(err)
}
reverse := ""
for _, c := range payload {
reverse = string(c) + reverse
}
log.Println("Reversed string:", payload, " -> ", reverse)
return ReverseSuccess(reverse)
}
func main() {
actions, dispatch := alpaca.DialMqtt(
"tcp://localhost:1883",
alpaca.Routes{
"strings": "v1/simple/strings",
})
for action := range actions {
switch action.Type {
case REVERSE_REQUEST:
dispatch(handleReverse(action))
}
}
}