Skip to content

Commit

Permalink
Averaging
Browse files Browse the repository at this point in the history
  • Loading branch information
onewheelskyward committed Sep 11, 2020
1 parent d03cca1 commit 774ee26
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
40 changes: 33 additions & 7 deletions lib/lita/handlers/irc_handlers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,40 @@ def handle_ansi_aqi(response)
end

label = aqi['results'][0]['Label']
stats = JSON.parse aqi['results'][0]['Stats']

stats = {v: [], v1: [], v2: [], v3: [], v4: [], v5: []}

Lita.logger.debug "Found #{aqi['results'].length} results, averaging"
aqi['results'].each do |r|
s = JSON.parse r['Stats']
Lita.logger.debug "Result: #{s}"
if s['v'] == 0
next
end
stats[:v].push s['v']
stats[:v1].push s['v1']
stats[:v2].push s['v2']
stats[:v3].push s['v3']
stats[:v4].push s['v4']
stats[:v5].push s['v5']
end

stats.keys.each do |statskey|
avg = 0
stats[statskey].each do |measurement|
avg += measurement
end
avg = avg / stats[statskey].length
stats[statskey] = avg.to_i
end

Lita.logger.debug stats
aqis = [stats['v5'],
stats['v4'],
stats['v3'],
stats['v2'],
stats['v1'],
stats['v']]
aqis = [stats[:v5],
stats[:v4],
stats[:v3],
stats[:v2],
stats[:v1],
stats[:v]]

reply = do_the_aqi_thing(aqis)
response.reply "AQI report for #{label}: PM2.5 #{reply} \x03#{colors[:grey]}(7 day average to 10 min average)\x03"
Expand Down
2 changes: 1 addition & 1 deletion spec/lita/handlers/forecast_io_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ def mock_up(filename)
it 'aqis' do
mock_up 'aqi'
send_command 'ansiaqi'
expect(replies.last).to eq("AQI report for PSU STAR LAB SEL: PM2.5 \u00030875.1\u0003 |\u000308_\u000304▅\u000305███\u0003| \u000305322.93\u0003 max: \u000305322.93\u0003 \u000314(7 day average to 10 min average)\u0003")
expect(replies.last).to eq("AQI report for PSU STAR LAB SEL: PM2.5 \u00030874\u0003 |\u000308_\u000304▅\u000306█\u000305███\u0003| \u000305318\u0003 max: \u000305318\u0003 \u000314(7 day average to 10 min average)\u0003")
end

end

0 comments on commit 774ee26

Please sign in to comment.