Floppy-currentcostd 1.3.0 → 1.4.0

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.
data/README CHANGED
@@ -30,4 +30,12 @@ builder >= 2.1.2
30
30
 
31
31
  2) Edit the settings in your new currentcostd.yml
32
32
 
33
- 3) Run "sudo currentcostd start" to start the daemon.
33
+ 3) Run "sudo currentcostd start" to start the daemon.
34
+
35
+ == AVAILABLE PUBLISHERS
36
+
37
+ * AMEE (www.amee.cc)
38
+ * Carbon Diet (www.carbondiet.org)
39
+ * Pachube (www.pachube.com)
40
+ * Twitter (www.twitter.com)
41
+ * Local HTTP server
@@ -34,3 +34,10 @@ amee:
34
34
  username: your_amee_username_goes_here
35
35
  password: your_amee_password_goes_here
36
36
  profile_uid: ABC123DEF456
37
+
38
+ # Enable this section to post to Twitter.
39
+ # This will post once a minute - I don't suggest you use your normal Twitter account ;)
40
+ twitter:
41
+ enabled: false
42
+ username: your_twitter_username_goes_here
43
+ password: your_twitter_password_goes_here
@@ -20,7 +20,7 @@
20
20
  #
21
21
  # http://www.opensource.org/licenses/mit-license.php
22
22
 
23
- require 'amee'
23
+ require 'net/http'
24
24
 
25
25
  module CurrentCostDaemon
26
26
 
@@ -36,11 +36,9 @@ module CurrentCostDaemon
36
36
  @profile_uid = config['amee']['profile_uid']
37
37
  @last_minute = Time.now.min - 1
38
38
  # Open AMEE connection
39
- server = config['amee']['server']
40
- username = config['amee']['username']
41
- password = config['amee']['password']
42
- @amee = AMEE::Connection.new(server, username, password)
43
- @amee.authenticate
39
+ @server = config['amee']['server']
40
+ @username = config['amee']['username']
41
+ @password = config['amee']['password']
44
42
  end
45
43
 
46
44
  def update(reading)
@@ -50,16 +48,28 @@ module CurrentCostDaemon
50
48
  @last_minute = Time.now.min
51
49
  # Estimate kwh figure from current power usage
52
50
  kwh = (reading.total_watts / 1000.0)
53
- # Add item to AMEE
54
- AMEE::Profile::Item.create_without_category(@amee,
55
- "/profiles/#{@profile_uid}/home/energy/quantity",
56
- "CDC2A0BA8DF3",
57
- :start_date => Time.now,
58
- :end_date => Time.now + 60,
59
- :energyConsumption => kwh,
60
- :energyConsumptionUnit => "kWh",
61
- :energyConsumptionPerUnit => "h",
62
- :name => "currentcost")
51
+ # Create POST options
52
+ options = {
53
+ :dataItemUid => "CDC2A0BA8DF3",
54
+ :startDate => Time.now.xmlschema,
55
+ :endDate => (Time.now + 60).xmlschema,
56
+ :energyConsumption => kwh,
57
+ :energyConsumptionUnit => "kWh",
58
+ :energyConsumptionPerUnit => "h",
59
+ :name => "currentcost"
60
+ }
61
+ puts "Storing in AMEE..."
62
+ # Post data to AMEE
63
+ req = Net::HTTP::Post.new("/profiles/#{@profile_uid}/home/energy/quantity")
64
+ req.basic_auth @username, @password
65
+ req['Accept'] = "application/xml"
66
+ req.set_form_data(options)
67
+ http = Net::HTTP.new(@server)
68
+ http.start do
69
+ response = http.request(req)
70
+ raise response.body if (response.code != "200" && response.code != "201")
71
+ end
72
+ puts "done"
63
73
  end
64
74
  rescue
65
75
  puts "Something went wrong (AMEE)!"
@@ -38,8 +38,15 @@ module CurrentCostDaemon
38
38
 
39
39
  def update(reading)
40
40
  # Carbon Diet is daily, so we only want to do something if there is
41
- # history data, and only once a day, really. Say around 5am, why not.
42
- if !reading.history.nil? && reading.hour == 7
41
+ # history data, and only once a day, ideally. If it's 3am, upload history
42
+ # if we have it.
43
+ # This is horribly hacky, it post every time there is day history during
44
+ # this hour. Also the carbondiet code at the other end is hacky.
45
+ # All this needs improving.
46
+ puts reading.to_yaml
47
+ puts "stuff"
48
+ if !reading.history.nil? && reading.hour == 3
49
+ puts "Storing in Carbon Diet..."
43
50
  # Create http post request
44
51
  post = Net::HTTP::Post.new("/data_entry/electricity/#{@account}/currentcost")
45
52
  post.basic_auth(@username, @password)
@@ -60,6 +67,7 @@ module CurrentCostDaemon
60
67
  http = Net::HTTP.new('www.carbondiet.org')
61
68
  http.start
62
69
  http.request(post)
70
+ puts "done"
63
71
  end
64
72
  rescue
65
73
  puts "Something went wrong (carbondiet)!"
