| Crates.io | patlite-beacon-serv |
| lib.rs | patlite-beacon-serv |
| version | 0.1.0 |
| created_at | 2025-09-12 05:00:16.958507+00 |
| updated_at | 2025-09-12 05:00:16.958507+00 |
| description | RESTful API server for controlling PATLITE USB beacons with comprehensive light patterns, sequences, and buzzer control |
| homepage | https://github.com/benburkhart1/patlite-beacon-serv |
| repository | https://github.com/benburkhart1/patlite-beacon-serv |
| max_upload_size | |
| id | 1835183 |
| size | 102,798 |
A RESTful API server for controlling PATLITE USB beacons using the rust-patlite-beacon crate.
git clone https://github.com/yourusername/patlite-beacon-serv.git
cd patlite-beacon-serv
cargo build --release
The compiled binary will be in target/release/patlite-beacon-serv
Run the server with default settings (localhost:38861):
cargo run
# or
./target/release/patlite-beacon-serv
patlite-beacon-serv [OPTIONS]
Options:
--host <HOST> Host address to bind to [default: 127.0.0.1]
--port <PORT> Port to listen on [default: 38861]
--api-key <API_KEY> API key for authentication (optional)
-h, --help Print help
-V, --version Print version
Run on all interfaces with custom port:
./patlite-beacon-serv --host 0.0.0.0 --port 8080
Run with API key authentication:
./patlite-beacon-serv --api-key my-secret-key
FROM rust:1.70 as builder
WORKDIR /app
COPY . .
RUN cargo build --release
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y libusb-1.0-0 && rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/target/release/patlite-beacon-serv /usr/local/bin/
EXPOSE 38861
CMD ["patlite-beacon-serv", "--host", "0.0.0.0"]
cargo run
# Turn on red light
curl -X PUT http://localhost:38861/light \
-H "Content-Type: application/json" \
-d '{"color": "red", "mode": "on"}'
# Play a melody
curl -X PUT http://localhost:38861/buzzer \
-H "Content-Type: application/json" \
-d '{"pattern": "london_bridge", "volume": 60}'
# Clear everything
curl -X POST http://localhost:38861/clear
http://localhost:38861
All endpoints support optional API key authentication via the ?apiKey=<key> query parameter if the server was started with an API key.
Example:
curl http://localhost:38861/status?apiKey=my-secret-key
/ or GET /statusReturns the current status of the light, sequence, and buzzer.
Response:
{
"light": {
"color": "red",
"mode": "on",
"active": true,
"remaining_ms": 5000
},
"sequence": {
"commands": [
{"color": "red", "mode": "on", "duration_ms": 3000},
{"color": "blue", "mode": "flash", "duration_ms": 3000}
],
"current_index": 0,
"loop_sequence": true,
"active": true
},
"buzzer": {
"active": false,
"pattern": null,
"volume": 50
},
"connected": true,
"last_updated": "2025-01-10T12:00:00Z"
}
/lightSet the light to a specific color and mode with optional duration.
Request Body:
{
"color": "red", // One of: red, yellow, amber, green, blue, white, clear
"mode": "pattern3", // One of: off, on, solid, pattern1-6, blink, flash
"duration_ms": 5000 // Optional, in milliseconds
}
Available Modes:
off - Light is offon or solid - Light is continuously onpattern1 or blink - Pattern 1 (slow blink)pattern2 or flash - Pattern 2 (fast flash)pattern3 - Pattern 3pattern4 - Pattern 4pattern5 - Pattern 5pattern6 - Pattern 6Response: Current beacon status
Example:
curl -X PUT http://localhost:38861/light \
-H "Content-Type: application/json" \
-d '{"color": "red", "mode": "flash", "duration_ms": 10000}'
/lightTurn off the current light.
Response: Current beacon status
Example:
curl -X DELETE http://localhost:38861/light
/sequenceStart a sequence of light commands that execute one after another.
Request Body:
{
"commands": [
{
"color": "red",
"mode": "on",
"duration_ms": 3000
},
{
"color": "blue",
"mode": "flash",
"duration_ms": 3000
},
{
"color": "green",
"mode": "blink",
"duration_ms": 2000
}
],
"loop_sequence": true // Optional, defaults to true. If false, sequence runs once
}
Response: Current beacon status
Example (looping sequence):
curl -X POST http://localhost:38861/sequence \
-H "Content-Type: application/json" \
-d '{
"commands": [
{"color": "red", "mode": "on", "duration_ms": 3000},
{"color": "blue", "mode": "flash", "duration_ms": 3000}
],
"loop_sequence": true
}'
Example (one-shot sequence):
curl -X POST http://localhost:38861/sequence \
-H "Content-Type: application/json" \
-d '{
"commands": [
{"color": "red", "mode": "on", "duration_ms": 1000},
{"color": "yellow", "mode": "on", "duration_ms": 1000},
{"color": "green", "mode": "on", "duration_ms": 1000}
],
"loop_sequence": false
}'
/sequenceStop the current running sequence.
Response: Current beacon status
Example:
curl -X DELETE http://localhost:38861/sequence
/updateTrigger an update of the sequence state (advances to next command if current duration has elapsed).
Response: Current beacon status
Example:
curl -X POST http://localhost:38861/update
/buzzerActivate the buzzer with a specific pattern.
Request Body:
{
"pattern": "strong_attention", // See available patterns below
"volume": 75, // Optional, 0-100
"duration_ms": 5000 // Optional, in milliseconds
}
Available Patterns:
off - No soundon or continuous - Continuous tonesweep - Sweeping sound patternintermittent - Intermittent beepingweak_attention or weak or chirp - Weak attention sound (gentle chirp)strong_attention or strong or alarm - Strong attention sound (alarm)shining_star - "Shining Star" melodylondon_bridge - "London Bridge" melodyResponse: Current beacon status
Example:
curl -X PUT http://localhost:38861/buzzer \
-H "Content-Type: application/json" \
-d '{"pattern": "strong_attention", "volume": 80, "duration_ms": 10000}'
Example with melody:
curl -X PUT http://localhost:38861/buzzer \
-H "Content-Type: application/json" \
-d '{"pattern": "london_bridge", "volume": 60}'
/buzzerStop the buzzer.
Response: Current beacon status
Example:
curl -X DELETE http://localhost:38861/buzzer
/buzzer/patternPlay a custom buzzer pattern with specific frequencies.
Request Body:
{
"pattern": [
{
"frequency_hz": 1000,
"duration_ms": 200,
"pause_ms": 100
},
{
"frequency_hz": 1500,
"duration_ms": 200,
"pause_ms": 100
}
],
"repetitions": 3,
"volume": 60 // Optional, 0-100
}
Response: Current beacon status
Example:
curl -X POST http://localhost:38861/buzzer/pattern \
-H "Content-Type: application/json" \
-d '{
"pattern": [
{"frequency_hz": 800, "duration_ms": 250, "pause_ms": 150},
{"frequency_hz": 1200, "duration_ms": 250, "pause_ms": 150}
],
"repetitions": 2,
"volume": 70
}'
/clearTurn off the light, stop any running sequence, and stop the buzzer.
Response: Current beacon status
Example:
curl -X POST http://localhost:38861/clear
/testRun a test sequence that cycles through all light colors.
Response:
{
"success": true,
"results": [
"Test sequence started",
"Step 1 executed",
"Step 2 executed",
"Step 3 executed",
"Step 4 executed",
"Step 5 executed"
]
}
Example:
curl -X POST http://localhost:38861/test
When the server is started with the --api-key parameter, all API endpoints require authentication:
# Start server with API key
./patlite-beacon-serv --api-key my-secret-key
# Make authenticated request
curl http://localhost:38861/status?apiKey=my-secret-key
Without the correct API key, requests will return 401 Unauthorized.
The server automatically monitors the beacon's touch sensor. When pressed:
This provides a hardware "panic button" for instant shutdown of all beacon activity.
Create complex light shows with sequences:
# Traffic light sequence (loops forever)
curl -X POST http://localhost:38861/sequence \
-H "Content-Type: application/json" \
-d '{
"commands": [
{"color": "red", "mode": "on", "duration_ms": 3000},
{"color": "yellow", "mode": "on", "duration_ms": 1000},
{"color": "green", "mode": "on", "duration_ms": 3000}
],
"loop_sequence": true
}'
# Alert sequence (runs once)
curl -X POST http://localhost:38861/sequence \
-H "Content-Type: application/json" \
-d '{
"commands": [
{"color": "red", "mode": "pattern3", "duration_ms": 500},
{"color": "blue", "mode": "pattern3", "duration_ms": 500}
],
"loop_sequence": false
}'
Both lights and buzzers support automatic timeouts:
# Red light for exactly 5 seconds
curl -X PUT http://localhost:38861/light \
-H "Content-Type: application/json" \
-d '{"color": "red", "mode": "on", "duration_ms": 5000}'
# Alarm for 10 seconds
curl -X PUT http://localhost:38861/buzzer \
-H "Content-Type: application/json" \
-d '{"pattern": "strong_attention", "volume": 80, "duration_ms": 10000}'
After the specified duration, the light/buzzer automatically turns off without affecting other active components.
off: Light is turned offon: Light is continuously onflash: Light flashes rapidlyblink: Light blinks slowlyredyellow or ambergreenbluewhite or clearoff - No soundon or continuous - Continuous tonesweep - Sweeping sound patternintermittent - Intermittent beepingweak_attention (aliases: weak, chirp) - Gentle attention soundstrong_attention (aliases: strong, alarm) - Strong alarm soundshining_star - Plays "Shining Star" melodylondon_bridge - Plays "London Bridge" melodyThe API returns appropriate HTTP status codes:
200 OK: Successful operation400 Bad Request: Invalid parameters or request body401 Unauthorized: Invalid or missing API key (when authentication is enabled)500 Internal Server Error: Server or beacon communication error"Device not found" error
sudo usermod -a -G dialout $USER"Resource busy" error
ps aux | grep patliteTouch sensor not working
API returns 401 Unauthorized
git clone https://github.com/yourusername/patlite-beacon-serv.git
cd patlite-beacon-serv
cargo build --release
cargo test
src/
├── main.rs # Server initialization and route handlers
├── beacon.rs # Beacon controller logic
├── api.rs # API types and serialization
└── auth.rs # Authentication middleware
cargo testMIT License
Copyright (c) 2025 Benjamin Burkhart
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.