Class: Moob::Megatrends

Inherits:
BaseLom show all
Defined in:
lib/moob/megatrends.rb

Instance Attribute Summary

Attributes inherited from BaseLom

#hostname, #username

Instance Method Summary collapse

Methods inherited from BaseLom

action, actions, #logout, name

Constructor Details

#initialize(hostname, options = {}) ⇒ Megatrends

Returns a new instance of Megatrends.


5
6
7
8
9
10
11
12
13
14
15
# File 'lib/moob/megatrends.rb', line 5

def initialize hostname, options = {}
  super hostname, options
  @username ||= 'root'
  @password ||= 'superuser'
  begin
    @ip = Socket.getaddrinfo(hostname, nil)[0][3]
  rescue
    raise "Couldn't resolve \"#{hostname}\""
  end
  @session.base_url = "#{@transport}://#{@ip}/"
end

Instance Method Details

#authenticateObject


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/moob/megatrends.rb', line 17

def authenticate
  auth = @session.post 'rpc/WEBSES/create.asp',
    { 'WEBVAR_USERNAME' => @username, 'WEBVAR_PASSWORD' => @password }

  raise ResponseError.new auth unless auth.status == 200

  auth.body =~ /'SESSION_COOKIE' *: *'([^']+)'/
  raise "Couldn't find auth cookie in \"#{auth.body}\"" unless $&

  @cookie = "test=1; path=/; SessionCookie=#{$1}"
  return self
end

#detectObject


30
31
32
33
34
35
36
37
# File 'lib/moob/megatrends.rb', line 30

def detect
  begin
    home = @session.get 'page/login.html'
    home.body =~ /\.\.\/res\/banner_right\.png/
  rescue
    false
  end
end

#jnlpObject


40
41
42
43
44
45
46
# File 'lib/moob/megatrends.rb', line 40

def jnlp
  @session.ignore_content_length = true
  viewer = @session.get 'Java/jviewer.jnlp', { 'Cookie' => @cookie }
  raise ResponseError.new viewer unless viewer.status == 200

  return viewer.body
end

#pcycleObject


66
# File 'lib/moob/megatrends.rb', line 66

def pcycle;    power_action 2; end

#poffObject


64
# File 'lib/moob/megatrends.rb', line 64

def poff;      power_action 0; end

#ponObject


65
# File 'lib/moob/megatrends.rb', line 65

def pon;       power_action 1; end

#power_action(action) ⇒ Object


48
49
50
51
52
53
54
55
56
57
# File 'lib/moob/megatrends.rb', line 48

def power_action action
  req = @session.post 'rpc/hostctl.asp',
    { 'WEBVAR_POWER_CMD' => action },
    { 'Cookie' => @cookie }
  raise ResponseError.new req unless req.status == 200
  unless req.body =~ /WEBVAR_STRUCTNAME_HL_POWERSTATUS/
    raise 'The answer looks wrong'
  end
  return nil
end

#presetObject


67
# File 'lib/moob/megatrends.rb', line 67

def preset;    power_action 3; end

#pstatusObject


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/moob/megatrends.rb', line 71

def pstatus
  status = @session.get 'rpc/hoststatus.asp',
    { 'Cookie' => @cookie }
  raise ResponseError.new status unless status.status == 200
  raise 'Couldn\'t read the state' unless status.body =~ /'JF_STATE' : (.),/
  case $1
  when '0'
    return :off
  when '1'
    return :on
  end
end

#soft_poffObject


68
# File 'lib/moob/megatrends.rb', line 68

def soft_poff; power_action 5; end