Browse Source

Added etherwake for Wake On Lan feature

  It now works... assuming you've added the ip and mac address via
   `arp -s ip mac`

  You can get the ip and mac from performing a connection reload.
Apollo 3 years ago
parent
commit
cfdf58636a
5 changed files with 156 additions and 26 deletions
  1. 3 0
      .gitignore
  2. BIN
      etherwake
  3. 19 0
      ewake.go
  4. 7 1
      poster.go
  5. 127 25
      webserver.go

+ 3 - 0
.gitignore

@@ -26,3 +26,6 @@ _testmain.go
 
 # Exclude WOL file
 WOL.txt
+
+# Exclude Log files
+*.log

BIN
etherwake


+ 19 - 0
ewake.go

@@ -0,0 +1,19 @@
+package main
+
+import (
+	"log"
+	"os/exec"
+)
+
+func sendEtherWake(mac string) {
+	var args []string
+	args = append(args, mac)
+	args = append(args, "-i")
+	args = append(args, "eno1")
+	var cmd *exec.Cmd = exec.Command("./etherwake", args...)
+	var err error = cmd.Run()
+	if err != nil {
+		log.Printf("sendEtherWake(%s) -> %v\n", mac, err)
+		log.Println(cmd.String())
+	}
+}

+ 7 - 1
poster.go

@@ -16,6 +16,8 @@ func Post(where string) *http.Response {
 	resp, err := http.PostForm(where, nil)
 	if err != nil {
 		log.Printf("POST got non 200 code, %v", err)
+		// Catch what error it is, if it's a no access to device try a etherwake
+		return nil
 	}
 	return resp
 }
@@ -121,9 +123,13 @@ func Query(ip, what string) string {
 	return fmt.Sprintf("http://%s:8060/query/%s", ip, what)
 }
 
-func PerformKey(ip, command string) {
+func PerformKey(ip, command string) bool {
 	r := Post(KeyPress(ip, command))
+	if r == nil {
+		return false
+	}
 	defer r.Body.Close()
+	return true
 }
 
 func GetQuery(ip, command string) map[string]interface{} {

+ 127 - 25
webserver.go

@@ -78,7 +78,7 @@ func (w *WebServer) connector(c echo.Context) error {
 	w.Tv_ip = cmd
 	log.Printf("Switched to %s", cmd)
 
-	return c.Redirect(http.StatusOK, "/connect")
+	return c.Redirect(http.StatusTemporaryRedirect, "/connect")
 }
 
 func (w *WebServer) con_refresh(c echo.Context) error {
@@ -124,39 +124,106 @@ func (w *WebServer) tvcmd(c echo.Context) error {
 	cmd := c.Param("cmd")
 	switch cmd {
 	case "power":
-		PerformKey(w.Tv_ip, "power")
+		var r bool = PerformKey(w.Tv_ip, "power")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+		}
 	case "back":
-		PerformKey(w.Tv_ip, "back")
+		var r bool = PerformKey(w.Tv_ip, "back")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "back")
+		}
 	case "home":
-		PerformKey(w.Tv_ip, "home")
+		var r bool = PerformKey(w.Tv_ip, "home")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "home")
+		}
 	case "up":
-		PerformKey(w.Tv_ip, "up")
+		var r bool = PerformKey(w.Tv_ip, "up")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "up")
+		}
 	case "left":
-		PerformKey(w.Tv_ip, "left")
+		var r bool = PerformKey(w.Tv_ip, "left")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "left")
+		}
 	case "ok":
-		PerformKey(w.Tv_ip, "select")
+		var r bool = PerformKey(w.Tv_ip, "select")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "select")
+		}
 	case "right":
-		PerformKey(w.Tv_ip, "right")
+		var r bool = PerformKey(w.Tv_ip, "right")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "right")
+		}
 	case "down":
-		PerformKey(w.Tv_ip, "down")
+		var r bool = PerformKey(w.Tv_ip, "down")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "down")
+		}
 	case "rewind":
-		PerformKey(w.Tv_ip, "rev")
+		var r bool = PerformKey(w.Tv_ip, "rev")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "rev")
+		}
 	case "play-pause":
-		PerformKey(w.Tv_ip, "play")
+		var r bool = PerformKey(w.Tv_ip, "play")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "play")
+		}
 	case "fast-forward":
