Skip to content

Commit

Permalink
clean-architecture (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvek authored Jun 17, 2024
1 parent ffc53b5 commit 030e38c
Show file tree
Hide file tree
Showing 18 changed files with 304 additions and 200 deletions.
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

0 comments on commit 030e38c

Please sign in to comment.