Improve code formatting
This commit is contained in:
parent
5a1e6c78a9
commit
acf8d4275d
2 changed files with 182 additions and 178 deletions
|
|
@ -10,3 +10,7 @@ Layout/IndentationWidth:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
Layout/FirstArgumentIndentation:
|
Layout/FirstArgumentIndentation:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
Layout/LineLength:
|
||||||
|
Enabled: false
|
||||||
|
Metrics/BlockLength:
|
||||||
|
Enabled: false
|
||||||
|
|
|
||||||
356
main.rb
356
main.rb
|
|
@ -11,204 +11,204 @@ options = {
|
||||||
:user_name => ENV['SMTP_UNAME'],
|
:user_name => ENV['SMTP_UNAME'],
|
||||||
:password => ENV['SMTP_PWORD'],
|
:password => ENV['SMTP_PWORD'],
|
||||||
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
|
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
|
||||||
:domain => ENV['SMTP_DOMAIN'], # the HELO domain provided by the client to the server
|
:domain => ENV['SMTP_DOMAIN'] # the HELO domain provided by the client to the server
|
||||||
}
|
}
|
||||||
|
|
||||||
begin
|
begin
|
||||||
client = Mysql2::Client.new(
|
client = Mysql2::Client.new(
|
||||||
host: ENV['MARIA_HOST'],
|
host: ENV['MARIA_HOST'],
|
||||||
port: ENV['MARIA_PORT'],
|
port: ENV['MARIA_PORT'],
|
||||||
username: ENV['MARIA_UNAME'],
|
username: ENV['MARIA_UNAME'],
|
||||||
password: ENV['MARIA_PWORD'],
|
password: ENV['MARIA_PWORD'],
|
||||||
database: ENV['MARIA_DBASE']
|
database: ENV['MARIA_DBASE']
|
||||||
)
|
)
|
||||||
puts "Successfully connected to MariaDB!"
|
|
||||||
|
|
||||||
puts "Compiling data..."
|
puts 'Successfully connected to MariaDB!'
|
||||||
results_today = client.query(%(
|
|
||||||
SELECT LogDateTime, temp, dew, hum, rrate, rmonth, ryear
|
|
||||||
FROM Realtime
|
|
||||||
WHERE CAST(LogDateTime AS DATE) = CAST(NOW() AS DATE)
|
|
||||||
ORDER BY LogDateTime ASC
|
|
||||||
))
|
|
||||||
results_now = client.query(%(
|
|
||||||
SELECT LogDateTime, temp, dew, hum, press, presstrendval, rfall, rrate, rainunit, wspeed, wgust, windunit,
|
|
||||||
windTM, TwindTM, wgustTM, TwgustTM
|
|
||||||
FROM Realtime
|
|
||||||
ORDER BY LogDateTime DESC
|
|
||||||
LIMIT 1
|
|
||||||
))
|
|
||||||
|
|
||||||
today = results_today.first
|
puts 'Compiling data...'
|
||||||
now = results_now.first
|
results_today = client.query(%(
|
||||||
|
SELECT LogDateTime, temp, dew, hum, rrate, rmonth, ryear
|
||||||
|
FROM Realtime
|
||||||
|
WHERE CAST(LogDateTime AS DATE) = CAST(NOW() AS DATE)
|
||||||
|
ORDER BY LogDateTime ASC
|
||||||
|
))
|
||||||
|
results_now = client.query(%(
|
||||||
|
SELECT LogDateTime, temp, dew, hum, press, presstrendval, rfall, rrate, rainunit, wspeed, wgust, windunit,
|
||||||
|
windTM, TwindTM, wgustTM, TwgustTM
|
||||||
|
FROM Realtime
|
||||||
|
ORDER BY LogDateTime DESC
|
||||||
|
LIMIT 1
|
||||||
|
))
|
||||||
|
|
||||||
maxmin = {
|
today = results_today.first
|
||||||
:temp => {
|
now = results_now.first
|
||||||
:max => {
|
|
||||||
:value => -1000,
|
maxmin = {
|
||||||
:time => nil
|
:temp => {
|
||||||
},
|
:max => {
|
||||||
:min => {
|
:value => -1000,
|
||||||
:value => 1000,
|
:time => nil
|
||||||
:time => nil
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
:dew => {
|
:min => {
|
||||||
:max => {
|
:value => 1000,
|
||||||
:value => -1000,
|
:time => nil
|
||||||
:time => nil
|
}
|
||||||
},
|
},
|
||||||
:min => {
|
:dew => {
|
||||||
:value => 1000,
|
:max => {
|
||||||
:time => nil
|
:value => -1000,
|
||||||
}
|
:time => nil
|
||||||
},
|
},
|
||||||
:hum => {
|
:min => {
|
||||||
:max => {
|
:value => 1000,
|
||||||
:value => -1,
|
:time => nil
|
||||||
:time => nil
|
}
|
||||||
},
|
},
|
||||||
:min => {
|
:hum => {
|
||||||
:value => 101,
|
:max => {
|
||||||
:time => nil
|
:value => -1,
|
||||||
}
|
:time => nil
|
||||||
},
|
},
|
||||||
:rainrate => {
|
:min => {
|
||||||
:max => {
|
:value => 101,
|
||||||
:value => 0,
|
:time => nil
|
||||||
:time => nil
|
}
|
||||||
}
|
},
|
||||||
|
:rainrate => {
|
||||||
|
:max => {
|
||||||
|
:value => 0,
|
||||||
|
:time => nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
results_today.each do |result|
|
results_today.each do |result|
|
||||||
# Max Temp
|
# Max Temp
|
||||||
if result['temp'] >= maxmin[:temp][:max][:value]
|
if result['temp'] >= maxmin[:temp][:max][:value]
|
||||||
maxmin[:temp][:max][:value] = result['temp'].to_f
|
maxmin[:temp][:max][:value] = result['temp'].to_f
|
||||||
maxmin[:temp][:max][:time] = result['LogDateTime'].strftime("%H:%M")
|
maxmin[:temp][:max][:time] = result['LogDateTime'].strftime('%H:%M')
|
||||||
end
|
|
||||||
|
|
||||||
# Min Temp
|
|
||||||
if result['temp'] <= maxmin[:temp][:min][:value]
|
|
||||||
maxmin[:temp][:min][:value] = result['temp'].to_f
|
|
||||||
maxmin[:temp][:min][:time] = result['LogDateTime'].strftime("%H:%M")
|
|
||||||
end
|
|
||||||
|
|
||||||
# Max Dew Point
|
|
||||||
if result['dew'] >= maxmin[:dew][:max][:value]
|
|
||||||
maxmin[:dew][:max][:value] = result['dew'].to_f
|
|
||||||
maxmin[:dew][:max][:time] = result['LogDateTime'].strftime("%H:%M")
|
|
||||||
end
|
|
||||||
|
|
||||||
# Min Dew Point
|
|
||||||
if result['dew'] <= maxmin[:dew][:min][:value]
|
|
||||||
maxmin[:dew][:min][:value] = result['dew'].to_f
|
|
||||||
maxmin[:dew][:min][:time] = result['LogDateTime'].strftime("%H:%M")
|
|
||||||
end
|
|
||||||
|
|
||||||
# Max Humidity
|
|
||||||
if result['hum'] >= maxmin[:hum][:max][:value]
|
|
||||||
maxmin[:hum][:max][:value] = result['hum']
|
|
||||||
maxmin[:hum][:max][:time] = result['LogDateTime'].strftime("%H:%M")
|
|
||||||
end
|
|
||||||
|
|
||||||
# Min Humidity
|
|
||||||
if result['hum'] <= maxmin[:hum][:min][:value]
|
|
||||||
maxmin[:hum][:min][:value] = result['hum']
|
|
||||||
maxmin[:hum][:min][:time] = result['LogDateTime'].strftime("%H:%M")
|
|
||||||
end
|
|
||||||
|
|
||||||
# Max Rain Rate
|
|
||||||
if result['rrate'] >= maxmin[:rainrate][:max][:value]
|
|
||||||
maxmin[:rainrate][:max][:value] = result['rrate'].to_f
|
|
||||||
maxmin[:rainrate][:max][:time] = result['LogDateTime'].strftime("%H:%M")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
puts "Data compiled!"
|
# Min Temp
|
||||||
|
if result['temp'] <= maxmin[:temp][:min][:value]
|
||||||
puts "Generating e-mail body..."
|
maxmin[:temp][:min][:value] = result['temp'].to_f
|
||||||
|
maxmin[:temp][:min][:time] = result['LogDateTime'].strftime('%H:%M')
|
||||||
table = Terminal::Table.new :title => "Weather Summary as of #{now["LogDateTime"].strftime("%l:%M %p, %a %b %d %Y")}" do |t|
|
|
||||||
t << [
|
|
||||||
{ :value => "Temperature", :colspan => 2, :alignment => :center},
|
|
||||||
{ :value => "Dew Point", :colspan => 2, :alignment => :center}
|
|
||||||
]
|
|
||||||
t << :separator
|
|
||||||
t << [
|
|
||||||
"Current", "#{now["temp"].to_s("F")}C",
|
|
||||||
"Current", "#{now["dew"].to_s("F")}C"
|
|
||||||
]
|
|
||||||
t << [
|
|
||||||
"Today's High", "#{maxmin[:temp][:max][:value]}C at #{maxmin[:temp][:max][:time]}",
|
|
||||||
"Today's High", "#{maxmin[:dew][:max][:value]}C at #{maxmin[:dew][:max][:time]}"
|
|
||||||
]
|
|
||||||
t << [
|
|
||||||
"Today's Low", "#{maxmin[:temp][:min][:value]}C at #{maxmin[:temp][:min][:time]}",
|
|
||||||
"Today's Low", "#{maxmin[:dew][:min][:value]}C at #{maxmin[:dew][:min][:time]}"
|
|
||||||
]
|
|
||||||
t << :separator
|
|
||||||
t << [
|
|
||||||
{ :value => "Humidity", :colspan => 2, :alignment => :center},
|
|
||||||
{ :value => "Barometric Pressure (SL)", :colspan => 2, :alignment => :center}
|
|
||||||
]
|
|
||||||
t << :separator
|
|
||||||
t << [
|
|
||||||
"Current", "#{now["hum"]}%",
|
|
||||||
"Current", "#{now["press"].to_s("F")} mb"
|
|
||||||
]
|
|
||||||
t << [
|
|
||||||
"Today's High", "#{maxmin[:hum][:max][:value]}% at #{maxmin[:hum][:max][:time]}",
|
|
||||||
"Trend", "#{now["presstrendval"]} mb/hr"
|
|
||||||
]
|
|
||||||
t << [
|
|
||||||
"Today's Low", "#{maxmin[:hum][:min][:value]}% at #{maxmin[:hum][:min][:time]}",
|
|
||||||
"",""
|
|
||||||
]
|
|
||||||
t << :separator
|
|
||||||
t << [
|
|
||||||
{ :value => "Rain", :colspan => 2, :alignment => :center},
|
|
||||||
{ :value => "Wind", :colspan => 2, :alignment => :center}
|
|
||||||
]
|
|
||||||
t << :separator
|
|
||||||
t << [
|
|
||||||
"Current Rate", "#{now["rrate"]} #{now["rainunit"]}",
|
|
||||||
"Current Speed", "#{now["wspeed"].to_s} #{now["windunit"]}"
|
|
||||||
]
|
|
||||||
t << [
|
|
||||||
"Today's High Rate", "#{maxmin[:rainrate][:max][:value]} #{now["rainunit"]} at #{maxmin[:rainrate][:max][:time]}",
|
|
||||||
"Gust", "#{now["wgust"].to_s} #{now["windunit"]}"
|
|
||||||
]
|
|
||||||
t << [
|
|
||||||
"Today's Total", "#{now["rfall"].to_s("F")} #{now["rainunit"]}",
|
|
||||||
"Today's High Speed", "#{now["windTM"].to_s} #{now["windunit"]} at #{now["TwindTM"]}"
|
|
||||||
]
|
|
||||||
t << [
|
|
||||||
"Monthly Total","#{today["rmonth"].to_s("F")} #{now["rainunit"]}",
|
|
||||||
"Today's High Gust", "#{now["wgustTM"]} #{now["windunit"]} at #{now["TwgustTM"]}"
|
|
||||||
]
|
|
||||||
t << [
|
|
||||||
"Yearly Total","#{today["ryear"].to_s("F")} #{now["rainunit"]}",
|
|
||||||
"", ""
|
|
||||||
]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
puts "E-mail body generated!"
|
# Max Dew Point
|
||||||
|
if result['dew'] >= maxmin[:dew][:max][:value]
|
||||||
|
maxmin[:dew][:max][:value] = result['dew'].to_f
|
||||||
|
maxmin[:dew][:max][:time] = result['LogDateTime'].strftime('%H:%M')
|
||||||
|
end
|
||||||
|
|
||||||
puts "Sending e-mail..."
|
# Min Dew Point
|
||||||
|
if result['dew'] <= maxmin[:dew][:min][:value]
|
||||||
|
maxmin[:dew][:min][:value] = result['dew'].to_f
|
||||||
|
maxmin[:dew][:min][:time] = result['LogDateTime'].strftime('%H:%M')
|
||||||
|
end
|
||||||
|
|
||||||
Pony.mail(
|
# Max Humidity
|
||||||
:to => ENV['MAIL_RECIPIENT'],
|
if result['hum'] >= maxmin[:hum][:max][:value]
|
||||||
:from => ENV['MAIL_SENDER'],
|
maxmin[:hum][:max][:value] = result['hum']
|
||||||
:subject => 'Daily Weather Summary',
|
maxmin[:hum][:max][:time] = result['LogDateTime'].strftime('%H:%M')
|
||||||
:body => table.to_s,
|
end
|
||||||
:via => :smtp,
|
|
||||||
:via_options => options
|
|
||||||
)
|
|
||||||
|
|
||||||
puts "E-mail sent!"
|
# Min Humidity
|
||||||
puts "DONE!"
|
if result['hum'] <= maxmin[:hum][:min][:value]
|
||||||
|
maxmin[:hum][:min][:value] = result['hum']
|
||||||
|
maxmin[:hum][:min][:time] = result['LogDateTime'].strftime('%H:%M')
|
||||||
|
end
|
||||||
|
|
||||||
|
# Max Rain Rate
|
||||||
|
if result['rrate'] >= maxmin[:rainrate][:max][:value]
|
||||||
|
maxmin[:rainrate][:max][:value] = result['rrate'].to_f
|
||||||
|
maxmin[:rainrate][:max][:time] = result['LogDateTime'].strftime('%H:%M')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
puts 'Data compiled!'
|
||||||
|
|
||||||
|
puts 'Generating e-mail body...'
|
||||||
|
|
||||||
|
table = Terminal::Table.new :title => "Weather Summary as of #{now['LogDateTime'].strftime('%l:%M %p, %a %b %d %Y')}" do |t|
|
||||||
|
t << [
|
||||||
|
{ :value => 'Temperature', :colspan => 2, :alignment => :center },
|
||||||
|
{ :value => 'Dew Point', :colspan => 2, :alignment => :center }
|
||||||
|
]
|
||||||
|
t << :separator
|
||||||
|
t << [
|
||||||
|
'Current', "#{now['temp'].to_s('F')}C",
|
||||||
|
'Current', "#{now['dew'].to_s('F')}C"
|
||||||
|
]
|
||||||
|
t << [
|
||||||
|
"Today's High", "#{maxmin[:temp][:max][:value]}C at #{maxmin[:temp][:max][:time]}",
|
||||||
|
"Today's High", "#{maxmin[:dew][:max][:value]}C at #{maxmin[:dew][:max][:time]}"
|
||||||
|
]
|
||||||
|
t << [
|
||||||
|
"Today's Low", "#{maxmin[:temp][:min][:value]}C at #{maxmin[:temp][:min][:time]}",
|
||||||
|
"Today's Low", "#{maxmin[:dew][:min][:value]}C at #{maxmin[:dew][:min][:time]}"
|
||||||
|
]
|
||||||
|
t << :separator
|
||||||
|
t << [
|
||||||
|
{ :value => 'Humidity', :colspan => 2, :alignment => :center },
|
||||||
|
{ :value => 'Barometric Pressure (SL)', :colspan => 2, :alignment => :center }
|
||||||
|
]
|
||||||
|
t << :separator
|
||||||
|
t << [
|
||||||
|
'Current', "#{now['hum']}%",
|
||||||
|
'Current', "#{now['press'].to_s('F')} mb"
|
||||||
|
]
|
||||||
|
t << [
|
||||||
|
"Today's High", "#{maxmin[:hum][:max][:value]}% at #{maxmin[:hum][:max][:time]}",
|
||||||
|
'Trend', "#{now['presstrendval']} mb/hr"
|
||||||
|
]
|
||||||
|
t << [
|
||||||
|
"Today's Low", "#{maxmin[:hum][:min][:value]}% at #{maxmin[:hum][:min][:time]}",
|
||||||
|
'', ''
|
||||||
|
]
|
||||||
|
t << :separator
|
||||||
|
t << [
|
||||||
|
{ :value => 'Rain', :colspan => 2, :alignment => :center },
|
||||||
|
{ :value => 'Wind', :colspan => 2, :alignment => :center }
|
||||||
|
]
|
||||||
|
t << :separator
|
||||||
|
t << [
|
||||||
|
'Current Rate', "#{now['rrate']} #{now['rainunit']}",
|
||||||
|
'Current Speed', "#{now['wspeed']} #{now['windunit']}"
|
||||||
|
]
|
||||||
|
t << [
|
||||||
|
"Today's High Rate", "#{maxmin[:rainrate][:max][:value]} #{now['rainunit']} at #{maxmin[:rainrate][:max][:time]}",
|
||||||
|
'Gust', "#{now['wgust']} #{now['windunit']}"
|
||||||
|
]
|
||||||
|
t << [
|
||||||
|
"Today's Total", "#{now['rfall'].to_s('F')} #{now['rainunit']}",
|
||||||
|
"Today's High Speed", "#{now['windTM']} #{now['windunit']} at #{now['TwindTM']}"
|
||||||
|
]
|
||||||
|
t << [
|
||||||
|
'Monthly Total', "#{today['rmonth'].to_s('F')} #{now['rainunit']}",
|
||||||
|
"Today's High Gust", "#{now['wgustTM']} #{now['windunit']} at #{now['TwgustTM']}"
|
||||||
|
]
|
||||||
|
t << [
|
||||||
|
'Yearly Total', "#{today['ryear'].to_s('F')} #{now['rainunit']}",
|
||||||
|
'', ''
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
puts 'E-mail body generated!'
|
||||||
|
|
||||||
|
puts 'Sending e-mail...'
|
||||||
|
|
||||||
|
Pony.mail(
|
||||||
|
:to => ENV['MAIL_RECIPIENT'],
|
||||||
|
:from => ENV['MAIL_SENDER'],
|
||||||
|
:subject => 'Daily Weather Summary',
|
||||||
|
:body => table.to_s,
|
||||||
|
:via => :smtp,
|
||||||
|
:via_options => options
|
||||||
|
)
|
||||||
|
|
||||||
|
puts 'E-mail sent!'
|
||||||
|
puts 'DONE!'
|
||||||
rescue Mysql2::Error => e
|
rescue Mysql2::Error => e
|
||||||
puts "Error connecting to database: #{e.message}"
|
puts "Error connecting to database: #{e.message}"
|
||||||
ensure
|
ensure
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue