webserver.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "github.com/labstack/echo/v4"
  7. "github.com/labstack/echo/v4/middleware"
  8. )
  9. type WebServer struct {
  10. Tv_ip string
  11. Port string
  12. Host string
  13. At string
  14. Device_list []string
  15. Power bool
  16. }
  17. func (w *WebServer) Init(tv, host, port string) {
  18. w.Tv_ip = tv
  19. w.Host = host
  20. w.Port = port
  21. }
  22. func (w *WebServer) homepage(c echo.Context) error {
  23. //return c.String(http.StatusOK, "Hello World!")
  24. /*file, err := os.ReadFile("templates/index.html")
  25. if err != nil {
  26. return err
  27. }*/
  28. //return c.HTMLBlob(http.StatusOK, file)
  29. data := make(map[string]interface{})
  30. data["at"] = w.At
  31. return c.Render(http.StatusOK, "index.html", data)
  32. }
  33. func (w *WebServer) connectpage(c echo.Context) error {
  34. data := make(map[string]interface{})
  35. data["connected"] = "Connected: " + w.Tv_ip
  36. //var dev_list []string
  37. var list []string
  38. list = append(list, w.Device_list...)
  39. data["resp"] = list
  40. return c.Render(http.StatusOK, "connect.html", data)
  41. }
  42. func (w *WebServer) connector(c echo.Context) error {
  43. cmd := c.Param("cmd")
  44. w.Tv_ip = cmd
  45. log.Printf("Switched to %s", cmd)
  46. return c.Redirect(http.StatusOK, "/connect")
  47. }
  48. func (w *WebServer) con_refresh(c echo.Context) error {
  49. log.Printf("Recieved Connection Refresh")
  50. var list *[]string = GetDevices()
  51. //w.Device_list = append(w.Device_list, *list...)
  52. w.Device_list = *list
  53. log.Printf("Found %d devices", len(w.Device_list))
  54. return c.Redirect(http.StatusOK, "/connect")
  55. }
  56. func (w *WebServer) tvcmd(c echo.Context) error {
  57. //w.At = DebugInfo(w.Tv_ip)
  58. cmd := c.Param("cmd")
  59. switch cmd {
  60. case "power":
  61. PerformKey(w.Tv_ip, "power")
  62. case "back":
  63. PerformKey(w.Tv_ip, "back")
  64. case "home":
  65. PerformKey(w.Tv_ip, "home")
  66. case "up":
  67. PerformKey(w.Tv_ip, "up")
  68. case "left":
  69. PerformKey(w.Tv_ip, "left")
  70. case "ok":
  71. PerformKey(w.Tv_ip, "select")
  72. case "right":
  73. PerformKey(w.Tv_ip, "right")
  74. case "down":
  75. PerformKey(w.Tv_ip, "down")
  76. case "rewind":
  77. PerformKey(w.Tv_ip, "rev")
  78. case "play-pause":
  79. PerformKey(w.Tv_ip, "play")
  80. case "fast-forward":
  81. PerformKey(w.Tv_ip, "fwd")
  82. case "star":
  83. PerformKey(w.Tv_ip, "info")
  84. case "reload":
  85. PerformKey(w.Tv_ip, "instantreplay")
  86. case "vol+":
  87. PerformKey(w.Tv_ip, "volumeup")
  88. case "vol-":
  89. PerformKey(w.Tv_ip, "volumedown")
  90. case "mute":
  91. PerformKey(w.Tv_ip, "volumemute")
  92. case "display":
  93. w.At = DebugInfo(w.Tv_ip)
  94. device := GetDeviceInfo(w.Tv_ip)
  95. if device["power-mode"] == "PowerOn" {
  96. w.Power = true
  97. } else {
  98. w.Power = false
  99. }
  100. data := make(map[string]interface{})
  101. data["at"] = w.At
  102. data["power"] = w.Power
  103. return c.JSON(http.StatusOK, data)
  104. case "pluto":
  105. Post(fmt.Sprintf("http://%s:8060/launch/%d", w.Tv_ip, 74519))
  106. case "pluto-cops":
  107. Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=367&mediaType=live", w.Tv_ip, 74519))
  108. case "pluto-thefirst":
  109. Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=244&mediaType=live", w.Tv_ip, 74519))
  110. case "pluto-mst3k":
  111. Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=488&mediaType=live", w.Tv_ip, 74519))
  112. case "pluto-rifftrax":
  113. Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=489&mediaType=live", w.Tv_ip, 74519))
  114. case "pluto-startrek":
  115. Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=150&mediaType=live", w.Tv_ip, 74519))
  116. default:
  117. c.Echo().Logger.Printf("Got '%v'\r\n", cmd)
  118. }
  119. return c.Redirect(http.StatusOK, "/")
  120. //return c.Render(http.StatusOK, "index.html", data)
  121. }
  122. func main() {
  123. web := WebServer{}
  124. web.Init("192.168.254.75", "", "8000")
  125. e := echo.New()
  126. e.Use(middleware.Logger())
  127. e.Use(middleware.Static("static"))
  128. e.Renderer = NewRenderer("./templates/*.html", true)
  129. e.GET("/", web.homepage)
  130. e.GET("/tv/:cmd", web.tvcmd)
  131. e.GET("/connect", web.connectpage)
  132. e.GET("/con/:cmd", web.connector)
  133. e.GET("/conn_refresh", web.con_refresh)
  134. e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%s", web.Host, web.Port)))
  135. }