Created
December 27, 2024 16:10
-
-
Save chrisns/61c34ee1f4b4d5637c156bea8950f604 to your computer and use it in GitHub Desktop.
snapmaker cli
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# allows basic control of things like enclosure lights and door detection, pausing+resuming jobs, parking the head so its easy to swap and status fetching of status, returns some JSON | |
# doesn't hang the touch screen by disconnecting as soon as it has an answer | |
# tested to work on my A350 | |
# Configuration | |
API_HOST="INSERT_IP_HERE:8080" | |
TOKEN_FILE="snapmaker.token" | |
exit_code=1 | |
# Helper functions | |
load_token() { | |
if [ -f "$TOKEN_FILE" ]; then | |
token=$(cat "$TOKEN_FILE") | |
else | |
token="" | |
fi | |
} | |
connect() { | |
if [ -z "$token" ]; then | |
echo '{"printStatus": "No Token"}' | |
exit 0 | |
fi | |
curl -X POST "http://$API_HOST/api/v1/connect?token=$token" > /dev/null 2>&1 | |
} | |
disconnect() { | |
curl -X POST --silent --output /dev/null "http://$API_HOST/api/v1/disconnect?token=$token" | |
} | |
# API functions | |
get_status() { | |
connect | |
status_json=$(curl -s "http://$API_HOST/api/v1/status?token=$token") | |
enclosure_json=$(curl -s "http://$API_HOST/api/v1/enclosure?token=$token") | |
prefixed_enclosure_json=$(echo "$enclosure_json" | jq 'with_entries(.key |= "enclosure_" + .)') | |
merged_json=$(echo "$status_json" | jq --argjson enclosure "$prefixed_enclosure_json" '. + $enclosure') | |
echo "$merged_json" | |
exit_code=$? | |
disconnect | |
} | |
get_token() { | |
curl -s -X POST "http://$API_HOST/api/v1/connect" | jq -r '.token' > "$TOKEN_FILE" | |
echo "Token $(cat "$TOKEN_FILE") saved to $TOKEN_FILE" | |
exit_code=$? | |
} | |
clear_token() { | |
rm "$TOKEN_FILE" | |
exit_code=$? | |
} | |
set_light() { | |
local value=$1 | |
connect | |
curl -X POST --silent --output /dev/null -d "token=$token&led=$value" "http://$API_HOST/api/v1/enclosure" | |
exit_code=$? | |
disconnect | |
} | |
set_door_detection() { | |
local enabled=$1 | |
connect | |
curl -X POST --silent --output /dev/null -d "token=$token&isDoorEnabled=$enabled" "http://$API_HOST/api/v1/enclosure" | |
exit_code=$? | |
disconnect | |
} | |
get_enclosure_status() { | |
connect | |
curl "http://$API_HOST/api/v1/enclosure?token=$token" | |
exit_code=$? | |
disconnect | |
} | |
control_print() { | |
local action=$1 | |
connect | |
curl -X POST --silent --output /dev/null -d "token=$token" "http://$API_HOST/api/v1/${action}_print" | |
exit_code=$? | |
disconnect | |
} | |
control_airpurifier() { | |
local action=$1 | |
local value=$2 | |
connect | |
case "$action" in | |
"switch") | |
curl -X POST --silent -d "token=$token&switch=$value" "http://$API_HOST/api/v1/air_purifier_switch" | |
;; | |
"speed") | |
curl -X POST --silent -d "token=$token&fan_speed=$value" "http://$API_HOST/api/v1/air_purifier_fan_speed" | |
;; | |
esac | |
exit_code=$? | |
disconnect | |
} | |
execute_code() { | |
local code=$1 | |
echo "ff" "$code" | |
connect | |
curl -X POST --silent -d "token=$token&code=$code" "http://$API_HOST/api/v1/execute_code" | |
exit_code=$? | |
disconnect | |
} | |
unload_filament() { | |
connect | |
curl -X POST --silent --output /dev/null -d "token=$token" "http://$API_HOST/api/v1/filament_unload" | |
exit_code=$? | |
disconnect | |
} | |
generate_completion() { | |
cat << 'EOF' | |
#compdef snapmaker | |
_snapmaker() { | |
local -a commands | |
commands=( | |
'status:Get printer status' | |
'get-token:Get a new authentication token' | |
'clear-token:Clear the stored token' | |
'light-on:Turn on the enclosure light' | |
'light-off:Turn off the enclosure light' | |
'door-detection-on:Enable door detection' | |
'door-detection-off:Disable door detection' | |
'enclosure-status:Get enclosure status' | |
'pause-print:Pause current print' | |
'resume-print:Resume paused print' | |
'airpurifier-on:Turn on air purifier' | |
'airpurifier-off:Turn off air purifier' | |
'airpurifier-speed:Set air purifier speed (1-3)' | |
'execute-code:Execute G-code command' | |
'unload-filament:Unload the current filament' | |
'completion:Generate zsh completion script' | |
) | |
_describe 'command' commands | |
case $words[2] in | |
airpurifier-speed) | |
_message 'speed (1-3)' | |
;; | |
execute-code) | |
_message 'G-code command' | |
;; | |
esac | |
} | |
_snapmaker "$@" | |
EOF | |
} | |
# Main execution | |
case "$1" in | |
"completion") | |
generate_completion | |
exit 0 | |
;; | |
esac | |
load_token | |
case "$1" in | |
"status") | |
get_status | |
;; | |
"get-token") | |
get_token | |
;; | |
"clear-token") | |
clear_token | |
;; | |
"light-off") | |
set_light 0 | |
;; | |
"light-on") | |
set_light 100 | |
;; | |
"door-detection-off") | |
set_door_detection false | |
;; | |
"door-detection-on") | |
set_door_detection true | |
;; | |
"enclosure-status") | |
get_enclosure_status | |
;; | |
"pause-print") | |
control_print "pause" | |
;; | |
"resume-print") | |
control_print "resume" | |
;; | |
"airpurifier-on") | |
control_airpurifier "switch" "true" | |
;; | |
"airpurifier-off") | |
control_airpurifier "switch" "false" | |
;; | |
"airpurifier-speed") | |
control_airpurifier "speed" "$2" | |
;; | |
"execute-code") | |
execute_code "$2" | |
;; | |
"unload-filament") | |
unload_filament | |
;; | |
esac | |
exit $exit_code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment