2019-02-12 11:12:50 +01:00
|
|
|
package emlid
|
2018-09-19 17:13:45 +02:00
|
|
|
|
|
|
|
import (
|
2018-09-26 12:05:55 +02:00
|
|
|
"context"
|
2018-09-20 11:20:35 +02:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Pyxis/golang-socketio"
|
2018-09-19 17:13:45 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
eventReboot = "reboot now"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RebootNow asks the ReachRS module to reboot now
|
2018-09-26 12:05:55 +02:00
|
|
|
func (c *Client) RebootNow(ctx context.Context, waitDisconnect bool) error {
|
2018-09-19 17:13:45 +02:00
|
|
|
|
|
|
|
var err error
|
2018-09-20 11:20:35 +02:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
if waitDisconnect {
|
2018-09-26 12:05:55 +02:00
|
|
|
|
|
|
|
var once sync.Once
|
|
|
|
|
|
|
|
done := func() {
|
2018-09-21 12:31:52 +02:00
|
|
|
c.Off(gosocketio.OnDisconnection)
|
2018-09-20 11:20:35 +02:00
|
|
|
wg.Done()
|
2018-09-26 12:05:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2018-09-20 11:20:35 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error while binding to '%s' event", gosocketio.OnDisconnection)
|
|
|
|
}
|
|
|
|
}
|
2018-09-19 17:13:45 +02:00
|
|
|
|
2018-09-21 12:31:52 +02:00
|
|
|
if err = c.Emit(eventReboot, nil); err != nil {
|
|
|
|
return err
|
2018-09-19 17:13:45 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 11:20:35 +02:00
|
|
|
if waitDisconnect {
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2018-09-19 17:13:45 +02:00
|
|
|
return err
|
|
|
|
|
|
|
|
}
|