Class: NanoKVM::Client
- Inherits:
-
Object
- Object
- NanoKVM::Client
- Defined in:
- lib/nanokvm/client.rb
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#token ⇒ Object
Returns the value of attribute token.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #download_image(url) ⇒ Object
- #get_account_info ⇒ Object
- #get_app_version ⇒ Object
- #get_available_images ⇒ Object
- #get_cdrom_info ⇒ Object
- #get_device_info ⇒ Object
- #get_download_image_status ⇒ Object
- #get_gpio_state ⇒ Object
- #get_hardware_info ⇒ Object
- #get_hid_mode ⇒ Object
- #get_interface_status ⇒ Object
- #get_mdns_status ⇒ Object
- #get_memory_limit ⇒ Object
- #get_mounted_image ⇒ Object
- #get_network_info ⇒ Object
- #get_oled_info ⇒ Object
- #get_preview_state ⇒ Object
- #get_script_info ⇒ Object
- #get_ssh_status ⇒ Object
- #get_tailscale_status ⇒ Object
- #get_virtual_device_status ⇒ Object
- #get_wol_mac ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #is_download_image_enabled ⇒ Object
- #is_password_updated ⇒ Object
- #login ⇒ Object
- #mount_image(file_path) ⇒ Object
- #power_button(duration = 800) ⇒ Object
- #reset_button(duration = 200) ⇒ Object
- #reset_hdmi ⇒ Object
- #reset_hid ⇒ Object
- #send_text(content) ⇒ Object
- #set_hid_mode(mode) ⇒ Object
- #set_memory_limit(enabled, limit_mb) ⇒ Object
- #set_oled_sleep(sleep_seconds) ⇒ Object
- #set_preview_state(enable) ⇒ Object
- #system_reboot ⇒ Object
- #toggle_virtual_device(device) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/nanokvm/client.rb', line 17 def initialize( = {}) @host = [:host] || NanoKVM.configuration.default_host @username = [:username] || NanoKVM.configuration.default_username @password = [:password] || NanoKVM.configuration.default_password @token = [:token] @debug = [:debug] @secret_key = [:secret_key] || NanoKVM::DEFAULT_SECRET_KEY raise ArgumentError, "Host is required" unless @host end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
15 16 17 |
# File 'lib/nanokvm/client.rb', line 15 def debug @debug end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
14 15 16 |
# File 'lib/nanokvm/client.rb', line 14 def host @host end |
#token ⇒ Object
Returns the value of attribute token.
15 16 17 |
# File 'lib/nanokvm/client.rb', line 15 def token @token end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
14 15 16 |
# File 'lib/nanokvm/client.rb', line 14 def username @username end |
Instance Method Details
#download_image(url) ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/nanokvm/client.rb', line 152 def download_image(url) payload = { file: url }.to_json post("/api/download/image", payload) end |
#get_account_info ⇒ Object
116 117 118 |
# File 'lib/nanokvm/client.rb', line 116 def get_account_info get("/api/auth/account") end |
#get_app_version ⇒ Object
124 125 126 |
# File 'lib/nanokvm/client.rb', line 124 def get_app_version get("/api/application/version") end |
#get_available_images ⇒ Object
76 77 78 |
# File 'lib/nanokvm/client.rb', line 76 def get_available_images get("/api/storage/image") end |
#get_cdrom_info ⇒ Object
201 202 203 |
# File 'lib/nanokvm/client.rb', line 201 def get_cdrom_info get("/api/storage/cdrom") end |
#get_device_info ⇒ Object
50 51 52 |
# File 'lib/nanokvm/client.rb', line 50 def get_device_info get("/api/vm/info") end |
#get_download_image_status ⇒ Object
148 149 150 |
# File 'lib/nanokvm/client.rb', line 148 def get_download_image_status get("/api/download/image/status") end |
#get_gpio_state ⇒ Object
54 55 56 |
# File 'lib/nanokvm/client.rb', line 54 def get_gpio_state get("/api/vm/gpio") end |
#get_hardware_info ⇒ Object
160 161 162 |
# File 'lib/nanokvm/client.rb', line 160 def get_hardware_info get("/api/vm/hardware") end |
#get_hid_mode ⇒ Object
209 210 211 |
# File 'lib/nanokvm/client.rb', line 209 def get_hid_mode get("/api/hid/mode") end |
#get_interface_status ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/nanokvm/client.rb', line 233 def get_interface_status # Collect information from multiple endpoints data = {} # Get virtual device status for ethernet begin virtual_devices = get_virtual_device_status["data"] data["eth_status"] = virtual_devices["network"] ? "CONNECTED" : "DISCONNECTED" if virtual_devices.key?("network") rescue => e # Ignore errors end # Get HID mode begin hid_info = get_hid_mode["data"] data["hid_mode"] = hid_info["mode"] if hid_info.key?("mode") data["hid_status"] = "CONNECTED" # Assume connected if we can get mode rescue => e # Ignore errors end # Get GPIO status for LEDs begin gpio = get_gpio_state["data"] # Map LED states to status data["pwr_led"] = gpio["pwr"] ? "ON" : "OFF" if gpio.key?("pwr") data["hdd_led"] = gpio["hdd"] ? "ON" : "OFF" if gpio.key?("hdd") rescue => e # Ignore errors end # Get device info for any additional information begin info = get_device_info["data"] data["device_ip"] = info["ip"] if info.key?("ip") data["application_version"] = info["application"] if info.key?("application") rescue => e # Ignore errors end # Get hardware info begin hardware = get_hardware_info["data"] # Add hardware info hardware.each do |key, value| data[key] = value end rescue => e # Ignore errors end # Note about HDMI status data["hdmi_status"] = "NOT AVAILABLE VIA API" # The API doesn't provide HDMI status data["hdmi_note"] = "HDMI status only visible on device screen" # Return all collected data { "code" => 0, "msg" => "success", "data" => data } end |
#get_mdns_status ⇒ Object
197 198 199 |
# File 'lib/nanokvm/client.rb', line 197 def get_mdns_status get("/api/vm/mdns") end |
#get_memory_limit ⇒ Object
168 169 170 |
# File 'lib/nanokvm/client.rb', line 168 def get_memory_limit get("/api/vm/memory/limit") end |
#get_mounted_image ⇒ Object
80 81 82 |
# File 'lib/nanokvm/client.rb', line 80 def get_mounted_image get("/api/storage/image/mounted") end |
#get_network_info ⇒ Object
120 121 122 |
# File 'lib/nanokvm/client.rb', line 120 def get_network_info get("/api/network/wifi") end |
#get_oled_info ⇒ Object
181 182 183 |
# File 'lib/nanokvm/client.rb', line 181 def get_oled_info get("/api/vm/oled") end |
#get_preview_state ⇒ Object
221 222 223 |
# File 'lib/nanokvm/client.rb', line 221 def get_preview_state get("/api/application/preview") end |
#get_script_info ⇒ Object
164 165 166 |
# File 'lib/nanokvm/client.rb', line 164 def get_script_info get("/api/vm/script") end |
#get_ssh_status ⇒ Object
193 194 195 |
# File 'lib/nanokvm/client.rb', line 193 def get_ssh_status get("/api/vm/ssh") end |
#get_tailscale_status ⇒ Object
140 141 142 |
# File 'lib/nanokvm/client.rb', line 140 def get_tailscale_status get("/api/extensions/tailscale/status") end |
#get_virtual_device_status ⇒ Object
92 93 94 |
# File 'lib/nanokvm/client.rb', line 92 def get_virtual_device_status get("/api/vm/device/virtual") end |
#get_wol_mac ⇒ Object
205 206 207 |
# File 'lib/nanokvm/client.rb', line 205 def get_wol_mac get("/api/network/wol/mac") end |
#is_download_image_enabled ⇒ Object
144 145 146 |
# File 'lib/nanokvm/client.rb', line 144 def is_download_image_enabled get("/api/download/image/enabled") end |
#is_password_updated ⇒ Object
136 137 138 |
# File 'lib/nanokvm/client.rb', line 136 def is_password_updated get("/api/auth/password") end |
#login ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/nanokvm/client.rb', line 28 def login puts "Attempting to authenticate with NanoKVM..." if @debug # Encrypt and encode password puts "Encrypting password..." if @debug = obfuscate_password(@password) puts "Obfuscated password: #{obfuscated_password}" if @debug # Create the JSON payload - the password is already URI encoded payload = { username: @username, password: }.to_json puts "Login payload: #{payload}" if @debug # Send the request response = post("/api/auth/login", payload) @token = response.dig("data", "token") raise AuthenticationError, "Failed to authenticate" unless @token puts "Authentication successful, token: #{@token}" if @debug @token end |
#mount_image(file_path) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/nanokvm/client.rb', line 84 def mount_image(file_path) payload = { file: file_path }.to_json post("/api/storage/image/mount", payload) end |
#power_button(duration = 800) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/nanokvm/client.rb', line 58 def (duration = 800) payload = { type: "power", duration: duration }.to_json post("/api/vm/gpio", payload) end |
#reset_button(duration = 200) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/nanokvm/client.rb', line 67 def (duration = 200) payload = { type: "reset", duration: duration }.to_json post("/api/vm/gpio", payload) end |
#reset_hdmi ⇒ Object
132 133 134 |
# File 'lib/nanokvm/client.rb', line 132 def reset_hdmi post("/api/vm/hdmi/reset", "{}") end |
#reset_hid ⇒ Object
112 113 114 |
# File 'lib/nanokvm/client.rb', line 112 def reset_hid post("/api/hid/reset", "{}") end |
#send_text(content) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/nanokvm/client.rb', line 104 def send_text(content) payload = { content: content }.to_json post("/api/hid/paste", payload) end |
#set_hid_mode(mode) ⇒ Object
213 214 215 216 217 218 219 |
# File 'lib/nanokvm/client.rb', line 213 def set_hid_mode(mode) payload = { mode: mode }.to_json post("/api/hid/mode", payload) end |
#set_memory_limit(enabled, limit_mb) ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/nanokvm/client.rb', line 172 def set_memory_limit(enabled, limit_mb) payload = { enabled: enabled, limit: limit_mb }.to_json post("/api/vm/memory/limit", payload) end |
#set_oled_sleep(sleep_seconds) ⇒ Object
185 186 187 188 189 190 191 |
# File 'lib/nanokvm/client.rb', line 185 def set_oled_sleep(sleep_seconds) payload = { sleep: sleep_seconds }.to_json post("/api/vm/oled", payload) end |
#set_preview_state(enable) ⇒ Object
225 226 227 228 229 230 231 |
# File 'lib/nanokvm/client.rb', line 225 def set_preview_state(enable) payload = { enable: enable }.to_json post("/api/application/preview", payload) end |
#system_reboot ⇒ Object
128 129 130 |
# File 'lib/nanokvm/client.rb', line 128 def system_reboot post("/api/vm/system/reboot", "{}") end |
#toggle_virtual_device(device) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/nanokvm/client.rb', line 96 def toggle_virtual_device(device) payload = { device: device }.to_json post("/api/vm/device/virtual", payload) end |