Skip to content

Commit

Permalink
improvements, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveicedgreentea committed Sep 12, 2023
1 parent 56664e5 commit 05b1e52
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 117 deletions.
15 changes: 15 additions & 0 deletions Contributing.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Contributing rules

## Rules

1) No major rewrites accepted. Sorry, don't have time to go through all that

2) Functional changes will not be looked at without a detailed explanation for why it is beneficial

3) I am doing this in my free time, you aren't entitled to anything

4) If you have a request, create an issue

5) Hacktoberfest and the like are not welcome

6) Please don't make useless PRs
10 changes: 9 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ use both denon and plex to compare codecs, make sure they are playing whats expe
Add support for detecting HDMI sync using madvr Envy. It will detect when media is synced and continue playing.
This prevents a black screen but stuff is playing which is annoying
It also makes the denon codec detection more accurate
New config homeAssistant.envyName
New config homeAssistant.envyName


9-10-23
Web UI flow for configuring server
New MQTT automations:
* "topicbeqcurrentprofile": -> which current profile is loaded
* "topicminidspmutestatus": -> true/false if minidsp(s) muted
* "topicplayingstatus": -> true/false if the tool is playing
11 changes: 11 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ import (
"github.com/iloveicedgreentea/go-plex/models"
)

// static files are cached which causes issues
func noCache() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0")
c.Header("Pragma", "no-cache")
c.Header("Expires", "Thu, 01 Jan 1970 00:00:00 GMT")
c.Next()
}
}

func main() {
log := logger.GetLogger()
log.Debug("Started in debug mode...")
r := gin.Default()
r.Use(noCache())

// you can copy this schema to create event handlers for any service
// create channel to receive jobs
Expand Down
26 changes: 13 additions & 13 deletions internal/handlers/plex_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
"sync"
"time"

"github.com/iloveicedgreentea/go-plex/internal/denon"
"github.com/gin-gonic/gin"
"github.com/iloveicedgreentea/go-plex/internal/config"
"github.com/iloveicedgreentea/go-plex/internal/denon"
"github.com/iloveicedgreentea/go-plex/internal/ezbeq"
"github.com/iloveicedgreentea/go-plex/internal/homeassistant"
"github.com/iloveicedgreentea/go-plex/internal/logger"
"github.com/iloveicedgreentea/go-plex/models"
"github.com/iloveicedgreentea/go-plex/internal/mqtt"
"github.com/iloveicedgreentea/go-plex/internal/plex"
"github.com/gin-gonic/gin"
"github.com/iloveicedgreentea/go-plex/models"
)

const showItemTitle = "episode"
Expand Down Expand Up @@ -152,12 +152,12 @@ func mediaPause(beqClient *ezbeq.BeqClient, haClient *homeassistant.HomeAssistan
}

