-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.cr
More file actions
85 lines (67 loc) · 2.37 KB
/
manager.cr
File metadata and controls
85 lines (67 loc) · 2.37 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require "./mappings"
require "./publishing/*"
require "./router/*"
module PlaceOS::Source
class Manager
Log = ::Log.for(self)
getter control_system_router : Router::ControlSystem
getter driver_router : Router::Driver
getter module_router : Router::Module
getter zone_router : Router::Zone
getter status_events : StatusEvents
getter publisher_managers : Array(PublisherManager)
getter? started = false
class_property instance : self { new }
def initialize(
@publisher_managers : Array(PublisherManager) = [] of PublisherManager,
@mappings : Mappings = Mappings.new,
)
@control_system_router = Router::ControlSystem.new(mappings, publisher_managers)
@driver_router = Router::Driver.new(mappings, publisher_managers)
@module_router = Router::Module.new(mappings)
@zone_router = Router::Zone.new(mappings, publisher_managers)
@status_events = StatusEvents.new(mappings, publisher_managers)
end
def start
return if started?
@started = true
# Start publisher managers first to establish connections
Log.info { "registering Publishers" }
publisher_managers.each(&.start)
# Acquire the Zones (hierarchy) first
Log.info { "starting Zone router" }
zone_router.start
# Map ControlSystems beneath Zones
Log.info { "starting ControlSystem router" }
control_system_router.start
# Map to driver_ids and module_names
Log.info { "starting Module router" }
module_router.start
# Publish any relevant driver metadata
Log.info { "starting Driver router" }
driver_router.start
# Setup callback for new broker connections to trigger state resync
publisher_managers.each do |manager|
if manager.is_a?(MqttBrokerManager)
manager.on_broker_ready = ->(broker_id : String) {
Log.info { "triggering state resync for new Broker<#{broker_id}>" }
spawn { status_events.resync_state }
}
end
end
Log.info { "listening for Module state events" }
spawn { status_events.start }
Fiber.yield
end
def stop
return unless started?
@started = false
status_events.stop
publisher_managers.each &.stop
control_system_router.stop
driver_router.stop
module_router.stop
zone_router.stop
end
end
end