-		PerformKey(w.Tv_ip, "fwd")
+		var r bool = PerformKey(w.Tv_ip, "fwd")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "fwd")
+		}
 	case "star":
-		PerformKey(w.Tv_ip, "info")
+		var r bool = PerformKey(w.Tv_ip, "info")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "info")
+		}
 	case "reload":
-		PerformKey(w.Tv_ip, "instantreplay")
+		var r bool = PerformKey(w.Tv_ip, "instantreplay")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "instantreplay")
+		}
 	case "vol+":
-		PerformKey(w.Tv_ip, "volumeup")
+		var r bool = PerformKey(w.Tv_ip, "volumeup")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "volumeup")
+		}
 	case "vol-":
-		PerformKey(w.Tv_ip, "volumedown")
+		var r bool = PerformKey(w.Tv_ip, "volumedown")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "volumedown")
+		}
 	case "mute":
-		PerformKey(w.Tv_ip, "volumemute")
+		var r bool = PerformKey(w.Tv_ip, "volumemute")
+		if !r {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			PerformKey(w.Tv_ip, "volumemute")
+		}
 	case "display":
 		w.At = DebugInfo(w.Tv_ip)
+		if w.At == "" {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			w.At = DebugInfo(w.Tv_ip)
+		}
 		device := GetDeviceInfo(w.Tv_ip)
 		if device["power-mode"] == "PowerOn" {
 			w.Power = true
@@ -168,25 +235,60 @@ func (w *WebServer) tvcmd(c echo.Context) error {
 		data["power"] = w.Power
 		return c.JSON(http.StatusOK, data)
 	case "pluto":
-		Post(fmt.Sprintf("http://%s:8060/launch/%d", w.Tv_ip, 74519))
+		r := Post(fmt.Sprintf("http://%s:8060/launch/%d", w.Tv_ip, 74519))
+		if r == nil {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			Post(fmt.Sprintf("http://%s:8060/launch/%d", w.Tv_ip, 74519))
+		}
 	case "pluto-cops":
-		Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=367&mediaType=live", w.Tv_ip, 74519))
+		r := Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=367&mediaType=live", w.Tv_ip, 74519))
+		if r == nil {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=367&mediaType=live", w.Tv_ip, 74519))
+		}
 	case "pluto-thefirst":
-		Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=244&mediaType=live", w.Tv_ip, 74519))
+		r := Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=244&mediaType=live", w.Tv_ip, 74519))
+		if r == nil {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=244&mediaType=live", w.Tv_ip, 74519))
+		}
 	case "pluto-mst3k":
-		Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=488&mediaType=live", w.Tv_ip, 74519))
+		r := Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=488&mediaType=live", w.Tv_ip, 74519))
+		if r == nil {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=488&mediaType=live", w.Tv_ip, 74519))
+		}
 	case "pluto-rifftrax":
-		Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=489&mediaType=live", w.Tv_ip, 74519))
+		r := Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=489&mediaType=live", w.Tv_ip, 74519))
+		if r == nil {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=489&mediaType=live", w.Tv_ip, 74519))
+		}
 	case "pluto-startrek":
-		Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=150&mediaType=live", w.Tv_ip, 74519))
+		r := Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=150&mediaType=live", w.Tv_ip, 74519))
+		if r == nil {
+			sendEtherWake(w.WakeOnLan[w.Tv_ip])
+			Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=150&mediaType=live", w.Tv_ip, 74519))
+		}
 	default:
-		c.Echo().Logger.Printf("Got '%v'\r\n", cmd)
+		c.Echo().Logger.Printf("Got '%v'\n", cmd)
 	}
-	return c.Redirect(http.StatusOK, "/")
+	return c.Redirect(http.StatusTemporaryRedirect, "/")
 	//return c.Render(http.StatusOK, "index.html", data)
 }
 
 func main() {
+	// Write log.print ect into a file
+	var logfilename string = "roku.log"
+	var logf *os.File
+	var err error
+	logf, err = os.OpenFile(logfilename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
+	if err != nil {
+		log.Panicf("Error creating log file %s: %v", logfilename, err)
+	}
+	log.SetOutput(logf)
+	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
+
 	web := WebServer{}
 	web.Init("192.168.254.75", "", "8000")