gps_watch_monitor.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #! /usr/bin/env python2
  2. # Copyright (C) 2014 mru@sisyphus.teil.cc
  3. # auto-start this in your gnome session.
  4. # calls `gps_watch_onconnect.sh` when the watch is connected.
  5. # `gps_watch_onconnect.sh` is searched:
  6. # in ~/.gps_watch_onconnect.sh
  7. # in $(prefix)/share/gps_watch_onconnect.sh
  8. # set up a gnome-terminal configuration "keep-open", that
  9. # doesn't close when the command finishes
  10. # monitors the udev messages to find out when the watch is connected.
  11. import subprocess
  12. import os
  13. import stat
  14. import sys
  15. import os.path
  16. import pyudev
  17. import pyudev.glib
  18. import gobject
  19. import glib
  20. import gtk
  21. import vte
  22. from pyudev.glib import GUDevMonitorObserver as MonitorObserver
  23. HOME=os.path.expanduser("~")
  24. class ShellWindow(gtk.Window):
  25. def __init__(self, command):
  26. gtk.Window.__init__(self)
  27. self.set_default_size(600, 300)
  28. box = gtk.VBox()
  29. label = gtk.Label("Importing tracks from watch")
  30. terminal = vte.Terminal()
  31. box.add(terminal)
  32. box.add(label)
  33. self.add(box)
  34. def on_exit(event):
  35. label.set_text("Finished")
  36. terminal.connect('child-exited', on_exit)
  37. terminal.fork_command(command = command[0], argv=command, directory=HOME)
  38. def device_added_callback(device, event, b):
  39. if event != "add":
  40. return
  41. if b['ID_MODEL'] != "CP2104_USB_to_UART_Bridge_Controller":
  42. return
  43. device_fn = b['DEVNAME']
  44. p1 = os.path.join(HOME, ".gps_watch_onconnect.sh")
  45. p2 = os.path.join("/usr/share/crane_gps_watch_client", "gps_watch_onconnect.sh")
  46. script = None
  47. if os.path.exists(p1):
  48. script = p1
  49. elif os.path.exists(p2):
  50. script = p2
  51. else:
  52. script = "gps_watch_onconnect.sh"
  53. win = ShellWindow([script, device_fn])
  54. win.show_all()
  55. context = pyudev.Context()
  56. monitor = pyudev.Monitor.from_netlink(context)
  57. monitor.filter_by(subsystem = 'tty')
  58. observer = MonitorObserver(monitor)
  59. observer.connect('device-event', device_added_callback)
  60. monitor.start();
  61. mainloop = gobject.MainLoop();
  62. mainloop.run()