代码语言:javascript复制
# path: /etc/udev/rules.d
# file: devices.rules
SUBSYSTEM=="video*", ATTR{index}=="0", KERNELS=="1-1.1:1.0", MODE="0666", SYMLINK ="camera0", OPTIONS ="last_rule"
SUBSYSTEM=="video*", ATTR{index}=="0", KERNELS=="1-1.2:1.0", MODE="0666", SYMLINK ="camera1", OPTIONS ="last_rule"
SUBSYSTEM=="video*", ATTR{index}=="0", KERNELS=="1-1.3:1.0", MODE="0666", SYMLINK ="camera2", OPTIONS ="last_rule"
SUBSYSTEM=="video*", ATTR{index}=="0", KERNELS=="1-1.4:1.0", MODE="0666", SYMLINK ="camera3", OPTIONS ="last_rule"
以USB摄像头为例:
代码语言:javascript复制import pyudev
def observe_camera(self):
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='video4linux') # Remove this line to listen for all devices.
monitor.start()
cmd = """
for dev in `find /dev -iname 'video*' -printf "%fn"`
do
v4l2-ctl --list-formats --device /dev/$dev |
grep -qE '[[0-9]]' &&
echo $dev
done
"""
for device in iter(monitor.poll, None):
# I can add more logic here, to run only certain kinds of devices are plugged.
_path = device.device_path
action = device.action # add remove
camera = list(device.device_links)[0]
if not camera.startswith('/dev/camera'):
continue
if '1-1.1:1.0' in _path:
location = 'usb—3.0_上'
elif '1-1.2:1.0' in _path:
location = 'usb-3.0_下'
elif '1-1.3:1.0' in _path:
location = 'usb-2.0_上'
elif '1-1.4:1.0' in _path:
location = 'usb-2.0_下'
else:
continue
print(camera)