-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbambu-add-ip.sh
executable file
·191 lines (174 loc) · 5.16 KB
/
bambu-add-ip.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/sh
# bambu-add-ip: Send the IP address of your BambuLab printer to port 2021/udp
# where BambuStudio is listening
# 17-Sep-2024 forked from https://github.com/gashton/bambustudio_tools/blob/master/bambudiscovery.sh
set -eu
SELF=$(basename "$0" '.sh')
if [ "$SELF" = "script" ]; then
if [ -L "$SELF" ]; then
# workaround/enhancement for platypus in dev/symlink mode
SCRIPT_PATH=$(readlink "$0")
SELF=$(basename "$SCRIPT_PATH" '.sh')
else
SELF="bambu-add-ip"
fi
fi
# set defaults (see usage below for more info)
CONFIG_FILE="${CONFIG_FILE:-${HOME}/.${SELF}.cfg}"
SLICER_IP="${SLICER_IP:-127.0.0.1}"
PRINTER_IP="${PRINTER_IP:-}"
PRINTER_USN="${PRINTER_USN:-000000000000000}"
PRINTER_DEV_MODEL="${PRINTER_DEV_MODEL:-3DPrinter-X1-Carbon}"
PRINTER_DEV_NAME="${PRINTER_DEV_NAME:-3DP-000-000}"
PRINTER_DEV_SIGNAL="${PRINTER_DEV_SIGNAL:--44}"
usage() {
exception="${1:-}"
[ -n "$exception" ] && printf 'ERROR: %s\n\n' "$exception"
printf '%s\n' \
"Usage: $SELF [-h|--help] [flags] [printer_ip]" \
"" \
"Sends the IP address of your BambuLab printer to port 2021/udp," \
"where BambuStudio is listening" \
"" \
"-h / --help show this message" \
"-d / --debug print additional debugging messages" \
"" \
"Arguments:" \
"printer_ip ip address of the printer" \
"" \
"Flags:" \
"--slicer-ip SLICER_IP" \
" IP address of PC running BambuStudio; default is appropriate for running $SELF on the same PC as BambuStudio" \
" (default: ${SLICER_IP})" \
"--printer-ip PRINTER_IP" \
" IP address of the BambuLab Printer (this flag can be used instead of the printer_ip positional argument)" \
" (default: ${PRINTER_IP})" \
"--printer-dev-model PRINTER_DEV_MODEL" \
" set this to the printer device model, use one of the following:" \
' "3DPrinter-X1-Carbon",' \
' "3DPrinter-X1",' \
' "C11" (for P1P),' \
' "C12" (for P1S),' \
' "C13" (for X1E),' \
' "N1" (for A1 mini),' \
' "N2S" (for A1)"' \
" (default: ${PRINTER_DEV_MODEL})" \
"--printer-dev-name PRINTER_DEV_NAME" \
" the device name of the printer" \
" (default: ${PRINTER_DEV_NAME})" \
"--printer-usn PRINTER_USN" \
" Printer Serial Number" \
" (default: ${PRINTER_USN})" \
"--config-file CONFIG_FILE" \
" path to an optional config file which can be sourced to load the above settings" \
" (default: ${CONFIG_FILE})" \
"" \
"Example config file content:" \
"SLICER_IP=127.0.0.1" \
"PRINTER_IP=192.168.1.234" \
"PRINTER_DEV_MODEL=N1" \
"PRINTER_DEV_NAME=tiny" \
"PRINTER_USN=030000000000001" \
"" \
"" # no trailing slash
[ -n "$exception" ] && exit 1
exit 0
}
log() {
printf '%s %s %s\n' "$(date '+%FT%T%z')" "$SELF" "$*" >&2
}
die() {
log "FATAL:" "$@"
exit 1
}
transmit_ip() {
printf '%s\r\n' \
"HTTP/1.1 200 OK" \
"Server: Buildroot/2018.02-rc3 UPnP/1.0 ssdpd/1.8" \
"Date: $(date)" \
"Location: ${PRINTER_IP}" \
"ST: urn:bambulab-com:device:3dprinter:1" \
"EXT:" \
"USN: ${PRINTER_USN}" \
"Cache-Control: max-age=1800" \
"DevModel.bambu.com: ${PRINTER_DEV_MODEL}" \
"DevName.bambu.com: ${PRINTER_DEV_NAME}" \
"DevSignal.bambu.com: ${PRINTER_DEV_SIGNAL}" \
"DevConnect.bambu.com: lan" \
"DevBind.bambu.com: free" \
"" \
| nc -u -w 0 "${SLICER_IP}" 2021
}
main() {
# arg-processing loop
while [ $# -gt 0 ]; do
arg="$1" # shift at end of loop; if you break in the loop don't forget to shift first
case "$arg" in
-h|-help|--help)
usage
;;
-d|--debug)
set -x
;;
--slicer-ip)
shift || usage "$arg requires an argument"
SLICER_IP=$1
;;
--printer-ip)
shift || usage "$arg requires an argument"
PRINTER_IP=$1
;;
--printer-usn)
shift || usage "$arg requires an argument"
PRINTER_USN=$1
;;
--printer-dev-model)
shift || usage "$arg requires an argument"
PRINTER_DEV_MODEL=$1
;;
--printer-dev-name)
shift || usage "$arg requires an argument"
PRINTER_DEV_NAME=$1
;;
--config-file)
shift || usage "$arg requires an argument"
CONFIG_FILE="$1"
;;
--)
shift || true
break
;;
*)
# unknown arg, leave it back in the positional params
break
;;
esac
shift || break
done
# ensure required environment variables are set
# : "${USER:?the USER environment variable must be set}"
if [ -f "$CONFIG_FILE" ]; then
log "sourcing config file: ${CONFIG_FILE}"
# shellcheck source=/dev/null
. "${CONFIG_FILE}"
fi
# we support one optional positional argument (the printer_ip)
case $# in
0)
: # no op
;;
1)
PRINTER_IP=$1
;;
*)
usage "unexpected arguments:" "$@"
;;
esac
[ "$PRINTER_IP" = "" ] && usage "printer_ip argument is required"
log "transmitting info about printer ${PRINTER_IP} to slicer ${SLICER_IP}"
transmit_ip || die "nc command failed"
exit 0
}
main "$@"
# shellcheck disable=SC2317
exit