-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient.go
438 lines (371 loc) · 10 KB
/
client.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"time"
term "github.com/nsf/termbox-go"
"github.com/stianeikeland/go-rpio"
"golang.org/x/net/websocket"
)
var (
keyPinBCM = 07
keyPinNumber = 26
spkrPinBCM = 10
spkrPinNumber = 19
state = "idle"
queue []string
outQueue []string
bufferReferenceTime int64
bufferDelay int64 = 500000 // Default buffer delay
lastKeyId string // identifier for the telegraph that the current queue came from
lastKeyVal = "0"
gpio bool
t tone
pingInterval int64 = 30000 // Interval between test pings to the server (milliseconds)
pingTimeout int64 = 5000 // How long to wait after sending a ping before reporting an error (milliseconds)
pingTimer int64
pingOutstanding = false
redialInterval int64 = 1000 // initial number of milliseconds between redial attempts
lastRedialTime int64
)
type Config struct {
Channel string
Server string
Port string
Gpio bool
}
type socketClient struct {
ip, port, channel, status string
conn *websocket.Conn
}
type morseKey struct {
lastState, lastDur, lastStart, lastEnd int64
keyPin rpio.Pin
}
type tone struct {
state string
spkrPin rpio.Pin
}
func (sc *socketClient) dial(firstDial bool) {
fmt.Println("Dialing 'ws://" + sc.ip + ":" + sc.port + "/channel/" + sc.channel)
sc.status = "dialling"
conn, err := websocket.Dial("ws://"+sc.ip+":"+sc.port+"/channel/"+sc.channel, "", "http://localhost")
if err == nil {
sc.conn = conn
playMorse(".-. . .- -.. -.--")
sc.status = "connected"
fmt.Println("sc.status = " + sc.status)
fmt.Print("sc.conn = ")
fmt.Println(sc.conn)
return
}
if err != nil {
fmt.Println("Error connecting to 'ws://" + sc.ip + ":" + sc.port + "/channel/" + sc.channel + "': " + err.Error())
lastRedialTime = milliseconds()
redialInterval = redialInterval * 2
if redialInterval > 30000 {
redialInterval = 30000
}
}
}
func (t *tone) set(value int) {
if gpio == true {
if value == 0 {
t.spkrPin.Write(rpio.Low)
t.state = "OFF"
} else if value == 1 {
t.spkrPin.Write(rpio.High)
t.state = "ON"
} else {
fmt.Print("Err! Couldn’t set tone to: ")
fmt.Println(value)
}
} else {
if value == 0 {
// need to figure out the best way to generate + play a tone
t.state = "OFF"
} else if value == 1 {
// need to figure out the best way to generate + play a tone
t.state = "ON"
} else {
fmt.Println("Err! Couldn’t set tone")
}
}
}
func (t *tone) start() {
if gpio == true {
t.spkrPin.Write(rpio.High)
} else {
// TODO: cross-platform way generate and play tone
}
t.state = "ON"
// This makes a very bad tone with a piezo buzzer.
// go func() {
// for {
// select {
// case <-stopPWM:
// return
// default:
// t.spkrPin.Write(rpio.High)
// time.Sleep(250 * time.Microsecond)
// t.spkrPin.Write(rpio.Low)
// time.Sleep(250 * time.Microsecond)
// }
// }
// }()
}
func (t *tone) stop() {
if gpio == true {
t.spkrPin.Write(rpio.Low)
} else {
// TODO: cross-platform generate and play tone
}
t.state = "OFF"
}
func playMorse(message string) {
speed := time.Duration(50)
for i := 0; i < len(message); i++ {
switch message[i] {
case 46: // == "."
t.start()
time.Sleep(speed * time.Millisecond)
t.stop()
time.Sleep(speed * time.Millisecond)
case 45: // == "-"
t.start()
time.Sleep(3 * speed * time.Millisecond)
t.stop()
time.Sleep(speed * time.Millisecond)
case 32: // == " "
time.Sleep(3 * speed * time.Millisecond)
default:
// Do nothing...
}
}
}
func microseconds() int64 {
t := time.Now().UnixNano()
us := t / int64(time.Microsecond)
return us
}
func milliseconds() int64 {
t := time.Now().UnixNano()
ms := t / int64(time.Millisecond)
return ms
}
func (sc *socketClient) onMessage(m string) {
// Process pongs from the server
if m == "pong" {
pingOutstanding = false
return
}
// value := m[:1] // whether the key went up or down
ts := m[1 : len(m)-4] // timestamp (in microseconds)
keyId := m[len(m)-4:] // last 4 digits of message is the key id
fmt.Print("Received message ")
fmt.Print(m)
fmt.Print(" from ")
fmt.Print(keyId)
fmt.Print(" at ")
fmt.Println(time.Now())
if keyId != lastKeyId { // if its a different telegraph sending
if len(queue) > 0 {
// ...and there's already a queue from a different telegraph, do nothing.
fmt.Print("New telegraph detected, but queue still has messages. Ignoring.")
} else {
fmt.Print("New telegraph detected. Setting bufferReferenceTime:")
lastKeyId = keyId
tsint64, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
fmt.Println(err)
}
// Set the time offset between local and remote clients, plus bufferDelay
bufferReferenceTime = (microseconds() + bufferDelay) - tsint64
fmt.Println(bufferReferenceTime)
queue = append(queue, m)
}
} else {
queue = append(queue, m)
}
}
func (sc *socketClient) listen() {
fmt.Println("Client listening…")
var msg string
for {
if sc.status == "connected" {
err := websocket.Message.Receive(sc.conn, &msg)
if err != nil {
fmt.Println("Websocket error on Message.Receive(): " + err.Error())
sc.status = "disconnected"
playMorse("........")
sc.dial(false)
} else {
sc.onMessage(msg)
}
} else {
time.Sleep(1 * time.Millisecond)
}
}
}
func (sc *socketClient) outputListen() {
for {
if len(outQueue) > 0 && sc.status == "connected" {
fmt.Println("Out queue detected in outputListen()")
fmt.Println("Sending message: " + outQueue[0])
sendErr := websocket.Message.Send(sc.conn, outQueue[0])
if sendErr != nil {
sc.status = "disconnected"
fmt.Print("sc.conn in send function = ")
fmt.Println(sc.conn)
fmt.Println("Could not send message:")
fmt.Println(sendErr.Error())
if outQueue[0][:1] != "1" { // Error beep only on keyup, to prevent confusion.
playMorse("........")
fmt.Println("Redialling websocket server…")
fmt.Println("Current status: " + sc.status)
sc.dial(false)
}
} else {
fmt.Print("Sent: ")
fmt.Println(outQueue[0])
outQueue = append(outQueue[:0], outQueue[0+1:]...)
}
}
time.Sleep(1 * time.Millisecond)
}
}
func main() {
if len(os.Getenv("TELEGRAPH_CONFIG_PATH")) == 0 {
os.Setenv("TELEGRAPH_CONFIG_PATH", "config.json")
}
file, _ := os.Open(os.Getenv("TELEGRAPH_CONFIG_PATH"))
decoder := json.NewDecoder(file)
config := Config{Gpio: true}
err := decoder.Decode(&config)
if err != nil {
fmt.Println("Error reading config.json: ", err)
fmt.Println("Falling back on default config…")
config.Channel = "lobby"
config.Server = "morse.autodidacts.io"
config.Port = "8000"
}
fmt.Println(config.Channel)
gpio = config.Gpio
// Initialize morse key
key := morseKey{lastState: 1, lastDur: 0, lastStart: 0, lastEnd: 0}
t = tone{state: "OFF"}
if gpio == true {
// Setup GPIO
openErr := rpio.Open()
if openErr != nil {
fmt.Println("Error initializing GPIO: " + err.Error())
}
keyPn := rpio.Pin(keyPinBCM)
keyPn.Input()
spkrPn := rpio.Pin(spkrPinBCM)
spkrPn.Output()
t.spkrPin = spkrPn
key.keyPin = keyPn
defer rpio.Close()
} else {
// Setup for keypress detection
err := term.Init()
if err != nil {
panic(err)
}
defer term.Close()
}
// Init socketClient & dial websocket
sc := socketClient{ip: config.Server, port: config.Port, channel: config.Channel}
sc.dial(true)
// Start the listener for incoming messages
go sc.listen()
// Start the listener that monitors the output queue and sends messages
go sc.outputListen()
var gpioKeyVal rpio.State = rpio.High
// Adding a simplified version of things...
for {
if sc.status != "connected" && sc.status != "dialling" {
fmt.Println("Disconnection detected in main loop. Redialling...")
playMorse("........")
sc.dial(false) // Connect if broken
}
if sc.status == "dialling" && (milliseconds() > (lastRedialTime + redialInterval)) {
fmt.Println("Redial timer complete in main loop. Redialling...")
sc.dial(false)
}
var keyVal string
if gpio == true {
gpioKeyVal = key.keyPin.Read()
} else {
keyPressLoop:
for {
switch ev := term.PollEvent(); ev.Type {
case term.EventKey:
switch ev.Key {
case term.KeyEsc:
os.Exit(1)
case term.KeySpace:
gpioKeyVal = rpio.Low
break keyPressLoop
case term.KeyEnter:
gpioKeyVal = rpio.High
break keyPressLoop
default:
break keyPressLoop
}
case term.EventError:
panic(ev.Err)
}
}
}
if gpioKeyVal == rpio.High {
keyVal = "0"
} else {
keyVal = "1"
}
if keyVal != lastKeyVal {
if sc.status == "connected" {
fmt.Print("key change: ")
fmt.Print(lastKeyVal)
fmt.Print(" → ")
fmt.Println(keyVal)
toneVal, _ := strconv.Atoi(keyVal)
t.set(toneVal)
timestamp := strconv.FormatInt(microseconds(), 10)
msg := keyVal + timestamp + "v2"
outQueue = append(outQueue, msg)
lastKeyVal = keyVal
} else {
playMorse("........")
redialInterval = 1
}
}
if len(queue) > 0 { // If there's an input queue, parse the next message
m := queue[0]
ts := m[1 : len(queue[0])-4]
ts64, _ := strconv.ParseInt(ts, 10, 64)
if ts64 < microseconds()-bufferReferenceTime { // If it's time to output this message, do so
msgValue, _ := strconv.Atoi(m[:1])
queue = append(queue[:0], queue[0+1:]...) // Pop message out of queue
t.set(msgValue)
}
}
if sc.status == "connected" {
// Ping the server periodically to check if we're actually connected
if milliseconds() > (pingTimer + pingInterval) {
pingTimer = milliseconds()
outQueue = append(outQueue, "ping")
pingOutstanding = true
}
if pingOutstanding == true && (milliseconds() > (pingTimer + pingTimeout)) {
fmt.Println("Server ping timeout. Connection error.")
sc.status = "disconnected"
pingOutstanding = false
}
}
time.Sleep(1 * time.Millisecond)
}
}