-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpms5003.sh
executable file
·75 lines (62 loc) · 1.45 KB
/
pms5003.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
#!/bin/bash
#set -x
: "${HOST:=192.168.0.65}"
DEV=""
status_data=""
status_timestamp=""
read_line() {
data=$(nc $HOST 23 \
| od -v -j0 -N30 -w2 -t u2 --endian=big \
| grep -o ' [0-9]*$' \
| tr -d "\n" \
| grep '^ 16973 28 ' \
| sed 's/^ 16973 28 //' \
)
echo $data
if [ -z "$data" ]
then
return 1
fi
}
f_status() {
item=$1
if [ $(($status_timestamp+1)) -lt $(date +%s) ]
then
data="$(read_line)"
status_data="$(
echo -n "PM1.0:"; echo "$data" | cut -d' ' -f1
echo -n "PM2.5:"; echo "$data" | cut -d' ' -f2
echo -n "PM10:"; echo "$data" | cut -d' ' -f3
echo -n "PM1.0_atm:"; echo "$data" | cut -d' ' -f4
echo -n "PM2.5_atm:"; echo "$data" | cut -d' ' -f5
echo -n "PM10_atm:"; echo "$data" | cut -d' ' -f6
echo -n "PM0.3_num:"; echo "$data" | cut -d' ' -f7
echo -n "PM0.5_num:"; echo "$data" | cut -d' ' -f8
echo -n "PM1.0_num:"; echo "$data" | cut -d' ' -f9
echo -n "PM2.5_num:"; echo "$data" | cut -d' ' -f10
echo -n "PM5.0_num:"; echo "$data" | cut -d' ' -f11
echo -n "PM10_num:"; echo "$data" | cut -d' ' -f12
)"
status_timestamp=$(date +%s)
fi
if [ -z "$item" ]
then
echo $status_data | tr ' ' "\n"
else
echo $status_data | tr ' ' "\n" | fgrep "$item:" | cut -d: -f2
fi
}
for f in "$@"
do
if echo "$f" | grep -q '^PM[0-9]'
then
f_status "$f"
continue
fi
if echo "$f" | grep -q '...:.'
then
val=$(echo $f | cut -d: -f2)
f=$(echo $f | cut -d: -f1)
fi
"f_$f" $val
done