-
Notifications
You must be signed in to change notification settings - Fork 0
/
propresenter.go
128 lines (113 loc) · 2.92 KB
/
propresenter.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
package main
import (
"encoding/json"
"fmt"
"github.com/murlokswarm/app"
"github.com/sacOO7/gowebsocket"
"os"
"os/signal"
"time"
)
var clocks = make(map[string]string)
var ProPresenterConnected bool
var ProPresenterConnecting bool
var LastPingAt time.Time
func HandleMessage(str string) {
type Message struct {
Acn string
Uid string
Txt string
}
message := Message{}
err := json.Unmarshal([]byte(str), &message)
if err != nil {
app.Log("JSON parsing error:", err)
}
if message.Acn == "tmr" {
clocks[message.Uid] = message.Txt
app.PostAction("clock-action", clocks)
PushToFirebase(LocationClock{
UpdatedAt: time.Now(),
Location: LocationName,
ClockUuid: message.Uid,
Value: message.Txt,
})
}
}
var closeSocket chan bool
func ConnectProPresenter() {
ProPresenterConnecting = true
if closeSocket == nil {
closeSocket = make(chan bool, 1)
}
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
// TODO: Ensure connection timeout is short, no concurrent watchdog connection attempts
socket := gowebsocket.New(WS_URL)
socket.OnConnectError = func(err error, socket gowebsocket.Socket) {
app.Log("ws: Received connect error - ", err)
ProPresenterConnecting = false
ProPresenterConnected = false
}
socket.OnConnected = func(socket gowebsocket.Socket) {
ProPresenterConnecting = false
ProPresenterConnected = true
app.Log("ws: Connected to server")
app.PostAction("connect-action", true)
// Log in to ProPresenter
socket.SendText(fmt.Sprintf(`{"pwd":"%s","ptl":610,"acn":"ath"}`, WS_PASSWORD))
}
socket.OnDisconnected = func(err error, socket gowebsocket.Socket) {
ProPresenterConnecting = false
ProPresenterConnected = false
app.Log("ws: Disconnected from server ")
app.PostAction("connect-action", false)
return
}
socket.OnTextMessage = func(message string, socket gowebsocket.Socket) {
app.Log("ws: Received message - " + message)
HandleMessage(message)
}
socket.OnPingReceived = func(data string, socket gowebsocket.Socket) {
LastPingAt = time.Now()
app.Log("ws: Received ping - " + data)
}
socket.OnPongReceived = func(data string, socket gowebsocket.Socket) {
app.Log("ws: Received pong - " + data)
}
socket.Connect()
go func() {
for {
select {
case <-closeSocket:
app.Log("Socket: Closing.")
socket.Close()
return
case <-interrupt:
app.Log("Socket: Interrupt.")
socket.Close()
return
}
}
}()
}
func ConnectionWatchdog() {
ticker := time.NewTicker(time.Second * 2)
go func() {
for _ = range ticker.C {
if ProPresenterConnected {
if time.Now().Sub(LastPingAt).Seconds() > 10 {
closeSocket <- true
ProPresenterConnecting = false
ProPresenterConnected = false
app.Log("Watchdog: Ping timeout, marked as disconnected.")
}
} else {
if ProPresenterConnecting == false {
app.Log("Watchdog: spawning connection attempt")
ConnectProPresenter()
}
}
}
}()
}