aboutsummaryrefslogtreecommitdiff
path: root/internal/monitor.go
blob: 99478abb04f28cca3689320876c54007d6c8a573 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package internal

import (
	"github.com/godbus/dbus/v5"
	"log"
	"sort"
	"strings"
	"time"
)

type Monitor struct {
	scanner       *Scanner
	keyboard      *KeyboardInfo
	callback      func(*KeyboardInfo)
	ticker        *time.Ticker
	stopCh        chan struct{}
	notifications *NotificationManager
	config        *Config
	lastConnected bool
}

func NewMonitor(scanner *Scanner, config *Config) (*Monitor, error) {
	notifications, err := NewNotificationManager()
	if err != nil {
		log.Printf("Warning: Could not initialize notifications: %v", err)
		notifications = nil
	}

	return &Monitor{
		scanner:       scanner,
		stopCh:        make(chan struct{}),
		keyboard:      &KeyboardInfo{LeftBattery: -1, RightBattery: -1},
		notifications: notifications,
		config:        config,
		lastConnected: false,
	}, nil
}

func (m *Monitor) SetDevice(name, address string) {
	m.keyboard.Name = name
	m.keyboard.Address = address
}

func (m *Monitor) SetCallback(cb func(*KeyboardInfo)) {
	m.callback = cb
}

func (m *Monitor) UpdateConfig(config *Config) {
	m.config = config
}

func (m *Monitor) Start(interval time.Duration) {
	m.ticker = time.NewTicker(interval)
	go m.run()
}

func (m *Monitor) Stop() {
	if m.ticker != nil {
		m.ticker.Stop()
	}
	if m.notifications != nil {
		m.notifications.Close()
	}
	close(m.stopCh)
}

func (m *Monitor) UpdateInterval(interval time.Duration) {
	if m.ticker != nil {
		m.ticker.Reset(interval)
	}
}

func (m *Monitor) run() {
	m.update() // Initial update
	for {
		select {
		case <-m.ticker.C:
			m.update()
		case <-m.stopCh:
			return
		}
	}
}

func (m *Monitor) update() {
	_, path, err := m.scanner.FindDevice(m.keyboard.Address)
	if err != nil || path == "" {
		m.keyboard.Connected = false
		m.handleConnectionChange()
		m.notify()
		return
	}

	m.keyboard.Connected = m.scanner.IsConnected(path)
	m.handleConnectionChange()

	if !m.keyboard.Connected {
		m.notify()
		return
	}

	prevLeft, prevRight := m.keyboard.LeftBattery, m.keyboard.RightBattery
	m.getBatteryLevels(path)

	if m.config.ShowNotifications && m.notifications != nil {
		m.checkLowBattery(prevLeft, prevRight)
	}

	m.notify()
}

func (m *Monitor) handleConnectionChange() {
	if m.config.ShowNotifications && m.notifications != nil && m.lastConnected != m.keyboard.Connected {
		m.notifications.ShowConnectionNotification(m.keyboard.Name, m.keyboard.Connected)
	}
	m.lastConnected = m.keyboard.Connected
}

func (m *Monitor) checkLowBattery(prevLeft, prevRight int) {
	threshold := m.config.LowBatteryThreshold

	// Only notify when battery drops to/below threshold, not on every update
	if m.keyboard.LeftBattery >= 0 && m.keyboard.LeftBattery <= threshold {
		if prevLeft < 0 || prevLeft > threshold {
			m.notifications.ShowLowBatteryNotification("Left", m.keyboard.LeftBattery, threshold)
		}
	}

	if m.keyboard.RightBattery >= 0 && m.keyboard.RightBattery <= threshold {
		if prevRight < 0 || prevRight > threshold {
			m.notifications.ShowLowBatteryNotification("Right", m.keyboard.RightBattery, threshold)
		}
	}
}

func (m *Monitor) getBatteryLevels(devicePath dbus.ObjectPath) {
	m.keyboard.LeftBattery, m.keyboard.RightBattery = -1, -1

	objects, err := m.scanner.GetManagedObjects()
	if err != nil {
		return
	}

	var services []dbus.ObjectPath
	prefix := string(devicePath) + "/"

	for path, ifaces := range objects {
		if !strings.HasPrefix(string(path), prefix) {
			continue
		}
		if serviceProps, ok := ifaces["org.bluez.GattService1"]; ok {
			if uuid, ok := serviceProps["UUID"].Value().(string); ok && strings.ToLower(uuid) == "0000180f-0000-1000-8000-00805f9b34fb" {
				services = append(services, path)
			}
		}
	}

	sort.Slice(services, func(i, j int) bool { return services[i] < services[j] })

	for i, service := range services {
		if level := m.readBatteryLevel(service, objects); level >= 0 {
			if i == 0 {
				m.keyboard.LeftBattery = level
			} else if i == 1 {
				m.keyboard.RightBattery = level
			}
		}
	}

	// Fallback to legacy method
	if len(services) == 1 && m.keyboard.RightBattery == -1 {
		if level, err := m.scanner.GetLegacyBattery(devicePath); err == nil {
			m.keyboard.LeftBattery = level
		}
	}

	if m.keyboard.LeftBattery >= 0 || m.keyboard.RightBattery >= 0 {
		m.keyboard.LastUpdate = time.Now()
	}
}

func (m *Monitor) readBatteryLevel(servicePath dbus.ObjectPath, objects map[dbus.ObjectPath]map[string]map[string]dbus.Variant) int {
	prefix := string(servicePath) + "/"
	for path, ifaces := range objects {
		if !strings.HasPrefix(string(path), prefix) {
			continue
		}
		if charProps, ok := ifaces["org.bluez.GattCharacteristic1"]; ok {
			if uuid, ok := charProps["UUID"].Value().(string); ok && strings.ToLower(uuid) == "00002a19-0000-1000-8000-00805f9b34fb" {
				if data, err := m.scanner.ReadCharacteristic(path); err == nil && len(data) > 0 {
					level := int(data[0])
					if level == 255 {
						return -1
					}
					return level
				}
			}
		}
	}
	return -1
}

func (m *Monitor) notify() {
	if m.callback != nil {
		m.callback(m.keyboard)
	}
}

func (m *Monitor) GetKeyboard() *KeyboardInfo {
	return m.keyboard
}