Class: NanoKVM::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nanokvm/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
# File 'lib/nanokvm/client.rb', line 17

def initialize(options = {})
  @host = options[:host] || NanoKVM.configuration.default_host
  @username = options[:username] || NanoKVM.configuration.default_username
  @password = options[:password] || NanoKVM.configuration.default_password
  @token = options[:token]
  @debug = options[:debug]
  @secret_key = options[:secret_key] || NanoKVM::DEFAULT_SECRET_KEY
  
  raise ArgumentError, "Host is required" unless @host
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



15
16
17
# File 'lib/nanokvm/client.rb', line 15

def debug
  @debug
end

#hostObject (readonly)

Returns the value of attribute host.



14
15
16
# File 'lib/nanokvm/client.rb', line 14

def host
  @host
end

#tokenObject

Returns the value of attribute token.



15
16
17
# File 'lib/nanokvm/client.rb', line 15

def token
  @token
end

#usernameObject (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_infoObject



116
117
118
# File 'lib/nanokvm/client.rb', line 116

def 
  get("/api/auth/account")
end

#get_app_versionObject



124
125
126
# File 'lib/nanokvm/client.rb', line 124

def get_app_version
  get("/api/application/version")
end

#get_available_imagesObject



76
77
78
# File 'lib/nanokvm/client.rb', line 76

def get_available_images
  get("/api/storage/image")
end

#get_cdrom_infoObject



201
202
203
# File 'lib/nanokvm/client.rb', line 201

def get_cdrom_info
  get("/api/storage/cdrom")
end

#get_device_infoObject



50
51
52
# File 'lib/nanokvm/client.rb', line 50

def get_device_info
  get("/api/vm/info")
end

#get_download_image_statusObject



148
149
150
# File 'lib/nanokvm/client.rb', line 148

def get_download_image_status
  get("/api/download/image/status")
end

#get_gpio_stateObject



54
55
56
# File 'lib/nanokvm/client.rb', line 54

def get_gpio_state
  get("/api/vm/gpio")
end

#get_hardware_infoObject



160
161
162
# File 'lib/nanokvm/client.rb', line 160

def get_hardware_info
  get("/api/vm/hardware")
end

#get_hid_modeObject



209
210
211
# File 'lib/nanokvm/client.rb', line 209

def get_hid_mode
  get("/api/hid/mode")
end

#get_interface_statusObject



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_statusObject



197
198
199
# File 'lib/nanokvm/client.rb', line 197

def get_mdns_status
  get("/api/vm/mdns")
end

#get_memory_limitObject



168
169
170
# File 'lib/nanokvm/client.rb', line 168

def get_memory_limit
  get("/api/vm/memory/limit")
end

#get_mounted_imageObject



80
81
82
# File 'lib/nanokvm/client.rb', line 80

def get_mounted_image
  get("/api/storage/image/mounted")
end

#get_network_infoObject



120
121
122
# File 'lib/nanokvm/client.rb', line 120

def get_network_info
  get("/api/network/wifi")
end

#get_oled_infoObject



181
182
183
# File 'lib/nanokvm/client.rb', line 181

def get_oled_info
  get("/api/vm/oled")
end

#get_preview_stateObject



221
222
223
# File 'lib/nanokvm/client.rb', line 221

def get_preview_state
  get("/api/application/preview")
end

#get_script_infoObject



164
165
166
# File 'lib/nanokvm/client.rb', line 164

def get_script_info
  get("/api/vm/script")
end

#get_ssh_statusObject



193
194
195
# File 'lib/nanokvm/client.rb', line 193

def get_ssh_status
  get("/api/vm/ssh")
end

#get_tailscale_statusObject



140
141
142
# File 'lib/nanokvm/client.rb', line 140

def get_tailscale_status
  get("/api/extensions/tailscale/status")
end

#get_virtual_device_statusObject



92
93
94
# File 'lib/nanokvm/client.rb', line 92

def get_virtual_device_status
  get("/api/vm/device/virtual")
end

#get_wol_macObject



205
206
207
# File 'lib/nanokvm/client.rb', line 205

def get_wol_mac
  get("/api/network/wol/mac")
end

#is_download_image_enabledObject



144
145
146
# File 'lib/nanokvm/client.rb', line 144

def is_download_image_enabled
  get("/api/download/image/enabled")
end

#is_password_updatedObject



136
137
138
# File 'lib/nanokvm/client.rb', line 136

def is_password_updated
  get("/api/auth/password")
end

#loginObject



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 
  puts "Attempting to authenticate with NanoKVM..." if @debug
  
  # Encrypt and encode password
  puts "Encrypting password..." if @debug
  obfuscated_password = obfuscate_password(@password)
  puts "Obfuscated password: #{obfuscated_password}" if @debug
  
  # Create the JSON payload - the password is already URI encoded
  payload = { username: @username, password: obfuscated_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 power_button(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 reset_button(duration = 200)
  payload = {
    type: "reset",
    duration: duration
  }.to_json
  
  post("/api/vm/gpio", payload)
end

#reset_hdmiObject



132
133
134
# File 'lib/nanokvm/client.rb', line 132

def reset_hdmi
  post("/api/vm/hdmi/reset", "{}")
end

#reset_hidObject



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_rebootObject



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