-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
74 lines (59 loc) · 1.66 KB
/
Copy pathmain.go
File metadata and controls
74 lines (59 loc) · 1.66 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
package main
import (
"context"
"time"
"devicecode-go/bus"
"devicecode-go/services/hal"
"devicecode-go/services/reactor"
"devicecode-go/types"
"devicecode-go/utilities"
)
// HAL
const halTimeout = 5 * time.Second
var halReadiness = bus.T("hal", "state")
// -----------------------------------------------------------------------------
// Main
// -----------------------------------------------------------------------------
func main() {
// Allow early USB/console settle if needed
time.Sleep(3 * time.Second)
log.SetStart(time.Now())
ctx := context.Background()
log.Println("[main] bootstrapping bus …")
b := bus.NewBus(3, "+", "#")
halConn := b.NewConnection("hal")
uiConn := b.NewConnection("ui")
log.Println("[main] starting hal.Run …")
go hal.Run(ctx, halConn)
// Wait for retained hal/state=ready (or time out)
if !waitHALReady(ctx, halConn, halTimeout) {
for {
log.Println("[main] HAL not ready within timeout")
time.Sleep(2 * time.Second)
}
}
// Reactor
r := reactor.NewReactor(uiConn)
r.Run(ctx)
}
// -----------------------------------------------------------------------------
// HAL readiness helper
// -----------------------------------------------------------------------------
func waitHALReady(ctx context.Context, c *bus.Connection, d time.Duration) bool {
sub := c.Subscribe(halReadiness)
defer c.Unsubscribe(sub)
ctx2, cancel := context.WithTimeout(ctx, d)
defer cancel()
for {
select {
case m := <-sub.Channel():
if st, ok := m.Payload.(types.HALState); ok && st.Level == "ready" {
return true
}
case <-ctx2.Done():
return false
}
}
}
// Global logger instance
var log = utilities.Logger{LineStart: true}