@@ -39,21 +39,25 @@ module CurrentCostDaemon
39
39
  response['Content-Type'] = "text/html"
40
40
  response.body = "
41
41
  <html>
42
+ <head>
43
+ <meta http-equiv='refresh' content='6' />
44
+ <meta name='viewport' content='width=420'/>
45
+ </head>
42
46
  <body>
43
47
  <div style='float:right'>
44
48
  <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
49
  <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
50
  </div>
47
- <h1>Current Total</h1>
48
- <p>#{@@total} Watts</p>
49
- <h1>History</h1>
50
- <h2>Hourly</h2>
51
+ <h1>#{@@total} Watts</h2>
52
+ <p>updated at #{@@updated_at}</p>
53
+ <h2>History</h2>
54
+ <h3>Hourly</h3>
51
55
  <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>
56
+ <h3>Daily</h3>
53
57
  <img src='http://chart.apis.google.com/chart?cht=lc&chs=400x125&chd=t:#{@@history[:days].reverse.map{|x| x.nil? ? 0 : x[0]}.join(',')}&chds=0,#{@@history[:days].delete_if{|x|x.nil?}.max}'/>
54
- <h2>Monthly</h2>
58
+ <h3>Monthly</h3>
55
59
  <img src='http://chart.apis.google.com/chart?cht=lc&chs=400x125&chd=t:#{@@history[:months].reverse.map{|x| x.nil? ? 0 : x[0]}.join(',')}&chds=0,#{@@history[:months].delete_if{|x|x.nil?}.max}'/>
56
- <h2>Yearly</h2>
60
+ <h3>Yearly</h3>
57
61
  <img src='http://chart.apis.google.com/chart?cht=lc&chs=400x125&chd=t:#{@@history[:years].reverse.map{|x| x.nil? ? 0 : x[0]}.join(',')}&chds=0,#{@@history[:years].delete_if{|x|x.nil?}.max}'/>
58
62
  </body>
59
63
  </html>"
@@ -102,7 +106,9 @@ module CurrentCostDaemon
102
106
  end
103
107
 
104
108
  def update(reading)
109
+ puts "Updating HTTP publisher..."
105
110
  Servlet.update(reading)
111
+ puts "done"
106
112
  rescue
107
113
  puts "Something went wrong (http)!"
108
114
  puts $!.inspect
@@ -48,12 +48,15 @@ module CurrentCostDaemon
48
48
  eeml[0].value = reading.total_watts
49
49
  eeml.set_updated!
50
50
  # Put data
51
- put = Net::HTTP::Put.new("/feeds/#{@feed}.xml")
51
+ puts "Storing in Pachube..."
52
+ put = Net::HTTP::Put.new("/api/#{@feed}.xml")
52
53
  put.body = eeml.to_eeml
53
54
  put['X-PachubeApiKey'] = @api_key
54
55
  http = Net::HTTP.new('www.pachube.com')
55
56
  http.start
56
- http.request(put)
57
+ response = http.request(put)
58
+ raise response.code if response.code != "200"
59
+ puts "done"
57
60
  rescue
58
61
  puts "Something went wrong (pachube)!"
59
62
  puts $!.inspect
@@ -0,0 +1,69 @@
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 'cgi'
24
+
25
+ module CurrentCostDaemon
26
+
27
+ module Publishers
28
+
29
+ class Twitter
30
+
31
+ def self.config_section
32
+ 'twitter'
33
+ end
34
+
35
+ def initialize(config)
36
+ @username = config['twitter']['username']
37
+ @password = config['twitter']['password']
38
+ @last_minute = Time.now.min - 1
39
+ end
40
+
41
+ def update(reading)
42
+ # Tweet once a minute
43
+ if Time.now.min != @last_minute
44
+ # Store time
45
+ @last_minute = Time.now.min
46
+ message = "At the moment, I'm using #{reading.total_watts} watts"
47
+ # Tweet
48
+ puts "Tweeting..."
49
+ req = Net::HTTP::Post.new("/statuses/update.json")
50
+ req.basic_auth @username, @password
51
+ req.set_form_data "status" => message
52
+ http = Net::HTTP.new("twitter.com")
53
+ http.start do
54
+ response = http.request(req)
55
+ raise response.body if (response.code[0] != "2")
56
+ end
57
+ puts "done"
58
+ end
59
+ rescue
60
+ puts "Something went wrong (twitter)!"
61
+ puts $!.inspect
62
+ nil
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Floppy-currentcostd
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Smith
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-19 00:00:00 -08:00
12
+ date: 2009-03-28 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 2.0.1
33
+ version: 2.0.5
34
34
  version:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: Floppy-eeml
@@ -78,6 +78,7 @@ files:
78
78
  - lib/currentcostd/publishers/http.rb
79
79
  - lib/currentcostd/publishers/carbondiet.rb
80
80
  - lib/currentcostd/publishers/amee.rb
81
+ - lib/currentcostd/publishers/twitter.rb
81
82
  - config/currentcostd.example.yml
82
83
  - bin/currentcostd
83
84
  has_rdoc: false