Class: Brewer::Controller

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

Overview

This class handles the physical brewing rig. Turning on valves, the pump, RIMS and such

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeController

Returns a new instance of Controller.



11
12
13
14
15
# File 'lib/brewer/controller.rb', line 11

def initialize
  @base_path = Dir.home + '/.brewer'
  Brewer::load_settings
  @temps = {}
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



8
9
10
# File 'lib/brewer/controller.rb', line 8

def base_path
  @base_path
end

#relaysObject

Returns the value of attribute relays.



9
10
11
# File 'lib/brewer/controller.rb', line 9

def relays
  @relays
end

#tempsObject

Returns the value of attribute temps.



9
10
11
# File 'lib/brewer/controller.rb', line 9

def temps
  @temps
end

Instance Method Details

#all_relays_statusObject

Returns the status of all relays



140
141
142
143
144
# File 'lib/brewer/controller.rb', line 140

def all_relays_status
  output = script("get_relay_status_test").split("\n")
  output.shift(3)
  return output
end

#hlt(state) ⇒ Object

Opens or closes hlt valve



208
209
210
211
# File 'lib/brewer/controller.rb', line 208

def hlt(state)
  relay($settings['hlt'], state)
  self
end

#hlt_to(location) ⇒ Object

Diverts hlt valve to mash or boil tun



196
197
198
199
200
201
202
203
204
205
# File 'lib/brewer/controller.rb', line 196

def hlt_to(location)
  if location == "mash"
    relay($settings['hltToMash'], 0)
  elsif location == "boil"
    relay($settings['hltToMash'], 1)
  else
    raise "Not a valid location for the hlt valve"
  end
  self
end

#monitorObject

This will display an updated status table every second :nocov:



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/brewer/controller.rb', line 98

def monitor
  while true do
    # Making a new table with TerminalTable takes about 1 second. I assign
    # it here so it has time to start up, and there's minimal lag between clearing
    # and displaying the table
    table = status_table
    wait(1)
    clear_screen
    puts table
  end
end

#pid(state = "status") ⇒ Object

Turns PID on or off, or gets status if no arg is provided



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/brewer/controller.rb', line 42

def pid(state="status")
  if state == "status"
    return {
      'pid_running' => script("is_pid_running"),
      'sv_temp' => sv,
      'pv_temp' => pv
    }
  end

  if state == 1
    script('set_pid_on')
    pump(1)
    return "Pump and PID are now on"
  else
    return script("set_pid_off")
  end
end

#pid_to_webObject

This is for jake’s js



61
62
63
64
65
66
67
# File 'lib/brewer/controller.rb', line 61

def pid_to_web
  return {
    'pid_running' => script('is_pid_running').to_b,
    'sv' => sv,
    'pv' => pv
  }
end

#pump(state = "status") ⇒ Object

Turns the pump on and off, or returns the status if no arg Turning the pump off will turn the pid off too, as it should not be on when the pump is off



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/brewer/controller.rb', line 26

def pump(state="status")
  if state == "status"
    return relay_status($settings['pump'])
  end

  if state == 1
    return script("set_pump_on")
  else
    if pid['pid_running'].to_b
      pid(0)
    end
    return script("set_pump_off")
  end
end

#pvObject

Returns the proccess value (this one can’t be changed)



79
80
81
# File 'lib/brewer/controller.rb', line 79

def pv
  script('get_pv').to_f
end

#relay(relay, state) ⇒ Object

Turns a relay on or off



125
126
127
128
# File 'lib/brewer/controller.rb', line 125

def relay(relay, state)
  script("set_relay", "#{relay} #{state}")
  true
end

#relay_config(params) ⇒ Object

Give this a relay configuration hash and it will set the relays to that configuration eg. => 0, ‘rims_to’ => ‘boil’, ‘pump’ => 1 This is so that we can set multiple valves at effectively the same time



172
173
174
175
176
177
178
179
180
# File 'lib/brewer/controller.rb', line 172

def relay_config(params)
  raise "Params must be a hash" unless params.is_a? Hash
  params.each do |method, setting|
    if method == "pump"
      setting = setting.to_i
    end
    send(method, setting)
  end
end

#relay_status(relay) ⇒ Object

Returns the status of a single relay



131
132
133
134
135
136
137
# File 'lib/brewer/controller.rb', line 131

def relay_status(relay)
  if script("get_relay_status", "#{relay}").include? "on"
    return "on"
  else
    return "off"
  end
end

#relays_statusObject

This returns a prettier version of all_relays_status, and only returns the relays in use, being 0-3.



148
149
150
151
152
153
154
155
156
# File 'lib/brewer/controller.rb', line 148

def relays_status
  statuses = {}
  all_relays_status.shift(4).each do |status|
    relay_num, status = status.match(/relay [0-9+]([0-9]+): (on|off)/).captures
    relay_names = $settings.select { |key, value| key.to_s.match(/hlt|rims[^A]|pump/) }
    statuses[relay_names.key(relay_num.to_i)] = status
  end
  statuses
end

#relays_status_to_webObject



158
159
160
161
162
163
164
165
166
167
# File 'lib/brewer/controller.rb', line 158

def relays_status_to_web
  statuses = relays_status
  statuses.each do |k, v|
    if v == "on"
      statuses[k] = 1
    else
      statuses[k] = 0
    end
  end
end

#rims_to(location) ⇒ Object

Diverts rims valve to mash or boil tun



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/brewer/controller.rb', line 183

def rims_to(location)
  if location == "mash"
    # we ended up swapping this relay, so the name is backwards
    relay($settings['rimsToMash'], 0)
  elsif location == "boil"
    relay($settings['rimsToMash'], 1)
  else
    raise "Not a valid location for rims valve"
  end
  self
end

#script(script, params = nil) ⇒ Object

Runs an adaptibrew script (written in python)



20
21
22
# File 'lib/brewer/controller.rb', line 20

def script(script, params=nil)
  `python #{@base_path}/adaptibrew/#{script}.py #{params}`.chomp
end

#status_tableObject

This returns a status table



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/brewer/controller.rb', line 112

def status_table
  status_table_rows = [
    ["Current Temp", pv],
    ["Set Value Temp", sv],
    ["PID is: ", pid['pid_running'].to_b ? "on" : "off"],
    ["Pump is: ", pump]
  ]

  status_table = Terminal::Table.new :headings => ["Item", "Status"], :rows => status_table_rows
  status_table
end

#sv(temp = nil) ⇒ Object

Sets the setpoint value (sv) on the PID, or returns the current SV



71
72
73
74
75
76
# File 'lib/brewer/controller.rb', line 71

def sv(temp=nil)
  if temp
    return script('set_sv', temp).to_f
  end
  script('get_sv').to_f
end

#watchObject

This method will wait until the pv >= sv Basically when the mash tun is at the set temperate, it will ping and return self. It will also display a status table every 2 seconds :nocov:



87
88
89
90
91
92
93
# File 'lib/brewer/controller.rb', line 87

def watch
  until pv >= sv do
    wait(2)
  end
  Slacker.new.ping("Temperature is now at #{pv.to_f} F")
  self
end