Class: Stoker

Inherits:
Object
  • Object
show all
Defined in:
lib/stoker.rb

Constant Summary collapse

ALARMS =
["None", "Food", "Fire"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = nil, options = {}) ⇒ Stoker

Returns a new instance of Stoker.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/stoker.rb', line 21

def initialize(host = nil, options = {})
  @host         = host
  @http_port    = options[:http_port]   || 80
  @telnet_port  = options[:telnet_port] || 23
  @user         = options[:user]        || "root"
  @pass         = options[:pass]        || "tini"
  @telnet       = nil
  @sensors      = []
  @blowers      = []
  
  read_sensors
end

Instance Attribute Details

#blower_optsObject (readonly)

Returns the value of attribute blower_opts.



17
18
19
# File 'lib/stoker.rb', line 17

def blower_opts
  @blower_opts
end

#blowersObject (readonly)

Returns the value of attribute blowers.



17
18
19
# File 'lib/stoker.rb', line 17

def blowers
  @blowers
end

#hostObject

Returns the value of attribute host.



15
16
17
# File 'lib/stoker.rb', line 15

def host
  @host
end

#http_portObject

Returns the value of attribute http_port.



15
16
17
# File 'lib/stoker.rb', line 15

def http_port
  @http_port
end

#passObject

Returns the value of attribute pass.



15
16
17
# File 'lib/stoker.rb', line 15

def pass
  @pass
end

#sensor_optsObject (readonly)

Returns the value of attribute sensor_opts.



17
18
19
# File 'lib/stoker.rb', line 17

def sensor_opts
  @sensor_opts
end

#sensorsObject (readonly)

Returns the value of attribute sensors.



17
18
19
# File 'lib/stoker.rb', line 17

def sensors
  @sensors
end

#telnetObject (readonly)

Returns the value of attribute telnet.



17
18
19
# File 'lib/stoker.rb', line 17

def telnet
  @telnet
end

#telnet_portObject

Returns the value of attribute telnet_port.



15
16
17
# File 'lib/stoker.rb', line 15

def telnet_port
  @telnet_port
end

#userObject

Returns the value of attribute user.



15
16
17
# File 'lib/stoker.rb', line 15

def user
  @user
end

Instance Method Details

#blower(str) ⇒ Object



131
132
133
134
135
# File 'lib/stoker.rb', line 131

def blower(str)
  raise "Blower not specified" if str.nil?
  str = str.downcase
  @blowers.find{|b| b.name.downcase == str or b.serial_number.downcase == str}
end

#connectObject



34
35
36
37
38
39
40
# File 'lib/stoker.rb', line 34

def connect
  @telnet = Net::Telnet.new("Host" => @host, "Port" => @telnet_port)
  @telnet.(@user, @pass)
  # bbq -k
  # gc
  # bbq -t
end

#disconnectObject



42
43
44
# File 'lib/stoker.rb', line 42

def disconnect
  @telnet.close
end

#post(params = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/stoker.rb', line 137

def post(params = {})
  post_url = "http://#{@host}:#{@http_port}/stoker.Post_Handler"
  
  queries = []

  if TEST
    params.each do |k,v|
      queries << "#{k}=#{CGI::escape(v)}"
    end

    q = queries.join("&")

    warn "#{post_url}?#{q}"
  else
    # stoker http doesn't like keep alive, so have to do post request the long way
    # res = HTTP.post_form(URI.parse(post_url), params)
    url = URI.parse(post_url)
    req = HTTP::Post.new(url.path)
    req["Keep-Alive"] = false
    req.set_form_data(params)
    res = HTTP.new(url.host, url.port).start {|http| http.request(req) }
    case res
    when Net::HTTPSuccess, Net::HTTPRedirection
      # puts res.body
      true
    else
      res.error!
    end
  end
end

#read_sensors(attempt = 1) ⇒ Object



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/stoker.rb', line 46

def read_sensors(attempt = 1)
  @sensors    = []
  @blowers    = []
  html        = TEST_HTML if TEST
  html      ||= open("http://#{@host}:#{@http_port}")
  contents    = html.read
  @sensor_opts = []
  @blower_opts = []
  
  doc = Hpricot(contents)

  (doc/"td.ser_num/b[text() = 'Blower']:first/../../../tr").each do |row|
    unless (row/"td:first/b").size > 0
      @blower_opts << {
        :serial_number  => row.at("td[1]").inner_html,
        :name           => row.at("td[2]/input")['value'].strip
      }
    end
  end
  
  (doc/"td.ser_num/b[text() = 'Temp Sensor']:first/../../../tr").each do |row|
    unless (row/"td:first/b").size > 0
      @sensor_opts << {
        :serial_number  => row.at("td[1]").inner_html,
        :name           => row.at("td[2]/input")['value'].strip,
        :temp           => row.at("td[3]").inner_html,
        :target         => row.at("td[4]/input")['value'].strip,
        :low            => row.at("td[6]/input")['value'].strip,
        :high           => row.at("td[7]/input")['value'].strip
      }
    end
  end

  if contents =~ /sel = \[(.*)\];$/
    blower_alarm_string = $1
  end
  
  count       = 0
  for_sensor  = 0
  blower_alarm_string.split(",").each do |val|
    case count
    when 0
      @sensor_opts[for_sensor][:alarm] = Stoker::ALARMS[val.to_i]
      count += 1
    when 1
      @sensor_opts[for_sensor][:blower_serial_number] = val.gsub(/\"/,'') unless val == '""'
      count = 0
      for_sensor += 1
    end
  end

  @sensor_opts.each do |s|
    if s[:blower_serial_number].to_s != ""
      @blower_opts.each do |b|
        if b[:serial_number] == s[:blower_serial_number]
          b[:sensor_serial_number] = s[:serial_number]
        end
      end
    end
  end

  @sensor_opts.each do |s|
    @sensors << Sensor.new(self, s)
  end

  @blower_opts.each do |b|
    @blowers << Blower.new(self, b)
  end

rescue Net::HTTPBadResponse
  if attempt > 4
    raise "Web page output corrupt.  Tried too many times, giving up."
  else
    attempt += 1
    warn "Warning: Web page output corrupt.  Retrying... attempt #{attempt}"
    read_sensors(attempt)
  end
end

#sensor(str) ⇒ Object



125
126
127
128
129
# File 'lib/stoker.rb', line 125

def sensor(str)
  raise "Sensor not specified" if str.nil?
  str = str.downcase
  @sensors.find{|s| s.name.downcase == str or s.serial_number.downcase == str}
end