-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (155 loc) · 3.56 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
package main
import (
"log"
"time"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/timer"
tea "github.com/charmbracelet/bubbletea"
"github.com/eclipse/paho.golang/autopaho"
zone "github.com/lrstanley/bubblezone"
zlog "github.com/rs/zerolog/log"
mpay "gitlab.com/moneropay/moneropay/v2/pkg/model"
"gitlab.com/openkiosk/proto"
)
type State int
const (
Idle State = iota
AddressIn
MoneyIn
TxInfo
)
type model struct {
timer timer.Model
broker *autopaho.ConnectionManager
state State
address string
fiat uint64
xmr uint64
fee float64
xmrPrice float64
err error
height int
width int
mpayHealth bool
tx *mpay.TransferPostResponse
textinput textinput.Model
spinner spinner.Model
}
var sub chan proto.Event
var priceUpdate chan priceEvent
var pricePause chan bool
var mpayHealthUpdate chan mpayHealthEvent
var mpayHealthPause chan bool
func waitForActivity() tea.Cmd {
return func() tea.Msg {
return <-sub
}
}
func waitForPriceUpdate() tea.Cmd {
return func() tea.Msg {
return <-priceUpdate
}
}
func waitForMpayHealthUpdate() tea.Cmd {
return func() tea.Msg {
return <-mpayHealthUpdate
}
}
func main() {
cfg = loadConfig()
priceUpdate = make(chan priceEvent)
pricePause = make(chan bool)
mpayHealthUpdate = make(chan mpayHealthEvent)
mpayHealthPause = make(chan bool)
go pricePoll(cfg.CurrencyShort, cfg.FiatEurRate)
go mpayHealthPoll()
zone.NewGlobal()
p := tea.NewProgram(InitialModel(), tea.WithMouseCellMotion())
if _, err := p.Run(); err != nil {
zlog.Fatal().Err(err)
}
}
func InitialModel() model {
ti := textinput.New()
ti.Placeholder = "..."
ti.CharLimit = 95
ti.Width = 95
ti.Focus()
sub = make(chan proto.Event)
xp, err := getXmrPrice(cfg.CurrencyShort, cfg.FiatEurRate)
if err != nil {
log.Fatal("Failed to get XMR price: ", err)
}
healthy := false
health, err := mpayHealth()
if err != nil {
log.Fatal("Failed to get MoneroPay health status: ", err)
}
if health.Status == 200 {
healthy = true
}
m := model{
timer: timer.NewWithInterval(cfg.StateTimeout, time.Second),
broker: connectToBroker(),
state: Idle,
textinput: ti,
xmrPrice: xp,
mpayHealth: healthy,
}
m.spinner = spinner.New()
m.spinner.Style = spinnerStyle
m.spinner.Spinner = spinner.Pulse
return m
}
func (m model) Init() tea.Cmd {
m.state = Idle
cmd(m.broker, "codescannerd", "start")
return tea.Batch(tea.EnterAltScreen,
waitForActivity(),
waitForPriceUpdate(),
waitForMpayHealthUpdate())
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
textStyleCentered.Width(msg.Width)
m.width = msg.Width
m.height = msg.Height
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC, tea.KeyEsc:
brokerDisconnect(m.broker)
return m, tea.Quit
}
case priceEvent:
zlog.Info().Float64("rate", float64(msg)).Msg("Got price update!")
m.xmrPrice = float64(msg)
return m, waitForPriceUpdate()
case mpayHealthEvent:
m.mpayHealth = bool(msg)
return m, waitForMpayHealthUpdate()
}
switch m.state {
case Idle:
return m.IdleUpdate(msg)
case AddressIn:
return m.AddressInUpdate(msg)
case MoneyIn:
return m.MoneyInUpdate(msg)
case TxInfo:
return m.TxInfoUpdate(msg)
}
return m, nil
}
func (m model) View() string {
switch m.state {
case AddressIn:
return zone.Scan(AddressInView(m))
case MoneyIn:
return zone.Scan(MoneyInView(m))
case TxInfo:
return zone.Scan(TxInfoView(m))
}
return IdleView(m)
}