roku1.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package main
  2. import (
  3. "errors"
  4. "log"
  5. "net"
  6. "os"
  7. "time"
  8. "fyne.io/fyne/v2"
  9. "fyne.io/fyne/v2/dialog"
  10. )
  11. func PostFind(w fyne.Window) string {
  12. addr, err := net.ResolveUDPAddr("udp", "239.255.255.250:1900")
  13. if err != nil {
  14. log.Print(err)
  15. }
  16. laddr, err := net.ResolveUDPAddr("udp", ":0") // "192.168.254.13:9000")
  17. if err != nil {
  18. log.Print(err)
  19. }
  20. // "192.168.254.13:9000")
  21. // conn, err := net.DialUDP("udp", laddr, addr)
  22. /*
  23. ListenUDP -- to SEND? WAT?
  24. https://github.com/golang/go/issues/13391
  25. Ok, maybe you don't want to read it -- the developers are cranky when
  26. asked why Listen would be able to SEND.
  27. */
  28. conn, err := net.ListenUDP("udp", laddr)
  29. if err != nil {
  30. log.Print(err)
  31. }
  32. defer conn.Close()
  33. conn.WriteTo([]byte("M-SEARCH * HTTP/1.1\nHost: 239.255.255.250:1900\nMan: \"ssdp:discover\"\nST: roku:ecp\n\n"), addr)
  34. // conn.Write(file)
  35. log.Println("Posted Request for devices")
  36. // future: Handle multiple devices
  37. conn.SetReadDeadline(time.Now().Add(time.Second * time.Duration(5)))
  38. buffer := make([]byte, 512)
  39. n, fromaddr, err := conn.ReadFrom(buffer) // Read(buffer)
  40. log.Printf("%d, %#v, %#v", n, fromaddr, err)
  41. if err != nil {
  42. dialog.ShowError(err, w)
  43. }
  44. var output string = string(buffer[0:n])
  45. log.Printf("GOT: [%s]", output)
  46. // log.Printf("Read: %#v\n", buffer)
  47. return output
  48. }
  49. func FileExists(path string) bool {
  50. _, err := os.Stat(path)
  51. return !errors.Is(err, os.ErrNotExist)
  52. }
  53. /*
  54. func main() {
  55. fmt.Println("Start:")
  56. PostFind()
  57. fmt.Println("Done!")
  58. }
  59. */