Skip to content

Commit

Permalink
make external
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveicedgreentea committed Sep 10, 2023
1 parent 7ebe724 commit 56664e5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
12 changes: 3 additions & 9 deletions internal/ezbeq/ezbeq.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"

"github.com/iloveicedgreentea/go-plex/internal/logger"
"github.com/iloveicedgreentea/go-plex/internal/config"
"github.com/iloveicedgreentea/go-plex/models"
"github.com/iloveicedgreentea/go-plex/internal/mqtt"
)
Expand Down Expand Up @@ -88,11 +87,6 @@ func urlEncode(s string) string {
return url.QueryEscape(s)
}

func publishWrapper(topic string, msg string) error {
// trigger automation
return mqtt.Publish([]byte(msg), config.GetString(fmt.Sprintf("mqtt.%s", topic)))
}

// MuteCommand sends a mute on/off true = muted, false = not muted
func (c *BeqClient) MuteCommand(status bool) error {
log.Debug("Running mute command")
Expand Down Expand Up @@ -127,7 +121,7 @@ func (c *BeqClient) MuteCommand(status bool) error {

}

return publishWrapper("topicMinidspMuteStatus", fmt.Sprintf("%v", status))
return mqtt.PublishWrapper("topicMinidspMuteStatus", fmt.Sprintf("%v", status))
}

// MakeCommand sends the command of payload
Expand Down Expand Up @@ -379,7 +373,7 @@ func (c *BeqClient) LoadBeqProfile(m *models.SearchRequest) error {
}
}

return publishWrapper("topicBeqCurrentProfile", catalog.SortTitle)
return mqtt.PublishWrapper("topicBeqCurrentProfile", catalog.SortTitle)
}

// UnloadBeqProfile will unload all profiles from all devices
Expand All @@ -400,5 +394,5 @@ func (c *BeqClient) UnloadBeqProfile(m *models.SearchRequest) error {
}
}

return publishWrapper("topicBeqCurrentProfile", "")
return mqtt.PublishWrapper("topicBeqCurrentProfile", "")
}
17 changes: 16 additions & 1 deletion internal/handlers/plex_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func ProcessWebhook(plexChan chan<- models.PlexWebhookPayload, c *gin.Context) {
// does plex send stop if you exit with back button? - Yes, with X for mobile player as well
func mediaStop(beqClient *ezbeq.BeqClient, haClient *homeassistant.HomeAssistantClient, payload models.PlexWebhookPayload, m *models.SearchRequest) {
wg := &sync.WaitGroup{}
err := mqtt.PublishWrapper("topicplayingstatus", "false")
if err != nil {
log.Error(err)
}
wg.Add(1)
go changeLight("on", wg)

Expand All @@ -122,6 +126,10 @@ func mediaStop(beqClient *ezbeq.BeqClient, haClient *homeassistant.HomeAssistant
// pause only happens with literally pausing
func mediaPause(beqClient *ezbeq.BeqClient, haClient *homeassistant.HomeAssistantClient, payload models.PlexWebhookPayload, m *models.SearchRequest, skipActions *bool) {
if !*skipActions {
err := mqtt.PublishWrapper("topicplayingstatus", "false")
if err != nil {
log.Error(err)
}
wg := &sync.WaitGroup{}
wg.Add(1)

Expand Down Expand Up @@ -262,7 +270,10 @@ func mediaPlay(client *plex.PlexClient, beqClient *ezbeq.BeqClient, haClient *ho

// stop processing webhooks
*skipActions = true

err := mqtt.PublishWrapper("topicplayingstatus", "true")
if err != nil {
log.Error(err)
}
wg.Add(3)
go changeLight("off", wg)
go changeAspect(client, payload, wg)
Expand Down Expand Up @@ -364,6 +375,10 @@ func mediaResume(beqClient *ezbeq.BeqClient, haClient *homeassistant.HomeAssista
wg := &sync.WaitGroup{}
// mediaType string, codec string, edition string
// trigger lights
err := mqtt.PublishWrapper("topicplayingstatus", "true")
if err != nil {
log.Error(err)
}
wg.Add(1)
go changeLight("off", wg)
// Changing on resume is disabled because its annoying if you changed it since playing
Expand Down
10 changes: 8 additions & 2 deletions internal/mqtt/mqtt.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package mqtt

import (
"fmt"
"time"

mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/iloveicedgreentea/go-plex/internal/logger"
"github.com/iloveicedgreentea/go-plex/internal/config"
"github.com/iloveicedgreentea/go-plex/internal/logger"
)

var log = logger.GetLogger()
Expand All @@ -25,13 +26,18 @@ func connect(clientID string) (mqtt.Client, error) {
return c, nil
}

func PublishWrapper(topic string, msg string) error {
// trigger automation
return Publish([]byte(msg), config.GetString(fmt.Sprintf("mqtt.%s", topic)))
}

// creates a connection to broker and sends the payload
func Publish(payload []byte, topic string) error {
// use the topic as clientID so each invocation
// of Publish does not trip over each other
c, err := connect(topic)
if err != nil {
return err
return fmt.Errorf("error connecting to topic - %v", err)
}

defer c.Disconnect(5000)
Expand Down

0 comments on commit 56664e5

Please sign in to comment.