-
Notifications
You must be signed in to change notification settings - Fork 0
/
registerNode.sh
executable file
·215 lines (174 loc) · 4.11 KB
/
registerNode.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
#// Copyright (c) 2023 ZKM | Hertz-Lab (http://www.zkm.de)
#// Bernd Lintermann <[email protected]>
#//
#// BSD Simplified License.
#// For information on usage and redistribution, and for a DISCLAIMER OF ALL
#// WARRANTIES, see the file, "LICENSE" in this distribution.
#//
dir=$(basename $(realpath -s `dirname $0`))
verbose=
entry=
sleepTimeout=5
once=
try=
config=false
printHelp() {
echo "usage: $(basename $0) [+v] [+try] [+once] [+timeout timeout] [entry] [+config [MAC]] "
echo
exit 0
}
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help|help)
printHelp
shift # past argument
;;
+try)
try=$1
shift # past argument
;;
+once)
once=$1
shift # past argument
;;
+config)
config=true
HWaddr="$2"
shift # past argument
shift # past value
;;
+timeout)
sleepTimeout="$2"
shift # past argument
shift # past value
;;
+v)
verbose=$1
shift # past argument
;;
*)
argentry="$1"
shift # past argument
;;
esac
done
if $config ; then
configServer=lidar-config
configPort=8888
if [ "$LIDAR_CONFIG_SERVER" != "" ] ; then
configServer="$LIDAR_CONFIG_SERVER"
fi
if [ "$LIDAR_CONFIG_PORT" != "" ] ; then
configPort="$LIDAR_CONFIG_PORT"
fi
if [ -f config_server.txt ] ; then
source config_server.txt
fi
if [ "$argentry" == "" ] ; then
entry="$(./install_node.sh entry)"
else
entry="$argentry"
fi
timeout=3
if [ "$verbose" != "" ] ; then
echo "wget --timeout=$timeout http://$configServer:$configPort/config?entry=$entry"
fi
result=$(wget --timeout=$timeout "http://$configServer:$configPort/config?entry=$entry" -O - -o /dev/null)
if [ "$result" != "" ] ; then
echo "$result" > config.txt
fi
exit 0
fi
if [ -f config.txt ] ; then
source config.txt
else
source config_default.txt
fi
if [ "$server" == "" ] ; then
echo "missing server definition !!!"
echo "please create a config.txt file on your server and copy it to $(pwd)/config.txt on this client"
exit 1
fi
success=false
while [ 1 ]
do
if [ "$argentry" == "" ] ; then
entry="$(./install_node.sh entry)"
else
entry="$argentry"
fi
if [ "$verbose" != "" ] ; then
echo "wget http://$server:$adminPort/nodes?register=$entry"
fi
result=$(wget "http://$server:$adminPort/nodes?register=$entry" -O - -o /dev/null)
if [ "$result" != "" ] ; then
if $(echo "$result" | jq 'has("nodeId")') ; then
nodeId=$(jq .nodeId <<<"$result" | tr -d '"')
else
nodeId=
fi
if $(echo "$result" | jq 'has("deviceType")') ; then
deviceType=$(jq .deviceType <<<"$result" | tr -d '"')
else
deviceType=
fi
if [ -f LidarDeviceType.txt ] ; then
LidarDeviceType="$(cat LidarDeviceType.txt)"
fi
if [ $(echo $nodeId | wc -c) == 4 ] ; then
if [ "$deviceType" != "$LidarDeviceType" ] ; then
if [ "$verbose" != "" ] ; then
echo setting deviceType $deviceType
fi
if [ "$deviceType" == "" ] ; then
rm -f LidarDeviceType.txt
else
echo $deviceType > LidarDeviceType.txt
fi
fi
if [ "$nodeId" != "$(cat LidarNodeId.txt)" ] ; then
if [ "$verbose" != "" ] ; then
echo setting nodeId $nodeId
fi
echo $nodeId > LidarNodeId.txt
sudo ./install_node.sh update
if [ "$argentry" == "" ] ; then
entry="$(./install_node.sh entry)"
else
entry="$argentry"
fi
if [ "$verbose" != "" ] ; then
echo "wget http://$server:$adminPort/nodes?register=$entry"
fi
wget "http://$server:$adminPort/nodes?register=$entry" -O - -o /dev/null
if [ "$once" != "" ] ; then
exit 1
fi
fi
if [ "$once" != "" ] ; then
exit 0
fi
success=true
fi
fi
if [ "$try" != "" ] ; then
exit 0
fi
if $success ; then
# if successful try again every hour
if [ "$time" == "" ] ; then
time=$(shuf -i 800-2400 -n 1)
sleep $time
else
time=$((3600-$time))
sleep $time
time=
fi
else
# otherwise every sleepTimeout seconds
sleep $sleepTimeout
fi
done