flukso4r 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of flukso4r might be problematic. Click here for more details.
- data/VERSION +1 -1
- data/bin/flukso_archive_watts +2 -3
- data/bin/flukso_export_db +108 -0
- data/bin/flukso_query +9 -6
- data/lib/flukso/api.rb +9 -8
- data/lib/flukso/http_auth.rb +32 -12
- data/lib/flukso.rb +1 -0
- metadata +3 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/bin/flukso_archive_watts
CHANGED
@@ -94,10 +94,9 @@ if not File.exists?(dbfile)
|
|
94
94
|
end
|
95
95
|
|
96
96
|
|
97
|
-
|
98
|
-
auth=Flukso::HTTPAuth.new("", "");
|
97
|
+
auth=Flukso::TokenAuth.new($CONFIG["ACCESS_TOKEN"]);
|
99
98
|
api=Flukso::API.new(auth);
|
100
|
-
query=Flukso::QueryReadings.new($CONFIG["
|
99
|
+
query=Flukso::QueryReadings.new($CONFIG["SENSOR_ID"], :hour, :watt)
|
101
100
|
begin
|
102
101
|
readings=query.execute(api);
|
103
102
|
rescue Exception => e
|
@@ -0,0 +1,108 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This file is part of fluksobot
|
3
|
+
# (c) 2009 Mathias Dalheimer, md@gonium.net
|
4
|
+
#
|
5
|
+
# FluksoBot is free software; you can
|
6
|
+
# redistribute it and/or modify it under the terms of the GNU General Public
|
7
|
+
# License as published by the Free Software Foundation; either version 2 of
|
8
|
+
# the License, or any later version.
|
9
|
+
#
|
10
|
+
# FluksoBot is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with FluksoBot; if not, write to the Free Software
|
17
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
18
|
+
|
19
|
+
# Read the fluksobot location
|
20
|
+
libpath=File.join(File.dirname(__FILE__), '..', 'lib')
|
21
|
+
$:.unshift << libpath
|
22
|
+
#puts "Using libraty path #{$:.join(":")}"
|
23
|
+
|
24
|
+
require 'rubygems'
|
25
|
+
require 'optparse'
|
26
|
+
require 'ostruct'
|
27
|
+
require 'flukso'
|
28
|
+
|
29
|
+
###
|
30
|
+
## Commandline parser
|
31
|
+
#
|
32
|
+
class Optparser
|
33
|
+
CODES = %w[iso-2022-jp shift_jis euc-jp utf8 binary]
|
34
|
+
CODE_ALIASES = { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
|
35
|
+
#
|
36
|
+
# Return a structure describing the options.
|
37
|
+
#
|
38
|
+
def self.parse(args)
|
39
|
+
# The options specified on the command line will be collected in *options*.
|
40
|
+
# We set default values here.
|
41
|
+
options = OpenStruct.new
|
42
|
+
options.inplace = false
|
43
|
+
options.encoding = "utf8"
|
44
|
+
options.verbose = false
|
45
|
+
opts = OptionParser.new do |opts|
|
46
|
+
opts.banner = "Usage: #{$0} [options]"
|
47
|
+
opts.separator ""
|
48
|
+
opts.separator "Specific options:"
|
49
|
+
opts.on("-c", "--config FILE", "The configuration file to use.") do |file|
|
50
|
+
options.config_file = file
|
51
|
+
end
|
52
|
+
# Boolean switch.
|
53
|
+
opts.on("-f", "--force", "Ignore warnings") do |f|
|
54
|
+
options.force = f
|
55
|
+
end
|
56
|
+
opts.on("-v", "--verbose", "Run verbosely") do |v|
|
57
|
+
options.verbose = v
|
58
|
+
end
|
59
|
+
opts.on_tail("-h", "--help", "Show this message") do
|
60
|
+
puts opts
|
61
|
+
exit
|
62
|
+
end
|
63
|
+
end
|
64
|
+
opts.parse!(args)
|
65
|
+
options
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
###
|
70
|
+
## Script startup
|
71
|
+
#
|
72
|
+
options = Optparser.parse(ARGV)
|
73
|
+
$verbose = options.verbose
|
74
|
+
$force = options.force
|
75
|
+
puts "FluksoBot database setup script."
|
76
|
+
if options.config_file == nil
|
77
|
+
puts "Please provide a configuration file... (-h for details)."
|
78
|
+
exit(-1);
|
79
|
+
end
|
80
|
+
if not File.exists?(options.config_file)
|
81
|
+
puts " Configuration file #{options.config_file} not found - no database configuration available!"
|
82
|
+
exit(-3);
|
83
|
+
else
|
84
|
+
$CONFIG=YAML.load_file(options.config_file);
|
85
|
+
puts "Using this configuration:" if $verbose
|
86
|
+
puts "#{$CONFIG}" if $verbose
|
87
|
+
dbfile=$CONFIG['DB_FILE']
|
88
|
+
end
|
89
|
+
if $verbose
|
90
|
+
puts " Using database file #{dbfile}"
|
91
|
+
end
|
92
|
+
# check and expand paths.
|
93
|
+
if not File.exists?(dbfile)
|
94
|
+
puts " Database does not exists. Aborting."
|
95
|
+
exit(-2);
|
96
|
+
end
|
97
|
+
|
98
|
+
# The database does not exist. Create the DB and populate it with
|
99
|
+
# tables.
|
100
|
+
|
101
|
+
db=Flukso::FluksoDB.open($CONFIG["DB_FILE"], $CONFIG["DB_TABLE_NAME"]);
|
102
|
+
begin
|
103
|
+
last=db.find_reading_last();
|
104
|
+
puts "Last reading: #{last}"
|
105
|
+
rescue Flukso::ElementNotFoundError => e
|
106
|
+
puts "Empty database - not able to provide last reading."
|
107
|
+
end
|
108
|
+
db.close
|
data/bin/flukso_query
CHANGED
@@ -55,8 +55,11 @@ class Optparser
|
|
55
55
|
opts.on("-u", "--unit VALUE", "The unit you want to query. Valid queries are: {watt, kwh, eur, aud}") do |unit|
|
56
56
|
options.unit = unit
|
57
57
|
end
|
58
|
-
opts.on("-
|
59
|
-
options.
|
58
|
+
opts.on("-i", "--sensor-id VALUE", "The sensor ID you want to query.") do |sensor_id|
|
59
|
+
options.sensor_id = sensor_id
|
60
|
+
end
|
61
|
+
opts.on("-a", "--access-token VALUE", "Your access token.") do |access_token|
|
62
|
+
options.access_token = access_token
|
60
63
|
end
|
61
64
|
# Boolean switch.
|
62
65
|
opts.on("-v", "--verbose", "Run verbosely") do |v|
|
@@ -107,12 +110,12 @@ else begin
|
|
107
110
|
exit(-2);
|
108
111
|
end
|
109
112
|
end
|
110
|
-
|
113
|
+
sensor_id=options.sensor_id
|
114
|
+
access_token=options.access_token
|
111
115
|
|
112
|
-
|
113
|
-
auth=Flukso::HTTPAuth.new("", "");
|
116
|
+
auth=Flukso::TokenAuth.new(access_token);
|
114
117
|
api=Flukso::API.new(auth);
|
115
|
-
query=Flukso::QueryReadings.new(
|
118
|
+
query=Flukso::QueryReadings.new(sensor_id, range, unit)
|
116
119
|
begin
|
117
120
|
readings=query.execute(api);
|
118
121
|
rescue Exception => e
|
data/lib/flukso/api.rb
CHANGED
@@ -22,8 +22,8 @@
|
|
22
22
|
|
23
23
|
module Flukso
|
24
24
|
class QueryReadings
|
25
|
-
def initialize(
|
26
|
-
@
|
25
|
+
def initialize(sensor_id, timerange=:hour, unit=:watt)
|
26
|
+
@sensor_id=sensor_id;
|
27
27
|
# sanity checks.
|
28
28
|
valid_timeranges=[:hour, :day, :month, :year, :night];
|
29
29
|
valid_units=[:watt, :kwh, :eur, :aud];
|
@@ -36,7 +36,7 @@ module Flukso
|
|
36
36
|
if api.class != Flukso::API
|
37
37
|
raise Flukso::General, "Cannot execute query: API object invalid"
|
38
38
|
end
|
39
|
-
query_url="#{Flukso::BASE_SENSOR_URL}/#{@
|
39
|
+
query_url="#{Flukso::BASE_SENSOR_URL}/#{@sensor_id}?interval=#{@timerange}&unit=#{@unit}";
|
40
40
|
puts "Using query url #{query_url}" if $verbose
|
41
41
|
response = api.perform_get(query_url)
|
42
42
|
return wrap_response(response);
|
@@ -62,21 +62,22 @@ module Flukso
|
|
62
62
|
def initialize(client)
|
63
63
|
@client = client
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
def perform_get(path, options={})
|
67
|
-
Flukso::Request.get(self, path, options)
|
67
|
+
Flukso::Request.get(self, path, options )
|
68
68
|
end
|
69
69
|
|
70
70
|
def perform_post(path, options={})
|
71
|
-
Flukso::Request.post(self, path, options)
|
71
|
+
Flukso::Request.post(self, path, add_version_header(options))
|
72
72
|
end
|
73
73
|
|
74
74
|
def perform_put(path, options={})
|
75
|
-
Flukso::Request.put(self, path, options)
|
75
|
+
Flukso::Request.put(self, path, add_version_header(options))
|
76
76
|
end
|
77
77
|
|
78
78
|
def perform_delete(path, options={})
|
79
|
-
Flukso::Request.delete(self, path, options)
|
79
|
+
Flukso::Request.delete(self, path, add_version_header(options))
|
80
80
|
end
|
81
|
+
|
81
82
|
end
|
82
83
|
end
|
data/lib/flukso/http_auth.rb
CHANGED
@@ -21,37 +21,57 @@
|
|
21
21
|
|
22
22
|
|
23
23
|
module Flukso
|
24
|
-
class
|
24
|
+
class TokenAuth
|
25
25
|
include HTTParty
|
26
26
|
format :plain
|
27
27
|
|
28
|
-
attr_reader :
|
28
|
+
attr_reader :options
|
29
29
|
|
30
|
-
def initialize(
|
31
|
-
@
|
32
|
-
|
33
|
-
self.class.base_uri "
|
30
|
+
def initialize(access_token)
|
31
|
+
@access_token = access_token
|
32
|
+
#@options = {:ssl => true}.merge(options)
|
33
|
+
self.class.base_uri "#{Flukso::BASE_SENSOR_URL}"
|
34
34
|
end
|
35
35
|
|
36
|
+
|
36
37
|
def get(uri, headers={})
|
37
|
-
|
38
|
+
headers=add_auth_header(headers);
|
39
|
+
self.class.get(uri, :headers => headers)
|
38
40
|
end
|
39
41
|
|
40
42
|
def post(uri, body={}, headers={})
|
41
|
-
self.class.post(uri, :body => body, :headers => headers
|
43
|
+
self.class.post(uri, :body => body, :headers => add_auth_header(headers))
|
42
44
|
end
|
43
45
|
|
44
46
|
def put(uri, body={}, headers={})
|
45
|
-
self.class.put(uri, :body => body, :headers => headers
|
47
|
+
self.class.put(uri, :body => body, :headers => add_auth_header(headers))
|
46
48
|
end
|
47
49
|
|
48
50
|
def delete(uri, body={}, headers={})
|
49
|
-
self.class.delete(uri, :body => body, :headers => headers
|
51
|
+
self.class.delete(uri, :body => body, :headers => add_auth_header(headers))
|
50
52
|
end
|
51
53
|
|
52
54
|
private
|
53
|
-
|
54
|
-
|
55
|
+
|
56
|
+
def add_version_header(headers)
|
57
|
+
if (headers==nil)
|
58
|
+
else
|
59
|
+
headers={"X-Version" => "#{Flukso::API_VERSION}"}.merge(headers);
|
60
|
+
end
|
61
|
+
return headers;
|
55
62
|
end
|
63
|
+
|
64
|
+
|
65
|
+
def add_auth_header(headers)
|
66
|
+
if (headers==nil)
|
67
|
+
headers={"X-Version" => "#{Flukso::API_VERSION}"}
|
68
|
+
headers.merge!({"X-Token" => "#{@access_token}"})
|
69
|
+
else
|
70
|
+
headers={"X-Version" => "#{Flukso::API_VERSION}"}.merge(headers);
|
71
|
+
headers={"X-Token" => "#{@access_token}"}.merge(headers);
|
72
|
+
end
|
73
|
+
return headers;
|
74
|
+
end
|
75
|
+
|
56
76
|
end
|
57
77
|
end
|
data/lib/flukso.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flukso4r
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mathias Dalheimer
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-26 00:00:00 +01:00
|
13
13
|
default_executable: flukso_query
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -50,6 +50,7 @@ files:
|
|
50
50
|
- VERSION
|
51
51
|
- bin/flukso_archive_watts
|
52
52
|
- bin/flukso_create_db
|
53
|
+
- bin/flukso_export_db
|
53
54
|
- bin/flukso_query
|
54
55
|
- lib/flukso.rb
|
55
56
|
- lib/flukso/api.rb
|