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 94e7421
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 61 deletions.
39 changes: 11 additions & 28 deletions datastore/domotik/core/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (a *Application) AddLog(elements []string, value float64) error {
return err
}

a.measure.UpdateFromLog(log)
a.measure.UpdateFromLog(log, time.Now().Unix())

return nil
}
Expand All @@ -30,15 +30,18 @@ func (a *Application) Process(presenter port.SummarizePresenter) error {
if a.measure.HasNewIndex() {

t := time.Now()
a.state.ProcessNewDay(t)
a.state.ProcessNewHour(t)

output := a.summarizeSince(t.Unix())
if err := presenter.Present(output); err != nil {
a.state.ProcessNewDay(t.Day())
a.state.ProcessNewHour(t.Hour())
a.state.ProcessLastIndice(t.Unix(), a.measure)

if err := a.stateRepository.Store(a.state); err != nil {
return err
}

if err := a.stateRepository.Store(a.state); err != nil {
output := *dto.NewOutputFromState(a.state)

if err := presenter.Present(output); err != nil {
return err
}

Expand All @@ -49,7 +52,8 @@ func (a *Application) Process(presenter port.SummarizePresenter) error {
}

func (a *Application) GetSummarize(presenter port.SummarizePresenter) error {
output := a.summarizeSince(a.state.LastIndiceTS)
output := *dto.NewOutputFromState(a.state)

if err := presenter.Present(output); err != nil {
return err
}
Expand All @@ -75,24 +79,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)
}
11 changes: 10 additions & 1 deletion datastore/domotik/core/dto/output.dto.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dto

import "github.com/sylvek/domotik/datastore/core/model"

type Output struct {
WattPerHourForLastMinute float64
WattPerHourForThisHour int64
Expand All @@ -8,11 +10,18 @@ type Output struct {
RatioLowTariffToday float64
}

func NewOutput(dailySumLow int64, dailySumHigh int64, hourlySum int64, hourlyNbIndices int64, consumptionSinceLastTime int64, minutesSinceTheLastIndice float64) *Output {
func NewOutputFromState(state model.State) *Output {

HIGH_TARIFF_PRICE := 0.0001963
LOW_TARIFF_PRICE := 0.0001457

dailySumHigh := state.DailySumHigh
dailySumLow := state.DailySumLow
hourlySum := state.HourlySum
hourlyNbIndices := state.HourlyNbIndices
consumptionSinceLastTime := state.GetConsumptionSinceLastTime()
minutesSinceTheLastIndice := state.GetMinutesSinceTheLastIndice()

ratioLowTariffToday := 1.0
if dailySumHigh > 0 {
ratioLowTariffToday = float64(dailySumLow) / float64(dailySumHigh+dailySumLow)
Expand Down
10 changes: 8 additions & 2 deletions datastore/domotik/core/model/measure.entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package model
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) {
func (i *Measure) UpdateFromLog(l Log, timestamp int64) {
if l.name == "linky" && l.unit == "indice" {
i.index = int64(l.value)
i.timestamp = timestamp
}
if l.name == "linky" && l.unit == "state" {
i.lowTariff = l.value == 0.0
Expand All @@ -30,6 +32,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
}
71 changes: 41 additions & 30 deletions datastore/domotik/core/model/state.entity.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package model

import "time"

type State struct {
LastIndice int64
LastIndiceTS int64
CurrentDay int
CurrentHour int
DailySumHigh int64
DailySumLow int64
HourlySum int64
HourlyNbIndices int64
LastIndice int64
LastIndiceTS int64
CurrentDay int
CurrentHour int
DailySumHigh int64
DailySumLow int64
HourlySum int64
HourlyNbIndices int64
consumptionSinceLastTime int64
minutesSinceTheLastIndice float64
}

func NewState() *State {
Expand All @@ -25,48 +25,59 @@ func NewState() *State {
}
}

func (s *State) GetConsumptionSinceLastTime(now int64, newIndice int64) (int64, float64) {
consumptionSinceLastTime := int64(0)
if s.LastIndice > 0 {
consumptionSinceLastTime = newIndice - s.LastIndice
}
s.LastIndice = newIndice
func (s *State) GetConsumptionSinceLastTime() int64 {
return s.consumptionSinceLastTime
}

func (s *State) GetMinutesSinceTheLastIndice() float64 {
return s.minutesSinceTheLastIndice
}

minutesSinceTheLastIndice := float64(1)
if consumptionSinceLastTime > 0 {
if s.LastIndiceTS > 0 {
minutesSinceTheLastIndice = float64(now-s.LastIndiceTS) / 60
func (s *State) setConsumptionSinceLastTime(now int64, newIndice int64) {
if s.LastIndice > 0 && s.LastIndiceTS > 0 {
diff := newIndice - s.LastIndice
if diff > 0 {
s.consumptionSinceLastTime = diff
s.minutesSinceTheLastIndice = float64(now-s.LastIndiceTS) / 60
}
}

return consumptionSinceLastTime, minutesSinceTheLastIndice
s.LastIndice = newIndice
s.LastIndiceTS = now
}

func (s *State) ProcessNewDay(t time.Time) {
if t.Day() != s.CurrentDay {
s.CurrentDay = t.Day()
func (s *State) ProcessNewDay(currentDay int) {
if currentDay != s.CurrentDay {
s.CurrentDay = currentDay
s.DailySumHigh = 0
s.DailySumLow = 0
}
}

func (s *State) ProcessNewHour(t time.Time) {
if t.Hour() != s.CurrentHour {
s.CurrentHour = t.Hour()
func (s *State) ProcessNewHour(currentHour int) {
if currentHour != s.CurrentHour {
s.CurrentHour = currentHour
s.HourlyNbIndices = 0
s.HourlySum = 0
}
}

func (s *State) IncHourlyConsumption(wattConsumedDuringBuffering int64) {
func (s *State) incHourlyConsumption(wattConsumedDuringBuffering int64) {
s.HourlySum += wattConsumedDuringBuffering
s.HourlyNbIndices += 1
}

func (s *State) IncDailyConsumption(wattConsumedDuringBuffering int64, lowTariff bool) {
func (s *State) incDailyConsumption(wattConsumedDuringBuffering int64, lowTariff bool) {
if lowTariff {
s.DailySumLow += wattConsumedDuringBuffering
} else {
s.DailySumHigh += wattConsumedDuringBuffering
}
}

func (s *State) ProcessLastIndice(now int64, measure Measure) {
newIndice := measure.GetLastIndex()
isLowTariff := measure.IsLowTariff()
s.setConsumptionSinceLastTime(now, newIndice)
s.incHourlyConsumption(s.consumptionSinceLastTime)
s.incDailyConsumption(s.consumptionSinceLastTime, isLowTariff)
}

0 comments on commit 94e7421

Please sign in to comment.