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



130
131
132
133
134
# File 'lib/brewer/controller.rb', line 130

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



187
188
189
190
# File 'lib/brewer/controller.rb', line 187

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

#hlt_to(location) ⇒ Object

Diverts hlt valve to mash or boil tun



175
176
177
178
179
180
181
182
183
184
# File 'lib/brewer/controller.rb', line 175

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:



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/brewer/controller.rb', line 88

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

#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)



69
70
71
# File 'lib/brewer/controller.rb', line 69

def pv
  script('get_pv').to_f
end

#relay(relay, state) ⇒ Object

Turns a relay on or off



115
116
117
118
# File 'lib/brewer/controller.rb', line 115

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



151
152
153
154
155
156
157
158
159
# File 'lib/brewer/controller.rb', line 151

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



121
122
123
124
125
126
127
# File 'lib/brewer/controller.rb', line 121

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.



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

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

#rims_to(location) ⇒ Object

Diverts rims valve to mash or boil tun



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/brewer/controller.rb', line 162

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



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/brewer/controller.rb', line 102

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



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

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:



77
78
79
80
81
82
83
# File 'lib/brewer/controller.rb', line 77

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