package reach import ( "sync" "forge.cadoles.com/Pyxis/golang-socketio" "github.com/pkg/errors" ) const ( eventUpdate = "update" eventOPKGUpdateResult = "opkg update result" ) // UpdateStatus embeds informations about update status type UpdateStatus struct { Active bool `json:"active"` Locked bool `json:"locked"` State string `json:"state"` } // Update asks the ReachRS module to start an OPKG update func (u *Updater) Update() (*UpdateStatus, error) { var err error var status *UpdateStatus var wg sync.WaitGroup wg.Add(1) err = u.conn.On(eventOPKGUpdateResult, func(h *gosocketio.Channel, st *UpdateStatus) { status = st u.conn.Off(eventOPKGUpdateResult) 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 { return nil, errors.Wrapf(err, "error while emitting '%s' event", eventUpdate) } u.logf("'%s' event sent", eventUpdate) wg.Wait() u.logf( "opkg update result: active: %v, state: %v, locked: %v", status.Active, status.State, status.Locked, ) return status, err }