summaryrefslogtreecommitdiff
path: root/temp.rb
blob: c899f7091bc8571ed9b402ac5ce3ff028c75d113 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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