/
agent.rb
52 lines (40 loc) · 1.07 KB
/
agent.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require "particle_agent/config"
require "particle_agent/firmware_runner"
require "particle_agent/settings"
module ParticleAgent
# The logic for the agent that monitor the firmware
# Usually runs in the background as a daemon
class Agent
attr_reader :settings
def initialize
@settings = Settings.new
end
def run!(daemon)
load_settings
puts "Starting agent"
start_firmware_runners daemon
sleep 1 until daemon.quit?
puts "Quitting agent gracefully"
end
def load_settings
settings.load
end
def start_firmware_runners(daemon)
puts "No firmware to run." if firmware_paths.empty?
firmware_paths.map do |firmware_path|
Thread.new do
runner = FirmwareRunner.new(firmware_path)
runner.run!(daemon)
end
end.each(&:join)
end
def active_devices
settings.values["devices"] || []
end
def firmware_paths
active_devices
.map { |id| File.join Config.devices_path, id }
.select { |f| File.directory?(f) }
end
end
end