This repository has been archived by the owner on Dec 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
js9msg
executable file
·142 lines (128 loc) · 3.55 KB
/
js9msg
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
#!/bin/bash
#set -x
# js9msg: send a command to JS9 using wget or curl
# node is not required, which makes the js9 app self-contained
# default variables
HELPER=${JS9_HELPER:-localhost}
ID=JS9
JS9WEBSITE="https://js9.si.edu:2718"
PORT=${JS9_HELPER_PORT:-2718}
TIMEOUT=600
usage(){
XEQ=`basename $0`
printf "$XEQ: send a command to JS9 (using wget or curl)\n\n"
printf " call:\n"
printf " %s [switches] [cmd] [arg1] [arg2] ... [argn]\n" $XEQ
printf "\n"
printf " switches:\n"
printf " -d|--debug # output debugging info\n"
printf " -h|--helper|--host| [host] # helper host (def: $HELPER)\n"
printf " -i|--id [id] # client id (def: $ID)\n"
printf " -m|--multi] # send to multiple clients\n"
printf " --pageid [id] # send to unique pageid\n"
printf " -t|--timeout # connect timeout (def: $TIMEOUT)\n"
printf " -v|--verbose # output debugging info\n"
printf " -W # connect to main JS9 web site\n"
printf "\n examples:\n"
printf " # load foo.fits, setting colormap and scale\n"
printf " %s Load foo.fits '{\"colormap\":\"heat\", \"scale\":\"log\"}'\n" $XEQ
printf " # get the colormap of the currently loaded image\n"
printf " %s GetColormap\n" $XEQ
printf " # set the colormap of the image loaded into %s\n" $JS9WEBSITE
printf " %s -W SetColormap viridis\n" $XEQ
printf "\n"
}
error() {
echo "ERROR: $1" 1>&2
exit 1
}
# https://stackoverflow.com/questions/296536/how-to-urlencode-data-for-curl-command
rawurlencode() {
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}" # You can either set a return variable (FASTER)
REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p
}
while [ x"$1" != x ]; do
case $1 in
-d|--debug) shift
DODEBUG=true;;
-i|--id) shift
ID="$1"
shift;;
-h|--helper|--host) shift
HELPER="$1"
shift;;
-m|--multi) shift
MULTI=',"multi":true';;
--pageid) shift
PAGEID=',"pageid":"'$1'"'
shift;;
-t|--timeout) shift
TIMEOUT="$1"
shift;;
-v|--verbose) shift
DODEBUG=true;;
-W) shift
HELPER="$JS9WEBSITE";;
*) break;;
esac
done
# arg1: cmd
if [ x"$1" = x ]; then
usage
exit 0
else
CMD="$1"
shift
fi
# arg2 ... argn: arguments
ARGS=""
while [ x"$1" != x ]; do
ARG=$(echo $1 | sed 's/"/\\"/g')
if [ x"$ARGS" = x ]; then
ARGS="\"$ARG\""
else
ARGS="${ARGS},\"$ARG\""
fi
shift
done
# general form of message sent to JS9:
QUERY='{"id":"'$ID'","cmd":"'$CMD'","args":['$ARGS']'${MULTI}${PAGEID}'}'
# get target host (add port if necessary)
HOST=$HELPER
echo $HOST | egrep ':[0-9][0-9]*$' 1>/dev/null 2>&1
if [ $? = 1 ]; then
HOST="$HOST:$PORT"
fi
# encode the query
URL=$HOST/msg?$( rawurlencode "$QUERY" )
command -v wget 1>/dev/null 2>&1
if [ $? = 0 ]; then
if [ x"$DODEBUG" != xtrue ]; then
XARGS="-q"
fi
wget $XARGS -O- -T $TIMEOUT "$URL"
else
command -v curl 1>/dev/null 2>&1
if [ $? = 0 ]; then
if [ x"$DODEBUG" = xtrue ]; then
XARGS="-v"
else
XARGS="-s"
fi
curl $XARGS --connect-timeout $TIMEOUT "$URL"
else
error "$0 requires either wget or curl"
fi
fi