orion/reach/update.go

56 lines
1.2 KiB
Go
Raw Normal View History

2018-09-19 16:30:37 +02:00
package reach
import (
"sync"
"forge.cadoles.com/Pyxis/golang-socketio"
"github.com/pkg/errors"
)
const (
eventUpdate = "update"
eventOPKGUpdateResult = "opkg update result"
)
2018-09-19 17:24:50 +02:00
// UpdateStatus embeds informations about update status
type UpdateStatus struct {
2018-09-19 16:30:37 +02:00
Active bool `json:"active"`
Locked bool `json:"locked"`
State string `json:"state"`
2018-09-19 16:30:37 +02:00
}
2018-09-19 17:24:50 +02:00
// Update asks the ReachRS module to start an OPKG update
func (u *Updater) Update() (*UpdateStatus, error) {
2018-09-19 16:30:37 +02:00
var err error
2018-09-19 17:24:50 +02:00
var status *UpdateStatus
2018-09-19 16:30:37 +02:00
var wg sync.WaitGroup
wg.Add(1)
2018-09-19 17:24:50 +02:00
err = u.conn.On(eventOPKGUpdateResult, func(h *gosocketio.Channel, st *UpdateStatus) {
2018-09-19 16:30:37 +02:00
status = st
u.conn.Off(eventOPKGUpdateResult)
2018-09-19 16:30:37 +02:00
wg.Done()
})
if err != nil {
return nil, errors.Wrapf(err, "error while binding to '%s' event", eventOPKGUpdateResult)
}
u.logf("sending '%s' event", eventUpdate)
if err = u.conn.Emit(eventUpdate, nil); err != nil {
2018-09-19 16:30:37 +02:00
return nil, errors.Wrapf(err, "error while emitting '%s' event", eventUpdate)
}
u.logf("'%s' event sent", eventUpdate)
2018-09-19 16:30:37 +02:00
wg.Wait()
u.logf(
2018-09-19 16:30:37 +02:00
"opkg update result: active: %v, state: %v, locked: %v",
status.Active, status.State, status.Locked,
)
return status, err
}