I thought it would be great to be notified when my network connectivity dies or is revived, so I put this in my Hammerspoon init.lua:
ping = nil
previousStatus = nil
function pingCallback(server, eventType, ...)
hs.alert.show(eventType)
if eventType == "receivedPacket"
then
newStatus = "success"
else
if eventType == "didFail" or eventType == "sendPacketFailed"
then
newStatus = "failure"
end
end
if not (newStatus == previousStatus)
then
hs.alert.show(string.format("Network status changed to %s", newStatus))
previousStatus = newStatus
end
end
while(true)
do
ping = hs.network.ping.ping("google.com", 5, 1.0, 2.0, "any", pingCallback)
os.execute("sleep 15")
end
The problem is the sleep. It sleeps Hammerspoon itself, making it hang. What I really need is a thread or timer, or maybe to start a different OS process. What should I do?