main.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package main
  2. import (
  3. "embed"
  4. "flag"
  5. "io/fs"
  6. "log"
  7. "net/http"
  8. "os"
  9. "os/signal"
  10. "syscall"
  11. "imuslab.com/remdeskvm/remdeskd/mod/remdeshid"
  12. "imuslab.com/remdeskvm/remdeskd/mod/usbcapture"
  13. )
  14. const development = true
  15. var (
  16. usbKVMDeviceName = flag.String("usbkvm", "/dev/ttyUSB0", "USB KVM device file path")
  17. usbKVMBaudRate = flag.Int("baudrate", 115200, "USB KVM baud rate")
  18. captureDeviceName = flag.String("capture", "/dev/video0", "Video capture device file path")
  19. usbKVM *remdeshid.Controller
  20. videoCapture *usbcapture.Instance
  21. )
  22. /* Web Server Static Files */
  23. //go:embed www
  24. var embeddedFiles embed.FS
  25. var webfs http.FileSystem
  26. func init() {
  27. // Initiate the web server static files
  28. if development {
  29. webfs = http.Dir("./www")
  30. } else {
  31. // Embed the ./www folder and trim the prefix
  32. subFS, err := fs.Sub(embeddedFiles, "www")
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. webfs = http.FS(subFS)
  37. }
  38. // Initiate the HID controller
  39. usbKVM = remdeshid.NewHIDController(&remdeshid.Config{
  40. PortName: *usbKVMDeviceName,
  41. BaudRate: *usbKVMBaudRate,
  42. })
  43. // Initiate the video capture device
  44. var err error
  45. videoCapture, err = usbcapture.NewInstance(&usbcapture.Config{
  46. DeviceName: *captureDeviceName,
  47. Resolution: &usbcapture.SizeInfo{
  48. Width: 1920,
  49. Height: 1080,
  50. FPS: []int{30},
  51. },
  52. })
  53. if err != nil {
  54. log.Fatalf("Failed to create video capture instance: %v", err)
  55. }
  56. }
  57. func main() {
  58. //Start the HID controller
  59. err := usbKVM.Connect()
  60. if err != nil {
  61. log.Fatal(err)
  62. }
  63. // Handle program exit to close the HID controller
  64. c := make(chan os.Signal, 1)
  65. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  66. go func() {
  67. <-c
  68. log.Println("Shutting down...")
  69. usbKVM.Close()
  70. os.Exit(0)
  71. }()
  72. // Start the web server
  73. http.HandleFunc("/hid", usbKVM.HIDWebSocketHandler)
  74. http.Handle("/", http.FileServer(webfs))
  75. addr := ":8080"
  76. log.Printf("Serving on http://localhost%s\n", addr)
  77. log.Fatal(http.ListenAndServe(addr, nil))
  78. }