wink_scheduler 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d2f9ba56abb1ab6827e665cbb5b11cb21356c281
4
+ data.tar.gz: 236b4cccdf80cab0cee1b2ec79c01855b2bd48cd
5
+ SHA512:
6
+ metadata.gz: 0a012a0492b4bccfb4ac650e697b16ecc38f274d7f7c2a182d394fe84129f288ca75b30b78d2ed288208b950a82d409aea921fe9f1aaccbd9243e6ceae0f6e3b
7
+ data.tar.gz: ad58f08c43b5dd3812deb501823d24c517df49684ed14f4fe7b1e15a1c1f6ee60e354aa46513dfe05567f195f6bec8a84379442f228bb5f2efb4ced292cc564d
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /conf/schedule.yaml
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wink_scheduler.gemspec
4
+ gemspec
5
+ gem 'wink', :git => 'https://github.com/dewski/wink.git'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Joe Lanford
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # WinkScheduler
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'wink_scheduler'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install wink_scheduler
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/wink_scheduler/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,11 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'wink_scheduler'
4
+
5
+ if ARGV[0]
6
+ conf = YAML.load_file(ARGV[0])
7
+ w = WinkScheduler::Client.new(conf)
8
+ w.run
9
+ else
10
+ $stderr.puts "ERROR: must provide configuration file"
11
+ end
@@ -0,0 +1,43 @@
1
+ ---
2
+ auth:
3
+ client_id: "john.doe@gmail.com"
4
+ client_secret: "password1234"
5
+ access_token: "<wink_access_token>"
6
+ refresh_token: "<wink_refresh_token>"
7
+ schedules:
8
+ - device: Lamp
9
+ days: :daily
10
+ method: :on
11
+ time: :sunset
12
+ window: 2
13
+ location: 12765838
14
+ - device: Lamp
15
+ days: :daily
16
+ method: :off
17
+ time: 12:00 AM
18
+ window: 10
19
+ location: 12765838
20
+ - device: Family Room
21
+ days: :daily
22
+ method: :on
23
+ time: :sunset
24
+ window: 10
25
+ location: 12765838
26
+ - device: Family Room
27
+ days: :daily
28
+ method: :off
29
+ time: 12:00 AM
30
+ window: 10
31
+ location: 12765838
32
+ - group: My Group
33
+ days: :daily
34
+ method: :on
35
+ time: :sunrise
36
+ window: 10
37
+ location: 12765838
38
+ - device: My Group
39
+ days: :daily
40
+ method: :off
41
+ time: 8:00 AM
42
+ window: 10
43
+ location: 12765838
@@ -0,0 +1,33 @@
1
+ require 'wink_scheduler/schedule'
2
+
3
+ module WinkScheduler
4
+ class Client
5
+ def initialize(conf)
6
+ @auth = conf["auth"]
7
+ @schedules = conf["schedules"]
8
+
9
+ Wink.configure do |wink|
10
+ wink.client_id = @auth["client_id"]
11
+ wink.client_secret = @auth["client_secret"]
12
+ wink.access_token = @auth["access_token"]
13
+ wink.refresh_token = @auth["refresh_token"]
14
+ end
15
+ end
16
+
17
+ def run
18
+ threads = []
19
+ @schedules.each do |s|
20
+ threads << Thread.new do
21
+ w = WinkScheduler::Schedule.new(Wink::Client.new, s)
22
+ w.run
23
+ end
24
+ end
25
+ threads.each {|t| t.join}
26
+ end
27
+
28
+ def test
29
+ c = Wink::Client.new
30
+ p c.group("All").on
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,68 @@
1
+ require 'wink'
2
+ require 'weather-api'
3
+ require 'yaml'
4
+ require 'time'
5
+
6
+ module WinkScheduler
7
+ class Schedule
8
+ def initialize(client, opts)
9
+ @client = client
10
+ if opts["device"]
11
+ @object = client.devices.select { |d| next if d == nil; d.name == opts["device"] }[0]
12
+ @type = :device
13
+ elsif opts["group"]
14
+ @object = client.group(opts["group"])
15
+ @type = :group
16
+ else
17
+ raise "device or group not defined"
18
+ end
19
+ @days = get_days(opts["days"])
20
+ @method = opts["method"]
21
+ @time = opts["time"]
22
+ @window = opts["window"]
23
+ @location = opts["location"]
24
+ end
25
+
26
+ def run
27
+ while true
28
+ run_once
29
+ end
30
+ end
31
+
32
+ private
33
+ def get_days(days)
34
+ if days.class == Array
35
+ map = { "Sunday" => 0, "Monday" => 1, "Tuesday" => 2, "Wednesday" => 3, "Thursday" => 4, "Friday" => 5, "Saturday" => 6,
36
+ "sunday" => 0, "monday" => 1, "tuesday" => 2, "wednesday" => 3, "thursday" => 4, "friday" => 5, "saturday" => 6,
37
+ :sunday => 0, :monday => 1, :tuesday => 2, :wednesday => 3, :thursday => 4, :friday => 5, :saturday => 6 }
38
+ return days.map { |d| map[d] }.sort
39
+ elsif days == :daily
40
+ return (0..6).to_a
41
+ end
42
+ end
43
+
44
+ def run_once
45
+ cur_time = Time.now
46
+ next_time = get_next_time
47
+ randomness = rand(@window*60) - @window*60/2
48
+ puts "Scheduled \"#{@method}\" on #{@type} \"#{@object.name}\" at #{next_time + randomness}."
49
+ sleep next_time - cur_time + randomness
50
+ @object.send(@method)
51
+ puts "Executed \"#{@method}\" on #{@type} \"#{@object.name}\" at #{next_time + randomness}."
52
+ end
53
+
54
+ def get_next_time
55
+ time = case @time
56
+ when :sunset
57
+ Weather.lookup(@location).astronomy.sunset
58
+ when :sunrise
59
+ Weather.lookup(@location).astronomy.sunrise
60
+ else
61
+ Time.parse(@time)
62
+ end
63
+ possible_next_times = 0.upto(6).collect{ |i| time + 3600*24*i }
64
+ cur_time = Time.now
65
+ possible_next_times.select{ |t| t > cur_time && @days.include?(t.wday) }[0]
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module WinkScheduler
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'wink_scheduler/client'
2
+
3
+ module WinkScheduler
4
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wink_scheduler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wink_scheduler"
8
+ spec.version = WinkScheduler::VERSION
9
+ spec.authors = ["Joe Lanford"]
10
+ spec.email = ["joe.lanford@gmail.com"]
11
+ spec.summary = %q{Schedule actions for Wink devices.}
12
+ spec.description = %q{Schedule actions for Wink devices. This provides some functionality that the Wink mobile app is missing.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "weather-api", "~> 1.2"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wink_scheduler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joe Lanford
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: weather-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Schedule actions for Wink devices. This provides some functionality that
56
+ the Wink mobile app is missing.
57
+ email:
58
+ - joe.lanford@gmail.com
59
+ executables:
60
+ - wink_scheduler
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/wink_scheduler
70
+ - conf/schedule.yaml.sample
71
+ - lib/wink_scheduler.rb
72
+ - lib/wink_scheduler/client.rb
73
+ - lib/wink_scheduler/schedule.rb
74
+ - lib/wink_scheduler/version.rb
75
+ - wink_scheduler.gemspec
76
+ homepage: ''
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Schedule actions for Wink devices.
100
+ test_files: []