-
Notifications
You must be signed in to change notification settings - Fork 11
/
simulator.sh
executable file
·124 lines (102 loc) · 3.04 KB
/
simulator.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
#!/bin/bash
start_devices() {
start=${1:?"Missing start id"}
stop=${2:?"Missing end id"}
kill_devices;
mkdir -p devices
echo "Starting uplink with simulator"
devices=($(seq $start $stop))
first=${devices[0]}
rest=${devices[@]:1}
download_auth_config $first
create_uplink_config $first
start_uplink 1 $first "-vv" "devices/uplink_$first.log"
for id in $rest
do
sleep 1
download_auth_config $id
create_uplink_config $id
start_uplink 0 $id
done
echo DONE
# Wait on pids and block till atleast one uplink is running
for file in $(find ./devices -type f -name "*.pid")
do
tail --pid=$(cat $file) -f /dev/null
done
}
create_uplink_config() {
id=${1:?"Missing id"}
printf "$(cat << EOF
processes = []
action_redirections = { send_file = \"load_file\", update_firmware = \"install_firmware\" }
persistence_path = \"/var/tmp/persistence/$id\"
[persistence]
max_file_size = 104857600
max_file_count = 3
[streams.gps]
topic = "/tenants/{tenant_id}/devices/{device_id}/events/gps/jsonarray"
persistence = { max_file_size = 0 }
[streams.bms]
topic = "/tenants/{tenant_id}/devices/{device_id}/events/bms/jsonarray"
persistence = { max_file_size = 0 }
[streams.imu]
topic = "/tenants/{tenant_id}/devices/{device_id}/events/imu/jsonarray"
persistence = { max_file_size = 0 }
[streams.motor]
topic = "/tenants/{tenant_id}/devices/{device_id}/events/motor/jsonarray"
persistence = { max_file_size = 0 }
[streams.peripheral_state]
topic = "/tenants/{tenant_id}/devices/{device_id}/events/peripheral_state/jsonarray"
persistence = { max_file_size = 0 }
[streams.device_shadow]
topic = "/tenants/{tenant_id}/devices/{device_id}/events/device_shadow/jsonarray"
persistence = { max_file_size = 0 }
[simulator]
gps_paths = "./paths"
actions= [{ name = \"load_file\" }, { name = \"install_firmware\" }, { name = \"update_config\" }, { name = \"unlock\" }, { name = \"lock\" }]
[downloader]
actions= [{ name = \"send_file\" }, { name = \"update_firmware\", timeout = 1800 }]
path = \"/var/tmp/ota/$id\"
EOF
)" > devices/device_$id.toml
}
download_auth_config() {
id=${1:?"Missing id"}
url="https://$CONSOLED_DOMAIN/api/v1/devices/$id/cert"
echo "Downloading config: $url"
mkdir -p devices
curl --location $url \
--header "x-bytebeam-tenant: $BYTEBEAM_TENANT_ID" \
--header "x-bytebeam-api-key: $BYTEBEAM_API_KEY" > devices/device_$id.json
}
run() {
printf "running: $2 $3"
if [ $1 -eq 1 ]
then
echo " > $4"
nohup $2 $3 > $4 2>&1 &
else
$2 &
fi
}
start_uplink() {
echo "Starting uplink with device_id: $2"
cmd="uplink -a devices/device_$2.json -c devices/device_$2.toml"
run $1 "$cmd" "$3" "$4"
echo $! >> "devices/$2.pid"
}
kill_devices() {
echo "Killing all devices in pids file"
for file in $(find ./devices -type f -name "*.pid")
do
kill_device $file
done
echo DONE
}
kill_device() {
echo "Killing $1"
kill $(cat $1)
rm $1
}
${1:?"Missing command"} ${@:2}