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"`
|
2018-09-20 15:43:34 +02:00
|
|
|
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
|
2018-09-19 17:13:45 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-09-19 17:13:45 +02:00
|
|
|
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)
|
|
|
|
}
|
2018-09-19 17:13:45 +02:00
|
|
|
u.logf("'%s' event sent", eventUpdate)
|
2018-09-19 16:30:37 +02:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2018-09-19 17:13:45 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|