webserver.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package main
  2. import (
  3. "net/http"
  4. "os"
  5. "github.com/labstack/echo/v4"
  6. "github.com/labstack/echo/v4/middleware"
  7. )
  8. const tv_ip = "192.168.254.75"
  9. func homepage(c echo.Context) error {
  10. DebugInfo(tv_ip)
  11. //return c.String(http.StatusOK, "Hello World!")
  12. file, err := os.ReadFile("templates/index.html")
  13. if err != nil {
  14. return err
  15. }
  16. return c.HTMLBlob(http.StatusOK, file)
  17. }
  18. func tvcmd(c echo.Context) error {
  19. cmd := c.Param("cmd")
  20. switch cmd {
  21. case "power":
  22. PerformKey(tv_ip, "power")
  23. case "back":
  24. PerformKey(tv_ip, "back")
  25. case "home":
  26. PerformKey(tv_ip, "home")
  27. case "up":
  28. PerformKey(tv_ip, "up")
  29. case "left":
  30. PerformKey(tv_ip, "left")
  31. case "ok":
  32. PerformKey(tv_ip, "select")
  33. case "right":
  34. PerformKey(tv_ip, "right")
  35. case "down":
  36. PerformKey(tv_ip, "down")
  37. case "rewind":
  38. PerformKey(tv_ip, "rev")
  39. case "play-pause":
  40. PerformKey(tv_ip, "play")
  41. case "fast-forward":
  42. PerformKey(tv_ip, "fwd")
  43. /*case "star":
  44. // Unknown
  45. case "reload":
  46. // Unknown*/
  47. case "vol+":
  48. PerformKey(tv_ip, "volumeup")
  49. case "vol-":
  50. PerformKey(tv_ip, "volumedown")
  51. case "mute":
  52. PerformKey(tv_ip, "volumemute")
  53. default:
  54. c.Echo().Logger.Printf("Got '%v'\r\n", cmd)
  55. }
  56. return c.Redirect(http.StatusAccepted, "/")
  57. }
  58. func main() {
  59. e := echo.New()
  60. e.Use(middleware.Logger())
  61. e.Use(middleware.Static("static"))
  62. e.GET("/", homepage)
  63. e.GET("/tv/:cmd", tvcmd)
  64. e.Logger.Fatal(e.Start(":8000"))
  65. }