summaryrefslogtreecommitdiff
path: root/temp.rb
diff options
context:
space:
mode:
authorNathan Lasseter <nathan@4574.co.uk>2013-06-08 19:19:37 +0100
committerNathan Lasseter <nathan@4574.co.uk>2013-06-08 19:19:37 +0100
commitb8bdb40813379082ed232b84f03f498421f32b14 (patch)
tree6bcd3db7f6361078b39f19dff6003e866397b7ca /temp.rb
parent833f4d033e7bd4cf9ff4219fdee4e022ba956cd0 (diff)
Untested lintinguntested
Diffstat (limited to 'temp.rb')
-rw-r--r--temp.rb98
1 files 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