zway-cli 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 89b5dc6bc4d1d4522ae48badc6dcad48dbe83a49
4
+ data.tar.gz: dc92c0606167737297344cd2faad6c03a4fc27cc
5
+ SHA512:
6
+ metadata.gz: ba927b1365d03e76c2da38095f325ca7d10978569031714cd77d358475d941b5ef5a93f809340e0c2fc432ffda3bdb2e26ba7e77645bdd28f64e7349e183ec17
7
+ data.tar.gz: 0eed7371a8ebe2f3ddb217a2bba5f4bd9cb534c183367bcd871a7b648d80c83fdbdf01b0ff131e4cf7a0865ca823def0344d18e2b125362a090c27138f7344b2
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zway-cli.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jeff McFadden
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ # Zway::Cli
2
+
3
+ ## Installation
4
+
5
+ Install it yourself:
6
+
7
+ $ gem install zway-cli
8
+
9
+ ## Usage
10
+
11
+ $ zway login
12
+
13
+ Then edit `~/.zway_config`
14
+
15
+ Now you can check levels and turn things on and off
16
+
17
+ $ zway ZWayVDev_zway_16-0-38 level
18
+ $ zway ZWayVDev_zway_16-0-38 on
19
+
20
+ ## Other Notes
21
+
22
+ The latest session id is kept in `~/.zway_session_id` and each request attempts to use the session cookie to keep things fast. If that fails, it will try to login and set a new session cookie. If you reboot your razberry you'll be forced to setup a new session as the old ones are purged on reboot, for example.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "zway/cli"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'zway'
5
+
6
+ options = {}
7
+ OptionParser.new do |opts|
8
+ opts.banner = "Usage: bin.rb [options]"
9
+
10
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
11
+ options[:verbose] = v
12
+ end
13
+ end.parse!
14
+
15
+ # p options
16
+ # p ARGV
17
+
18
+ zway = ZWay::Client.new
19
+ zway.load_config!
20
+
21
+ # puts "Aliases:"
22
+ # puts zway.aliases
23
+
24
+ if ARGV.last == "login"
25
+ zway.login!
26
+ elsif ARGV.last == "aliases"
27
+ puts "Known Aliases:"
28
+ zway.aliases.each do |a|
29
+ a.each do |k,v|
30
+ puts "#{k} => #{v}"
31
+ end
32
+ end
33
+ else
34
+
35
+ device_id = ARGV.shift
36
+
37
+ while command = ARGV.shift
38
+ if zway.respond_to?( command.to_sym )
39
+ zway.send( command.to_sym, device_id )
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,122 @@
1
+ require "zway/cli/version"
2
+ require 'net/http'
3
+ require 'json'
4
+ require 'yaml'
5
+
6
+ module Zway
7
+ module Cli
8
+ class Client
9
+
10
+ attr_accessor :host
11
+ attr_accessor :username
12
+ attr_accessor :password
13
+ attr_accessor :session_id
14
+ attr_accessor :aliases
15
+
16
+ def initialize( host = "", username = "", password = "" )
17
+ self.host = host
18
+ self.username = username
19
+ self.password = password
20
+ end
21
+
22
+ def load_config!
23
+ if File.exists?( File.join(Dir.home, ".zway_config") )
24
+ config = YAML.load_file(File.join(Dir.home, ".zway_config"))
25
+
26
+ self.host = config["host"] rescue ""
27
+ self.username = config["username"] rescue ""
28
+ self.password = config["password"] rescue ""
29
+ self.aliases = config["aliases"] rescue []
30
+ else
31
+ File.open( File.join(Dir.home, ".zway_config"), 'w' ){ |f|
32
+ config_template = %Q(---
33
+ host: 192.168.10.25:8083
34
+ username: foobarjones
35
+ password: fizzbuzz
36
+ aliases:
37
+ - livingroom_lights: ZWayVDev_zway_15-0-37
38
+
39
+ )
40
+
41
+
42
+ f.write( config_template )
43
+ }
44
+ end
45
+
46
+ if File.exists?( File.join(Dir.home, ".zway_session_id") )
47
+ self.session_id = File.open( File.join(Dir.home, ".zway_session_id") ).read.strip
48
+ else
49
+ # Nothing right now
50
+ end
51
+ end
52
+
53
+ def login!
54
+ uri = URI( "http://#{self.host}/ZAutomation/api/v1/login")
55
+ req = Net::HTTP::Post.new(uri)
56
+ req.body = { form: true, default_ui: 1, keepme: true, login: "#{self.username}", password: "#{self.password}" }.to_json
57
+ req.content_type = 'application/json'
58
+ res = Net::HTTP.start(uri.hostname, uri.port) do |http|
59
+ http.request(req)
60
+ end
61
+
62
+ self.session_id = res['set-cookie'].split( ';' ).delete_if{ |i| i.split( '=' )[0] != 'ZWAYSession' }.first.split( "=" ).last rescue nil
63
+
64
+ if self.session_id.nil?
65
+ puts "Error logging in."
66
+ exit 1
67
+ else
68
+ File.open( File.join(Dir.home, ".zway_session_id"), 'w+' ){ |f| f.write( self.session_id ) }
69
+ end
70
+ end
71
+
72
+ def on( device )
73
+ data = run_command( device, '/command/on' )
74
+ puts data["message"]
75
+ end
76
+
77
+ def off( device )
78
+ data = run_command( device, '/command/off' )
79
+ puts data["message"]
80
+ end
81
+
82
+ def update( device )
83
+ data = run_command( device, '/command/update' )
84
+ puts data["message"]
85
+ end
86
+
87
+ def level( device )
88
+ data = run_command( device, '' )
89
+ puts data["data"]["metrics"]["level"] rescue data
90
+ end
91
+
92
+ def run_command( device, command )
93
+ self.login! if self.session_id.nil?
94
+
95
+ self.aliases.each do |a|
96
+ if a[device]
97
+ device = a[device]
98
+ end
99
+ end
100
+
101
+ uri = URI( "http://#{self.host}/ZAutomation/api/v1/devices/#{device}#{command}")
102
+ req = Net::HTTP::Get.new(uri)
103
+ req['Cookie']="ZWAYSession=#{self.session_id}"
104
+ res = Net::HTTP.start(uri.hostname, uri.port) do |http|
105
+ http.request(req)
106
+ end
107
+ res.body
108
+
109
+ # puts "Body: "
110
+ # puts res.body
111
+
112
+ if res.code == "401"
113
+ puts "Unauthorized. Try logging in first."
114
+ exit 1
115
+ else
116
+ return JSON.parse( res.body )
117
+ end
118
+ end
119
+
120
+ end #Client
121
+ end
122
+ end
@@ -0,0 +1,5 @@
1
+ module Zway
2
+ module Cli
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8 #
2
+
3
+
4
+ lib = File.expand_path('../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'zway/cli/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "zway-cli"
10
+ spec.version = Zway::Cli::VERSION
11
+ spec.authors = ["jeffmcfadden"]
12
+ spec.email = ["jeff@forgeapps.com"]
13
+
14
+ spec.summary = %q{Control your ZWay Server from the command line.}
15
+ spec.description = %q{Control your ZWay Server from the command line.}
16
+ spec.homepage = "https://github.com/jeffmcfadden/zway-cli"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.9"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zway-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jeffmcfadden
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Control your ZWay Server from the command line.
42
+ email:
43
+ - jeff@forgeapps.com
44
+ executables:
45
+ - zway
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - exe/zway
57
+ - lib/zway/cli.rb
58
+ - lib/zway/cli/version.rb
59
+ - zway-cli.gemspec
60
+ homepage: https://github.com/jeffmcfadden/zway-cli
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.4.5
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Control your ZWay Server from the command line.
84
+ test_files: []