forked from aleskandro/nextcloud-kobo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontoller.go
153 lines (142 loc) · 3.81 KB
/
contoller.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package pkg
import (
"context"
"fmt"
"log"
"sync"
"time"
"github.com/godbus/dbus/v5"
)
type NetworkConnectionReconciler struct {
conn *dbus.Conn
dbusChan chan *dbus.Signal
config *Config
toastsChan chan string
wg *sync.WaitGroup
syncCtx context.Context
syncCtxCancel context.CancelFunc
}
var networkConnectionFailedErr = fmt.Errorf("network connection failed")
func NewNetworkConnectionReconciler(config *Config, ctx context.Context) *NetworkConnectionReconciler {
conn, err := dbus.SystemBus()
if err != nil {
log.Fatalf("Failed to connect to system bus: %v", err)
}
ch := make(chan *dbus.Signal, 10)
call := conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0,
"type='signal',interface='com.github.shermp.nickeldbus',member='wmNetworkConnected',path='/nickeldbus'")
if call.Err != nil {
log.Fatalf("Failed to add D-Bus match: %v", call.Err)
}
conn.Signal(ch)
n := &NetworkConnectionReconciler{
conn: conn,
dbusChan: ch,
config: config,
toastsChan: make(chan string, 16),
wg: &sync.WaitGroup{},
}
go n.dispatchMessages(ctx)
return n
}
func (n *NetworkConnectionReconciler) Run(ctx context.Context) {
defer func() {
fmt.Println("Exiting network connection reconciler")
n.conn.RemoveSignal(n.dbusChan)
close(n.dbusChan)
//nolint:errcheck
n.conn.Close()
}()
for {
fmt.Println("Listening for network connection signals from Nickel...")
select {
case <-ctx.Done():
fmt.Println("Context done")
return
case signal, ok := <-n.dbusChan:
if !ok {
log.Println("Signal channel closed")
return
}
if signal == nil {
log.Println("Received nil signal")
continue
}
fmt.Printf("Received signal: %s\n", signal.Name)
// Check if the signal is the one we are interested in
if signal.Name != "com.github.shermp.nickeldbus.wmNetworkConnected" {
log.Println("Received unexpected signal", signal.Name)
continue
}
n.HandleWmNetworkConnected(ctx)
}
}
}
func (n *NetworkConnectionReconciler) HandleWmNetworkConnected(ctx context.Context) {
if n.syncCtxCancel != nil {
n.syncCtxCancel()
}
n.wg.Wait()
n.syncCtx, n.syncCtxCancel = context.WithCancel(ctx)
n.wg.Add(2)
go func() {
defer n.wg.Done()
n.keepNetworkAlive(n.syncCtx)
}()
go func() {
defer n.wg.Done()
defer func() {
if n.syncCtx.Err() == nil {
n.syncCtxCancel()
n.syncCtxCancel = nil
}
}()
n.sync(n.syncCtx)
if n.config.AutoUpdate {
n.updateNow()
}
}()
}
func (n *NetworkConnectionReconciler) keepNetworkAlive(ctx context.Context) {
ticker := time.NewTicker(time.Second * 30)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
log.Println("[keepNetworkAlive] context closed")
return
case <-ticker.C:
obj := n.conn.Object("com.github.shermp.nickeldbus", "/nickeldbus")
call := obj.Call("com.github.shermp.nickeldbus.wfmConnectWirelessSilently", 0)
if call.Err != nil {
log.Println("Failed to notify Nickel", call.Err)
}
}
}
}
func (n *NetworkConnectionReconciler) dispatchMessages(ctx context.Context) {
for {
select {
case <-ctx.Done():
log.Println("[dispatchMessages] context closed")
return
case message := <-n.toastsChan:
n.notifyNickel(message)
}
}
}
func (n *NetworkConnectionReconciler) rescanBooks() {
obj := n.conn.Object("com.github.shermp.nickeldbus", "/nickeldbus")
call := obj.Call("com.github.shermp.nickeldbus.pfmRescanBooks", 0)
if call.Err != nil {
log.Println("Failed to rescan books", call.Err)
}
}
func (n *NetworkConnectionReconciler) notifyNickel(message string) {
obj := n.conn.Object("com.github.shermp.nickeldbus", "/nickeldbus")
call := obj.Call("com.github.shermp.nickeldbus.mwcToast", 0, 5000, "NextCloud Kobo Syncer", message)
if call.Err != nil {
log.Println("Failed to notify Nickel", call.Err)
}
time.Sleep(time.Second * 5)
}