// readAttrAndWait is a generic func to read attr from HA
func readAttrAndWait(waitTime int, entName string, entType string, attrResp homeassistant.HAAttributeResponse, haClient *homeassistant.HomeAssistantClient) (bool, error) {
func readAttrAndWait(waitTime int, entType string, attrResp homeassistant.HAAttributeResponse, haClient *homeassistant.HomeAssistantClient) (bool, error) {
var err error
var isSignal bool

for i := 0; i < waitTime; i++ {
isSignal, err = haClient.ReadAttributes(entName, attrResp, entType)
isSignal, err = haClient.ReadAttributes(haClient.EntityName, attrResp, entType)
if isSignal {
log.Debug("HDMI sync complete")
return isSignal, nil
Expand Down Expand Up @@ -190,7 +190,7 @@ func interfaceRemote(cmd string, c *homeassistant.HomeAssistantClient) error {
default:
return errors.New("unknown cmd given")
}

}

// playbackInteface is an interface to support various forms of playback
Expand All @@ -205,7 +205,7 @@ func playbackInteface(action string, h *homeassistant.HomeAssistantClient, p *pl
// TODO: test this
// waitForHDMISync will wait until the envy reports a signal to assume hdmi sync. No API to do this with denon afaik
func waitForHDMISync(wg *sync.WaitGroup, skipActions *bool, haClient *homeassistant.HomeAssistantClient, plexClient *plex.PlexClient) {
if !config.GetBool("signal.waitforHDMIsync") {
if !config.GetBool("signal.enabled") {
wg.Done()
return
}
Expand Down Expand Up @@ -239,12 +239,12 @@ func waitForHDMISync(wg *sync.WaitGroup, skipActions *bool, haClient *homeassist
switch signalSource {
case "envy":
// read envy attributes until its not nosignal
signal, err = readAttrAndWait(30, haClient.EnvyEntityName, "remote", &models.HAEnvyResponse{}, haClient)
signal, err = readAttrAndWait(30, "remote", &models.HAEnvyResponse{}, haClient)
case "jvc":
// read jvc attributes until its not nosignal
signal, err = readAttrAndWait(30, haClient.JVCEntityName, "remote", &models.HAjvcResponse{}, haClient)
signal, err = readAttrAndWait(30, "remote", &models.HAjvcResponse{}, haClient)
case "sensor":
signal, err = readAttrAndWait(30, haClient.BinaryName, "binary_sensor", &models.HABinaryResponse{}, haClient)
signal, err = readAttrAndWait(30, "binary_sensor", &models.HABinaryResponse{}, haClient)
default:
// TODO: maybe use a 15 sec delay?
log.Debug("using seconds for hdmi sync")
Expand All @@ -253,8 +253,8 @@ func waitForHDMISync(wg *sync.WaitGroup, skipActions *bool, haClient *homeassist
log.Errorf("waitforHDMIsync enabled but no valid source provided: %v -- %v", signalSource, err)
return
}
time.Sleep(time.Duration(sec)*time.Second)
time.Sleep(time.Duration(sec) * time.Second)

}

log.Debugf("HDMI Signal value is %v", signal)
Expand Down Expand Up @@ -669,7 +669,7 @@ func PlexWorker(plexChan <-chan models.PlexWebhookPayload, readyChan chan<- bool

if config.GetBool("homeAssistant.enabled") {
log.Info("Started with HA enabled")
haClient = homeassistant.NewClient(config.GetString("homeAssistant.url"), config.GetString("homeAssistant.port"), config.GetString("homeAssistant.token"), config.GetString("homeAssistant.envyRemoteName"), config.GetString("homeAssistant.jvcRemoteName"), config.GetString("homeAssistant.binarySensorName"))
haClient = homeassistant.NewClient(config.GetString("homeAssistant.url"), config.GetString("homeAssistant.port"), config.GetString("homeAssistant.token"), config.GetString("homeAssistant.remoteentityname"))
}
if config.GetBool("ezbeq.useAVRCodecSearch") {
log.Info("Started with AVR codec search enabled")
Expand Down
12 changes: 4 additions & 8 deletions internal/homeassistant/ha.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,16 @@ type HomeAssistantClient struct {
Port string
Token string
HTTPClient http.Client
EnvyEntityName string
JVCEntityName string
BinaryName string
EntityName string
}

// // A client to interface with home assistant
func NewClient(url, port string, token string, envyName string, jvcName string, binaryName string) *HomeAssistantClient {
func NewClient(url, port string, token string, entityName string) *HomeAssistantClient {
return &HomeAssistantClient{
ServerURL: url,
Port: port,
Token: token,
EnvyEntityName: envyName,
JVCEntityName: jvcName,
BinaryName: binaryName,
EntityName: entityName,
HTTPClient: http.Client{
Timeout: 5 * time.Second,
},
Expand Down Expand Up @@ -148,7 +144,7 @@ func (c *HomeAssistantClient) ReadAttributes(entityName string, respObj HAAttrib
switch entType {
case "remote":
if respObj.GetState() == "off" {
return false, fmt.Errorf("envy state is %s", respObj.GetState())
return false, fmt.Errorf("entity state is %s", respObj.GetState())
}

return respObj.GetSignalStatus(), err
Expand Down
8 changes: 4 additions & 4 deletions internal/homeassistant/ha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func testSetup() (*HomeAssistantClient) {

haClient := NewClient(config.GetString("homeAssistant.url"), config.GetString("homeAssistant.port"), config.GetString("homeAssistant.token"), config.GetString("homeAssistant.envyRemoteName"), config.GetString("homeAssistant.jvcRemoteName"), config.GetString("homeAssistant.binarySensorName"))
haClient := NewClient(config.GetString("homeAssistant.url"), config.GetString("homeAssistant.port"), config.GetString("homeAssistant.token"), config.GetString("homeAssistant.remoteentityname"))

return haClient
}
Expand Down Expand Up @@ -62,17 +62,17 @@ func TestReadAttributes(t *testing.T) {
}
tt := []testStruct{
{
entName: haClient.JVCEntityName,
entName: "nz7",
test: &models.HAjvcResponse{},
entType: "remote",
},
{
entName: haClient.EnvyEntityName,
entName: "envy",
test: &models.HAEnvyResponse{},
entType: "remote",
},
{
entName: haClient.BinaryName,
entName: "test_sensor",
test: &models.HABinaryResponse{},
entType: "binary_sensor",
},
Expand Down
29 changes: 21 additions & 8 deletions internal/plex/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,27 @@ func (c *PlexClient) getPlexReq(path string) ([]byte, error) {

func (c *PlexClient) getRunningSession() (models.SessionMediaContainer, error) {
// Get session object
var data models.SessionMediaContainer
var err error

res, err := c.getPlexReq("/status/sessions")
if err != nil {
return models.SessionMediaContainer{}, err
}
data, err := parseSessionMediaContainer(res)
if err != nil {
return models.SessionMediaContainer{}, err
// loop until not empty
for i := 0; i < 30; i++ {
res, err := c.getPlexReq("/status/sessions")
if err != nil {
return models.SessionMediaContainer{}, err
}
// if no response, keep trying
if len(res) == 0 {
log.Debugf("Plex session empty, waiting for %v", 30-i)
time.Sleep(1 * time.Second)
continue
}
data, err = parseSessionMediaContainer(res)
if err != nil {
return models.SessionMediaContainer{}, err
}

break
}

return data, err
Expand Down Expand Up @@ -563,4 +576,4 @@ func (c *PlexClient) DoPlaybackAction(action string) error {
_, err := c.makePlexReq(fmt.Sprintf("/player/playback/%s", action))

return err
}
}
Loading

0 comments on commit 05b1e52

Please sign in to comment.