Class: Communicator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommunicator



7
8
9
10
# File 'lib/communicator.rb', line 7

def initialize
  @slack = configure_slack
  @brewer = Brewer.new
end

Instance Attribute Details

#brewerObject

Returns the value of attribute brewer.



5
6
7
# File 'lib/communicator.rb', line 5

def brewer
  @brewer
end

#slackObject

Returns the value of attribute slack.



5
6
7
# File 'lib/communicator.rb', line 5

def slack
  @slack
end

Instance Method Details

#configure_slackObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/communicator.rb', line 12

def configure_slack
  raise "settings.rb must be called before slack is configured" unless $settings
  unless File.readlines($settings['settings_cache']).grep(/webhook_url/).size > 0
    store = YAML::Store.new $settings['settings_cache']
    print "Enter your Slack webhook url: "
    webhook_url = gets.chomp
    store.transaction { store['webhook_url'] = webhook_url }
  end
  return Slack::Notifier.new YAML.load(File.open($settings['settings_cache']))['webhook_url']
end

#monitorObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/communicator.rb', line 49

def monitor
  while true do
    status_table_rows = [
      ["Current Temp", @brewer.pv],
      ["Set Value Temp", @brewer.sv],
      ["PID is: ", @brewer.pid['pid_running'].to_b ? "on" : "off"],
      ["Pump is: ", @brewer.pump]
    ]

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

    clear_screen
    puts status_table
    sleep(1)
  end
end

#ping(message = "ping at #{Time.now}") ⇒ Object



23
24
25
26
27
28
29
# File 'lib/communicator.rb', line 23

def ping(message="ping at #{Time.now}")
  if message.is_a? Array
    final = message.join("\n")
    @slack.ping(final)
  end
  @slack.ping(message)
end

#slack_monitor(delay = 10) ⇒ Object

TODO: test these methods



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/communicator.rb', line 32

def slack_monitor(delay=10)
  while true do
    before_temp = @brewer.pv
    @brewer.wait(to_seconds(delay))
    diff = @brewer.pv - before_temp

    ping([
      "Current Temperature: #{@brewer.pid['pv_temp']} F",
      "Set Value Temperature: #{@brewer.pid['sv_temp']} F",
      "Current temperature has climed #{diff} F since #{delay} minute(s) ago",
      "Sent at #{Time.now.strftime("%H:%M")}",
      ""
    ])
  end
  true
end