webserver.go 1.4 KB

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