-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
304 additions
and
200 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/sylvek/domotik/datastore/domain" | ||
"github.com/sylvek/domotik/datastore/port" | ||
) | ||
|
||
type Application struct { | ||
input domain.Input | ||
state domain.State | ||
stateRepository port.StateRepository | ||
logRepository port.LogRepository | ||
notificationRepository port.NotificationRepository | ||
} | ||
|
||
func (a *Application) AddLog(l domain.Log) error { | ||
if err := a.logRepository.Store(l); err != nil { | ||
return err | ||
} | ||
|
||
a.input.UpdateFromLog(l) | ||
|
||
return nil | ||
} | ||
|
||
func (a *Application) Process() error { | ||
if a.input.HasIndice() { | ||
newState, output := domain.GenerateStatistics(time.Now(), a.state, a.input) | ||
|
||
if err := a.stateRepository.Store(newState); err != nil { | ||
return err | ||
} | ||
if err := a.notificationRepository.Notify(output); err != nil { | ||
return err | ||
} | ||
|
||
a.input.ResetIndice() | ||
a.state = newState | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func NewApplication( | ||
stateRepository port.StateRepository, | ||
logRepository port.LogRepository, | ||
notificationRepository port.NotificationRepository) (*Application, error) { | ||
|
||
state, err := stateRepository.Retrieve() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Application{ | ||
input: *domain.NewInput(), | ||
state: state, | ||
stateRepository: stateRepository, | ||
logRepository: logRepository, | ||
notificationRepository: notificationRepository, | ||
}, nil | ||
} |
This file was deleted.
Oops, something went wrong.
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,27 @@ | ||
package domain | ||
|
||
type Input struct { | ||
lowTariff bool | ||
indice int64 | ||
} | ||
|
||
func NewInput() *Input { | ||
return &Input{lowTariff: false, indice: 0} | ||
} | ||
|
||
func (i *Input) UpdateFromLog(l Log) { | ||
if l.name == "linky" && l.unit == "indice" { | ||
i.indice = int64(l.value) | ||
} | ||
if l.name == "linky" && l.unit == "state" { | ||
i.lowTariff = l.value == 0.0 | ||
} | ||
} | ||
|
||
func (i *Input) ResetIndice() { | ||
i.indice = 0 | ||
} | ||
|
||
func (i *Input) HasIndice() bool { | ||
return i.indice > 0 | ||
} |
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,33 @@ | ||
package domain | ||
|
||
type Log struct { | ||
topic string | ||
name string | ||
unit string | ||
value float64 | ||
} | ||
|
||
func NewLog(elements []string, value float64) *Log { | ||
return &Log{ | ||
topic: elements[0], | ||
name: elements[1], | ||
unit: elements[2], | ||
value: value, | ||
} | ||
} | ||
|
||
func (l *Log) GetTopic() string { | ||
return l.topic | ||
} | ||
|
||
func (l *Log) GetName() string { | ||
return l.name | ||
} | ||
|
||
func (l *Log) GetUnit() string { | ||
return l.unit | ||
} | ||
|
||
func (l *Log) GetValue() float64 { | ||
return l.value | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,39 @@ | ||
package domain | ||
|
||
type Output struct { | ||
wattPerHourForLastMinute float64 | ||
wattPerHourForThisHour int64 | ||
wattConsumedToday int64 | ||
euroSpentToday float64 | ||
ratioLowTariffToday float64 | ||
} | ||
|
||
func NewOutput(state State, consumptionSinceLastTime int64, minutesSinceTheLastIndice float64, ratioLowTariffToday float64) *Output { | ||
return &Output{ | ||
wattPerHourForLastMinute: float64(consumptionSinceLastTime) * 60 / minutesSinceTheLastIndice, | ||
wattPerHourForThisHour: state.HourlySum * 60 / state.HourlyNbIndices, | ||
wattConsumedToday: state.DailySumHigh + state.DailySumLow, | ||
euroSpentToday: float64(state.DailySumHigh)*0.0001963 + float64(state.DailySumLow)*0.0001457, | ||
ratioLowTariffToday: ratioLowTariffToday, | ||
} | ||
} | ||
|
||
func (o *Output) GetEuroSpentToday() float64 { | ||
return o.euroSpentToday | ||
} | ||
|
||
func (o *Output) GetRatioLowTariffToday() float64 { | ||
return o.ratioLowTariffToday | ||
} | ||
|
||
func (o *Output) GetWattConsumedToday() float64 { | ||
return float64(o.wattConsumedToday) | ||
} | ||
|
||
func (o *Output) GetWattPerHourForThisHour() float64 { | ||
return float64(o.wattPerHourForThisHour) | ||
} | ||
|
||
func (o *Output) GetWattPerHourForLastMinute() float64 { | ||
return float64(o.wattPerHourForLastMinute) | ||
} |
Oops, something went wrong.