poster.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "net/http"
  7. "strings"
  8. "github.com/beanzilla/glom"
  9. "github.com/beego/x2j"
  10. )
  11. // https://zetcode.com/golang/getpostrequest/ post_req_form.go
  12. func Post(where string) *http.Response {
  13. resp, err := http.PostForm(where, nil)
  14. if err != nil {
  15. log.Printf("POST got non 200 code, %v", err)
  16. // Catch what error it is, if it's a no access to device try a etherwake
  17. return nil
  18. }
  19. return resp
  20. }
  21. /*func ListenToRequests(ln *net.UDPConn, data *string) {
  22. done := false
  23. do_send := true
  24. for !done {
  25. buffer := make([]byte, 1024) // 1k
  26. ln.SetReadDeadline(time.Now().Add(3 * time.Second))
  27. for !done {
  28. b, _, err := ln.ReadFromUDP(buffer)
  29. if b > 0 {
  30. str := string(buffer[0:])
  31. *data += str
  32. }
  33. if err != nil {
  34. if _, ok := err.(net.Error); !ok {
  35. log.Print(err)
  36. done = true
  37. }
  38. }
  39. if do_send {
  40. err = PostFind()
  41. if err != nil {
  42. log.Printf("POST Find returned %v", err)
  43. }
  44. do_send = false
  45. }
  46. }
  47. }
  48. }*/
  49. /*
  50. func PostFind() {
  51. file, err := os.ReadFile("finder.txt")
  52. if err != nil {
  53. log.Print(err)
  54. }
  55. addr, err := net.ResolveUDPAddr("udp", "239.255.255.250:1900")
  56. if err != nil {
  57. log.Print(err)
  58. }
  59. conn, err := net.DialUDP("udp", nil, addr)
  60. if err != nil {
  61. log.Print(err)
  62. }
  63. defer conn.Close()
  64. conn.Write(file)
  65. log.Println("Posted Request for devices")
  66. }
  67. func GetFind(data *[]string) {
  68. addr, err := net.ResolveUDPAddr("udp", ":1900")
  69. if err != nil {
  70. log.Printf("GET Find failed getting port 1900, %v", err)
  71. return
  72. }
  73. fmt.Printf("Listening on %s\r\n", addr.String())
  74. ln, err := net.ListenUDP("udp", addr)
  75. if err != nil {
  76. log.Printf("GET Find started listener for 1900, %v", err)
  77. return
  78. }
  79. var list []string
  80. buffer := make([]byte, 1024) // 1k
  81. defer ln.Close()
  82. ln.SetReadDeadline(time.Now().Add(3 * time.Second))
  83. for {
  84. n, adr, err := ln.ReadFromUDP(buffer)
  85. str := string(buffer[0:n])
  86. fmt.Printf("Got '%s' from %s\r\n", str, adr.String())
  87. if err != nil {
  88. log.Println(err)
  89. break
  90. }
  91. list = append(list, adr.String())
  92. }
  93. *data = list
  94. }
  95. */
  96. func Get(where string) *http.Response {
  97. resp, err := http.Get(where)
  98. if err != nil {
  99. log.Printf("GET got non 200 code, %v", err)
  100. return nil
  101. } else {
  102. return resp
  103. }
  104. }
  105. // https://developer.roku.com/docs/developer-program/debugging/external-control-api.md#keypress-key-values codes
  106. func KeyPress(ip, command string) string {
  107. return fmt.Sprintf("http://%s:8060/keypress/%s", ip, command)
  108. }
  109. // https://developer.roku.com/docs/developer-program/debugging/external-control-api.md#general-ecp-commands
  110. func Query(ip, what string) string {
  111. return fmt.Sprintf("http://%s:8060/query/%s", ip, what)
  112. }
  113. func PerformKey(ip, command string) bool {
  114. r := Post(KeyPress(ip, command))
  115. if r == nil {
  116. return false
  117. }
  118. defer r.Body.Close()
  119. return true
  120. }
  121. func GetQuery(ip, command string) map[string]interface{} {
  122. resp := Get(Query(ip, command))
  123. if resp == nil {
  124. return nil
  125. }
  126. body, err := io.ReadAll(resp.Body)
  127. if err != nil {
  128. log.Printf("Reading from body got error, %v", err)
  129. }
  130. r := make(map[string]interface{})
  131. err = x2j.Unmarshal(body, &r)
  132. if err != nil {
  133. log.Printf("Got a error parsing XML, %v", err)
  134. }
  135. defer resp.Body.Close()
  136. return r
  137. }
  138. func GetCurrentPlay(ip string) map[string]interface{} {
  139. resp := GetQuery(ip, "media-player")
  140. if resp == nil {
  141. return nil
  142. }
  143. return resp
  144. }
  145. func ObtainDeviceInfo(ip string) interface{} {
  146. resp := GetQuery(ip, "device-info")
  147. if resp == nil {
  148. return nil
  149. }
  150. return resp
  151. }
  152. func ParseDeviceInfo(dev_info interface{}) map[string]interface{} {
  153. dev, err := glom.Glom(dev_info, "device-info.*")
  154. if err != nil {
  155. log.Print(err)
  156. return nil
  157. }
  158. data := make(map[string]interface{})
  159. keys := glom.GetPossible(dev)
  160. for _, key := range keys {
  161. val, err := glom.Glom(dev, key)
  162. if err != nil {
  163. log.Printf("%s -> %v", key, err)
  164. } else {
  165. data[key] = val
  166. }
  167. }
  168. return data
  169. }
  170. // Returns a map of device-infos
  171. func GetDeviceInfo(ip string) map[string]interface{} {
  172. dev := ParseDeviceInfo(ObtainDeviceInfo(ip))
  173. /*keys := glom.GetPossible(dev)
  174. for _, key := range keys {
  175. log.Printf("%s = %v", key, dev[key])
  176. }*/
  177. return dev
  178. }
  179. func GetDevices(wake_on_lan map[string]string) (*[]string, map[string]string) {
  180. var str []string
  181. resp := strings.Split(PostFind(), "\n")
  182. //fmt.Printf("Processing %d lines\n", len(resp))
  183. ip := ""
  184. for _, line := range resp {
  185. if strings.Contains(line, "LOCATION") {
  186. st := strings.ReplaceAll(line, "LOCATION: http://", "")
  187. st = strings.ReplaceAll(st, ":8060/", "")
  188. st = strings.ReplaceAll(st, "\r", "")
  189. //fmt.Printf("IP='%s'", st)
  190. str = append(str, st)
  191. ip = st
  192. } else if strings.Contains(line, "WAKEUP") {
  193. st := strings.ReplaceAll(line, "WAKEUP: MAC=", "")
  194. st = strings.ReplaceAll(st, ";Timeout=10", "")
  195. st = strings.ReplaceAll(st, "\r", "")
  196. //fmt.Printf("IP='%s' MAC='%s'", ip, st)
  197. wake_on_lan[ip] = st
  198. }
  199. }
  200. return &str, wake_on_lan
  201. }
  202. func DebugInfo(ip string) string {
  203. // Query the current playing thing, and get the tv channels
  204. r := GetQuery(ip, "media-player")
  205. if r == nil {
  206. return ""
  207. }
  208. name, _ := glom.Glom(r, "player.plugin.-name")
  209. id, _ := glom.Glom(r, "player.plugin.-id")
  210. /*r1 := GetQuery(ip, "tv-channels")
  211. chan_name, _ := glom.Glom(r1, "tv-channels.channel.name")
  212. chan_phy_id, _ := glom.Glom(r1, "tv-channels.channel.physical-channel")
  213. chan_number, _ := glom.Glom(r1, "tv-channels.channel.number")*/
  214. if name == nil && id == nil {
  215. name = "TV"
  216. id = "No Connection"
  217. }
  218. // Debug print
  219. fmt.Printf("Name: %v (%v)\r\n", name, id)
  220. //fmt.Printf("Channel: %v (%v / %v)\r\n", chan_name, chan_phy_id, chan_number)
  221. return fmt.Sprintf("%v (%v)", name, id)
  222. }
  223. func examplePost() {
  224. keys := GetKeys()
  225. _ = keys // Keep alive
  226. PerformKey("192.168.254.75", keys["mute"])
  227. //Post("http://192.168.254.75:8060/launch/74519")
  228. //PerformKey("192.168.254.75", keys["home"])
  229. /*
  230. //resp := Post("http://192.168.254.75:8060/keypress/volumeMute")
  231. //resp := Post(KeyPress("192.168.254.75", "volumeMute"))
  232. //resp := Get(Query("192.168.254.75", "device-info"))
  233. //resp := GetQuery("192.168.254.75", "tv-active-channel")
  234. resp := GetQuery("192.168.254.75", "tv-channels")
  235. //result, err := glom.Glom(resp, "tv-channels.channel")
  236. _, err := glom.Glom(resp, "*")
  237. if err != nil {
  238. log.Println(err)
  239. } else {
  240. //fmt.Println(result)
  241. fmt.Println(glom.GetPossible(result))
  242. }
  243. */
  244. // Query the current playing thing, and get the tv channels
  245. r := GetQuery("192.168.254.75", "media-player")
  246. name, _ := glom.Glom(r, "player.plugin.-name")
  247. id, _ := glom.Glom(r, "player.plugin.-id")
  248. r1 := GetQuery("192.168.254.75", "tv-channels")
  249. chan_name, _ := glom.Glom(r1, "tv-channels.channel.name")
  250. chan_phy_id, _ := glom.Glom(r1, "tv-channels.channel.physical-channel")
  251. chan_number, _ := glom.Glom(r1, "tv-channels.channel.number")
  252. // Debug print
  253. fmt.Printf("Name: %v (%v)\r\n", name, id)
  254. fmt.Printf("Channel: %v (%v / %v)\r\n", chan_name, chan_phy_id, chan_number)
  255. }