-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
129 lines (106 loc) · 2.79 KB
/
Copy pathmain.go
File metadata and controls
129 lines (106 loc) · 2.79 KB
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
package main
import (
"fmt"
"klipper-cloud-control-client/auth"
"klipper-cloud-control-client/config"
"klipper-cloud-control-client/rpc"
"log"
"net/url"
"os"
"os/signal"
"sync"
"time"
)
const (
ReconnectTimeout = time.Second * 5
)
func main() {
config.LoadConfig("config.yaml")
if config.GetConfig().Token == nil {
auth := auth.DeviceAuth{}
code, err := auth.GetDeviceCode()
if err != nil {
log.Fatal("Failed to get device code: ", err)
}
fmt.Println("Authorize printer using url ", code.GetUrl(), " and code ", code.GetCode())
token, err := code.GetToken()
if err != nil {
log.Fatal("Failed to get device token: ", err)
}
err = config.StoreToken("config.yaml", token)
if err != nil {
log.Fatal("Failed to save token: ", err)
}
} else {
token, err := auth.RefreshToken(*config.GetConfig().Token)
if err != nil {
log.Fatal("Failed to refresh device token: ", err)
}
err = config.StoreToken("config.yaml", token)
if err != nil {
log.Fatal("Failed to save token: ", err)
}
}
jar, err := auth.DoAuth(config.GetConfig().Token)
if err != nil {
log.Fatal("Failed to check token: ", err)
}
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
cloudUrl, err := url.Parse(config.GetConfig().GetUpstream())
if err != nil {
log.Fatal("Failed to parse hostname: ", err)
}
moonrakerUrl, err := url.Parse(config.GetConfig().MoonrakerSocket)
if err != nil {
log.Fatal("Failed to parse moonraker url: ", err)
}
cloudRx := make(chan []byte, rpc.ChannelSize)
cloudTx := make(chan []byte, rpc.ChannelSize)
printerRx := make(chan []byte, rpc.ChannelSize)
printerTx := make(chan []byte, rpc.ChannelSize)
wg := &sync.WaitGroup{}
var cloudSocket *rpc.Socket
var printerSocket *rpc.Socket
_ = rpc.NewBridge(
cloudRx,
cloudTx,
printerRx,
printerTx, jar)
cloudReconnect := time.NewTicker(3)
printerReconnect := time.NewTicker(3)
for {
select {
case _ = <-cloudReconnect.C:
cloudReconnect.Stop()
cloudSocket, err = rpc.NewSocket(*cloudUrl, jar, cloudRx, cloudTx, wg)
if err != nil {
log.Println("Failed to connect to cloud", err)
cloudReconnect.Reset(ReconnectTimeout)
continue
}
log.Println("Connected to cloud")
go func() {
<-cloudSocket.C
log.Println("Reconnecting to cloud")
cloudReconnect.Reset(ReconnectTimeout)
}()
case _ = <-printerReconnect.C:
printerReconnect.Stop()
printerSocket, err = rpc.NewSocket(*moonrakerUrl, jar, printerRx, printerTx, wg)
if err != nil {
log.Println("Failed to connect to printer", err)
continue
}
printerReconnect.Stop()
log.Println("Connected to printer")
go func() {
<-printerSocket.C
log.Println("Reconnecting to printer")
printerReconnect.Reset(ReconnectTimeout)
}()
case <-interrupt:
break
}
}
}