Floppy-currentcostd 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/COPYING ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2008 James Smith (james@floppy.org.uk)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,33 @@
1
+ == currentcostd
2
+
3
+ A system daemon for monitoring and publishing data from a CurrentCost energy meter
4
+ (http://www.currentcost.com)
5
+
6
+ Licensed under the MIT license (See COPYING file for details)
7
+
8
+ Author: James Smith (james@floppy.org.uk / http://www.floppy.org.uk)
9
+
10
+ Homepage: http://github.com/Floppy/currentcost-daemon
11
+
12
+ == INSTALLATION
13
+
14
+ 1) Enable gems from github, if you haven't already done so (rubygems >= 1.2):
15
+ > sudo gem sources -a http://gems.github.com
16
+
17
+ 2) Install gem
18
+ > sudo gem install Floppy-currentcostd
19
+
20
+ == REQUIREMENTS
21
+
22
+ Floppy-currentcost >= 0.2.4
23
+ Floppy-eeml >= 0.1.0
24
+ daemons >= 1.0.10
25
+ builder >= 2.1.2
26
+
27
+ == USAGE
28
+
29
+ 1) Copy config/currentcostd.example.yml to /etc/currentcost.yml
30
+
31
+ 2) Edit the settings in your new currentcostd.yml
32
+
33
+ 3) Run "sudo currentcostd start" to start the daemon.
data/bin/currentcostd ADDED
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Copyright (c) 2008 James Smith (www.floppy.org.uk)
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.
22
+ #
23
+ # http://www.opensource.org/licenses/mit-license.php
24
+
25
+ require 'rubygems'
26
+ require 'daemons'
27
+ require 'currentcost/meter'
28
+
29
+ # Load config
30
+ config = nil
31
+ config_files = [
32
+ "/etc/currentcostd.yml",
33
+ File.join(File.dirname(__FILE__), '/../config/currentcostd.yml')
34
+ ]
35
+ config_files.each do |c|
36
+ if File.exists?(c)
37
+ config = YAML.load_file(c)
38
+ break
39
+ end
40
+ end
41
+ if config.nil?
42
+ puts "Couldn't load configuration from " + config_files.join(" or ")
43
+ exit
44
+ end
45
+
46
+ # Require all available publishers
47
+ Dir.glob(File.join(File.dirname(__FILE__), '/../lib/currentcostd/publishers/*.rb')).each { |f| require f }
48
+
49
+ Daemons.run_proc('currentcostd', :dir_mode => :system) do
50
+
51
+ # Create meter object
52
+ meter = CurrentCost::Meter.new config['currentcost']['port']
53
+
54
+ # Register publishers with meter if their configuration sections are defined
55
+ CurrentCostDaemon::Publishers.constants.each do |publisher|
56
+ pub_class = CurrentCostDaemon::Publishers.const_get(publisher)
57
+ if config[pub_class.config_section] && config[pub_class.config_section]['enabled'] == true
58
+ meter.add_observer(pub_class.new(config))
59
+ end
60
+ end
61
+
62
+ # Just let it run
63
+ while (true)
64
+ sleep(30)
65
+ end
66
+
67
+ end
@@ -0,0 +1,20 @@
1
+ # Copy this file to /etc/currentcostd.yml OR ./currentcostd.yml and add your
2
+ # own settings.
3
+
4
+ currentcost:
5
+ port: /dev/ttyS0
6
+
7
+ # Enable this to dump meter data to standard output
8
+ debug:
9
+ enabled: true
10
+
11
+ # Enable this to publish to a feed on www.pachube.com
12
+ pachube:
13
+ enabled: false
14
+ feed_id: your_pachube_feed_id_goes_here (e.g. 123)
15
+ api_key: your_pachube_api_key_goes_here
16
+
17
+ # Enable this to run a web server which you can look at in a browser
18
+ http:
19
+ enabled: false
20
+ port: 3500
@@ -0,0 +1,45 @@
1
+ # Copyright (c) 2008 James Smith (www.floppy.org.uk)
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+ #
21
+ # http://www.opensource.org/licenses/mit-license.php
22
+
23
+ module CurrentCostDaemon
24
+
25
+ module Publishers
26
+
27
+ class Debug
28
+
29
+ def self.config_section
30
+ 'debug'
31
+ end
32
+
33
+ def initialize(config)
34
+ end
35
+
36
+ def update(reading)
37
+ # Print out measurement
38
+ puts "New reading received: #{reading.total_watts} W"
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,112 @@
1
+ # Copyright (c) 2008 James Smith (www.floppy.org.uk)
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+ #
21
+ # http://www.opensource.org/licenses/mit-license.php
22
+
23
+ require 'webrick'
24
+
25
+ module CurrentCostDaemon
26
+
27
+ module Publishers
28
+
29
+ class Http
30
+
31
+ def self.config_section
32
+ 'http'
33
+ end
34
+
35
+ class Servlet < WEBrick::HTTPServlet::AbstractServlet
36
+ def do_GET(request, response)
37
+ if request.query['format'].nil? || request.query['format'] == "html"
38
+ response.status = 200
39
+ response['Content-Type'] = "text/html"
40
+ response.body = "
41
+ <html>
42
+ <body>
43
+ <div style='float:right'>
44
+ <a title='EEML' href='/?format=eeml'><span style='border:1px solid;border-color:#9C9 #030 #030 #696;padding:0 3px;font:bold 10px verdana,sans-serif;color:#FFF;background:#090;text-decoration:none;margin:0;'>EEML</span></a>
45
+ <a title='XML' href='/?format=xml'><span style='border:1px solid;border-color:#FC9 #630 #330 #F96;padding:0 3px;font:bold 10px verdana,sans-serif;color:#FFF;background:#F60;text-decoration:none;margin:0;'>XML</span></a>
46
+ </div>
47
+ <h1>Current Total</h1>
48
+ <p>#{@@total} Watts</p>
49
+ <h1>History</h1>
50
+ <h2>Hourly</h2>
51
+ <img src='http://chart.apis.google.com/chart?cht=lc&chs=400x125&chd=t:#{@@history[:hours].reverse.delete_if{|x|x.nil?}.join(',')}&chds=0,#{@@history[:hours].delete_if{|x|x.nil?}.max}'/>
52
+ <h2>Daily</h2>
53
+ <img src='http://chart.apis.google.com/chart?cht=lc&chs=400x125&chd=t:#{@@history[:days].reverse.map{|x| x.nil? ? 0 : x}.join(',')}&chds=0,#{@@history[:days].delete_if{|x|x.nil?}.max}'/>
54
+ <h2>Monthly</h2>
55
+ <img src='http://chart.apis.google.com/chart?cht=lc&chs=400x125&chd=t:#{@@history[:months].reverse.map{|x| x.nil? ? 0 : x}.join(',')}&chds=0,#{@@history[:months].delete_if{|x|x.nil?}.max}'/>
56
+ <h2>Yearly</h2>
57
+ <img src='http://chart.apis.google.com/chart?cht=lc&chs=400x125&chd=t:#{@@history[:years].reverse.map{|x| x.nil? ? 0 : x}.join(',')}&chds=0,#{@@history[:years].delete_if{|x|x.nil?}.max}'/>
58
+ </body>
59
+ </html>"
60
+ elsif request.query['format'] == "eeml"
61
+ # Create EEML document
62
+ eeml = EEML::Environment.new
63
+ # Create data object
64
+ data = EEML::Data.new(0)
65
+ data.unit = EEML::Unit.new("Watts", :symbol => 'W', :type => :derivedSI)
66
+ eeml << data
67
+ eeml[0].value = @@total
68
+ eeml.updated_at = @@updated_at
69
+ response.status = 200
70
+ response['Content-Type'] = "text/xml"
71
+ response.body = eeml.to_eeml
72
+ elsif request.query['format'] == "xml"
73
+ response.status = 200
74
+ response['Content-Type'] = "text/xml"
75
+ # Create XML
76
+ xml = Builder::XmlMarkup.new
77
+ xml.instruct!
78
+ response.body = xml.currentcost { xml.watts @@total }
79
+ else
80
+ response.status = 400
81
+ end
82
+ end
83
+ def self.update(reading)
84
+ @@reading = reading
85
+ @@updated_at = Time.now
86
+ # Add all channels to get real total
87
+ @@total = @@reading.total_watts
88
+ # Store history if available
89
+ @@history = @@reading.history if @@reading.history
90
+ end
91
+ end
92
+
93
+ def initialize(config)
94
+ # Initialise storage
95
+ @watts = 0
96
+ # Create WEBrick server
97
+ @server = WEBrick::HTTPServer.new(:Port => config['http']['port'] )
98
+ # Create a simple webrick servlet for HTML output
99
+ @server.mount("/", Servlet)
100
+ trap("INT") {@server.shutdown}
101
+ Thread.new{@server.start}
102
+ end
103
+
104
+ def update(reading)
105
+ Servlet.update(reading)
106
+ end
107
+
108
+ end
109
+
110
+ end
111
+
112
+ end
@@ -0,0 +1,63 @@
1
+ # Copyright (c) 2008 James Smith (www.floppy.org.uk)
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+ #
21
+ # http://www.opensource.org/licenses/mit-license.php
22
+
23
+ require 'eeml'
24
+ require 'net/http'
25
+
26
+ module CurrentCostDaemon
27
+
28
+ module Publishers
29
+
30
+ class Pachube
31
+
32
+ def self.config_section
33
+ 'pachube'
34
+ end
35
+
36
+ def initialize(config)
37
+ @feed = config['pachube']['feed_id']
38
+ @api_key = config['pachube']['api_key']
39
+ @http = Net::HTTP.new('www.pachube.com')
40
+ @http.start
41
+ end
42
+
43
+ def update(reading)
44
+ # Create EEML document
45
+ eeml = EEML::Environment.new
46
+ # Create data object
47
+ data = EEML::Data.new(0)
48
+ data.unit = EEML::Unit.new("Watts", :symbol => 'W', :type => :derivedSI)
49
+ eeml << data
50
+ eeml[0].value = reading.total_watts
51
+ eeml.set_updated!
52
+ # Put data
53
+ put = Net::HTTP::Put.new("/feeds/#{@feed}.xml")
54
+ put.body = eeml.to_eeml
55
+ put['X-PachubeApiKey'] = @api_key
56
+ @http.request(put)
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Floppy-currentcostd
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.1"
5
+ platform: ruby
6
+ authors:
7
+ - James Smith
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-26 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: Floppy-currentcost
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.2.4
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: Floppy-eeml
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 0.1.0
32
+ version:
33
+ - !ruby/object:Gem::Dependency
34
+ name: daemons
35
+ version_requirement:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.10
41
+ version:
42
+ - !ruby/object:Gem::Dependency
43
+ name: builder
44
+ version_requirement:
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.1.2
50
+ version:
51
+ description:
52
+ email: james@floppy.org.uk
53
+ executables:
54
+ - currentcostd
55
+ extensions: []
56
+
57
+ extra_rdoc_files: []
58
+
59
+ files:
60
+ - README
61
+ - COPYING
62
+ - lib/currentcostd/publishers/debug.rb
63
+ - lib/currentcostd/publishers/pachube.rb
64
+ - lib/currentcostd/publishers/http.rb
65
+ - config/currentcostd.example.yml
66
+ - bin/currentcostd
67
+ has_rdoc: false
68
+ homepage: http://github.com/Floppy/currentcost-daemon
69
+ post_install_message:
70
+ rdoc_options: []
71
+
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ version:
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.2.0
90
+ signing_key:
91
+ specification_version: 2
92
+ summary: A system daemon for monitoring and publishing currentcost data
93
+ test_files: []
94
+