From 6f1b7357dee2d95675dcc338170506675be5dab3 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Sun, 15 Sep 2019 19:22:12 -0500 Subject: [PATCH] Added unit conversion support and associated sanity checks --- run.rb | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/run.rb b/run.rb index cc3fe0c..0e4bb8d 100755 --- a/run.rb +++ b/run.rb @@ -25,8 +25,10 @@ admin = @conf['admin'] @tunit = @conf['units'] ## Non-configurable -@simplehot = 75 -@simplecold = 50 +#@simplehot = 75 +#@simplecold = 50 +@simplehot = 24 +@simplecold = 10 ### Begin sanity check ### STDOUT.sync = true @@ -126,7 +128,15 @@ if @tunit.nil? puts "Error(#{errcount.to_s}): Temperature units not configured! Defaulting to Rankine. Continuing...\n\n" @tunit = 'R' else - print "OK\n".green.bold + case @tunit + when "C", "F", "K", "R" + print "OK\n".green.bold + else + errcount += 1 + print "FAIL!\n\n".red.bold + puts "Error(#{errcount.to_s}): Temperature unit '#{@tunit}' not recognized! Defaulting to Rankine. Continuing...\n\n" + @tunit = 'R' + end end print "Checking probes list .................... " if @probes.nil? @@ -179,7 +189,9 @@ end class String def is_integer? /\A[-+]?\d+\z/ === self - #!!(self =~ /\A[-+]?[0-9]+\z/) + end + def is_float? + /^\s*[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)(\s*|([eE][+-]?(\d+_?)*\d+)\s*)$/ === self end end def process_tdata(host, simple=false) @@ -187,8 +199,23 @@ def process_tdata(host, simple=false) STDOUT.flush if system("nmap #{host} -p 22 2>&1 | grep 22 | grep open >/dev/null") tdata = `ssh -oBatchMode=yes #{host} heatbot_gettemp` - if tdata.is_integer? - print tdata + "°#{@tunit}" + #if tdata.is_integer? + if tdata.is_float? + tdf = tdata.to_f + case @tunit + when "C" + #Do nothing; expected input is Celsius + when "F" + tdf = (tdf * 1.8) + 32 + when "K" + tdf = tdf + 273.15 + when "R" + tdf = (tdf * 1.8) + 491.67 + else + puts "#{@tunit} not valid temperature unit! Submitting unmodified output!" + end + ctdata = tdf.round.to_s + print ctdata + "°#{@tunit}" + " (" + "Raw: ".bold + "#{tdata})" if simple #This is mostly just an Easter egg if tdata.to_i > @simplehot sdata = "Hot" @@ -203,7 +230,7 @@ def process_tdata(host, simple=false) else print "\n" STDOUT.flush - return tdata + "°#{@tunit}" + return ctdata + "°#{@tunit}" end else puts "Unexpected output from [".red.bold + host.bold + "]: ".red.bold + tdata