2018-09-19 15:35:56 +02:00
|
|
|
package reach
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Pyxis/golang-socketio"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
eventGetTimeSyncStatus = "get time sync status"
|
|
|
|
eventTimeSyncResults = "time sync status"
|
|
|
|
)
|
|
|
|
|
|
|
|
type timeSyncStatus struct {
|
|
|
|
Status bool `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimeSyncStatus returns the ReachRS module time sync status
|
2018-09-19 17:13:45 +02:00
|
|
|
func (u *Updater) TimeSyncStatus() (bool, error) {
|
2018-09-19 15:35:56 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
var synced bool
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
2018-09-19 17:13:45 +02:00
|
|
|
err = u.conn.On(eventTimeSyncResults, func(h *gosocketio.Channel, st *timeSyncStatus) {
|
2018-09-19 15:35:56 +02:00
|
|
|
synced = st.Status
|
2018-09-19 17:13:45 +02:00
|
|
|
u.conn.Off(eventTimeSyncResults)
|
2018-09-19 15:35:56 +02:00
|
|
|
wg.Done()
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrapf(err, "error while binding to '%s' event", eventTimeSyncResults)
|
|
|
|
}
|
|
|
|
|
2018-09-19 17:13:45 +02:00
|
|
|
u.logf("sending '%s' event", eventGetTimeSyncStatus)
|
|
|
|
if err = u.conn.Emit(eventGetTimeSyncStatus, nil); err != nil {
|
2018-09-19 15:35:56 +02:00
|
|
|
return false, errors.Wrapf(err, "error while emitting '%s' event", eventGetTimeSyncStatus)
|
|
|
|
}
|
2018-09-19 17:13:45 +02:00
|
|
|
u.logf("'%s' event sent", eventGetTimeSyncStatus)
|
2018-09-19 15:35:56 +02:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2018-09-19 17:13:45 +02:00
|
|
|
u.logf("time sync result: %v", synced)
|
2018-09-19 15:54:11 +02:00
|
|
|
|
2018-09-19 15:35:56 +02:00
|
|
|
return synced, err
|
|
|
|
|
|
|
|
}
|