-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
328 lines (271 loc) · 8.48 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
flatbuffers "github.com/google/flatbuffers/go"
"github.com/gorilla/websocket"
Gateway "OpenShock/Serialization/Gateway"
Types "OpenShock/Serialization/Types"
)
// Global variable to track the program's start time
var startTime time.Time
type Config struct {
AuthToken string `json:"auth_token"`
BaseURL string `json:"base_url"`
}
const (
configFile = "config.json"
)
func initializeUptime() {
startTime = time.Now()
}
func getUptimeMs() uint64 {
return uint64(time.Since(startTime).Milliseconds())
}
func loadConfig() (*Config, error) {
file, err := os.Open(configFile)
if err != nil {
if os.IsNotExist(err) {
return &Config{BaseURL: "https://api.openshock.app"}, nil
}
return nil, err
}
defer file.Close()
var config Config
decoder := json.NewDecoder(file)
if err := decoder.Decode(&config); err != nil {
return nil, err
}
if config.BaseURL == "" {
config.BaseURL = "https://api.openshock.app"
}
return &config, nil
}
func saveConfig(config *Config) error {
file, err := os.Create(configFile)
if err != nil {
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
return encoder.Encode(config)
}
func getAuthToken(baseURL, pairingCode string) (string, error) {
url := fmt.Sprintf("%s/1/device/pair/%s", baseURL, pairingCode)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.Header.Set("Accept", "application/json")
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to retrieve token: %s", resp.Status)
}
// Try decoding as JSON object first
var jsonResponse struct {
Token string `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&jsonResponse); err == nil && jsonResponse.Token != "" {
return jsonResponse.Token, nil
}
// If JSON object decoding fails, reset the body and try decoding as plain string
resp.Body.Close()
resp, err = client.Get(url) // Re-fetch the response to reset the body
if err != nil {
return "", err
}
defer resp.Body.Close()
var token string
if err := json.NewDecoder(resp.Body).Decode(&token); err != nil {
return "", fmt.Errorf("failed to parse response: %w", err)
}
return token, nil
}
func getLCGURL(baseURL, authToken string) (string, error) {
url := fmt.Sprintf("%s/1/device/assignLCG", baseURL)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("DeviceToken", authToken)
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to retrieve LCG URL: %s", resp.Status)
}
var response struct {
Data struct {
FQDN string `json:"fqdn"`
Country string `json:"country"`
} `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return "", err
}
return response.Data.FQDN, nil
}
func buildSemVer(builder *flatbuffers.Builder, major, minor, patch uint16, build, prerelease *string) flatbuffers.UOffsetT {
buildOffset := flatbuffers.UOffsetT(0)
prereleaseOffset := flatbuffers.UOffsetT(0)
if build != nil {
buildOffset = builder.CreateString(*build)
}
if prerelease != nil {
prereleaseOffset = builder.CreateString(*prerelease)
}
Types.SemVerStart(builder)
Types.SemVerAddMajor(builder, major)
Types.SemVerAddMinor(builder, minor)
Types.SemVerAddPatch(builder, patch)
if build != nil {
Types.SemVerAddBuild(builder, buildOffset)
}
if prerelease != nil {
Types.SemVerAddPrerelease(builder, prereleaseOffset)
}
return Types.SemVerEnd(builder)
}
func buildBootStatus(builder *flatbuffers.Builder, bootType Types.FirmwareBootType, firmwareVersion flatbuffers.UOffsetT, otaUpdateID int) flatbuffers.UOffsetT {
Gateway.BootStatusStart(builder)
Gateway.BootStatusAddBootType(builder, bootType)
Gateway.BootStatusAddFirmwareVersion(builder, firmwareVersion)
Gateway.BootStatusAddOtaUpdateId(builder, int32(otaUpdateID))
return Gateway.BootStatusEnd(builder)
}
func buildPong(builder *flatbuffers.Builder, uptime uint64, rssi int32) flatbuffers.UOffsetT {
Gateway.PongStart(builder)
Gateway.PongAddUptime(builder, uptime)
Gateway.PongAddRssi(builder, rssi)
return Gateway.PongEnd(builder)
}
func buildHubToGatewayMessage(builder *flatbuffers.Builder, payloadType Gateway.HubToGatewayMessagePayload, payload flatbuffers.UOffsetT) []byte {
Gateway.HubToGatewayMessageStart(builder)
Gateway.HubToGatewayMessageAddPayloadType(builder, payloadType)
Gateway.HubToGatewayMessageAddPayload(builder, payload)
message := Gateway.HubToGatewayMessageEnd(builder)
builder.Finish(message)
return builder.FinishedBytes()
}
func sendBootStatus(conn *websocket.Conn) error {
builder := flatbuffers.NewBuilder(1024)
semver := buildSemVer(builder, 1, 0, 0, nil, nil) // Example: Major=1, Minor=0, Patch=0, no build or prerelease
bootStatus := buildBootStatus(builder, Types.FirmwareBootTypeNormal, semver, 0)
message := buildHubToGatewayMessage(builder, Gateway.HubToGatewayMessagePayloadBootStatus, bootStatus)
if err := conn.WriteMessage(websocket.BinaryMessage, message); err != nil {
return fmt.Errorf("failed to send boot status message: %w", err)
}
return nil
}
func sendPong(conn *websocket.Conn) error {
builder := flatbuffers.NewBuilder(1024)
pong := buildPong(builder, getUptimeMs(), -65)
message := buildHubToGatewayMessage(builder, Gateway.HubToGatewayMessagePayloadPong, pong)
if err := conn.WriteMessage(websocket.BinaryMessage, message); err != nil {
return fmt.Errorf("failed to send boot status message: %w", err)
}
return nil
}
func connectWebSocket(authToken, lcgURL string) error {
headers := http.Header{}
headers.Add("Firmware-Version", "1.0.0-Dummy")
headers.Add("Device-Token", authToken)
fullURL := fmt.Sprintf("wss://%s/2/ws/hub", lcgURL)
log.Printf("Connecting to WebSocket at %s", fullURL)
conn, _, err := websocket.DefaultDialer.Dial(fullURL, headers)
if err != nil {
return fmt.Errorf("failed to connect to WebSocket: %w", err)
}
defer conn.Close()
log.Println("Connected to WebSocket")
err = sendBootStatus(conn)
if err != nil {
return err
}
return handleWebSocketMessages(conn)
}
// Handle Incoming WebSocket Messages
func handleWebSocketMessages(conn *websocket.Conn) error {
for {
_, bytes, err := conn.ReadMessage()
if err != nil {
return fmt.Errorf("WebSocket read error: %w", err)
}
message := Gateway.GetRootAsGatewayToHubMessage(bytes, 0)
switch message.PayloadType() {
case Gateway.GatewayToHubMessagePayloadPing:
err := sendPong(conn)
if err != nil {
return err
}
case Gateway.GatewayToHubMessagePayloadShockerCommandList:
err := handleCommandList(message)
if err != nil {
return err
}
default:
log.Printf("Unknown message type: %d", message.PayloadType())
}
}
}
func handleCommandList(message *Gateway.GatewayToHubMessage) error {
table := flatbuffers.Table{}
if !message.Payload(&table) {
return fmt.Errorf("failed to deserialize commandlist")
}
commandlist := Gateway.ShockerCommandList{}
commandlist.Init(table.Bytes, table.Pos)
ncommands := commandlist.CommandsLength()
command := Gateway.ShockerCommand{}
for i := 0; i < ncommands; i++ {
if !commandlist.Commands(&command, i) {
return fmt.Errorf("failed to deserialize command")
}
fmt.Printf("[COMMAND] %s at %v%% for %v seconds\n", command.Type().String(), command.Intensity(), float32(command.Duration())/1000.0)
}
return nil
}
func main() {
initializeUptime()
config, err := loadConfig()
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
if config.AuthToken == "" {
fmt.Print("Enter pairing code: ")
reader := bufio.NewReader(os.Stdin)
pairingCode, _ := reader.ReadString('\n')
pairingCode = strings.TrimSpace(pairingCode)
config.AuthToken, err = getAuthToken(config.BaseURL, pairingCode)
if err != nil {
log.Fatalf("Failed to get auth token: %v", err)
}
if err := saveConfig(config); err != nil {
log.Fatalf("Failed to save config: %v", err)
}
}
lcgURL, err := getLCGURL(config.BaseURL, config.AuthToken)
if err != nil {
log.Fatalf("Failed to get LCG URL: %v", err)
}
log.Printf("Got lcgURL: %s", lcgURL)
if err := connectWebSocket(config.AuthToken, lcgURL); err != nil {
log.Fatalf("WebSocket error: %v", err)
}
}