diff options
author | Nathan Lasseter <nathan@4574.co.uk> | 2013-06-05 21:00:05 +0100 |
---|---|---|
committer | Nathan Lasseter <nathan@4574.co.uk> | 2013-06-05 21:00:05 +0100 |
commit | b64d5c1ddf48906b924ace4190d5fd5153901b7d (patch) | |
tree | 715d87b5a21d63cd79fda30c8c6512875b701433 /temp.rb |
Initial commit
Diffstat (limited to 'temp.rb')
-rw-r--r-- | temp.rb | 97 |
1 files changed, 97 insertions, 0 deletions
@@ -0,0 +1,97 @@ +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 = "" + + IO.readlines("/sys/devices/w1_bus_master1/w1_master_slaves").each do |idn| + id = idn.chomp + + lines = IO.readlines "/sys/devices/w1_bus_master1/#{id}/w1_slave" + + 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 + + else + + File.open("tempworker.pid", "w") do |file| + file.write "#{CONFIG[:broker][:clientid]} : #{pid}" + end + + puts "Worker #{CONFIG[:broker][:clientid]} started on PID #{pid}." + + Process.detach(pid) + + end + +end + +def stop + + IO.readlines("tempworker.pid").each do |line| + + linearr = line.split ":" + Process.kill "TERM", linearr[1].to_i + + puts "Worker #{CONFIG[:broker][:clientid]}, PID #{linearr[1].to_i} killed." + + end + + File.unlink "tempworker.pid" + +end + +def restart + stop + start +end + +case ARGV.first + when "start" + start + when "stop" + stop + when "restart" + restart + else + puts "Usage: ruby temp.rb { start | stop | restart }" +end |