diff --git a/experimental/libbox/service.go b/experimental/libbox/service.go index a484c64b..b30ca22f 100644 --- a/experimental/libbox/service.go +++ b/experimental/libbox/service.go @@ -32,6 +32,8 @@ type BoxService struct { instance *box.Box pauseManager pause.Manager urlTestHistoryStorage *urltest.HistoryStorage + + servicePauseFields } func NewService(configContent string, platformInterface PlatformInterface) (*BoxService, error) { @@ -75,16 +77,6 @@ func (s *BoxService) Close() error { return s.instance.Close() } -func (s *BoxService) Sleep() { - s.pauseManager.DevicePause() - _ = s.instance.Router().ResetNetwork() -} - -func (s *BoxService) Wake() { - s.pauseManager.DeviceWake() - _ = s.instance.Router().ResetNetwork() -} - var ( _ platform.Interface = (*platformInterfaceWrapper)(nil) _ log.PlatformWriter = (*platformInterfaceWrapper)(nil) diff --git a/experimental/libbox/service_pause.go b/experimental/libbox/service_pause.go new file mode 100644 index 00000000..4a58fb50 --- /dev/null +++ b/experimental/libbox/service_pause.go @@ -0,0 +1,45 @@ +package libbox + +import ( + "sync" + "time" +) + +type servicePauseFields struct { + pauseAccess sync.Mutex + pauseTimer *time.Timer +} + +func (s *BoxService) Pause() { + s.pauseAccess.Lock() + defer s.pauseAccess.Unlock() + + if s.pauseTimer != nil { + s.pauseTimer.Stop() + } + + s.pauseTimer = time.AfterFunc(time.Minute, s.pause) +} + +func (s *BoxService) pause() { + s.pauseAccess.Lock() + defer s.pauseAccess.Unlock() + + s.pauseManager.DevicePause() + _ = s.instance.Router().ResetNetwork() + s.pauseTimer = nil +} + +func (s *BoxService) Wake() { + s.pauseAccess.Lock() + defer s.pauseAccess.Unlock() + + if s.pauseTimer != nil { + s.pauseTimer.Stop() + s.pauseTimer = nil + return + } + + s.pauseManager.DeviceWake() + _ = s.instance.Router().ResetNetwork() +}