handlers.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package dezukvm
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. )
  6. func (d *DezukVM) HandleVideoStreams(w http.ResponseWriter, r *http.Request, instanceUuid string) {
  7. targetInstance, err := d.GetInstanceByUUID(instanceUuid)
  8. if err != nil {
  9. http.Error(w, "Instance with specified UUID not found", http.StatusNotFound)
  10. return
  11. }
  12. targetInstance.usbCaptureDevice.ServeVideoStream(w, r)
  13. }
  14. func (d *DezukVM) HandleAudioStreams(w http.ResponseWriter, r *http.Request, instanceUuid string) {
  15. targetInstance, err := d.GetInstanceByUUID(instanceUuid)
  16. if err != nil {
  17. http.Error(w, "Instance with specified UUID not found", http.StatusNotFound)
  18. return
  19. }
  20. targetInstance.usbCaptureDevice.AudioStreamingHandler(w, r)
  21. }
  22. func (d *DezukVM) HandleHIDEvents(w http.ResponseWriter, r *http.Request, instanceUuid string) {
  23. targetInstance, err := d.GetInstanceByUUID(instanceUuid)
  24. if err != nil {
  25. http.Error(w, "Instance with specified UUID not found", http.StatusNotFound)
  26. return
  27. }
  28. targetInstance.usbKVMController.HIDWebSocketHandler(w, r)
  29. }
  30. // HandleMassStorageSideSwitch handles the request to switch the USB mass storage side.
  31. // there is only two state for the USB mass storage side, KVM side or Remote side.
  32. // isKvmSide = true means switch to KVM side, otherwise switch to Remote side.
  33. func (d *DezukVM) HandleMassStorageSideSwitch(w http.ResponseWriter, r *http.Request, instanceUuid string, isKvmSide bool) {
  34. targetInstance, err := d.GetInstanceByUUID(instanceUuid)
  35. if err != nil {
  36. http.Error(w, "Instance with specified UUID not found", http.StatusNotFound)
  37. return
  38. }
  39. if targetInstance.auxMCUController == nil {
  40. http.Error(w, "Auxiliary MCU controller not initialized or missing", http.StatusInternalServerError)
  41. return
  42. }
  43. if isKvmSide {
  44. err = targetInstance.auxMCUController.SwitchUSBToKVM()
  45. } else {
  46. err = targetInstance.auxMCUController.SwitchUSBToRemote()
  47. }
  48. if err != nil {
  49. http.Error(w, "Failed to switch USB mass storage side: "+err.Error(), http.StatusInternalServerError)
  50. return
  51. }
  52. w.WriteHeader(http.StatusOK)
  53. w.Write([]byte("OK"))
  54. }
  55. func (d *DezukVM) HandleListInstances(w http.ResponseWriter, r *http.Request) {
  56. instances := []map[string]interface{}{}
  57. for _, instance := range d.UsbKvmInstance {
  58. instances = append(instances, map[string]interface{}{
  59. "uuid": instance.UUID(),
  60. "video_capture_dev": instance.Config.VideoCaptureDevicePath,
  61. "audio_capture_dev": instance.Config.AudioCaptureDevicePath,
  62. "video_resolution_width": instance.Config.CaptureVideoResolutionWidth,
  63. "video_resolution_height": instance.Config.CaptureeVideoResolutionHeight,
  64. "video_framerate": instance.Config.CaptureeVideoFPS,
  65. "audio_sample_rate": instance.Config.CaptureAudioSampleRate,
  66. "audio_channels": instance.Config.CaptureAudioChannels,
  67. "stream_info": instance.usbCaptureDevice.GetStreamInfo(),
  68. "usb_kvm_device": instance.Config.USBKVMDevicePath,
  69. "aux_mcu_device": instance.Config.AuxMCUDevicePath,
  70. "usb_mass_storage_side": instance.auxMCUController.GetUSBMassStorageSide(),
  71. })
  72. }
  73. w.Header().Set("Content-Type", "application/json")
  74. json.NewEncoder(w).Encode(instances)
  75. }