Class: Communicator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommunicator

Returns a new instance of Communicator.



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

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

Instance Attribute Details

#brewerObject

Returns the value of attribute brewer.



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

def brewer
  @brewer
end

#slackObject

Returns the value of attribute slack.



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

def slack
  @slack
end

Instance Method Details

#configure_slackObject



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

def configure_slack
  unless @settings.settings['webhook_url']
    print "Slack Webhook URL: "
    webhook_url = gets.chomp
    @settings.add({
      'webhook_url' => webhook_url
    })
  end
  return Slack::Notifier.new @settings.settings['webhook_url']
end

#monitorObject



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

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



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

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



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

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