Skip to content

Commit 271f031

Browse files
fix hang bug, better logging, readme
1 parent 39c3ae7 commit 271f031

File tree

6 files changed

+45
-13
lines changed

6 files changed

+45
-13
lines changed

build.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker buildx build --platform linux/amd64 --tag "ghcr.io/iloveicedgreentea/plex-webhook-automation:v$1" --push .

ezbeq/ezbeq.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,17 @@ func (c *BeqClient) GetStatus() error {
6161
}
6262
log.Debugf("BEQ payload: %#v", payload)
6363

64+
log.Debugf("Len of payload is: %v", len(payload))
6465
// add devices to client, it returns as a map not list
6566
for _, v := range payload {
6667
log.Debugf("BEQ device: %#v", v.Name)
6768
c.DeviceInfo = append(c.DeviceInfo, v)
6869
}
6970

70-
if c.DeviceInfo == nil {
71+
if len(c.DeviceInfo) == 0 || c.DeviceInfo == nil {
7172
return errors.New("no devices found")
7273
}
74+
log.Debug("c.DeviceInfo is not 0")
7375

7476
return nil
7577
}

handlers/plex_handler.go

+35-6
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func ProcessWebhook(plexChan chan<- models.PlexWebhookPayload, vip *viper.Viper)
6565
return
6666
}
6767
clientUUID := decodedPayload.Player.UUID
68-
log.Debugf("!!! Your Player UUID is %s !!!!!", clientUUID)
68+
log.Infof("Got a request from UUID: %s", clientUUID)
6969

