Skip to content

Commit 9e8ada3

Browse files
committed
cleanup: ota state
1 parent 2447e8f commit 9e8ada3

File tree

6 files changed

+84
-56
lines changed

6 files changed

+84
-56
lines changed

internal/ota/app.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ func (s *State) updateApp(ctx context.Context, appUpdate *componentUpdateStatus)
2727

2828
l := s.l.With().Str("path", appUpdatePath).Logger()
2929

30-
if err := s.downloadFile(ctx, appUpdatePath, appUpdate.url, &appUpdate.downloadProgress); err != nil {
30+
if err := s.downloadFile(ctx, appUpdatePath, appUpdate.url, "app"); err != nil {
3131
return s.componentUpdateError("Error downloading app update", err, &l)
3232
}
3333

3434
downloadFinished := time.Now()
3535
appUpdate.downloadFinishedAt = downloadFinished
3636
appUpdate.downloadProgress = 1
37-
s.triggerStateUpdate()
37+
s.triggerComponentUpdateState("app", appUpdate)
3838

3939
if err := s.verifyFile(
4040
appUpdatePath,
@@ -48,7 +48,7 @@ func (s *State) updateApp(ctx context.Context, appUpdate *componentUpdateStatus)
4848
appUpdate.verificationProgress = 1
4949
appUpdate.updatedAt = verifyFinished
5050
appUpdate.updateProgress = 1
51-
s.triggerStateUpdate()
51+
s.triggerComponentUpdateState("app", appUpdate)
5252

5353
l.Info().Msg("App update downloaded")
5454

internal/ota/ota.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ func (s *State) triggerStateUpdate() {
9292
s.onStateUpdate(s.ToRPCState())
9393
}
9494

95+
func (s *State) triggerComponentUpdateState(component string, update *componentUpdateStatus) {
96+
s.componentUpdateStatuses[component] = *update
97+
s.triggerStateUpdate()
98+
}
99+
95100
func (s *State) doUpdate(ctx context.Context, params UpdateParams) error {
96101
scopedLogger := s.l.With().
97102
Interface("params", params).
@@ -102,32 +107,35 @@ func (s *State) doUpdate(ctx context.Context, params UpdateParams) error {
102107
return fmt.Errorf("update already in progress")
103108
}
104109

105-
s.updating = true
106-
s.triggerStateUpdate()
107-
108-
defer func() {
109-
s.updating = false
110+
if !params.CheckOnly {
111+
s.updating = true
110112
s.triggerStateUpdate()
111-
}()
113+
defer func() {
114+
s.updating = false
115+
s.triggerStateUpdate()
116+
}()
117+
}
112118

113119
appUpdate, systemUpdate, err := s.getUpdateStatus(ctx, params)
114120
if err != nil {
115121
return s.componentUpdateError("Error checking for updates", err, &scopedLogger)
116122
}
117123

124+
s.metadataFetchedAt = time.Now()
125+
s.triggerStateUpdate()
126+
118127
if params.CheckOnly {
119128
return nil
120129
}
121130

122-
s.metadataFetchedAt = time.Now()
123-
s.triggerStateUpdate()
124-
125131
if appUpdate.available || appUpdate.downgradeAvailable {
126132
appUpdate.pending = true
133+
s.triggerComponentUpdateState("app", appUpdate)
127134
}
128135

129136
if systemUpdate.available || systemUpdate.downgradeAvailable {
130137
systemUpdate.pending = true
138+
s.triggerComponentUpdateState("system", systemUpdate)
131139
}
132140

133141
if appUpdate.pending {

internal/ota/state.go

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,25 @@ type componentUpdateStatus struct {
6767

6868
// RPCState represents the current OTA state for the RPC API
6969
type RPCState struct {
70-
Updating bool `json:"updating"`
71-
Error string `json:"error,omitempty"`
72-
MetadataFetchedAt time.Time `json:"metadataFetchedAt,omitempty"`
73-
AppUpdatePending bool `json:"appUpdatePending"`
74-
SystemUpdatePending bool `json:"systemUpdatePending"`
75-
AppDownloadProgress float32 `json:"appDownloadProgress,omitempty"` //TODO: implement for progress bar
76-
AppDownloadFinishedAt time.Time `json:"appDownloadFinishedAt,omitempty"`
77-
SystemDownloadProgress float32 `json:"systemDownloadProgress,omitempty"` //TODO: implement for progress bar
78-
SystemDownloadFinishedAt time.Time `json:"systemDownloadFinishedAt,omitempty"`
79-
AppVerificationProgress float32 `json:"appVerificationProgress,omitempty"`
80-
AppVerifiedAt time.Time `json:"appVerifiedAt,omitempty"`
81-
SystemVerificationProgress float32 `json:"systemVerificationProgress,omitempty"`
82-
SystemVerifiedAt time.Time `json:"systemVerifiedAt,omitempty"`
83-
AppUpdateProgress float32 `json:"appUpdateProgress,omitempty"` //TODO: implement for progress bar
84-
AppUpdatedAt time.Time `json:"appUpdatedAt,omitempty"`
85-
SystemUpdateProgress float32 `json:"systemUpdateProgress,omitempty"` //TODO: port rk_ota, then implement
86-
SystemUpdatedAt time.Time `json:"systemUpdatedAt,omitempty"`
87-
SystemTargetVersion string `json:"systemTargetVersion,omitempty"`
88-
AppTargetVersion string `json:"appTargetVersion,omitempty"`
70+
Updating bool `json:"updating"`
71+
Error string `json:"error,omitempty"`
72+
MetadataFetchedAt *time.Time `json:"metadataFetchedAt,omitempty"`
73+
AppUpdatePending bool `json:"appUpdatePending"`
74+
SystemUpdatePending bool `json:"systemUpdatePending"`
75+
AppDownloadProgress *float32 `json:"appDownloadProgress,omitempty"` //TODO: implement for progress bar
76+
AppDownloadFinishedAt *time.Time `json:"appDownloadFinishedAt,omitempty"`
77+
SystemDownloadProgress *float32 `json:"systemDownloadProgress,omitempty"` //TODO: implement for progress bar
78+
SystemDownloadFinishedAt *time.Time `json:"systemDownloadFinishedAt,omitempty"`
79+
AppVerificationProgress *float32 `json:"appVerificationProgress,omitempty"`
80+
AppVerifiedAt *time.Time `json:"appVerifiedAt,omitempty"`
81+
SystemVerificationProgress *float32 `json:"systemVerificationProgress,omitempty"`
82+
SystemVerifiedAt *time.Time `json:"systemVerifiedAt,omitempty"`
83+
AppUpdateProgress *float32 `json:"appUpdateProgress,omitempty"` //TODO: implement for progress bar
84+
AppUpdatedAt *time.Time `json:"appUpdatedAt,omitempty"`
85+
SystemUpdateProgress *float32 `json:"systemUpdateProgress,omitempty"` //TODO: port rk_ota, then implement
86+
SystemUpdatedAt *time.Time `json:"systemUpdatedAt,omitempty"`
87+
SystemTargetVersion *string `json:"systemTargetVersion,omitempty"`
88+
AppTargetVersion *string `json:"appTargetVersion,omitempty"`
8989
}
9090

9191
// HwRebootFunc is a function that reboots the hardware
@@ -221,35 +221,48 @@ func NewState(opts Options) *State {
221221
}
222222

223223
// ToRPCState converts the State to the RPCState
224+
// probably we need a generator for this ...
224225
func (s *State) ToRPCState() *RPCState {
225226
r := &RPCState{
226227
Updating: s.updating,
227228
Error: s.error,
228-
MetadataFetchedAt: s.metadataFetchedAt,
229+
MetadataFetchedAt: &s.metadataFetchedAt,
229230
}
230231

231232
app, ok := s.componentUpdateStatuses["app"]
232233
if ok {
233234
r.AppUpdatePending = app.pending
234-
r.AppDownloadProgress = app.downloadProgress
235-
r.AppDownloadFinishedAt = app.downloadFinishedAt
236-
r.AppVerificationProgress = app.verificationProgress
237-
r.AppVerifiedAt = app.verifiedAt
238-
r.AppUpdateProgress = app.updateProgress
239-
r.AppUpdatedAt = app.updatedAt
240-
r.AppTargetVersion = app.targetVersion
235+
r.AppDownloadProgress = &app.downloadProgress
236+
if !app.downloadFinishedAt.IsZero() {
237+
r.AppDownloadFinishedAt = &app.downloadFinishedAt
238+
}
239+
r.AppVerificationProgress = &app.verificationProgress
240+
if !app.verifiedAt.IsZero() {
241+
r.AppVerifiedAt = &app.verifiedAt
242+
}
243+
r.AppUpdateProgress = &app.updateProgress
244+
if !app.updatedAt.IsZero() {
245+
r.AppUpdatedAt = &app.updatedAt
246+
}
247+
r.AppTargetVersion = &app.targetVersion
241248
}
242249

243250
system, ok := s.componentUpdateStatuses["system"]
244251
if ok {
245252
r.SystemUpdatePending = system.pending
246-
r.SystemDownloadProgress = system.downloadProgress
247-
r.SystemDownloadFinishedAt = system.downloadFinishedAt
248-
r.SystemVerificationProgress = system.verificationProgress
249-
r.SystemVerifiedAt = system.verifiedAt
250-
r.SystemUpdateProgress = system.updateProgress
251-
r.SystemUpdatedAt = system.updatedAt
252-
r.SystemTargetVersion = system.targetVersion
253+
r.SystemDownloadProgress = &system.downloadProgress
254+
if !system.downloadFinishedAt.IsZero() {
255+
r.SystemDownloadFinishedAt = &system.downloadFinishedAt
256+
}
257+
r.SystemVerificationProgress = &system.verificationProgress
258+
if !system.verifiedAt.IsZero() {
259+
r.SystemVerifiedAt = &system.verifiedAt
260+
}
261+
r.SystemUpdateProgress = &system.updateProgress
262+
if !system.updatedAt.IsZero() {
263+
r.SystemUpdatedAt = &system.updatedAt
264+
}
265+
r.SystemTargetVersion = &system.targetVersion
253266
}
254267

255268
return r

internal/ota/sys.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ func (s *State) updateSystem(ctx context.Context, systemUpdate *componentUpdateS
1717

1818
l := s.l.With().Str("path", systemUpdatePath).Logger()
1919

20-
if err := s.downloadFile(ctx, systemUpdatePath, systemUpdate.url, &systemUpdate.downloadProgress); err != nil {
20+
if err := s.downloadFile(ctx, systemUpdatePath, systemUpdate.url, "system"); err != nil {
2121
return s.componentUpdateError("Error downloading system update", err, &l)
2222
}
2323

2424
downloadFinished := time.Now()
2525
systemUpdate.downloadFinishedAt = downloadFinished
2626
systemUpdate.downloadProgress = 1
27-
s.triggerStateUpdate()
27+
s.triggerComponentUpdateState("system", systemUpdate)
2828

2929
if err := s.verifyFile(
3030
systemUpdatePath,
@@ -38,7 +38,7 @@ func (s *State) updateSystem(ctx context.Context, systemUpdate *componentUpdateS
3838
systemUpdate.verificationProgress = 1
3939
systemUpdate.updatedAt = verifyFinished
4040
systemUpdate.updateProgress = 1
41-
s.triggerStateUpdate()
41+
s.triggerComponentUpdateState("system", systemUpdate)
4242

4343
l.Info().Msg("System update downloaded")
4444

@@ -68,7 +68,7 @@ func (s *State) updateSystem(ctx context.Context, systemUpdate *componentUpdateS
6868
if systemUpdate.updateProgress > 0.99 {
6969
systemUpdate.updateProgress = 0.99
7070
}
71-
s.triggerStateUpdate()
71+
s.triggerComponentUpdateState("system", systemUpdate)
7272
case <-ctx.Done():
7373
return
7474
}
@@ -86,7 +86,7 @@ func (s *State) updateSystem(ctx context.Context, systemUpdate *componentUpdateS
8686
rkLogger.Info().Msg("rk_ota success")
8787
systemUpdate.updateProgress = 1
8888
systemUpdate.updatedAt = verifyFinished
89-
s.triggerStateUpdate()
89+
s.triggerComponentUpdateState("system", systemUpdate)
9090

9191
return nil
9292
}

internal/ota/utils.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ func syncFilesystem() error {
2525
return nil
2626
}
2727

28-
func (s *State) downloadFile(ctx context.Context, path string, url string, downloadProgress *float32) error {
28+
func (s *State) downloadFile(ctx context.Context, path string, url string, component string) error {
29+
componentUpdate, ok := s.componentUpdateStatuses[component]
30+
if !ok {
31+
return fmt.Errorf("component %s not found", component)
32+
}
33+
34+
downloadProgress := componentUpdate.downloadProgress
35+
2936
if _, err := os.Stat(path); err == nil {
3037
if err := os.Remove(path); err != nil {
3138
return fmt.Errorf("error removing existing file: %w", err)
@@ -80,9 +87,9 @@ func (s *State) downloadFile(ctx context.Context, path string, url string, downl
8087
return fmt.Errorf("error writing to file: %w", ew)
8188
}
8289
progress := float32(written) / float32(totalSize)
83-
if progress-*downloadProgress >= 0.01 {
84-
*downloadProgress = progress
85-
s.triggerStateUpdate()
90+
if progress-downloadProgress >= 0.01 {
91+
componentUpdate.downloadProgress = progress
92+
s.triggerComponentUpdateState(component, &componentUpdate)
8693
}
8794
}
8895
if er != nil {

ui/src/routes/devices.$id.settings.advanced.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export default function SettingsAdvancedRoute() {
199199
// Navigate to update page
200200
navigateTo("/settings/general/update");
201201
});
202-
}, [updateTarget, appVersion, systemVersion, devChannel, send, navigateTo]);
202+
}, [appVersion, systemVersion, devChannel, send, navigateTo]);
203203

204204
return (
205205
<div className="space-y-4">

0 commit comments

Comments
 (0)