57 lines
856 B
Go
57 lines
856 B
Go
package emlid
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"forge.cadoles.com/Pyxis/golang-socketio"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
const (
|
|
eventReboot = "reboot now"
|
|
)
|
|
|
|
// RebootNow asks the ReachRS module to reboot now
|
|
func (c *Client) RebootNow(ctx context.Context, waitDisconnect bool) error {
|
|
|
|
var err error
|
|
var wg sync.WaitGroup
|
|
|
|
if waitDisconnect {
|
|
|
|
var once sync.Once
|
|
|
|
done := func() {
|
|
c.Off(gosocketio.OnDisconnection)
|
|
wg.Done()
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
<-ctx.Done()
|
|
err = ctx.Err()
|
|
once.Do(done)
|
|
}()
|
|
|
|
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
|
|
once.Do(done)
|
|
})
|
|
if err != nil {
|
|
return errors.Wrapf(err, "error while binding to '%s' event", gosocketio.OnDisconnection)
|
|
}
|
|
}
|
|
|
|
if err = c.Emit(eventReboot, nil); err != nil {
|
|
return err
|
|
}
|
|
|
|
if waitDisconnect {
|
|
wg.Wait()
|
|
}
|
|
|
|
return err
|
|
|
|
}
|