Guest User

Untitled

a guest
Dec 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. require 'open3'
  2.  
  3. def log s
  4. p Time.now.to_s + " #{s}"
  5. end
  6.  
  7. def is_device_there?(mac_addr)
  8. exit_status = -1
  9. Open3.popen3("sudo l2ping -c1 -v #{mac_addr}") do |stdin, stdout, stderr, wait_thr|
  10. exit_status = wait_thr.value
  11. end
  12.  
  13. exit_status.to_i == 0
  14. end
  15.  
  16.  
  17. def hello_handler
  18. log "Hello!"
  19. end
  20.  
  21. def goodbye_handler
  22. log "Goodbye!"
  23. end
  24.  
  25.  
  26. def poll_device_status(mac_addr)
  27. device_was_there = false
  28.  
  29. loop do
  30. device_is_there = is_device_there? mac_addr
  31.  
  32. hello_handler if device_is_there and not device_was_there
  33. goodbye_handler if not device_is_there and device_was_there
  34.  
  35. device_was_there = device_is_there
  36. sleep 30 if device_is_there
  37. end
  38. end
  39.  
  40. log "Started."
  41. poll_device_status "14:5A:05:5C:A8:B4"
Add Comment
Please, Sign In to add comment