From b8bdb40813379082ed232b84f03f498421f32b14 Mon Sep 17 00:00:00 2001 From: Nathan Lasseter Date: Sat, 8 Jun 2013 19:19:37 +0100 Subject: Untested linting --- temp.rb | 98 +++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 50 insertions(+), 48 deletions(-) diff --git a/temp.rb b/temp.rb index c899f70..9641df5 100644 --- a/temp.rb +++ b/temp.rb @@ -2,71 +2,70 @@ require 'rubygems' require 'mqtt' require 'yaml' -CONFIG = open("config.yml") do |fd| YAML.load fd end - -def start - - pid = Process.fork - - if pid.nil? then - - cli = MQTT::Client.new( - :remote_host => CONFIG[:broker][:remote], - :client_id => CONFIG[:broker][:clientid], - :will_topic => CONFIG[:topics][:lwt], - :will_payload => "Last Will and Testament: #{CONFIG[:broker][:clientid]} has died!" - ) - - cli.connect do |c| - - Signal.trap("TERM") do - c.publish "#{CONFIG[:topics][:status]}/#{CONFIG[:broker][:clientid]}", "Stopped" - c.disconnect - Process.exit! true - end - - c.publish "#{CONFIG[:topics][:status]}/#{CONFIG[:broker][:clientid]}", "Started" - - while true do - - bulk = "" +CONFIG = open("config.yml") do |fd| + YAML.load fd +end - IO.readlines("/sys/devices/w1_bus_master1/w1_master_slaves").each do |idn| - id = idn.chomp +def beaworker + cli = MQTT::Client.new( + :remote_host => CONFIG[:broker][:remote], + :client_id => CONFIG[:broker][:clientid], + :will_topic => CONFIG[:topics][:lwt], + :will_payload => "Last Will and Testament: #{CONFIG[:broker][:clientid]} has died!" + ) + + cli.connect do |c| + Signal.trap("TERM") do + c.publish "#{CONFIG[:topics][:status]}/#{CONFIG[:broker][:clientid]}", "Stopped" + c.disconnect + Process.exit! true + end - lines = IO.readlines "/sys/devices/w1_bus_master1/#{id}/w1_slave" + c.publish "#{CONFIG[:topics][:status]}/#{CONFIG[:broker][:clientid]}", "Started" - spl = lines[1].split "=" - temp = spl[1] + while true do + bulk = "" - c.publish "#{CONFIG[:topics][:report]}/#{CONFIG[:broker][:clientid]}/#{id}", "#{temp}" - bulk += "#{id} : #{temp}" - end + IO.readlines("/sys/devices/w1_bus_master1/w1_master_slaves").each do |idn| + id = idn.chomp - c.publish "#{CONFIG[:topics][:report]}/#{CONFIG[:broker][:clientid]}/bulk", bulk + lines = IO.readlines "/sys/devices/w1_bus_master1/#{id}/w1_slave" - sleep CONFIG[:reports][:period] + spl = lines[1].split "=" + temp = spl[1] + c.publish "#{CONFIG[:topics][:report]}/#{CONFIG[:broker][:clientid]}/#{id}", "#{temp}" + bulk += "#{id} : #{temp}" end + c.publish "#{CONFIG[:topics][:report]}/#{CONFIG[:broker][:clientid]}/bulk", bulk + + sleep CONFIG[:reports][:period] end + end +end - else +def beaparent pid + File.open("tempworker.pid", "w") do |file| + file.write "#{CONFIG[:broker][:clientid]} : #{pid}" + end - File.open("tempworker.pid", "w") do |file| - file.write "#{CONFIG[:broker][:clientid]} : #{pid}" - end + puts "Worker #{CONFIG[:broker][:clientid]} started on PID #{pid}." - puts "Worker #{CONFIG[:broker][:clientid]} started on PID #{pid}." + Process.detach(pid) +end - Process.detach(pid) +def start + pid = Process.fork + if pid.nil? then + beaworker + else + beaparent pid end - end def stop - IO.readlines("tempworker.pid").each do |line| linearr = line.split ":" @@ -77,7 +76,6 @@ def stop end File.unlink "tempworker.pid" - end def restart @@ -85,6 +83,10 @@ def restart start end +def usage + puts "Usage: ruby temp.rb { start | stop | restart }" +end + case ARGV.first when "start" start @@ -93,5 +95,5 @@ case ARGV.first when "restart" restart else - puts "Usage: ruby temp.rb { start | stop | restart }" + usage end -- cgit v1.2.1