Class: ZWay::Cli::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zway/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "", username = "", password = "") ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
# File 'lib/zway/cli.rb', line 16

def initialize( host = "", username = "", password = "" )
  self.host     = host
  self.username = username
  self.password = password
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



14
15
16
# File 'lib/zway/cli.rb', line 14

def aliases
  @aliases
end

#hostObject

Returns the value of attribute host.



10
11
12
# File 'lib/zway/cli.rb', line 10

def host
  @host
end

#passwordObject

Returns the value of attribute password.



12
13
14
# File 'lib/zway/cli.rb', line 12

def password
  @password
end

#session_idObject

Returns the value of attribute session_id.



13
14
15
# File 'lib/zway/cli.rb', line 13

def session_id
  @session_id
end

#usernameObject

Returns the value of attribute username.



11
12
13
# File 'lib/zway/cli.rb', line 11

def username
  @username
end

Instance Method Details

#level(device) ⇒ Object



87
88
89
90
# File 'lib/zway/cli.rb', line 87

def level( device )
  data = run_command( device, '' )
  puts data["data"]["metrics"]["level"] rescue data
end

#load_config!Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zway/cli.rb', line 22

def load_config!
  if File.exists?( File.join(Dir.home, ".zway_config") )
    config = YAML.load_file(File.join(Dir.home, ".zway_config"))

    self.host     = config["host"]     rescue ""
    self.username = config["username"] rescue ""
    self.password = config["password"] rescue ""
    self.aliases  = config["aliases"]  rescue []
  else
    File.open( File.join(Dir.home, ".zway_config"), 'w' ){ |f|
config_template = %Q(---
host:     192.168.10.25:8083
username: foobarjones
password: fizzbuzz
aliases:
    - livingroom_lights: ZWayVDev_zway_15-0-37

)


      f.write( config_template )
    }
  end

  if File.exists?( File.join(Dir.home, ".zway_session_id") )
    self.session_id = File.open( File.join(Dir.home, ".zway_session_id") ).read.strip
  else
    # Nothing right now
  end
end

#login!Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/zway/cli.rb', line 53

def login!
  uri = URI( "http://#{self.host}/ZAutomation/api/v1/login")
  req = Net::HTTP::Post.new(uri)
  req.body = { form: true, default_ui: 1, keepme: true, login: "#{self.username}", password: "#{self.password}" }.to_json
  req.content_type = 'application/json'
  res = Net::HTTP.start(uri.hostname, uri.port) do |http|
    http.request(req)
  end

  self.session_id = res['set-cookie'].split( ';' ).delete_if{ |i| i.split( '=' )[0] != 'ZWAYSession' }.first.split( "=" ).last rescue nil

  if self.session_id.nil?
    puts "Error logging in."
    exit 1
  else
    File.open( File.join(Dir.home, ".zway_session_id"), 'w+' ){ |f| f.write( self.session_id ) }
  end
end

#off(device) ⇒ Object



77
78
79
80
# File 'lib/zway/cli.rb', line 77

def off( device )
  data = run_command( device, '/command/off' )
  puts data["message"]
end

#on(device) ⇒ Object



72
73
74
75
# File 'lib/zway/cli.rb', line 72

def on( device )
  data = run_command( device, '/command/on' )
  puts data["message"]
end

#run_command(device, command) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/zway/cli.rb', line 92

def run_command( device, command )
  self.login! if self.session_id.nil?

  self.aliases.each do |a|
    if a[device]
      device = a[device]
    end
  end

  uri = URI( "http://#{self.host}/ZAutomation/api/v1/devices/#{device}#{command}")
  req = Net::HTTP::Get.new(uri)
  req['Cookie']="ZWAYSession=#{self.session_id}"
  res = Net::HTTP.start(uri.hostname, uri.port) do |http|
    http.request(req)
  end
  res.body

  # puts "Body: "
  # puts res.body

  if res.code == "401"
    puts "Unauthorized. Try logging in first."
    exit 1
  else
    return JSON.parse( res.body )
  end
end

#update(device) ⇒ Object



82
83
84
85
# File 'lib/zway/cli.rb', line 82

def update( device )
  data = run_command( device, '/command/update' )
  puts data["message"]
end