Skip to content

Commit

Permalink
use telnet also for sending commands
Browse files Browse the repository at this point in the history
  • Loading branch information
splattner committed Oct 31, 2023
1 parent 72383cf commit c40295d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
17 changes: 15 additions & 2 deletions pkg/denonavr/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ import (
log "github.com/sirupsen/logrus"
)

func (d *DenonAVR) sendCommandToDevice(denonCommandType DenonCommand, command string) (int, error) {
func (d *DenonAVR) sendCommandToDevice(cmd DenonCommand, payload string) (int, error) {

if d.telnetEnabled {

err := d.sendTelnetCommand(cmd, payload)
if err != nil {
return 404, err
}
}

return d.sendHTTPCommand(cmd, payload)
}

func (d *DenonAVR) sendHTTPCommand(denonCommandType DenonCommand, command string) (int, error) {

url := "http://" + d.Host + COMMAND_URL + "?" + url.QueryEscape(string(denonCommandType)+command)
log.WithFields(log.Fields{
Expand All @@ -21,7 +34,7 @@ func (d *DenonAVR) sendCommandToDevice(denonCommandType DenonCommand, command st
return req.StatusCode, fmt.Errorf("error sending command: %w", err)
}

// Trigger a updata data, handled in the Listen Loop
// Trigger a update to get updated data handled in the Listen Loop
d.updateTrigger <- "update"

return req.StatusCode, nil
Expand Down
20 changes: 11 additions & 9 deletions pkg/denonavr/telnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,18 @@ func (d *DenonAVR) listenTelnet() {
}
}

// func (d *DenonAVR) sendTelnetCommand(cmd DenonCommand, payload string) {
func (d *DenonAVR) sendTelnetCommand(cmd DenonCommand, payload string) error {

// d.telnetMutex.Lock()
d.telnetMutex.Lock()

// defer d.telnetMutex.Unlock()
defer d.telnetMutex.Unlock()

// log.WithFields(log.Fields{
// "cmd": string(cmd),
// "payload": payload,
// }).Debug("send telnet command")
log.WithFields(log.Fields{
"cmd": string(cmd),
"payload": payload,
}).Debug("send telnet command")

// d.telnet.Write([]byte(string(cmd) + payload + "\r"))
// }
_, err := d.telnet.Write([]byte(string(cmd) + payload + "\r"))

return err
}

0 comments on commit c40295d

Please sign in to comment.