Skip to content

Commit

Permalink
should be better
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvek committed Aug 1, 2024
1 parent d494b25 commit 278a3cc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
47 changes: 24 additions & 23 deletions datastore/domotik/core/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,24 @@ func (a *Application) Process(presenter port.SummarizePresenter) error {
if a.measure.HasNewIndex() {

t := time.Now()
now := t.Unix()

a.state.ProcessNewDay(t)
a.state.ProcessNewHour(t)

output := a.summarizeSince(t.Unix())
consumptionSinceLastTime, minutesSinceTheLastIndice := a.state.GetConsumptionSinceLastTime(now, a.measure.GetLastIndex())
a.state.LastIndiceTS = now
a.state.IncHourlyConsumption(consumptionSinceLastTime)
a.state.IncDailyConsumption(consumptionSinceLastTime, a.measure.IsLowTariff())

output := *dto.NewOutput(
a.state.DailySumLow,
a.state.DailySumHigh,
a.state.HourlySum,
a.state.HourlyNbIndices,
consumptionSinceLastTime,
minutesSinceTheLastIndice)

if err := presenter.Present(output); err != nil {
return err
}
Expand All @@ -49,7 +63,15 @@ func (a *Application) Process(presenter port.SummarizePresenter) error {
}

func (a *Application) GetSummarize(presenter port.SummarizePresenter) error {
output := a.summarizeSince(a.state.LastIndiceTS)
consumptionSinceLastTime, minutesSinceTheLastIndice := a.state.GetConsumptionSinceLastTime(a.measure.GetLastTimestamp(), a.measure.GetLastIndex())
output := *dto.NewOutput(
a.state.DailySumLow,
a.state.DailySumHigh,
a.state.HourlySum,
a.state.HourlyNbIndices,
consumptionSinceLastTime,
minutesSinceTheLastIndice)

if err := presenter.Present(output); err != nil {
return err
}
Expand All @@ -75,24 +97,3 @@ func NewApplication(
logRepository: logRepository,
}, nil
}

func (a *Application) summarizeSince(timestamp int64) dto.Output {

doChanges := a.state.LastIndiceTS < timestamp

consumptionSinceLastTime, minutesSinceTheLastIndice := a.state.GetConsumptionSinceLastTime(timestamp, a.measure.GetLastIndex())

if doChanges {
a.state.LastIndiceTS = timestamp
a.state.IncHourlyConsumption(consumptionSinceLastTime)
a.state.IncDailyConsumption(consumptionSinceLastTime, a.measure.IsLowTariff())
}

return *dto.NewOutput(
a.state.DailySumLow,
a.state.DailySumHigh,
a.state.HourlySum,
a.state.HourlyNbIndices,
consumptionSinceLastTime,
minutesSinceTheLastIndice)
}
10 changes: 9 additions & 1 deletion datastore/domotik/core/model/measure.entity.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package model

import "time"

type Measure struct {
lowTariff bool
index int64
timestamp int64
}

func NewMeasure() *Measure {
return &Measure{lowTariff: false, index: 0}
return &Measure{lowTariff: false, index: 0, timestamp: 0}
}

func (i *Measure) UpdateFromLog(l Log) {
if l.name == "linky" && l.unit == "indice" {
i.index = int64(l.value)
i.timestamp = time.Now().Unix()
}
if l.name == "linky" && l.unit == "state" {
i.lowTariff = l.value == 0.0
Expand All @@ -30,6 +34,10 @@ func (i *Measure) GetLastIndex() int64 {
return i.index
}

func (i *Measure) GetLastTimestamp() int64 {
return i.timestamp
}

func (i *Measure) IsLowTariff() bool {
return i.lowTariff
}

0 comments on commit 278a3cc

Please sign in to comment.