Skip to content

Commit fd5f2d7

Browse files
author
kihamo
committed
Mega refactoring. Integration DI #2
1 parent 1438d77 commit fd5f2d7

File tree

143 files changed

+787
-813
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+787
-813
lines changed

components/boggart/bind.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55

66
"github.com/kihamo/go-workers/event"
7-
"github.com/kihamo/shadow/components/logging"
87
)
98

109
var (
@@ -36,15 +35,10 @@ type BindItem interface {
3635

3736
type BindStatusManager func() BindStatus
3837

39-
type Bind interface {
40-
Run() error
41-
SetID(string)
42-
SetStatusManager(BindStatusManager)
43-
SerialNumber() string
44-
}
38+
type Bind interface{}
4539

46-
type BindLogger interface {
47-
SetLogger(logging.Logger)
40+
type BindRunner interface {
41+
Run() error
4842
}
4943

5044
type BindHasReadinessProbe interface {

components/boggart/bind/alsa/bind.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/faiface/beep/wav"
1515
"github.com/hajimehoshi/oto"
1616
"github.com/kihamo/boggart/atomic"
17-
"github.com/kihamo/boggart/components/boggart"
1817
"github.com/kihamo/boggart/components/boggart/di"
1918
)
2019

@@ -32,8 +31,8 @@ var (
3231
)
3332

3433
type Bind struct {
35-
boggart.BindBase
3634
di.MQTTBind
35+
3736
config *Config
3837

3938
playerStatus *atomic.Int64
@@ -125,7 +124,7 @@ func (b *Bind) setPlayerStatus(status Status) {
125124
return
126125
}
127126

128-
_ = b.MQTTContainer().PublishAsync(context.Background(), b.config.TopicStateStatus, status.String())
127+
_ = b.MQTT().PublishAsync(context.Background(), b.config.TopicStateStatus, status.String())
129128
}
130129

131130
func (b *Bind) PlayerStatus() Status {

components/boggart/bind/alsa/control.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (b *Bind) SetVolume(percent int64) error {
169169

170170
b.getStream().SetVolume(percent)
171171

172-
return b.MQTTContainer().PublishAsync(context.Background(), b.config.TopicStateVolume, percent)
172+
return b.MQTT().PublishAsync(context.Background(), b.config.TopicStateVolume, percent)
173173
}
174174

175175
func (b *Bind) Mute() bool {
@@ -183,5 +183,5 @@ func (b *Bind) SetMute(mute bool) error {
183183

184184
b.getStream().SetMute(mute)
185185

186-
return b.MQTTContainer().PublishAsync(context.Background(), b.config.TopicStateMute, mute)
186+
return b.MQTT().PublishAsync(context.Background(), b.config.TopicStateMute, mute)
187187
}

components/boggart/bind/alsa/mqtt.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,38 @@ func (b *Bind) MQTTPublishes() []mqtt.Topic {
1919

2020
func (b *Bind) MQTTSubscribers() []mqtt.Subscriber {
2121
return []mqtt.Subscriber{
22-
mqtt.NewSubscriber(b.config.TopicVolume, 0, b.MQTTContainer().WrapSubscribeDeviceIsOnline(func(ctx context.Context, _ mqtt.Component, message mqtt.Message) error {
22+
mqtt.NewSubscriber(b.config.TopicVolume, 0, b.MQTT().WrapSubscribeDeviceIsOnline(func(ctx context.Context, _ mqtt.Component, message mqtt.Message) error {
2323
volume, err := strconv.ParseInt(message.String(), 10, 64)
2424
if err != nil {
2525
return err
2626
}
2727

2828
return b.SetVolume(volume)
2929
})),
30-
mqtt.NewSubscriber(b.config.TopicMute, 0, b.MQTTContainer().WrapSubscribeDeviceIsOnline(func(ctx context.Context, _ mqtt.Component, message mqtt.Message) error {
30+
mqtt.NewSubscriber(b.config.TopicMute, 0, b.MQTT().WrapSubscribeDeviceIsOnline(func(ctx context.Context, _ mqtt.Component, message mqtt.Message) error {
3131
return b.SetMute(message.IsTrue())
3232
})),
33-
mqtt.NewSubscriber(b.config.TopicPause, 0, b.MQTTContainer().WrapSubscribeDeviceIsOnline(func(ctx context.Context, _ mqtt.Component, message mqtt.Message) error {
33+
mqtt.NewSubscriber(b.config.TopicPause, 0, b.MQTT().WrapSubscribeDeviceIsOnline(func(ctx context.Context, _ mqtt.Component, message mqtt.Message) error {
3434
return b.Pause()
3535
})),
36-
mqtt.NewSubscriber(b.config.TopicStop, 0, b.MQTTContainer().WrapSubscribeDeviceIsOnline(func(ctx context.Context, _ mqtt.Component, message mqtt.Message) error {
36+
mqtt.NewSubscriber(b.config.TopicStop, 0, b.MQTT().WrapSubscribeDeviceIsOnline(func(ctx context.Context, _ mqtt.Component, message mqtt.Message) error {
3737
return b.Stop()
3838
})),
39-
mqtt.NewSubscriber(b.config.TopicPlay, 0, b.MQTTContainer().WrapSubscribeDeviceIsOnline(func(ctx context.Context, _ mqtt.Component, message mqtt.Message) error {
39+
mqtt.NewSubscriber(b.config.TopicPlay, 0, b.MQTT().WrapSubscribeDeviceIsOnline(func(ctx context.Context, _ mqtt.Component, message mqtt.Message) error {
4040
if u := message.String(); u != "" {
4141
return b.PlayFromURL(u)
4242
}
4343

4444
return b.Play()
4545
})),
46-
mqtt.NewSubscriber(b.config.TopicResume, 0, b.MQTTContainer().WrapSubscribeDeviceIsOnline(func(ctx context.Context, _ mqtt.Component, message mqtt.Message) error {
46+
mqtt.NewSubscriber(b.config.TopicResume, 0, b.MQTT().WrapSubscribeDeviceIsOnline(func(ctx context.Context, _ mqtt.Component, message mqtt.Message) error {
4747
if u := message.String(); u != "" {
4848
return b.PlayFromURL(u)
4949
}
5050

5151
return b.Play()
5252
})),
53-
mqtt.NewSubscriber(b.config.TopicAction, 0, b.MQTTContainer().WrapSubscribeDeviceIsOnline(func(ctx context.Context, _ mqtt.Component, message mqtt.Message) error {
53+
mqtt.NewSubscriber(b.config.TopicAction, 0, b.MQTT().WrapSubscribeDeviceIsOnline(func(ctx context.Context, _ mqtt.Component, message mqtt.Message) error {
5454
action := message.String()
5555

5656
switch strings.ToLower(action) {

components/boggart/bind/alsa/type.go

-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,5 @@ func (t Type) CreateBind(c interface{}) (boggart.Bind, error) {
4242
mute: atomic.NewBoolDefault(config.Mute),
4343
}
4444

45-
bind.SetSerialNumber(sn)
46-
4745
return bind, nil
4846
}

components/boggart/bind/astro/sun/bind.go

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package sun
33
import (
44
"time"
55

6-
"github.com/kihamo/boggart/components/boggart"
76
"github.com/kihamo/boggart/components/boggart/di"
87
"github.com/kihamo/go-workers/task"
98
"github.com/mourner/suncalc-go"
@@ -14,7 +13,6 @@ const (
1413
)
1514

1615
type Bind struct {
17-
boggart.BindBase
1816
di.MQTTBind
1917
di.WorkersBind
2018

components/boggart/bind/astro/sun/tasks.go

+32-32
Original file line numberDiff line numberDiff line change
@@ -22,131 +22,131 @@ func (b *Bind) Tasks() []workers.Task {
2222
func (b *Bind) taskUpdater(ctx context.Context) (_ interface{}, err error) {
2323
times := b.Times()
2424

25-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicNadir, times.Nadir); e != nil {
25+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicNadir, times.Nadir); e != nil {
2626
err = multierr.Append(err, e)
2727
}
2828

29-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicNightBeforeStart, times.NightBefore.Start); e != nil {
29+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicNightBeforeStart, times.NightBefore.Start); e != nil {
3030
err = multierr.Append(err, e)
3131
}
3232

33-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicNightBeforeEnd, times.NightBefore.End); e != nil {
33+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicNightBeforeEnd, times.NightBefore.End); e != nil {
3434
err = multierr.Append(err, e)
3535
}
3636

37-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicNightBeforeDuration, times.NightBefore.Duration); e != nil {
37+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicNightBeforeDuration, times.NightBefore.Duration); e != nil {
3838
err = multierr.Append(err, e)
3939
}
4040

41-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicAstronomicalDawnStart, times.AstronomicalDawn.Start); e != nil {
41+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicAstronomicalDawnStart, times.AstronomicalDawn.Start); e != nil {
4242
err = multierr.Append(err, e)
4343
}
4444

45-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicAstronomicalDawnEnd, times.AstronomicalDawn.End); e != nil {
45+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicAstronomicalDawnEnd, times.AstronomicalDawn.End); e != nil {
4646
err = multierr.Append(err, e)
4747
}
4848

49-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicAstronomicalDawnDuration, times.AstronomicalDawn.Duration); e != nil {
49+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicAstronomicalDawnDuration, times.AstronomicalDawn.Duration); e != nil {
5050
err = multierr.Append(err, e)
5151
}
5252

53-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicNauticalDawnStart, times.NauticalDawn.Start); e != nil {
53+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicNauticalDawnStart, times.NauticalDawn.Start); e != nil {
5454
err = multierr.Append(err, e)
5555
}
5656

57-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicNauticalDawnEnd, times.NauticalDawn.End); e != nil {
57+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicNauticalDawnEnd, times.NauticalDawn.End); e != nil {
5858
err = multierr.Append(err, e)
5959
}
6060

61-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicNauticalDawnDuration, times.NauticalDawn.Duration); e != nil {
61+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicNauticalDawnDuration, times.NauticalDawn.Duration); e != nil {
6262
err = multierr.Append(err, e)
6363
}
6464

65-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicCivilDawnStart, times.CivilDawn.Start); e != nil {
65+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicCivilDawnStart, times.CivilDawn.Start); e != nil {
6666
err = multierr.Append(err, e)
6767
}
6868

69-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicCivilDawnEnd, times.CivilDawn.End); e != nil {
69+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicCivilDawnEnd, times.CivilDawn.End); e != nil {
7070
err = multierr.Append(err, e)
7171
}
7272

73-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicCivilDawnDuration, times.CivilDawn.Duration); e != nil {
73+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicCivilDawnDuration, times.CivilDawn.Duration); e != nil {
7474
err = multierr.Append(err, e)
7575
}
7676

77-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicRiseStart, times.Sunrise.Start); e != nil {
77+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicRiseStart, times.Sunrise.Start); e != nil {
7878
err = multierr.Append(err, e)
7979
}
8080

81-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicRiseEnd, times.Sunrise.End); e != nil {
81+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicRiseEnd, times.Sunrise.End); e != nil {
8282
err = multierr.Append(err, e)
8383
}
8484

85-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicRiseDuration, times.Sunrise.Duration); e != nil {
85+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicRiseDuration, times.Sunrise.Duration); e != nil {
8686
err = multierr.Append(err, e)
8787
}
8888

89-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicSolarNoon, times.SolarNoon); e != nil {
89+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicSolarNoon, times.SolarNoon); e != nil {
9090
err = multierr.Append(err, e)
9191
}
9292

93-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicSetStart, times.Sunset.Start); e != nil {
93+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicSetStart, times.Sunset.Start); e != nil {
9494
err = multierr.Append(err, e)
9595
}
9696

97-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicSetEnd, times.Sunset.Start); e != nil {
97+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicSetEnd, times.Sunset.Start); e != nil {
9898
err = multierr.Append(err, e)
9999
}
100100

101-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicSetDuration, times.Sunset.Duration); e != nil {
101+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicSetDuration, times.Sunset.Duration); e != nil {
102102
err = multierr.Append(err, e)
103103
}
104104

105-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicCivilDuskStart, times.CivilDusk.Start); e != nil {
105+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicCivilDuskStart, times.CivilDusk.Start); e != nil {
106106
err = multierr.Append(err, e)
107107
}
108108

109-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicCivilDuskEnd, times.CivilDusk.Start); e != nil {
109+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicCivilDuskEnd, times.CivilDusk.Start); e != nil {
110110
err = multierr.Append(err, e)
111111
}
112112

113-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicCivilDuskDuration, times.CivilDusk.Duration); e != nil {
113+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicCivilDuskDuration, times.CivilDusk.Duration); e != nil {
114114
err = multierr.Append(err, e)
115115
}
116116

117-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicNauticalDuskStart, times.NauticalDusk.Start); e != nil {
117+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicNauticalDuskStart, times.NauticalDusk.Start); e != nil {
118118
err = multierr.Append(err, e)
119119
}
120120

121-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicNauticalDuskEnd, times.NauticalDusk.Start); e != nil {
121+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicNauticalDuskEnd, times.NauticalDusk.Start); e != nil {
122122
err = multierr.Append(err, e)
123123
}
124124

125-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicNauticalDuskDuration, times.NauticalDusk.Duration); e != nil {
125+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicNauticalDuskDuration, times.NauticalDusk.Duration); e != nil {
126126
err = multierr.Append(err, e)
127127
}
128128

129-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicAstronomicalDuskStart, times.AstronomicalDusk.Start); e != nil {
129+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicAstronomicalDuskStart, times.AstronomicalDusk.Start); e != nil {
130130
err = multierr.Append(err, e)
131131
}
132132

133-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicAstronomicalDuskEnd, times.AstronomicalDusk.Start); e != nil {
133+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicAstronomicalDuskEnd, times.AstronomicalDusk.Start); e != nil {
134134
err = multierr.Append(err, e)
135135
}
136136

137-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicAstronomicalDuskDuration, times.AstronomicalDusk.Duration); e != nil {
137+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicAstronomicalDuskDuration, times.AstronomicalDusk.Duration); e != nil {
138138
err = multierr.Append(err, e)
139139
}
140140

141-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicNightAfterStart, times.NightAfter.Start); e != nil {
141+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicNightAfterStart, times.NightAfter.Start); e != nil {
142142
err = multierr.Append(err, e)
143143
}
144144

145-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicNightAfterEnd, times.NightAfter.End); e != nil {
145+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicNightAfterEnd, times.NightAfter.End); e != nil {
146146
err = multierr.Append(err, e)
147147
}
148148

149-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicNightAfterDuration, times.NightAfter.Duration); e != nil {
149+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicNightAfterDuration, times.NightAfter.Duration); e != nil {
150150
err = multierr.Append(err, e)
151151
}
152152

components/boggart/bind/astro/sun/type.go

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ func (t Type) CreateBind(c interface{}) (boggart.Bind, error) {
4646
bind := &Bind{
4747
config: config,
4848
}
49-
bind.SetSerialNumber(config.Name)
5049

5150
return bind, nil
5251
}

components/boggart/bind/boggart/bind.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ package boggart
33
import (
44
"context"
55

6-
"github.com/kihamo/boggart/components/boggart"
76
"github.com/kihamo/boggart/components/boggart/di"
87
"go.uber.org/multierr"
98
)
109

1110
type Bind struct {
12-
boggart.BindBase
1311
di.MQTTBind
1412

1513
config *Config
@@ -18,15 +16,15 @@ type Bind struct {
1816
func (b *Bind) Run() (err error) {
1917
ctx := context.Background()
2018

21-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicName, b.config.ApplicationName); e != nil {
19+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicName, b.config.ApplicationName); e != nil {
2220
err = multierr.Append(err, e)
2321
}
2422

25-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicVersion, b.config.ApplicationVersion); e != nil {
23+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicVersion, b.config.ApplicationVersion); e != nil {
2624
err = multierr.Append(err, e)
2725
}
2826

29-
if e := b.MQTTContainer().PublishAsync(ctx, b.config.TopicBuild, b.config.ApplicationBuild); e != nil {
27+
if e := b.MQTT().PublishAsync(ctx, b.config.TopicBuild, b.config.ApplicationBuild); e != nil {
3028
err = multierr.Append(err, e)
3129
}
3230

components/boggart/bind/broadlink/rm/bind.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package rm
33
import (
44
"context"
55

6-
"github.com/kihamo/boggart/components/boggart"
76
"github.com/kihamo/boggart/components/boggart/di"
87
"github.com/kihamo/boggart/providers/broadlink"
98
)
109

1110
type Bind struct {
12-
boggart.BindBase
1311
di.MQTTBind
12+
1413
config *ConfigRM
1514

1615
provider interface{}
@@ -34,5 +33,5 @@ type SupportRF433Mhz interface {
3433
}
3534

3635
func (b *Bind) Run() error {
37-
return b.MQTTContainer().PublishAsync(context.Background(), b.config.TopicCaptureState, false)
36+
return b.MQTT().PublishAsync(context.Background(), b.config.TopicCaptureState, false)
3837
}

0 commit comments

Comments
 (0)