-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
129 lines (104 loc) · 2.61 KB
/
app.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
package main
import (
"context"
"crypto/sha256"
"fmt"
"os"
"github.com/kacpermalachowski/marshal-controller/pkg/station"
"github.com/kacpermalachowski/marshal-controller/pkg/td2"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type App struct {
ctx context.Context
stationHash []byte
station station.Definition
client *td2.Client
}
func NewApp() *App {
return &App{}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
runtime.LogInfo(ctx, fmt.Sprintf("Version: %s", "test"))
}
func (a *App) domReady(ctx context.Context) {
}
func (a *App) beforeClose(ctx context.Context) (prevent bool) {
return false
}
func (a *App) shutdown(ctx context.Context) {
}
func (a *App) LoadStationFile() station.Definition {
file, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{})
if err != nil {
runtime.LogError(a.ctx, fmt.Sprint(err))
return a.station
}
data, err := os.ReadFile(file)
if err != nil {
runtime.LogError(a.ctx, fmt.Sprint(err))
return a.station
}
station, err := station.ParseStationDefinition(data)
if err != nil {
runtime.LogError(a.ctx, fmt.Sprint(err))
return a.station
}
a.station = station
a.stationHash = calculateSHA256(data)
a.client = td2.New(a.ctx, fmt.Sprintf("%x", a.stationHash))
runtime.LogInfof(a.ctx, "Loaded station file with hash: %s", a.stationHash)
return station
}
func (a *App) Connect(address string) error {
err := a.client.Connect(address)
if err != nil {
runtime.LogError(a.ctx, fmt.Sprint(err, address))
return err
}
go func() {
for {
message := <-a.client.ReadChan
if message == "" {
continue
}
runtime.LogInfo(a.ctx, fmt.Sprint(message))
runtime.EventsEmit(a.ctx, "message", message)
}
}()
return nil
}
func (a *App) Disconnect() {
a.client.Disconnect()
}
func (a *App) SetSignal(hill station.Hill, signal string) {
if !a.client.IsConnected {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Disconnected",
Message: "You cannot set signal while you're disconnected!",
})
return
}
runtime.LogInfo(a.ctx, fmt.Sprint("Setting: ", signal))
err := a.client.Write(fmt.Sprintf("%s:%s", hill.Signal, signal))
if err != nil {
runtime.LogError(a.ctx, fmt.Sprint(err))
return
}
for _, repeater := range hill.Repeaters {
err := a.client.Write(fmt.Sprintf("%s:%s", repeater, signal))
if err != nil {
runtime.LogError(a.ctx, fmt.Sprint(err))
return
}
}
}
func (a *App) GetStationHash() string {
return fmt.Sprintf("%x", a.stationHash)
}
func calculateSHA256(data []byte) []byte {
h := sha256.New()
h.Write(data)
return h.Sum(nil)
}