-
Notifications
You must be signed in to change notification settings - Fork 25
/
command.sh
executable file
·187 lines (164 loc) · 4.3 KB
/
command.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
#!/usr/bin/env bash
# command.sh - simple script to interact with EdgeX's core-command
# to see and manipulate ROs, reader config, and reader capabilities.
# Use --help to view usage.
set -euo pipefail
IFS=$'\n\t'
HOST=localhost
CMDS_PORT=59882
CURL_OPTS="-o-"
DEVICE=""
usage() {
echo "usage:"
echo " $0 [OPTS] list LISTABLE"
echo " $0 [OPTS] get TARGET"
echo " $0 [OPTS] SET_ACTION TARGET FILEPATH"
echo ""
echo "LISTABLE: devices commands"
echo "SET_ACTIONS: set add enable start stop disable delete"
echo "TARGETS: ReaderConfig ReaderCapabilities ROSpec AccessSpec"
echo ""
echo "OPTS:"
echo " -d | --device NAME specific device to use default: all LLRP devices"
echo " -h | --host HOST edgex-core-commands host default: localhost"
echo " -p | --port PORT edgex-core-commands port default: 59882"
echo " -v | --verbose use verbose curl output"
echo " -s | --silent use silent curl output"
echo ""
echo "examples:"
echo " $0 list devices"
echo " $0 get ReaderCapabilities"
echo " $0 set ReaderConfig path/to/config.json"
echo " $0 add ROSpec path/to/ROSpec.json"
echo " $0 enable ROSpec 1"
echo " $0 delete ROSpec 0"
}
if [[ $# -lt 1 ]]; then
usage; exit
fi
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
-h | --host )
shift; HOST=$1
;;
-d | --device )
shift; DEVICE=$1
;;
-p | --core-cmds-port )
shift; CMDS_PORT=$1
;;
-v | --verbose )
CURL_OPTS="-vvvvo-"
;;
-s | --silent )
CURL_OPTS='-so-'
;;
--help )
usage; exit
;;
*)
echo "unknown flag $1"
usage; exit
;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi
devices() {
if [[ -n "$DEVICE" ]]; then
echo "$DEVICE"
else
curl ${CURL_OPTS} "$HOST:$CMDS_PORT/api/v2/device/all" | jq '[.deviceCoreCommands[]|select(.profileName | startswith("LLRP"))]'
fi
}
put_file() {
if [[ $# -ne 3 ]]; then
usage; exit
fi
if [[ "$1" == "put" ]]; then
CMD_NAME="${2}"
else
CMD_NAME="${2}"
fi
TYPE=${2}
FILE_PATH="${3}"
devices | jq '.[].coreCommands[]|select(.name=="'"$CMD_NAME"'")|.url+.path' | \
sed -e "s/edgex-core-command/${HOST}/" | \
xargs -L1 curl ${CURL_OPTS} -X PUT -H 'Content-Type: application/json' \
--data '@'<(echo "{\"${TYPE}\":$(<"$FILE_PATH")}")
}
put() {
set -x
devices | jq '.[].coreCommands[]|select(.name=="'"${2}"'")|.url+.path' | \
sed -e "s/edgex-core-command/${HOST}/" | \
xargs -L1 curl ${CURL_OPTS} -X PUT -H 'Content-Type: application/json' \
--data "${3}"
}
put_num() {
if [[ $# -ne 3 ]]; then
usage; exit
fi
CMD_NAME="${1}${2}"
TYPE="${2}ID"
NUM="${3}"
devices | jq '.[].coreCommands[]|select(.name=="'"$CMD_NAME"'")|.url+.path' | sed -e "s/edgex-core-command/${HOST}/" | \
xargs -L1 curl ${CURL_OPTS} -X PUT -H 'Content-Type: application/json' \
--data '{"'"$TYPE"'": "'"$NUM"'"}'
}
get() {
if [[ $# -gt 2 ]]; then
usage; exit
fi
if [[ "$1" == "pull" ]]; then
CMD_NAME="${2}"
else
CMD_NAME="${1}"
fi
devices | jq '.[].coreCommands[]|select(.name=="'"$CMD_NAME"'") | .url+.path' | \
sed -e "s/edgex-core-command/${HOST}/" | \
xargs -L1 curl ${CURL_OPTS} | jq '.event.readings[].objectValue'
}
list() {
if [[ $# -ne 1 ]]; then
echo "list takes one argument"
usage; exit
fi
case "$1" in
devices)
devices | jq '.[].deviceName' | tr -d '"'
;;
commands)
devices | jq '.[].coreCommands[]' | \
jq -s '.|=.'
;;
*)
echo "unknown list request: $1"
usage; exit
;;
esac
}
if [[ $# -lt 1 ]]; then
echo "missing command"
usage; exit
fi
case "$1" in
list)
shift; list "$@"
;;
get)
shift; get "$@"
;;
pull)
get "$@"
;;
set | add)
put_file "$@"
;;
put)
put "$@"
;;
enable | start | stop | disable | delete)
put_num "$@"
;;
*)
echo "unknown ACTION: $1"
usage
;;
esac