printer-raspberry-pi/prusaconnect

48 lines
1.5 KiB
Text
Raw Normal View History

2024-02-27 19:00:24 +01:00
#!/bin/bash
# Set default values for environment variables
2024-02-27 19:12:19 +01:00
: "${PRUSA_CONNECT_URL:=https://webcam.connect.prusa3d.com/c/snapshot}"
: "${WEBCAM_SNAPSHOT_URL:=http://127.0.0.1:8080/snapshot}"
: "${PRUSA_LINK_STATUS_URL:=http://192.168.0.130/api/v1/status}"
2024-02-27 19:00:24 +01:00
: "${DELAY_SECONDS:=10}"
: "${LONG_DELAY_SECONDS:=60}"
: "${IMG_PATH:=$HOME/output.jpg}"
2024-02-27 19:12:19 +01:00
# secrets
: "${PRUSA_CONNECT_CAMERA_FINGERPRINT:=FINGERPRINT}"
: "${PRUSA_CONNECT_CAMERA_TOKEN:=TOKEN}"
: "${PRUSA_LINK_KEY:=KEY}"
2024-02-27 19:31:17 +01:00
source ./secrets
2024-02-27 19:00:24 +01:00
while true; do
2024-02-27 19:12:19 +01:00
state=$(curl -m 5 -sSL $PRUSA_LINK_STATUS_URL -H "X-Api-Key: $PRUSA_LINK_KEY" | jq -r ".printer.state")
2024-02-27 19:00:24 +01:00
if [ $state == "PRINTING" ]; then
2024-02-27 19:12:19 +01:00
curl -k -sSL $WEBCAM_SNAPSHOT_URL -o $IMG_PATH
2024-02-27 19:00:24 +01:00
# If no error, upload it.
if [ $? -eq 0 ]; then
# POST the image to the HTTP URL using curl
2024-02-27 19:12:19 +01:00
curl -X PUT "$PRUSA_CONNECT_URL" \
2024-02-27 19:00:24 +01:00
-H "accept: */*" \
-H "content-type: image/jpg" \
2024-02-27 19:12:19 +01:00
-H "fingerprint: $PRUSA_CONNECT_CAMERA_FINGERPRINT" \
-H "token: $PRUSA_CONNECT_CAMERA_TOKEN" \
2024-02-27 19:00:24 +01:00
--data-binary "@$IMG_PATH" \
--no-progress-meter \
--compressed
# Reset delay to the normal value
DELAY=$DELAY_SECONDS
else
2024-02-27 19:31:17 +01:00
echo "Couldn't capture snapshot. Retrying after ${LONG_DELAY_SECONDS}s..."
2024-02-27 19:00:24 +01:00
# Set delay to the longer value
DELAY=$LONG_DELAY_SECONDS
fi
else
echo "printer not printing. retrying after ${LONG_DELAY_SECONDS}s..."
DELAY=$LONG_DELAY_SECONDS
fi
sleep "$DELAY"
done