-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhardware.lua
More file actions
228 lines (179 loc) · 5.4 KB
/
hardware.lua
File metadata and controls
228 lines (179 loc) · 5.4 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
require 'utils'
hardware = {}
hardware._index = hardware
hardwareWatchers = {
battery = nil,
volumes = nil,
usb = nil,
wifi = nil,
network = nil
}
local log = hs.logger.new("hardware")
local currentWifi = hs.wifi.currentNetwork()
---------------
-- Helpers --
---------------
local function basename(str)
local name = string.gsub(str, "(.*/)(.*)", "%2")
return name
end
local function timeRemainingString(minutes)
local hours = math.floor(minutes/60)
local rest = minutes % 60
if hours == 0 then
return string.format("%0.f minutes", rest)
else
return string.format("%d:%.2d hours", hours, rest)
end
end
-----------------------------
-- Battery Notifications --
-----------------------------
local batteryState = nil
local function initBattery()
batteryState = hs.battery.getAll()
end
local function notifyBattery(showOnScreen)
local chargeInfo
if batteryState.isCharging then
chargeInfo = "Charging"
elseif batteryState.isCharged == true then
chargeInfo = "Charged"
else
chargeInfo = ""
end
local subtitle = string.format("Battery: %s at %0.f%%", chargeInfo, batteryState.percentage)
local additional
if batteryState.timeRemaining > 0 then
additional = string.format("Time remaining: %s", timeRemainingString(batteryState.timeRemaining))
end
if not showOnScreen then
-- display system notification
utils.notify("On " .. batteryState.powerSource, subtitle, additional)
else
-- display on screen notification
local statusText = string.format("On %s\n%s", batteryState.powerSource, subtitle)
if additional then
statusText = string.format("%s\n%s", statusText, additional)
end
hs.alert(statusText, 4)
end
end
local onBatteryChange = function()
local oldState = batteryState
batteryState = hs.battery.getAll()
log.d(hs.inspect(batteryState))
if oldState.powerSource ~= batteryState.powerSource then
-- battery <> AC change
notifyBattery()
elseif oldState.isCharged == false and batteryState.isCharged then
-- battery fully charged
notifyBattery()
elseif oldState.timeRemaining < 0 and batteryState.timeRemaining > 0 then
-- battery remaining time calculation finished
notifyBattery()
end
end
----------------------------
-- Volume Notifications --
----------------------------
local onVolumeChange = function(event, info)
if event == hs.fs.volume.didMount then
-- register callback for "Click to open"
hs.notify.register(info.path, function() os.execute("open " .. info.path) end)
utils.notify(basename(info.path) .. " Mounted", "Click to open", nil, info.path)
elseif event == hs.fs.volume.didUnmount then
-- remove callback for this path
hs.notify.unregister(info.path)
utils.notify(basename(info.path) .. " Unmounted")
end
end
-------------------------
-- USB Notifications --
-------------------------
local onUSBChange = function(info)
if info.productName and info.productName ~= "" then
local event = (info.eventType == "added") and "USB Connection" or "USB Disconnection"
utils.notify(event, string.format("%s / %s", info.productName, info.vendorName))
end
end
--------------------------
-- WiFi Notifications --
--------------------------
local onWifiChange = function()
if not hs.wifi.currentNetwork() then
-- no wifi connection found
if currentWifi then
log.d("wifi disconnect: " .. currentWifi)
utils.notify("WiFi Disconnection", currentWifi)
currentWifi = nil
end
else
local info = hs.wifi.interfaceDetails()
currentWifi = string.format("%s (%s)", hs.wifi.currentNetwork(), info.bssid)
log.d("wifi connect: " .. currentWifi)
utils.notify("WiFi Connection", currentWifi, info.security)
end
end
-----------------------------
-- Network Notifications --
-----------------------------
local KEY_IPV4 = "State:/Network/Interface/en1/IPv4"
local onIPChange = function(configuration, keys)
local ipv4Table = configuration:contents(KEY_IPV4)
if next(ipv4Table) == nil then
utils.notify("IP address released")
else
local ipv4 = ipv4Table[KEY_IPV4]
local ip = ipv4.Addresses[1]
utils.notify("IP address acquired", ip)
end
end
-------------------------
-- Lifecycle Methods --
-------------------------
local function startAll()
for w,_ in pairs(hardwareWatchers) do
if hardwareWatchers[w] then
log.i("Starting watcher for " .. w)
hardwareWatchers[w]:start()
end
end
end
local function stopAll()
for w,_ in pairs(hardwareWatchers) do
if hardwareWatchers[w] then
log.i("Stopping watcher for " .. w)
hardwareWatchers[w]:stop()
end
end
end
function hardware.start()
local cfg = cfg.notifications
if cfg.enableBattery then
initBattery()
hardwareWatchers.battery = hs.battery.watcher.new(onBatteryChange)
end
if cfg.enableVolumes then
hardwareWatchers.volumes = hs.fs.volume.new(onVolumeChange)
end
if cfg.enableUsb then
hardwareWatchers.usb = hs.usb.watcher.new(onUSBChange)
end
if cfg.enableWifi then
hardwareWatchers.wifi = hs.wifi.watcher.new(onWifiChange)
end
if cfg.enableNetwork then
hardwareWatchers.network = hs.network.configuration.open()
hardwareWatchers.network:setCallback(onIPChange)
hardwareWatchers.network:monitorKeys(KEY_IPV4)
end
startAll()
end
function hardware.stop()
stopAll()
end
function hardware.showBatteryStatus(showOnScreen)
notifyBattery(showOnScreen)
end
return hardware