7070
log.Debugf("ProcessWebhook: Media type is: %s", decodedPayload.Metadata.Type)
7171
log.Debugf("ProcessWebhook: Media title is: %s", decodedPayload.Metadata.Title)
@@ -75,7 +75,16 @@ func ProcessWebhook(plexChan chan<- models.PlexWebhookPayload, vip *viper.Viper)
7575
// only respond to events on a particular account if you share servers and only for movies and shows
7676
if userID == "" || decodedPayload.Account.Title == userID {
7777
if decodedPayload.Metadata.Type == movieItemTitle || decodedPayload.Metadata.Type == showItemTitle {
78-
plexChan <- decodedPayload
78+
log.Debugf("Current length of plexChan: %d", len(plexChan))
79+
select {
80+
case plexChan <- decodedPayload:
81+
// send succeeded
82+
case <-time.After(time.Second * 3):
83+
log.Error("Send on plexChan timed out")
84+
http.Error(w, err.Error(), http.StatusTooManyRequests)
85+
return
86+
}
87+
log.Debugf("Added length of plexChan: %d", len(plexChan))
7988
}
8089
} else {
8190
log.Debugf("userID '%s' does not match filter", userID)
@@ -134,7 +143,13 @@ func mediaPause(vip *viper.Viper, beqClient *ezbeq.BeqClient, haClient *homeassi
134143
}
135144

136145
// waitForHDMISync will wait until the envy reports a signal to assume hdmi sync. No API to do this with denon afaik
137-
func waitForHDMISync(wg *sync.WaitGroup, skipActions *bool, haClient *homeassistant.HomeAssistantClient, PlexClient *plex.PlexClient) {
146+
func waitForHDMISync(wg *sync.WaitGroup, skipActions *bool, haClient *homeassistant.HomeAssistantClient, PlexClient *plex.PlexClient, vip *viper.Viper) {
147+
if vip.GetBool("ezbeq.waitforHDMIsync") {
148+
wg.Done()
149+
return
150+
}
151+
152+
log.Debug("Running HDMI sync wait")
138153
defer func() {
139154
// play item no matter what
140155
err := PlexClient.PlayPlex()
@@ -169,6 +184,11 @@ func waitForHDMISync(wg *sync.WaitGroup, skipActions *bool, haClient *homeassist
169184
log.Debug("HDMI sync complete")
170185
return
171186
}
187+
if err != nil {
188+
log.Errorf("Error reading envy attributes: %v", err)
189+
return
190+
}
191+
// otherwise continue
172192
time.Sleep(1 * time.Second)
173193
}
174194
if err != nil {
@@ -193,8 +213,9 @@ func mediaPlay(client *plex.PlexClient, vip *viper.Viper, beqClient *ezbeq.BeqCl
193213
// if not using denoncodec, do this in background
194214
if !useDenonCodec {
195215
wg.Add(1)
216+
// TODO: get this working
196217
// sets skipActions to false on completion
197-
go waitForHDMISync(wg, skipActions, haClient, client)
218+
go waitForHDMISync(wg, skipActions, haClient, client, vip)
198219
}
199220

200221
if vip.GetBool("ezbeq.enabled") {
@@ -209,8 +230,8 @@ func mediaPlay(client *plex.PlexClient, vip *viper.Viper, beqClient *ezbeq.BeqCl
209230
if useDenonCodec {
210231
// wait for sync
211232
wg.Add(1)
212-
waitForHDMISync(wg, skipActions, haClient, client)
213-
// denon needs time to process mutli ch in as atmos first
233+
waitForHDMISync(wg, skipActions, haClient, client, vip)
234+
// denon needs time to show mutli ch in as atmos
214235
// TODO: test this
215236
time.Sleep(5 * time.Second)
216237

@@ -454,6 +475,7 @@ func getPlexMovieDb(payload models.PlexWebhookPayload) string {
454475

455476
// will change aspect ratio
456477
func changeAspect(client *plex.PlexClient, payload models.PlexWebhookPayload, vip *viper.Viper, wg *sync.WaitGroup) {
478+
defer wg.Done()
457479
if vip.GetBool("homeAssistant.triggerAspectRatioChangeOnEvent") && vip.GetBool("homeAssistant.enabled") {
458480

459481
// if madvr enabled, only send a trigger via mqtt
@@ -497,6 +519,7 @@ func changeAspect(client *plex.PlexClient, payload models.PlexWebhookPayload, vi
497519

498520
// trigger HA for MV change per type
499521
func changeMasterVolume(vip *viper.Viper, mediaType string, wg *sync.WaitGroup) {
522+
defer wg.Done()
500523
if vip.GetBool("homeAssistant.triggerAvrMasterVolumeChangeOnEvent") && vip.GetBool("homeAssistant.enabled") {
501524
log.Debug("changeMasterVolume: Changing volume")
502525
err := mqtt.Publish(vip, []byte(fmt.Sprintf("{\"type\":\"%s\"}", mediaType)), vip.GetString("mqtt.topicVolume"))
@@ -508,6 +531,7 @@ func changeMasterVolume(vip *viper.Viper, mediaType string, wg *sync.WaitGroup)
508531

509532
// trigger HA for light change given entity and desired state
510533
func changeLight(vip *viper.Viper, state string, wg *sync.WaitGroup) {
534+
defer wg.Done()
511535
if vip.GetBool("homeAssistant.triggerLightsOnEvent") && vip.GetBool("homeAssistant.enabled") {
512536
log.Debug("changeLight: Changing light")
513537
err := mqtt.Publish(vip, []byte(fmt.Sprintf("{\"state\":\"%s\"}", state)), vip.GetString("mqtt.topicLights"))
@@ -582,7 +606,12 @@ func PlexWorker(plexChan <-chan models.PlexWebhookPayload, vip *viper.Viper) {
582606

583607
// block forever until closed so it will wait in background for work
584608
for i := range plexChan {
609+
log.Debugf("Current length of plexChan in PlexWorker: %d", len(plexChan))
585610
// determine what to do
611+
log.Debug("Sending new payload to eventRouter")
586612
eventRouter(plexClient, beqClient, haClient, denonClient, useDenonCodec, i, vip, model, skipActions)
613+
log.Debug("eventRouter done processing payload")
587614
}
615+
616+
log.Debug("Plex worker stopped")
588617
}

homeassistant/ha.go

+3
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ func (c *HomeAssistantClient) ReadEnvyAttributes() (bool, error) {
137137
// unmarshal
138138
var envyResp models.HAEnvyResponse
139139
err = json.Unmarshal(resp, &envyResp)
140+
if envyResp.State == "off" {
141+
return false, fmt.Errorf("envy state is %s", envyResp.State)
142+
}
140143

141144
return envyResp.Attributes.NoSignal, err
142145
}

main.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,19 @@ func main() {
4747
// pass the chan to the handlers
4848
plexWh := handlers.ProcessWebhook(plexChan, vip)
4949
dspWh := handlers.ProcessMinidspWebhook(minidspChan, vip)
50-
// jellyfin might one day be supported
51-
// jfWh := handlers.ProcessJellyfinWebhook(plexChan, vip)
50+
5251
// healthcheck
5352
health := handlers.ProcessHealthcheckWebhook()
5453

5554
// Add plex webhook handler
5655
mux.Handle("/plexwebhook", plexWh)
56+
5757
// minidsp
5858
mux.Handle("/minidspwebhook", dspWh)
59-
// jellyfin
60-
// mux.Handle("/jellyfinwebhook", jfWh)
6159

6260
// healthcheck
6361
mux.Handle("/health", health)
6462

65-
6663
log.Info("Starting server")
6764
err = http.ListenAndServe(fmt.Sprintf(":%s", vip.GetString("main.listenPort")), mux)
6865
log.Fatal(err)

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ create file named config.json, paste this in, remove the comments after
248248
// leave blank if you don't want to filter on accounts
249249
"ownerNameFilter": "PLEX_OWNER_NAME to filter events on",
250250
// filter based on device UUID so only the client you want triggers things, or leave blank
251-
// Must be UUID. Easy way to get it is running this in debug mode and then play a movie
251+
// Must be UUID. Easy way to get it is playing anything and searching logs for 'Got a request from UUID:'
252252
"deviceUUIDFilter": "",
253253
"url": "http://xyz",
254254
"port": "32400",

0 commit comments

Comments
 (0)