handlers.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package kvmaux
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. )
  7. // Handler for switching USB to KVM side
  8. func (c *AuxMcu) HandleSwitchUSBToKVM(w http.ResponseWriter, r *http.Request) {
  9. if err := c.SwitchUSBToKVM(); err != nil {
  10. http.Error(w, err.Error(), http.StatusInternalServerError)
  11. return
  12. }
  13. log.Println("Switched USB mass storage to KVM side")
  14. w.WriteHeader(http.StatusOK)
  15. }
  16. // Handler for switching USB to Remote side
  17. func (c *AuxMcu) HandleSwitchUSBToRemote(w http.ResponseWriter, r *http.Request) {
  18. if err := c.SwitchUSBToRemote(); err != nil {
  19. http.Error(w, err.Error(), http.StatusInternalServerError)
  20. return
  21. }
  22. log.Println("Switched USB mass storage to remote side")
  23. w.WriteHeader(http.StatusOK)
  24. }
  25. // Handler for pressing the power button
  26. func (c *AuxMcu) HandlePressPowerButton(w http.ResponseWriter, r *http.Request) {
  27. if err := c.PressPowerButton(); err != nil {
  28. http.Error(w, err.Error(), http.StatusInternalServerError)
  29. return
  30. }
  31. w.WriteHeader(http.StatusOK)
  32. }
  33. // Handler for releasing the power button
  34. func (c *AuxMcu) HandleReleasePowerButton(w http.ResponseWriter, r *http.Request) {
  35. if err := c.ReleasePowerButton(); err != nil {
  36. http.Error(w, err.Error(), http.StatusInternalServerError)
  37. return
  38. }
  39. w.WriteHeader(http.StatusOK)
  40. }
  41. // Handler for pressing the reset button
  42. func (c *AuxMcu) HandlePressResetButton(w http.ResponseWriter, r *http.Request) {
  43. if err := c.PressResetButton(); err != nil {
  44. http.Error(w, err.Error(), http.StatusInternalServerError)
  45. return
  46. }
  47. w.WriteHeader(http.StatusOK)
  48. }
  49. // Handler for releasing the reset button
  50. func (c *AuxMcu) HandleReleaseResetButton(w http.ResponseWriter, r *http.Request) {
  51. if err := c.ReleaseResetButton(); err != nil {
  52. http.Error(w, err.Error(), http.StatusInternalServerError)
  53. return
  54. }
  55. w.WriteHeader(http.StatusOK)
  56. }
  57. // Handler for getting the UUID
  58. func (c *AuxMcu) HandleGetUUID(w http.ResponseWriter, r *http.Request) {
  59. uuid, err := c.GetUUID()
  60. if err != nil {
  61. http.Error(w, err.Error(), http.StatusInternalServerError)
  62. return
  63. }
  64. w.Header().Set("Content-Type", "application/json")
  65. json.NewEncoder(w).Encode(map[string]string{"uuid": uuid})
  66. }
  67. // Handler for getting the USB mass storage side
  68. func (c *AuxMcu) HandleGetUSBMassStorageSide(w http.ResponseWriter, r *http.Request) {
  69. side := c.GetUSBMassStorageSide()
  70. w.Header().Set("Content-Type", "application/json")
  71. json.NewEncoder(w).Encode(map[string]interface{}{"usb_mass_storage_side": side})
  72. }