Browse Source

Added ability to change to Pluto Tv

  Currently unable to understand why asking for channel doesn't work.
Apollo 3 years ago
parent
commit
d490e04a3f
5 changed files with 135 additions and 33 deletions
  1. 11 0
      CHANNELS_PLUTO.md
  2. 14 2
      poster.go
  3. 33 0
      renderer.go
  4. 12 2
      templates/index.html
  5. 65 29
      webserver.go

+ 11 - 0
CHANNELS_PLUTO.md

@@ -0,0 +1,11 @@
+# Pluto Tv - 74519
+
+Cops - 367
+
+TheFirst - 244
+
+MST3K - 488
+
+RiffTrax - 489
+
+StarTrel - 150

+ 14 - 2
poster.go

@@ -23,8 +23,10 @@ func Get(where string) *http.Response {
 	resp, err := http.Get(where)
 	if err != nil {
 		log.Printf("GET got non 200 code, %v", err)
+		return nil
+	} else {
+		return resp
 	}
-	return resp
 }
 
 // https://developer.roku.com/docs/developer-program/debugging/external-control-api.md#keypress-key-values codes
@@ -43,6 +45,9 @@ func PerformKey(ip, command string) {
 
 func GetCurrentPlay(ip string) map[string]interface{} {
 	resp := Get(Query(ip, "media-player"))
+	if resp == nil {
+		return nil
+	}
 	body, err := io.ReadAll(resp.Body)
 	if err != nil {
 		log.Printf("Reading from body got error, %v", err)
@@ -57,6 +62,9 @@ func GetCurrentPlay(ip string) map[string]interface{} {
 
 func GetQuery(ip, command string) map[string]interface{} {
 	resp := Get(Query(ip, command))
+	if resp == nil {
+		return nil
+	}
 	body, err := io.ReadAll(resp.Body)
 	if err != nil {
 		log.Printf("Reading from body got error, %v", err)
@@ -69,9 +77,12 @@ func GetQuery(ip, command string) map[string]interface{} {
 	return r
 }
 
-func DebugInfo(ip string) {
+func DebugInfo(ip string) string {
 	// Query the current playing thing, and get the tv channels
 	r := GetQuery(ip, "media-player")
+	if r == nil {
+		return ""
+	}
 	name, _ := glom.Glom(r, "player.plugin.-name")
 	id, _ := glom.Glom(r, "player.plugin.-id")
 	/*r1 := GetQuery(ip, "tv-channels")
@@ -82,6 +93,7 @@ func DebugInfo(ip string) {
 	// Debug print
 	fmt.Printf("Name: %v (%v)\r\n", name, id)
 	//fmt.Printf("Channel: %v (%v / %v)\r\n", chan_name, chan_phy_id, chan_number)
+	return fmt.Sprintf("%v (%v)", name, id)
 }
 
 func examplePost() {

+ 33 - 0
renderer.go

@@ -0,0 +1,33 @@
+package main
+
+import (
+	"html/template"
+	"io"
+
+	"github.com/labstack/echo/v4"
+)
+
+type Renderer struct {
+	template *template.Template
+	debug    bool
+	location string
+}
+
+func NewRenderer(location string, debug bool) *Renderer {
+	r := Renderer{}
+	r.location = location
+	r.debug = debug
+	r.ReloadTemplates()
+	return &r
+}
+
+func (r *Renderer) ReloadTemplates() {
+	r.template = template.Must(template.ParseGlob(r.location))
+}
+
+func (r *Renderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
+	if r.debug {
+		r.ReloadTemplates()
+	}
+	return r.template.ExecuteTemplate(w, name, data)
+}

+ 12 - 2
templates/index.html

@@ -12,9 +12,9 @@
         <div class="grid-x">
             <h1 class="large-12 medium-12 small-12 cell text-center">goRoku Controller</h1>
             <div class="large-12 medium-12 small-12 cell text-center">
-                <h3>TV:</h3>
+                <h3 id="at">TV: </h3>
                 <p>
-                    <button type="button" class="alert button" onclick="sendCommandTV('power')">Power</button><br>
+                    <button type="button" class="alert button" onclick="sendCommandTV('power')">Power</button> <button type="button" class="alert button" onclick="sendCommandTV('display')">Display</button><br>
                     <button type="button" class="button" onclick="sendCommandTV('back')">Back</button> <button type="button" class="button" onclick="sendCommandTV('home')">Home</button><br>
                     <button type="button" class="button" onclick="sendCommandTV('up')">^</button><br>
                     <button type="button" class="button" onclick="sendCommandTV('left')"><</button> <button type="button" class="success button" onclick="sendCommandTV('ok')">Ok</button> <button type="button" class="button" onclick="sendCommandTV('right')">></button><br>
@@ -22,6 +22,10 @@
                     <button type="button" class="button" onclick="sendCommandTV('reload')">🔁</button> <button type="button" class="button" onclick="sendCommandTV('star')">*</button><br>
                     <button type="button" class="button" onclick="sendCommandTV('rewind')"><<</button> <button type="button" class="button" onclick="sendCommandTV('play-pause')">Play/Pause</button> <button type="button" class="button" onclick="sendCommandTV('fast-forward')">>></button><br>
                     <button type="button" class="button" onclick="sendCommandTV('vol+')">Vol +</button> <button type="button" class="button" onclick="sendCommandTV('mute')">Vol Mute</button> <button type="button" class="button" onclick="sendCommandTV('vol-')">Vol -</button><br>
+                    <br>
+                    <button type="button" class="button" onclick="sendCommandTV('pluto')">Pluto Tv</button> <br><!--
+                    <button type="button" class="button" onclick="sendCommandTV('pluto-cops')">Cops</button> <button type="button" class="button" onclick="sendCommandTV('pluto-thefirst')">The First</button> <button type="button" class="button" onclick="sendCommandTV('pluto-mst3k')">MST3K</button><br>
+                    <button type="button" class="button" onclick="sendCommandTV('pluto-rifftrax')">Riff Trax</button> <button type="button" class="button" onclick="sendCommandTV('pluto-startrek')">Star Trek</button><br>-->
                 </p>
             </div>
             <!-- <div class="large-6 medium-6 small6 cell text-center">
@@ -39,8 +43,14 @@
             function sendCommandTV(cmd) {
                 const xhttp = new XMLHttpRequest();
                 xhttp.onload = function() {}
+                xhttp.onreadystatechange = function() {
+                    if (this.readyState == 4 && this.status == 200 && cmd == "display") {
+                     document.getElementById("at").innerHTML = this.responseText;
+                    }
+                  };
                 xhttp.open("GET", "tv/"+cmd, true);
                 xhttp.send();
+
             }
             /*function sendCommandMB(cmd) {
                 const xhttp = new XMLHttpRequest();

+ 65 - 29
webserver.go

@@ -1,72 +1,108 @@
 package main
 
 import (
+	"fmt"
 	"net/http"
-	"os"
 
 	"github.com/labstack/echo/v4"
 	"github.com/labstack/echo/v4/middleware"
 )
 
-const tv_ip = "192.168.254.75"
+type WebServer struct {
+	tv_ip string
+	Port  string
+	Host  string
+	At    string
+}
+
+func (w *WebServer) Init(tv, host, port string) {
+	w.tv_ip = tv
+	w.Host = host
+	w.Port = port
+	w.At = DebugInfo(w.tv_ip)
+}
 
-func homepage(c echo.Context) error {
-	DebugInfo(tv_ip)
+func (w *WebServer) homepage(c echo.Context) error {
 	//return c.String(http.StatusOK, "Hello World!")
-	file, err := os.ReadFile("templates/index.html")
+	/*file, err := os.ReadFile("templates/index.html")
 	if err != nil {
 		return err
-	}
+	}*/
 
-	return c.HTMLBlob(http.StatusOK, file)
+	//return c.HTMLBlob(http.StatusOK, file)
+	data := make(map[string]interface{})
+	data["at"] = w.At
+	return c.Render(http.StatusOK, "index.html", data)
 }
 
-func tvcmd(c echo.Context) error {
+func (w *WebServer) tvcmd(c echo.Context) error {
+	//w.At = DebugInfo(w.tv_ip)
 	cmd := c.Param("cmd")
 	switch cmd {
 	case "power":
-		PerformKey(tv_ip, "power")
+		PerformKey(w.tv_ip, "power")
 	case "back":
-		PerformKey(tv_ip, "back")
+		PerformKey(w.tv_ip, "back")
 	case "home":
-		PerformKey(tv_ip, "home")
+		PerformKey(w.tv_ip, "home")
 	case "up":
-		PerformKey(tv_ip, "up")
+		PerformKey(w.tv_ip, "up")
 	case "left":
-		PerformKey(tv_ip, "left")
+		PerformKey(w.tv_ip, "left")
 	case "ok":
-		PerformKey(tv_ip, "select")
+		PerformKey(w.tv_ip, "select")
 	case "right":
-		PerformKey(tv_ip, "right")
+		PerformKey(w.tv_ip, "right")
 	case "down":
-		PerformKey(tv_ip, "down")
+		PerformKey(w.tv_ip, "down")
 	case "rewind":
-		PerformKey(tv_ip, "rev")
+		PerformKey(w.tv_ip, "rev")
 	case "play-pause":
-		PerformKey(tv_ip, "play")
+		PerformKey(w.tv_ip, "play")
 	case "fast-forward":
-		PerformKey(tv_ip, "fwd")
-	/*case "star":
-		// Unknown
+		PerformKey(w.tv_ip, "fwd")
+	case "star":
+		PerformKey(w.tv_ip, "info")
 	case "reload":
-		// Unknown*/
+		PerformKey(w.tv_ip, "instantreplay")
 	case "vol+":
-		PerformKey(tv_ip, "volumeup")
+		PerformKey(w.tv_ip, "volumeup")
 	case "vol-":
-		PerformKey(tv_ip, "volumedown")
+		PerformKey(w.tv_ip, "volumedown")
 	case "mute":
-		PerformKey(tv_ip, "volumemute")
+		PerformKey(w.tv_ip, "volumemute")
+	case "display":
+		w.At = DebugInfo(w.tv_ip)
+		data := make(map[string]interface{})
+		data["at"] = w.At
+		return c.String(http.StatusOK, "TV: "+w.At)
+	case "pluto":
+		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", w.tv_ip, 74519))
+	case "pluto-thefirst":
+		Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=244", w.tv_ip, 74519))
+	case "pluto-mst3k":
+		Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=488", w.tv_ip, 74519))
+	case "pluto-rifftrax":
+		Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=489", w.tv_ip, 74519))
+	case "pluto-startrek":
+		Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=150", w.tv_ip, 74519))
 	default:
 		c.Echo().Logger.Printf("Got '%v'\r\n", cmd)
 	}
-	return c.Redirect(http.StatusAccepted, "/")
+	return c.Redirect(http.StatusOK, "/")
+	//return c.Render(http.StatusOK, "index.html", data)
 }
 
 func main() {
+	web := WebServer{}
+	web.Init("192.168.254.75", "", "8000")
 	e := echo.New()
 	e.Use(middleware.Logger())
 	e.Use(middleware.Static("static"))
-	e.GET("/", homepage)
-	e.GET("/tv/:cmd", tvcmd)
-	e.Logger.Fatal(e.Start(":8000"))
+	e.Renderer = NewRenderer("./templates/*.html", true)
+	e.GET("/", web.homepage)
+	e.GET("/tv/:cmd", web.tvcmd)
+	e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%s", web.Host, web.Port)))
 }