Skip to content

Commit

Permalink
chore: use time.Tick (evcc-io#17601)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Dec 5, 2024
1 parent 56e5285 commit a85fcc4
Show file tree
Hide file tree
Showing 19 changed files with 23 additions and 44 deletions.
5 changes: 2 additions & 3 deletions charger/easee.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,11 @@ func (c *Easee) ProductUpdate(i json.RawMessage) {
c.stopTicker = make(chan struct{})

go func() {
ticker := time.NewTicker(5 * time.Minute)
for {
for tick := time.Tick(5 * time.Minute); ; {
select {
case <-c.stopTicker:
return
case <-ticker.C:
case <-tick:
c.requestLifetimeEnergyUpdate()
}
}
Expand Down
5 changes: 2 additions & 3 deletions core/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,12 +1082,11 @@ func (site *Site) Run(stopC chan struct{}, interval time.Duration) {
loadpointChan := make(chan updater)
go site.loopLoadpoints(loadpointChan)

ticker := time.NewTicker(interval)
site.update(<-loadpointChan) // start immediately

for {
for tick := time.Tick(interval); ; {
select {
case <-ticker.C:
case <-tick:
site.update(<-loadpointChan)
case lp := <-site.lpUpdateChan:
site.update(lp)
Expand Down
6 changes: 2 additions & 4 deletions hems/semp/semp.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,10 @@ func (s *SEMP) Run() {
ads = append(ads, ad)
}

ticker := time.NewTicker(maxAge * time.Second / 2)

ANNOUNCE:
for {
for tick := time.Tick(maxAge * time.Second / 2); ; {
select {
case <-ticker.C:
case <-tick:
for _, ad := range ads {
if err := ad.Alive(); err != nil {
s.log.ERROR.Println(err)
Expand Down
3 changes: 1 addition & 2 deletions tariff/amber.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ func NewAmberFromConfig(other map[string]interface{}) (api.Tariff, error) {
func (t *Amber) run(done chan error) {
var once sync.Once

tick := time.NewTicker(time.Minute)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var res []amber.PriceInfo
uri := fmt.Sprintf("%s&endDate=%s", t.uri, time.Now().AddDate(0, 0, 2).Format("2006-01-02"))

Expand Down
3 changes: 1 addition & 2 deletions tariff/awattar.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ func (t *Awattar) run(done chan error) {

client := request.NewHelper(t.log)

tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var res awattar.Prices

// Awattar publishes prices for next day around 13:00 CET/CEST, so up to 35h of price data are available
Expand Down
3 changes: 1 addition & 2 deletions tariff/edf-tempo.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ func (t *EdfTempo) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) {
func (t *EdfTempo) run(done chan error) {
var once sync.Once

tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var res struct {
Data struct {
Values []struct {
Expand Down
3 changes: 1 addition & 2 deletions tariff/electricitymaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ func (t *ElectricityMaps) run(done chan error) {

uri := fmt.Sprintf("%s/carbon-intensity/forecast?zone=%s", t.uri, t.zone)

tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var res CarbonIntensity

if err := backoff.Retry(func() error {
Expand Down
3 changes: 1 addition & 2 deletions tariff/elering.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ func (t *Elering) run(done chan error) {
var once sync.Once
client := request.NewHelper(t.log)

tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var res elering.NpsPrice

ts := time.Now().Truncate(time.Hour)
Expand Down
3 changes: 1 addition & 2 deletions tariff/energinet.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ func (t *Energinet) run(done chan error) {
var once sync.Once
client := request.NewHelper(t.log)

tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var res energinet.Prices

ts := time.Now().Truncate(time.Hour)
Expand Down
3 changes: 1 addition & 2 deletions tariff/entsoe.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ func (t *Entsoe) run(done chan error) {
var once sync.Once

// Data updated by ESO every half hour, but we only need data every hour to stay current.
tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var tr entsoe.PublicationMarketDocument

if err := backoff.Retry(func() error {
Expand Down
3 changes: 1 addition & 2 deletions tariff/groupe-e.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ func (t *GroupeE) run(done chan error) {

client := request.NewHelper(t.log)

tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var res []struct {
StartTimestamp time.Time `json:"start_timestamp"`
EndTimestamp time.Time `json:"end_timestamp"`
Expand Down
3 changes: 1 addition & 2 deletions tariff/gruenstromindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ func (t *GrünStromIndex) run(done chan error) {

uri := fmt.Sprintf("https://api.corrently.io/v2.0/gsi/prediction?zip=%s", t.zip)

tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var res gsiForecast

err := backoff.Retry(func() error {
Expand Down
3 changes: 1 addition & 2 deletions tariff/ngeso.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ func (t *Ngeso) run(done chan error) {
}

// Data updated by ESO every half hour, but we only need data every hour to stay current.
tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
res, err := backoff.RetryWithData(func() (ngeso.CarbonForecastResponse, error) {
res, err := tReq.DoRequest(client)
return res, backoffPermanentError(err)
Expand Down
3 changes: 1 addition & 2 deletions tariff/octopus.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ func (t *Octopus) run(done chan error) {
}

// TODO tick every 15 minutes if GraphQL is available to poll for Intelligent slots.
tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var res octoRest.UnitRates

if err := backoff.Retry(func() error {
Expand Down
6 changes: 2 additions & 4 deletions tariff/ostrom.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ func (t *Ostrom) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) {
func (t *Ostrom) runStatic(done chan error) {
var once sync.Once

tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
price, err := t.getFixedPrice()
if err != nil {
once.Do(func() { done <- err })
Expand Down Expand Up @@ -205,8 +204,7 @@ func (t *Ostrom) runStatic(done chan error) {
func (t *Ostrom) run(done chan error) {
var once sync.Once

tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var res ostrom.Prices

start := now.BeginningOfDay()
Expand Down
3 changes: 1 addition & 2 deletions tariff/pun.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ func NewPunFromConfig(other map[string]interface{}) (api.Tariff, error) {
func (t *Pun) run(done chan error) {
var once sync.Once

tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var today api.Rates
if err := backoff.Retry(func() error {
var err error
Expand Down
3 changes: 1 addition & 2 deletions tariff/smartenergy.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ func (t *SmartEnergy) run(done chan error) {
var once sync.Once
client := request.NewHelper(t.log)

tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var res smartenergy.Prices

if err := backoff.Retry(func() error {
Expand Down
3 changes: 1 addition & 2 deletions tariff/tariff.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ func NewConfigurableFromConfig(ctx context.Context, other map[string]interface{}
func (t *Tariff) run(forecastG func() (string, error), done chan error) {
var once sync.Once

tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var data api.Rates
if err := backoff.Retry(func() error {
s, err := forecastG()
Expand Down
3 changes: 1 addition & 2 deletions tariff/tibber.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ func (t *Tibber) run(done chan error) {
"id": graphql.ID(t.homeID),
}

tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
for tick := time.Tick(time.Hour); ; <-tick {
var res struct {
Viewer struct {
Home struct {
Expand Down

0 comments on commit a85fcc4

Please sign in to comment.