poster.go 6.7 KB

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