forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wakeup sleeping cars using api or charger (evcc-io#2265)
- Loading branch information
Showing
8 changed files
with
169 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package core | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/benbjohnson/clock" | ||
) | ||
|
||
const wakeupTimeout = 30 * time.Second | ||
|
||
// Timer measures active time between start and stop events | ||
type Timer struct { | ||
sync.Mutex | ||
clck clock.Clock | ||
started time.Time | ||
} | ||
|
||
// NewTimer creates timer that can expire | ||
func NewTimer() *Timer { | ||
return &Timer{ | ||
clck: clock.New(), | ||
} | ||
} | ||
|
||
// Start starts the timer if not started already | ||
func (m *Timer) Start() { | ||
m.Lock() | ||
defer m.Unlock() | ||
|
||
if !m.started.IsZero() { | ||
return | ||
} | ||
|
||
m.started = m.clck.Now() | ||
} | ||
|
||
// Reset resets the timer | ||
func (m *Timer) Stop() { | ||
m.Lock() | ||
defer m.Unlock() | ||
|
||
m.started = time.Time{} | ||
} | ||
|
||
// Expired checks if the timer has elapsed and if resets its status | ||
func (m *Timer) Expired() bool { | ||
m.Lock() | ||
defer m.Unlock() | ||
|
||
res := !m.started.IsZero() && (m.clck.Since(m.started) >= wakeupTimeout) | ||
if res { | ||
m.started = time.Time{} | ||
} | ||
|
||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/benbjohnson/clock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTimer(t *testing.T) { | ||
at := NewTimer() | ||
|
||
clck := clock.NewMock() | ||
at.clck = clck | ||
|
||
// start | ||
at.Start() | ||
clck.Add(20 * time.Second) | ||
require.Equal(t, at.Expired(), false) | ||
|
||
// wait another 20 sec to expire the timer - this will reset the timer as well | ||
clck.Add(20 * time.Second) | ||
require.Equal(t, at.Expired(), true) | ||
|
||
// start | ||
at.Start() | ||
clck.Add(time.Minute) | ||
require.Equal(t, at.Expired(), true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters