Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean-architecture #17

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions datastore/domotik/application.go
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
}
129 changes: 0 additions & 129 deletions datastore/domotik/domain/application.go

This file was deleted.

27 changes: 27 additions & 0 deletions datastore/domotik/domain/input.entity.go
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
}
33 changes: 33 additions & 0 deletions datastore/domotik/domain/log.entity.go
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
}
6 changes: 0 additions & 6 deletions datastore/domotik/domain/model/input.go

This file was deleted.

8 changes: 0 additions & 8 deletions datastore/domotik/domain/model/log.go

This file was deleted.

9 changes: 0 additions & 9 deletions datastore/domotik/domain/model/output.go

This file was deleted.

12 changes: 0 additions & 12 deletions datastore/domotik/domain/model/state.go

This file was deleted.

39 changes: 39 additions & 0 deletions datastore/domotik/domain/output.entity.go
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)
}
Loading
Loading