diff options
Diffstat (limited to 'power-meter')
-rwxr-xr-x | power-meter | 67 |
1 files changed, 42 insertions, 25 deletions
diff --git a/power-meter b/power-meter index 4e05af5..013da25 100755 --- a/power-meter +++ b/power-meter @@ -1,37 +1,54 @@ #!/usr/bin/env ruby +# FIXME +$LOAD_PATH << '/usr/local/lib/ruby/2.5.0/gems/mqtt-0.5.0/lib/' +$LOAD_PATH << '/usr/local/lib/ruby/2.5.0/gems/optimist-3.0.1/lib/' + +require 'optimist' require 'sqlite3' def main - db = SQLite3::Database.new ARGV[0] - devices = db.execute "select distinct device_location_primary, device_location_secondary FROM power" - puts devices.to_s - exit - devices.reject! { |i| not ["office", "living_room", "guest_restroom", "kitchen", "bedroom_2"].include? i[0] } - start_date = Date.new(2021,01,01).to_time.to_i - end_date = Date.new(2021,01,30).to_time.to_i + options = Optimist::options do + opt :db, "SQLite3 database file", :type => :string, :required => true, :short => '-d' + opt :from, "Calculate power consumption from date/time", :type => :string, :required => false, :short => '-f' + opt :to, "Calculate power consumption to date/time", :type => :string, :required => false, :short => '-t' + end + + db = SQLite3::Database.new options[:db] + + today = Date.today + if ! options[:from] + from = Date.new(today.year, today.month, 1).to_time + else + from = Time.parse(options[:from]) + end + if ! options[:to] + to = Time.parse "#{today.year}-#{today.month}-#{today.day}T23:59:59" + else + to = Time.parse(options[:to]) + end + + handles = db.execute "SELECT DISTINCT handle FROM power" + handles.map! { |i| i[0] }.sort! + max_handle_size = handles.map { |i| i.size }.max + totals = {} totals.default = 0.0 - devices.sort_by{|i| i[0] }.each { |device| - puts "select TOTAL, TIMESTAMP from power WHERE TIMESTAMP >= #{start_date} and TIMESTAMP <= #{end_date} AND DEVICE_LOCATION_PRIMARY = '#{device[0]}' AND DEVICE_LOCATION_SECONDARY = '#{device[1]}' ORDER BY TIMESTAMP ASC LIMIT 1" - exit - first = db.execute "select TOTAL, TIMESTAMP from power WHERE TIMESTAMP >= #{start_date} and TIMESTAMP <= #{end_date} AND DEVICE_LOCATION_PRIMARY = '#{device[0]}' AND DEVICE_LOCATION_SECONDARY = '#{device[1]}' ORDER BY TIMESTAMP ASC LIMIT 1" - last = db.execute "select TOTAL, TIMESTAMP from power WHERE TIMESTAMP >= #{start_date} and TIMESTAMP <= #{end_date} AND DEVICE_LOCATION_PRIMARY = '#{device[0]}' AND DEVICE_LOCATION_SECONDARY = '#{device[1]}' ORDER BY TIMESTAMP DESC LIMIT 1" - if first.size > 0 and last.size > 0 - #puts "#{Time.at(first[0][1])} --- #{Time.at(last[0][1])}" - #puts "#{first[0][0]} ::: #{last[0][0]}" - kwh = last[0][0] - first[0][0] - #puts "#{device.join '/'}: #{kwh.round 0} kW/h" - #puts - totals[device[0]] += kwh + + handles.each { |handle| + first = db.execute "SELECT total FROM power WHERE timestamp >= #{from.to_i} AND timestamp <= #{to.to_i} AND handle='#{handle}' ORDER BY timestamp ASC LIMIT 1" + last = db.execute "SELECT total FROM power WHERE timestamp >= #{from.to_i} AND timestamp <= #{to.to_i} AND handle='#{handle}' ORDER BY timestamp DESC LIMIT 1" + if first.size == 1 and last.size == 1 + consumption = last.first.first - first.first.first + totals[handle] += consumption end } - puts "TOTAL for #{start_date} - #{end_date}: #{totals.values.inject(:+).round 0} kW/h" - puts "\nBy Room" - puts "-------" - totals.each_key { |k| - puts " #{k}: #{totals[k].round 0} kW/h" - } + + puts "Power consumption from #{from} to #{to} = #{totals.values.inject(:+).round 1} kW/h" + puts "\nBy handle" + puts "---------" + totals.each_key { |handle| puts " - #{handle.ljust(max_handle_size)} = #{totals[handle].round(2).to_s.ljust(4)} kW/h" } + db.close end |