-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhub.go
229 lines (211 loc) · 5.45 KB
/
hub.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"log"
"encoding/json"
"strconv"
"strings"
)
type hub struct {
// Registered connections.
connections map[*connection]bool
// Inbound messages from the connections.
broadcast chan []byte
// Inbound messages from the system
broadcastSys chan []byte
// Register requests from the connections.
register chan *connection
// Unregister requests from connections.
unregister chan *connection
gpio *GPIO
}
var h = hub{
broadcast: make(chan []byte),
broadcastSys: make(chan []byte),
register: make(chan *connection),
unregister: make(chan *connection),
connections: make(map[*connection]bool),
}
func (h *hub) run(gpio *GPIO) {
h.gpio = gpio
for {
select {
case c := <-h.register:
h.connections[c] = true
// send supported commands
c.send <- []byte("{\"Type\" : \"Version\", \"Version\" : \"" + version + "\"} ")
c.send <- []byte("{\"Type\" : \"Commands\", \"Commands\" : [ \"gethost\", \"getpinmap\", \"getpinstates\", \"initpin\", \"setpin\", \"removepin\" ]} ")
case c := <-h.unregister:
delete(h.connections, c)
// put close in func cuz it was creating panics and want
// to isolate
func() {
// this method can panic if websocket gets disconnected
// from users browser and we see we need to unregister a couple
// of times, i.e. perhaps from incoming data from serial triggering
// an unregister. (NOT 100% sure why seeing c.send be closed twice here)
defer func() {
if e := recover(); e != nil {
log.Println("Got panic: ", e)
}
}()
close(c.send)
}()
case m := <-h.broadcast:
/*log.Print("Got a broadcast")
log.Print(m)
log.Print(len(m))*/
if len(m) > 0 {
/*log.Print(string(m))
log.Print(h.broadcast)*/
h.checkCmd(m)
//log.Print("-----")
}
case m := <-h.broadcastSys:
/*log.Printf("Got a system broadcast: %v\n", string(m))
log.Print(string(m))
log.Print("-----")*/
for c := range h.connections {
select {
case c.send <- m:
/*log.Print("did broadcast to ")
log.Print(c.ws.RemoteAddr())*/
//c.send <- []byte("hello world")
default:
delete(h.connections, c)
close(c.send)
go c.ws.Close()
}
}
}
}
}
func (h *hub) sendErr(msg string) {
msgMap := map[string]string {"error": msg}
log.Println("Error: " + msg)
bytes, err := json.Marshal(msgMap)
if err!=nil {
log.Println("Failed to marshal data!")
return
}
h.broadcastSys <- bytes
}
func (h *hub) sendMsg(name string, msg interface{}) {
msgMap := make(map[string] interface{})
msgMap[name] = msg
msgMap["Type"] = name
//log.Println("Sent: " + name)
bytes, err := json.Marshal(msgMap)
if err!=nil {
log.Println("Failed to marshal data!")
return
}
h.broadcastSys <- bytes
}
func (h *hub) checkCmd(m []byte) {
//log.Println("Inside checkCmd")
s := string(m[:])
s = strings.Replace(s, "\n", "", -1)
sl := strings.ToLower(s)
log.Print(sl)
if strings.HasPrefix(sl, "gethost") {
hostname,err := h.gpio.Host()
if err!=nil {
go h.sendErr(err.Error())
}
go h.sendMsg("Host", hostname)
} else if strings.HasPrefix(sl, "getpinmap") {
pinMap,err := h.gpio.PinMap()
if err!=nil {
go h.sendErr(err.Error())
}
go h.sendMsg("PinMap", pinMap)
} else if strings.HasPrefix(sl, "getpinstates") {
pinStates,err := h.gpio.PinStates()
if err!=nil {
go h.sendErr(err.Error())
}
go h.sendMsg("PinStates", pinStates)
} else if strings.HasPrefix(sl, "initpin") {
// format : setpin pinId dir pullup
args := strings.Split(s, " ")
if len(args) < 4 {
go h.sendErr("You did not specify a pin and a direction [0|1|low|high] and a name")
return
}
if len(args[1]) < 1 {
go h.sendErr("You did not specify a pin")
return
}
pin := args[1]
dirStr := args[2]
name := args[4]
dir := In
switch {
case dirStr == "1" || dirStr == "out" || dirStr == "output":
dir = Out
case dirStr == "0" || dirStr == "in" || dirStr == "input":
dir = In
case dirStr == "pwm":
dir = PWM
}
pullup := Pull_None
switch {
case args[3] == "1" || args[3] == "up":
pullup = Pull_Up
case args[3] == "0" || args[3] == "down":
pullup = Pull_Down
}
err := h.gpio.PinInit(pin, dir, pullup, name)
if err != nil {
go h.sendErr(err.Error())
}
} else if strings.HasPrefix(sl, "removepin") {
// format : removepin pinId
args := strings.Split(s, " ")
if len(args) < 2 {
go h.sendErr("You did not specify a pin id")
return
}
err := h.gpio.PinRemove(args[1])
if err != nil {
go h.sendErr(err.Error())
}
} else if strings.HasPrefix(sl, "setpin") {
// format : setpin pinId high/low/1/0
args := strings.Split(s, " ")
if len(args) < 3 {
go h.sendErr("You did not specify a pin and a state [0|1|low|high]")
return
}
if len(args[1]) < 1 {
go h.sendErr("You did not specify a pin")
return
}
pin := args[1]
stateStr := args[2]
state := 0
switch {
case stateStr == "1" || stateStr == "high":
state = 1
case stateStr == "0" || stateStr == "low":
state = 0
default:
// assume its a pwm value...if it converts to integer in 0-255 range
s, err := strconv.Atoi(stateStr)
if err != nil {
go h.sendErr("Invalid value, must be between 0 and 255 : " + stateStr)
return
}
if s < 0 || s > 255 {
go h.sendErr("Invalid value, must be between 0 and 255 : " + stateStr)
return
}
state = s
}
err := h.gpio.PinSet(pin, byte(state))
if err != nil {
go h.sendErr(err.Error())
}
}
//log.Println("Done with checkCmd")
}