-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapp_device.go
More file actions
329 lines (265 loc) · 8.21 KB
/
Copy pathapp_device.go
File metadata and controls
329 lines (265 loc) · 8.21 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
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
329
package main
import (
"fmt"
"strings"
"reManager/internal/debug"
"reManager/internal/support"
"reManager/internal/vellum"
rmdevice "github.com/rmitchellscott/remarkable-go/device"
"github.com/wailsapp/wails/v2/pkg/runtime"
"reManager/internal/commands"
"reManager/internal/component"
)
type XochitlStatus struct {
Running bool `json:"running"`
XoviActive bool `json:"xoviActive"`
ActiveLauncher string `json:"activeLauncher"`
CurrentLauncher string `json:"currentLauncher"`
}
type HashtabVersionStatus struct {
Installed bool `json:"installed"`
HashtabVersion string `json:"hashtabVersion"`
FirmwareVersion string `json:"firmwareVersion"`
NeedsRebuild bool `json:"needsRebuild"`
}
type TimezoneStatus struct {
DeviceTimezone string `json:"deviceTimezone"`
SavedTimezone string `json:"savedTimezone"`
NeedsUpdate bool `json:"needsUpdate"`
}
func (a *App) GetDeviceInfo() map[string]string {
a.mu.Lock()
info := make(map[string]string)
if a.client == nil {
a.mu.Unlock()
return info
}
info["machine"] = a.connectedDeviceType.DisplayName()
info["firmware"] = a.connectedFirmware
vellumClient := a.vellumClient
a.mu.Unlock()
if vellumClient != nil {
installed, _ := vellumClient.IsInstalled()
if installed {
info["vellum_installed"] = "yes"
} else {
info["vellum_installed"] = "no"
}
}
return info
}
func (a *App) GetUpdateServiceStatus() support.UpdateServiceStatus {
a.mu.Lock()
defer a.mu.Unlock()
status := support.UpdateServiceStatus{
Enabled: false,
Running: false,
}
if a.client == nil {
debug.Println("[DEBUG] GetUpdateServiceStatus: client is nil")
return status
}
output, err := a.runCommand("systemctl is-enabled update-engine.service")
debug.Printf("[DEBUG] GetUpdateServiceStatus: is-enabled output=%q, err=%v\n", output, err)
if err == nil {
status.Enabled = strings.TrimSpace(output) == "enabled"
}
output, err = a.runCommand("systemctl is-active update-engine.service")
debug.Printf("[DEBUG] GetUpdateServiceStatus: is-active output=%q, err=%v\n", output, err)
if err == nil {
status.Running = strings.TrimSpace(output) == "active"
}
output, err = a.runCommand("grep -m1 '^AutoUpdate=' /home/root/.config/remarkable/xochitl.conf")
debug.Printf("[DEBUG] GetUpdateServiceStatus: AutoUpdate output=%q, err=%v\n", output, err)
if err == nil && strings.TrimSpace(output) == "AutoUpdate=false" {
status.Enabled = false
}
debug.Printf("[DEBUG] GetUpdateServiceStatus: returning enabled=%v, running=%v\n", status.Enabled, status.Running)
return status
}
func (a *App) GetXochitlStatus() XochitlStatus {
return a.xochitlStatus(true)
}
func (a *App) GetXochitlStatusQuick() XochitlStatus {
return a.xochitlStatus(false)
}
func (a *App) xochitlStatus(includeLauncher bool) XochitlStatus {
a.mu.Lock()
defer a.mu.Unlock()
if a.client == nil {
return XochitlStatus{}
}
probe := "systemctl is-active xochitl"
if includeLauncher {
probe = fmt.Sprintf("%s; echo %[2]s; [ -x %[3]s ] && %[3]s active-launcher 2>/dev/null; echo %[2]s; [ -x %[3]s ] && %[3]s current-launcher 2>/dev/null",
probe, vellum.LauncherProbeMarker, vellum.LauncherctlBin)
}
output, _ := a.runCommand(probe)
sections := strings.Split(output, vellum.LauncherProbeMarker)
running := strings.TrimSpace(sections[0]) == "active"
activeLauncher := ""
if len(sections) > 1 {
activeLauncher = vellum.ParseLauncherName(sections[1])
}
currentLauncher := ""
if len(sections) > 2 {
currentLauncher = vellum.ParseLauncherName(sections[2])
}
xoviActive := false
if running {
_, err := a.runCommand(`tr '\0' '\n' < /proc/$(pidof xochitl)/environ | grep -q LD_PRELOAD.*xovi`)
xoviActive = err == nil
}
return XochitlStatus{
Running: running,
XoviActive: xoviActive,
ActiveLauncher: activeLauncher,
CurrentLauncher: currentLauncher,
}
}
func (a *App) CheckHashtabVersion() HashtabVersionStatus {
status := HashtabVersionStatus{}
a.mu.Lock()
if a.client == nil {
a.mu.Unlock()
return status
}
if output, err := a.runCommand("grep REMARKABLE_RELEASE_VERSION /usr/share/remarkable/update.conf"); err == nil {
if parts := strings.SplitN(output, "=", 2); len(parts) == 2 {
status.FirmwareVersion = strings.TrimSpace(parts[1])
}
} else if output, err := a.runCommand("grep IMG_VERSION /etc/os-release"); err == nil {
if parts := strings.SplitN(output, "=", 2); len(parts) == 2 {
status.FirmwareVersion = strings.Trim(strings.TrimSpace(parts[1]), "\"")
}
}
a.mu.Unlock()
checker := vellum.NewHashtabChecker(&wailsExecutor{app: a})
exists, err := checker.CheckHashtabExists()
if err != nil || !exists {
return status
}
status.Installed = true
hashtabVersion, err := checker.GetHashtabVersion()
if err != nil {
return status
}
status.HashtabVersion = hashtabVersion
status.NeedsRebuild = status.FirmwareVersion != "" &&
status.HashtabVersion != "" &&
status.FirmwareVersion != status.HashtabVersion
return status
}
func (a *App) GetDeviceTimezone() (string, error) {
a.mu.Lock()
defer a.mu.Unlock()
if a.client == nil {
return "", fmt.Errorf("not connected")
}
output, err := a.runCommand("readlink /etc/localtime | sed 's|.*/zoneinfo/||'")
if err != nil {
return "", fmt.Errorf("failed to get timezone: %w", err)
}
return strings.TrimSpace(output), nil
}
func (a *App) GetTimezoneStatus() TimezoneStatus {
status := TimezoneStatus{}
a.mu.Lock()
if a.client == nil {
a.mu.Unlock()
return status
}
output, err := a.runCommand("readlink /etc/localtime | sed 's|.*/zoneinfo/||'")
if err != nil {
a.mu.Unlock()
return status
}
status.DeviceTimezone = strings.TrimSpace(output)
deviceID := a.connectedDeviceID
a.mu.Unlock()
if deviceID != "" && a.deviceStore != nil {
device, err := a.deviceStore.Get(deviceID)
if err == nil && device.Timezone != "" {
status.SavedTimezone = device.Timezone
status.NeedsUpdate = device.Timezone != status.DeviceTimezone
}
}
return status
}
func (a *App) SaveDeviceTimezone(timezone string) error {
a.mu.Lock()
deviceID := a.connectedDeviceID
a.mu.Unlock()
if deviceID == "" {
return fmt.Errorf("no device connected")
}
if a.deviceStore == nil {
return fmt.Errorf("device store not initialized")
}
return a.deviceStore.UpdateTimezone(deviceID, timezone)
}
func (a *App) SetDeviceTimezone(timezone string, deviceType string) {
go func() {
a.mu.Lock()
if a.client == nil {
a.mu.Unlock()
runtime.EventsEmit(a.ctx, "timezone:error", "Not connected")
runtime.EventsEmit(a.ctx, "command:done", false)
return
}
a.mu.Unlock()
dev := rmdevice.Type(deviceType)
if err := a.acquireWriteableRoot(dev); err != nil {
runtime.EventsEmit(a.ctx, "timezone:error", err.Error())
runtime.EventsEmit(a.ctx, "command:done", false)
return
}
defer a.releaseWriteableRoot(dev)
cmdResults := []component.CommandResult{
{
Script: fmt.Sprintf("systemctl restart systemd-timedated && timedatectl set-timezone %s", timezone),
Description: "Set device timezone",
},
}
if dev.IsPaperPro() {
cmdResults = commands.WrapWithWriteableRoot(cmdResults, dev)
}
var operationErr error
if a.logger != nil {
a.operationLog = a.logger.StartCommandLog(a.connectedDeviceID, "set-timezone")
defer func() {
a.operationLog.WriteExitCode(operationErr)
a.operationLog.Close()
a.operationLog = nil
}()
}
for _, cmd := range cmdResults {
runtime.EventsEmit(a.ctx, "command:output", fmt.Sprintf("$ %s\n", cmd.Script))
done := make(chan bool, 1)
unsub := runtime.EventsOn(a.ctx, "command:done", func(optionalData ...interface{}) {
if len(optionalData) > 0 {
if success, ok := optionalData[0].(bool); ok {
done <- success
return
}
}
done <- false
})
a.RunCommandWithOutput(cmd.Script, cmd.RequiresPTY)
success := <-done
unsub()
if !success {
operationErr = fmt.Errorf("command failed: %s", cmd.Script)
runtime.EventsEmit(a.ctx, "timezone:error", "Failed to set timezone")
return
}
}
runtime.EventsEmit(a.ctx, "timezone:complete", timezone)
}()
}
func (a *App) GetDeviceDisplayName(machine string) string {
return a.connectedDeviceType.DisplayName()
}
func (a *App) GetDeviceArchitecture(deviceType string) string {
return commands.GetDownloadArch(a.connectedDeviceArch)
}