webserver.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "strings"
  9. "github.com/labstack/echo/v4"
  10. )
  11. type WebServer struct {
  12. Tv_ip string
  13. Port string
  14. Host string
  15. At string
  16. Device_list []string
  17. WakeOnLan map[string]string
  18. Power bool
  19. }
  20. func (w *WebServer) Init(tv, host, port string) {
  21. w.Tv_ip = tv
  22. w.Host = host
  23. w.Port = port
  24. w.WakeOnLan = make(map[string]string)
  25. if FileExists("WOL.txt") {
  26. log.Print("Loading WOLs from WOL.txt")
  27. file, err := os.Open("WOL.txt")
  28. if err != nil {
  29. log.Printf("Failed reading WOL.txt got %v", err)
  30. }
  31. defer file.Close()
  32. var scanner *bufio.Scanner = bufio.NewScanner(file)
  33. for scanner.Scan() {
  34. var line string = scanner.Text()
  35. if len(line) == 0 {
  36. continue
  37. }
  38. var parts []string = strings.Split(line, " = ")
  39. var ip string = parts[0]
  40. var mac string = parts[1]
  41. log.Printf("IP='%s' MAC='%s'", ip, mac)
  42. w.WakeOnLan[ip] = mac
  43. }
  44. }
  45. }
  46. func (w *WebServer) homepage(c echo.Context) error {
  47. //return c.String(http.StatusOK, "Hello World!")
  48. /*file, err := os.ReadFile("templates/index.html")
  49. if err != nil {
  50. return err
  51. }*/
  52. //return c.HTMLBlob(http.StatusOK, file)
  53. data := make(map[string]interface{})
  54. data["at"] = w.At
  55. return c.Render(http.StatusOK, "index.html", data)
  56. }
  57. func (w *WebServer) connectpage(c echo.Context) error {
  58. data := make(map[string]interface{})
  59. data["connected"] = "Connected: " + w.Tv_ip
  60. //var dev_list []string
  61. var list []string
  62. list = append(list, w.Device_list...)
  63. data["resp"] = list
  64. return c.Render(http.StatusOK, "connect.html", data)
  65. }
  66. func (w *WebServer) connector(c echo.Context) error {
  67. cmd := c.Param("cmd")
  68. w.Tv_ip = cmd
  69. log.Printf("Switched to %s", cmd)
  70. return c.Redirect(http.StatusTemporaryRedirect, "/connect")
  71. }
  72. func (w *WebServer) con_refresh(c echo.Context) error {
  73. log.Printf("Recieved Connection Refresh")
  74. var list *[]string
  75. list, w.WakeOnLan = GetDevices(w.WakeOnLan)
  76. log.Printf("Found %d devices", len(*list))
  77. log.Printf("Obtained %d WOLs", len(w.WakeOnLan))
  78. var save_file []byte
  79. for ip, mac := range w.WakeOnLan {
  80. log.Printf("IP='%v' MAC='%v'\n", ip, mac)
  81. if ip != "" && mac != "" {
  82. for _, r := range ip {
  83. save_file = append(save_file, byte(r))
  84. }
  85. save_file = append(save_file, ' ')
  86. save_file = append(save_file, '=')
  87. save_file = append(save_file, ' ')
  88. for _, r := range mac {
  89. save_file = append(save_file, byte(r))
  90. }
  91. }
  92. }
  93. if len(save_file) != 0 {
  94. err := os.WriteFile("WOL.txt", save_file, 0775)
  95. if err != nil {
  96. log.Printf("Saving WOL.txt failed with %v", err)
  97. } else {
  98. log.Print("Saved WOL to WOL.txt")
  99. }
  100. }
  101. for _, ip := range *list {
  102. if ip != w.Tv_ip {
  103. w.Device_list = append(w.Device_list, ip)
  104. }
  105. }
  106. //w.Device_list = *list
  107. return c.Redirect(http.StatusTemporaryRedirect, "/connect")
  108. }
  109. func (w *WebServer) tvcmd(c echo.Context) error {
  110. //w.At = DebugInfo(w.Tv_ip)
  111. cmd := c.Param("cmd")
  112. switch cmd {
  113. case "power":
  114. var r bool = PerformKey(w.Tv_ip, "power")
  115. if !r {
  116. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  117. }
  118. case "back":
  119. var r bool = PerformKey(w.Tv_ip, "back")
  120. if !r {
  121. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  122. PerformKey(w.Tv_ip, "back")
  123. }
  124. case "home":
  125. var r bool = PerformKey(w.Tv_ip, "home")
  126. if !r {
  127. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  128. PerformKey(w.Tv_ip, "home")
  129. }
  130. case "up":
  131. var r bool = PerformKey(w.Tv_ip, "up")
  132. if !r {
  133. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  134. PerformKey(w.Tv_ip, "up")
  135. }
  136. case "left":
  137. var r bool = PerformKey(w.Tv_ip, "left")
  138. if !r {
  139. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  140. PerformKey(w.Tv_ip, "left")
  141. }
  142. case "ok":
  143. var r bool = PerformKey(w.Tv_ip, "select")
  144. if !r {
  145. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  146. PerformKey(w.Tv_ip, "select")
  147. }
  148. case "right":
  149. var r bool = PerformKey(w.Tv_ip, "right")
  150. if !r {
  151. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  152. PerformKey(w.Tv_ip, "right")
  153. }
  154. case "down":
  155. var r bool = PerformKey(w.Tv_ip, "down")
  156. if !r {
  157. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  158. PerformKey(w.Tv_ip, "down")
  159. }
  160. case "rewind":
  161. var r bool = PerformKey(w.Tv_ip, "rev")
  162. if !r {
  163. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  164. PerformKey(w.Tv_ip, "rev")
  165. }
  166. case "play-pause":
  167. var r bool = PerformKey(w.Tv_ip, "play")
  168. if !r {
  169. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  170. PerformKey(w.Tv_ip, "play")
  171. }
  172. case "fast-forward":
  173. var r bool = PerformKey(w.Tv_ip, "fwd")
  174. if !r {
  175. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  176. PerformKey(w.Tv_ip, "fwd")
  177. }
  178. case "star":
  179. var r bool = PerformKey(w.Tv_ip, "info")
  180. if !r {
  181. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  182. PerformKey(w.Tv_ip, "info")
  183. }
  184. case "reload":
  185. var r bool = PerformKey(w.Tv_ip, "instantreplay")
  186. if !r {
  187. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  188. PerformKey(w.Tv_ip, "instantreplay")
  189. }
  190. case "vol+":
  191. var r bool = PerformKey(w.Tv_ip, "volumeup")
  192. if !r {
  193. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  194. PerformKey(w.Tv_ip, "volumeup")
  195. }
  196. case "vol-":
  197. var r bool = PerformKey(w.Tv_ip, "volumedown")
  198. if !r {
  199. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  200. PerformKey(w.Tv_ip, "volumedown")
  201. }
  202. case "mute":
  203. var r bool = PerformKey(w.Tv_ip, "volumemute")
  204. if !r {
  205. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  206. PerformKey(w.Tv_ip, "volumemute")
  207. }
  208. case "display":
  209. w.At = DebugInfo(w.Tv_ip)
  210. if w.At == "" {
  211. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  212. w.At = DebugInfo(w.Tv_ip)
  213. }
  214. var device *DeviceInfo = GetDeviceInfo(w.Tv_ip)
  215. if device.PowerMode == "PowerOn" {
  216. w.Power = true
  217. } else {
  218. w.Power = false
  219. }
  220. var data map[string]interface{} = make(map[string]interface{})
  221. data["at"] = w.At
  222. data["power"] = w.Power
  223. return c.JSON(http.StatusOK, data)
  224. case "pluto":
  225. r := Post(fmt.Sprintf("http://%s:8060/launch/%d", w.Tv_ip, 74519))
  226. if r == nil {
  227. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  228. Post(fmt.Sprintf("http://%s:8060/launch/%d", w.Tv_ip, 74519))
  229. }
  230. case "pluto-cops":
  231. r := Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=367&mediaType=live", w.Tv_ip, 74519))
  232. if r == nil {
  233. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  234. Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=367&mediaType=live", w.Tv_ip, 74519))
  235. }
  236. case "pluto-thefirst":
  237. r := Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=244&mediaType=live", w.Tv_ip, 74519))
  238. if r == nil {
  239. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  240. Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=244&mediaType=live", w.Tv_ip, 74519))
  241. }
  242. case "pluto-mst3k":
  243. r := Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=488&mediaType=live", w.Tv_ip, 74519))
  244. if r == nil {
  245. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  246. Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=488&mediaType=live", w.Tv_ip, 74519))
  247. }
  248. case "pluto-rifftrax":
  249. r := Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=489&mediaType=live", w.Tv_ip, 74519))
  250. if r == nil {
  251. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  252. Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=489&mediaType=live", w.Tv_ip, 74519))
  253. }
  254. case "pluto-startrek":
  255. r := Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=150&mediaType=live", w.Tv_ip, 74519))
  256. if r == nil {
  257. sendEtherWake(w.WakeOnLan[w.Tv_ip])
  258. Post(fmt.Sprintf("http://%s:8060/launch/%d?contentId=150&mediaType=live", w.Tv_ip, 74519))
  259. }
  260. default:
  261. c.Echo().Logger.Printf("Got '%v'\n", cmd)
  262. }
  263. return c.Redirect(http.StatusTemporaryRedirect, "/")
  264. //return c.Render(http.StatusOK, "index.html", data)
  265. }