1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package main
- import (
- "embed"
- "flag"
- "io/fs"
- "log"
- "net/http"
- "os"
- "os/signal"
- "syscall"
- "imuslab.com/remdeskvm/remdeskd/mod/remdeshid"
- "imuslab.com/remdeskvm/remdeskd/mod/usbcapture"
- )
- const development = true
- var (
- usbKVMDeviceName = flag.String("usbkvm", "/dev/ttyUSB0", "USB KVM device file path")
- usbKVMBaudRate = flag.Int("baudrate", 115200, "USB KVM baud rate")
- captureDeviceName = flag.String("capture", "/dev/video0", "Video capture device file path")
- usbKVM *remdeshid.Controller
- videoCapture *usbcapture.Instance
- )
- /* Web Server Static Files */
- //go:embed www
- var embeddedFiles embed.FS
- var webfs http.FileSystem
- func init() {
- // Initiate the web server static files
- if development {
- webfs = http.Dir("./www")
- } else {
- // Embed the ./www folder and trim the prefix
- subFS, err := fs.Sub(embeddedFiles, "www")
- if err != nil {
- log.Fatal(err)
- }
- webfs = http.FS(subFS)
- }
- // Initiate the HID controller
- usbKVM = remdeshid.NewHIDController(&remdeshid.Config{
- PortName: *usbKVMDeviceName,
- BaudRate: *usbKVMBaudRate,
- })
- // Initiate the video capture device
- var err error
- videoCapture, err = usbcapture.NewInstance(&usbcapture.Config{
- DeviceName: *captureDeviceName,
- Resolution: &usbcapture.SizeInfo{
- Width: 1920,
- Height: 1080,
- FPS: []int{30},
- },
- })
- if err != nil {
- log.Fatalf("Failed to create video capture instance: %v", err)
- }
- }
- func main() {
- //Start the HID controller
- err := usbKVM.Connect()
- if err != nil {
- log.Fatal(err)
- }
- // Handle program exit to close the HID controller
- c := make(chan os.Signal, 1)
- signal.Notify(c, os.Interrupt, syscall.SIGTERM)
- go func() {
- <-c
- log.Println("Shutting down...")
- usbKVM.Close()
- os.Exit(0)
- }()
- // Start the web server
- http.HandleFunc("/hid", usbKVM.HIDWebSocketHandler)
- http.Handle("/", http.FileServer(webfs))
- addr := ":8080"
- log.Printf("Serving on http://localhost%s\n", addr)
- log.Fatal(http.ListenAndServe(addr, nil))
- }
|