diff --git a/datastore/domotik/core/application.go b/datastore/domotik/core/application.go index 1c915ae..e4f26b6 100644 --- a/datastore/domotik/core/application.go +++ b/datastore/domotik/core/application.go @@ -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 } @@ -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 } @@ -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) -} diff --git a/datastore/domotik/core/model/measure.entity.go b/datastore/domotik/core/model/measure.entity.go index 133be32..1c5bae9 100644 --- a/datastore/domotik/core/model/measure.entity.go +++ b/datastore/domotik/core/model/measure.entity.go @@ -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 @@ -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 }