Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- '''
- Timeout /dev/input/jsX device when no input received
- This script is designed to be used with a udev rule that triggers a systemd service:
- /etc/udev/rules.d/10-jstimeout.rules
- ACTION=="add",ATTRS{name}=="Sony PLAYSTATION(R)3 Controller", ATTR{properties}=="0", TAG+="systemd", ENV{SYSTEMD_WANTS}="jstimeout@%E{UNIQ}.service"
- sudo udevadm control --reload-rules; sudo service udev restart; sudo systemctl restart systemd-udevd.service
- /etc/systemd/system/jstimeout@.service
- [Unit]
- Description=js Device Timeout
- [Service]
- Type=simple
- RemainAfterExit=no
- ExecStart=/usr/local/bin/jstimeout.py %I
- [Install]
- WantedBy=multi-user.target
- sudo systemctl enable jstimeout@
- '''
- import glob
- import os
- import struct
- import sys
- import select
- import time
- import logging
- from datetime import datetime as dt
- from tendo import singleton
- logging.basicConfig()
- # logging.getLogger().setLevel(logging.ERROR)
- logging.getLogger().setLevel(logging.DEBUG)
- try:
- max_idle_time = 10*60
- jsid = sys.argv[1]
- me = singleton.SingleInstance(flavor_id=jsid)
- except IndexError:
- logging.error("Missing argument: Bluetooth address")
- sys.exit(-1)
- except singleton.SingleInstanceException:
- sys.exit(-1)
- # Fetch the newest device for monitoring
- files = glob.glob("/dev/input/js*")
- if not files:
- logging.error("No js device input files found")
- sys.exit(0)
- files.sort(key=lambda x: os.path.getctime(x))
- dev_path = files[-1]
- logging.info("Monitoring " + dev_path)
- file = open(dev_path, "rb")
- Flag = True
- prevtime = dt.now()
- while True:
- currtime = dt.now()
- r, w, e = select.select([file], [], [], 0)
- if file in r:
- print("Activity on " + dev_path)
- Flag = True
- prevtime = currtime
- os.read(file.fileno(), 1024*1024)
- else:
- if not Flag:
- idle = (currtime-prevtime).total_seconds()
- print("Idle time: " + str(idle))
- if idle >= max_idle_time:
- time.sleep(1)
- os.system("echo disconnect "+jsid+"| bluetoothctl >/dev/null")
- break
- else:
- print("Idle on " + dev_path)
- Flag = False
- time.sleep(30)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement