This sample provides a practical way to manage USB devices like webcams using a backend server built with FastAPI. The system handles events for connecting and disconnecting devices, while the frontend includes a toggle button to control the webcam feed. For more information about device types in Polyscope X, please see our documentation.
Note
This sample currently only works on a physical controller. It will fail on the simulator.
- Device Add Endpoint (
/device_add):- Processes information when a USB device is connected.
- Decides whether to accept or reject the device.
- Logs key details about the device and the action taken.
- Device Remove Endpoint (
/device_remove):- Handles events when a USB device is disconnected.
- Logs the removal for future reference.
- Webcam Demo:
- The frontend utilizes the
ngx-webcamlibrary. It runs a modified version of the ngx-webcam-demo with buttons to toggle the webcam and take a snapshot
- The frontend utilizes the
-
Device Add Process:
- When a USB device is plugged in,
on_device_add.pyruns automatically. - This script parses the device details and sends them to the
/device_addendpoint. - The backend validates the details, checks the device type, and logs whether the device is accepted or rejected.
- When a USB device is plugged in,
-
Device Remove Process:
- When a USB device is unplugged,
on_device_remove.pyruns automatically. - This script sends the device information to the
/device_removeendpoint. - The backend logs the removal for records or debugging.
- When a USB device is unplugged,
-
Frontend Integration:
- A toggle button lets users enable or disable the webcam feed.
- Users can also take a snapshot which is displayed next to the active webcam feed.
- Purpose: Handles new device connections.
- Request Body (sample):
{ "idProduct": "2137", "idVendor": "0bda", "logicalDevices": [ {"deviceNode": "/dev/video0", "major": 81, "minor": 0} ], "manufacturer": "HFGK-N", "product": "USB Camera", "serial": "200901010001", "urDeviceType": "VIDEO", "urDeviceAPIVersion": "0.1" } - Response:
200 OKwith anexit_codefield (0 = accepted, 1 = rejected).
- Purpose: Logs device disconnections.
- Request Body (sample):
{ "idProduct": "2137", "idVendor": "0bda", "logicalDevices": [ {"deviceNode": "/dev/video0", "major": 81, "minor": 0} ], "manufacturer": "HFGK-N", "product": "USB Camera", "serial": "200901010001", "urDeviceType": "VIDEO", "urDeviceAPIVersion": "0.1" } - Response:
200 OK(exit codes are not relevant here).
Each component uses logging to keep track of events and actions:
main.py: Logs device decisions and frontend requests.on_device_add.py: Logs initialization and device addition processing [currently not used]on_device_remove.py: Logs initialization and device removal events. [currently not used]
2024-12-12 15:01:36,728 - main - DEBUG - Validated payload: idProduct='2137' idVendor='0bda' logicalDevices=[{'deviceNode': '/dev/video1', 'major': 81, 'minor': 1}, {'deviceNode': '/dev/video0', 'major': 81, 'minor': 0}] manufacturer='HFGK-N' product='USB Camera' serial='200901010001' urDeviceType='VIDEO' urDeviceAPIVersion='0.1'
2024-12-12 15:01:36,728 - main - INFO - Device was removed.
To install the contribution type:
$ npm install
To build the contribution type:
$ npm run build
To deploy the contribution to the simulator type:
$ npm run install-urcap
Get more help from the included SDK documentation.
This project is released under the MIT License. See the LICENSE file for details.