co2-notify 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a52c578e28a7d5ca006be753377076e25f28fd00
4
+ data.tar.gz: d6f47971b1c818c585a93fbc9aa41f8158792225
5
+ SHA512:
6
+ metadata.gz: 87d221ceb2483397304c17ca7b77a4e42a95c91ca0abb91a548ad8fdd04acd1b6d267f6633c3d08a58da5ad79e6bea7eb6f0c1f59bef8f371fbc643715f7e529
7
+ data.tar.gz: ac3fc9fed3cfff9d1e2fe8f0c3108b1bbb734b285d3e22bd2f0ad0a7a8dacf3b0576134d913d4390e8ee060d0649af58461c9101a1a64f81fd6d2712a6b8a71b
data/.gitignore ADDED
@@ -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,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # Co2-notifier
2
+
3
+ ## Requirements
4
+
5
+ $ brew install hidapi
6
+
7
+ ## Install
8
+
9
+ $ gem install co2-notify
10
+
11
+ ## Usage
12
+
13
+ $ co2-notify init
14
+ $ co2-notify start
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/co2-notify ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require_relative "../lib/co2-notify"
5
+
6
+ Co2Notify.start(ARGV)
@@ -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 'co2-notify/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "co2-notify"
8
+ spec.version = Co2Notify::VERSION
9
+ spec.authors = ["Arkadiy Butermanov"]
10
+ spec.email = ["arkadiy.butermanov@flatstack.com"]
11
+
12
+ spec.summary = %q{Hipchat CO2 level notifier}
13
+ spec.homepage = "https://github.com/arkadiybutermanov/co2mon-notifier"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "bin"
18
+ spec.executables << "co2-notify"
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "thor"
22
+ spec.add_dependency "hipchat"
23
+ spec.add_dependency "activesupport"
24
+ spec.add_dependency "co2mon"
25
+ end
@@ -0,0 +1,44 @@
1
+ require "yaml"
2
+
3
+ class Co2Notify::Config < OpenStruct
4
+ CONFIG_FILE_NAME = ".co2-notify.yml".freeze
5
+ DEFAULT_TIMEOUT = 5.freeze
6
+ DEFAULT_COOLDOWN = 15.freeze
7
+ DEFAULT_HIGH_LEVEL = 800.freeze
8
+ DEFAULT_VERY_HIGH_LEVEL = 1200.freeze
9
+
10
+ def self.get
11
+ new YAML.load_file(config_file)
12
+ end
13
+
14
+ def self.set
15
+ data = {}.tap do |h|
16
+ print "Monitor location (required): "
17
+ h["location"] = STDIN.gets.chomp
18
+ print "Monitor user (required): "
19
+ h["user"] = STDIN.gets.chomp
20
+ print "Hipchat API token (required): "
21
+ h["api_token"] = STDIN.gets.chomp
22
+ print "Hipchat room name (required): "
23
+ h["room"] = STDIN.gets.chomp
24
+ print "Timeout (default: #{DEFAULT_TIMEOUT} mins): "
25
+ h["timeout"] = STDIN.gets.chomp.presence || DEFAULT_TIMEOUT
26
+ print "Cooldown (default: #{DEFAULT_COOLDOWN} mins): "
27
+ h["cooldown"] = STDIN.gets.chomp.presence || DEFAULT_COOLDOWN
28
+ print "High CO2 level (default: #{DEFAULT_HIGH_LEVEL}): "
29
+ h["high_level"] = STDIN.gets.chomp.presence || DEFAULT_HIGH_LEVEL
30
+ print "Very High CO2 level (default: #{DEFAULT_VERY_HIGH_LEVEL}): "
31
+ h["very_high_level"] = STDIN.gets.chomp.presence || DEFAULT_VERY_HIGH_LEVEL
32
+ print "Mention (optional): "
33
+ h["mention"] = STDIN.gets.chomp
34
+ end
35
+
36
+ File.open(config_file, "w") do |f|
37
+ YAML.dump(data, f)
38
+ end
39
+ end
40
+
41
+ def self.config_file
42
+ File.join(ENV['HOME'], CONFIG_FILE_NAME)
43
+ end
44
+ end
@@ -0,0 +1,13 @@
1
+ class Co2Notify::HipchatClient
2
+ attr_reader :client, :config
3
+ delegate :room, :location, :api_token, to: :config
4
+
5
+ def initialize(config)
6
+ @config = config
7
+ @client = HipChat::Client.new(api_token, api_version: "v2")
8
+ end
9
+
10
+ def send(status)
11
+ client[room].send("Co2 (#{location})", status.message, color: status.color, message_format: "text")
12
+ end
13
+ end
@@ -0,0 +1,37 @@
1
+ class Co2Notify::Notifier
2
+ def self.start
3
+ new(Co2Notify::Config.get).start
4
+ end
5
+
6
+ attr_reader :config, :client, :status
7
+ delegate :timeout, to: :status
8
+
9
+ def initialize(config)
10
+ @config = config
11
+ @client = Co2Notify::HipchatClient.new(config)
12
+ @status = Co2Notify::Status::Empty.new(config)
13
+ end
14
+
15
+ def start
16
+ loop do
17
+ notify
18
+ sleep timeout * 60
19
+ end
20
+ rescue Interrupt
21
+ end
22
+
23
+ private
24
+
25
+ def get_data
26
+ Co2mon.get_data[:co2]
27
+ end
28
+
29
+ def notify
30
+ new_status = Co2Notify::Status.build(get_data, config, status)
31
+
32
+ if status.changed?(new_status)
33
+ client.send(new_status)
34
+ @status = new_status
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,101 @@
1
+ class Co2Notify::Status
2
+ class Base
3
+ attr_reader :co2, :config, :previous
4
+ delegate :timeout, :cooldown, :mention, :user, to: :config
5
+
6
+ def initialize(config, previous = nil, co2 = nil)
7
+ @config, @co2, @previous = config, co2, previous
8
+ end
9
+
10
+ private
11
+
12
+ def hc_mention
13
+ "@#{mention} " if mention.present?
14
+ end
15
+
16
+ def hc_user
17
+ "@#{user} " if user.present?
18
+ end
19
+ end
20
+
21
+ class VeryHigh < Base
22
+ def message
23
+ "#{hc_mention}CO2 level is very high - #{co2}. Please open the windows!"
24
+ end
25
+
26
+ def color
27
+ :red
28
+ end
29
+
30
+ def changed?(new_status)
31
+ new_status.is_a?(Empty) || new_status.is_a?(Normal) || new_status.is_a?(VeryHigh)
32
+ end
33
+
34
+ def timeout
35
+ previous.is_a?(Empty) || previous.is_a?(Normal) ? cooldown : super
36
+ end
37
+ end
38
+
39
+ class High < Base
40
+ def message
41
+ "#{hc_mention}CO2 level is high - #{co2}. Please open the windows!"
42
+ end
43
+
44
+ def color
45
+ previous.is_a?(High) ? :red : :yellow
46
+ end
47
+
48
+ def changed?(new_status)
49
+ new_status.is_a?(Empty) || new_status.is_a?(Normal) || new_status.is_a?(High) || new_status.is_a?(VeryHigh)
50
+ end
51
+
52
+ def timeout
53
+ previous.is_a?(Empty) || previous.is_a?(Normal) ? cooldown : super
54
+ end
55
+ end
56
+
57
+ class Normal < Base
58
+ def message
59
+ "#{hc_mention}CO2 level is normalized - #{co2}. You can close the windows."
60
+ end
61
+
62
+ def color
63
+ :green
64
+ end
65
+
66
+ def changed?(new_status)
67
+ new_status.is_a?(Empty) || new_status.is_a?(High) || new_status.is_a?(VeryHigh)
68
+ end
69
+
70
+ def timeout
71
+ previous.is_a?(High) || previous.is_a?(VeryHigh) ? cooldown : super
72
+ end
73
+ end
74
+
75
+ class Empty < Base
76
+ def message
77
+ "#{hc_user}Please check that CO2 monitor is connected."
78
+ end
79
+
80
+ def color
81
+ :purple
82
+ end
83
+
84
+ def changed?(new_status)
85
+ new_status.is_a?(Empty) || new_status.is_a?(High) || new_status.is_a?(VeryHigh)
86
+ end
87
+ end
88
+
89
+ def self.build(co2, config, previous = nil)
90
+ case co2
91
+ when 1..config.high_level
92
+ Normal.new(config, previous, co2)
93
+ when config.high_level..config.very_high_level
94
+ High.new(config, previous, co2)
95
+ when proc { |n| n >= config.very_high_level }
96
+ VeryHigh.new(config, previous, co2)
97
+ else
98
+ Empty.new(config)
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ class Co2Notify
2
+ VERSION = "0.2.0"
3
+ end
data/lib/co2-notify.rb ADDED
@@ -0,0 +1,22 @@
1
+ require "thor"
2
+ require "co2mon"
3
+ require "hipchat"
4
+ require "active_support/core_ext/object/blank"
5
+ require "active_support/core_ext/module/delegation"
6
+
7
+ class Co2Notify < Thor
8
+ desc "start", "start notifier"
9
+ def start
10
+ Notifier.start
11
+ end
12
+
13
+ desc "init", "setup config"
14
+ def init
15
+ Config.set
16
+ end
17
+ end
18
+
19
+ require_relative "co2-notify/config"
20
+ require_relative "co2-notify/status"
21
+ require_relative "co2-notify/hipchat_client"
22
+ require_relative "co2-notify/notifier"
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: co2-notify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Arkadiy Butermanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hipchat
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: co2mon
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - arkadiy.butermanov@flatstack.com
72
+ executables:
73
+ - co2-notify
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - bin/co2-notify
82
+ - co2-notify.gemspec
83
+ - lib/co2-notify.rb
84
+ - lib/co2-notify/config.rb
85
+ - lib/co2-notify/hipchat_client.rb
86
+ - lib/co2-notify/notifier.rb
87
+ - lib/co2-notify/status.rb
88
+ - lib/co2-notify/version.rb
89
+ homepage: https://github.com/arkadiybutermanov/co2mon-notifier
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.5.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Hipchat CO2 level notifier
113
+ test_files: []