-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshmcollectbme680fs.rc
executable file
·79 lines (55 loc) · 1.91 KB
/
shmcollectbme680fs.rc
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
#!/bin/rc
# collects data from bme680fs to shared memory created with ramfs
# requirements and set up:
# - set up shmsensors service (/srv/shmsensors) at boot somewhere (/cfg/.../cpustart)
# -- do that with "ramfs -S shmsensors" and dont forget to set permissions "chmod 666 /srv/shmsensors"
# - mount shared memory per process (/mnt/shmsensors) with "mount -c /srv/shmsensors /mnt/shmsensors"
# - (optional) have a script to periodicly save dato from shared memory to disk if desired
# -- (cron e.g.) "3 * * * * local mount -c /srv/shmsensors /mnt/shmsensors && cp /mnt/shmsensors/* /sys/log/"
# set file retention history in seconds (0 for unlimited)
fh=604800
# log file location
lf=/mnt/shmsensors/bme680
# human readable date format (no whitespace)
hdf=YYYY.MM.DD-hh:mm:ss
# only run if shared ramfs is accesible
if (test ! -r /srv/shmsensors) {
exit
}
# only run sensor is accesible
if (test ! -r /srv/bme680) {
exit
}
# mount ...
if (test `{ns | grep shmsensors | wc -l} -eq '0') {
mount -c /srv/shmsensors /mnt/shmsensors
}
# if no data yet in ramfs, copy from storage
if (test ! -r $lf) {
cp /sys/log/bme680* /mnt/shmsensors
}
# create file with data headder to describe values
if (! test -r $lf.header){
echo 'timestamp datetime temp(C) hum(%r.H.) pres(hPa) gas(Ohm)' \
| awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >>$lf.header
}
# mount sensor if not mounted
if (test ! -r /mnt/bme680/all) {
mount -b /srv/bme680 /mnt
}
# if mounted collect data and save
if (test -r /mnt/bme680/all) {
# read sensor data
bmeraw=`{cat /mnt/bme680/all}
mdate=`{date -n}
hdate=`{date -f $hdf $mdate}
echo $mdate $hdate $bmeraw | awk '{print $1"\t"$2"\t"$4"\t"$6"\t"$8"\t"$10}' >>$lf
}
# prune older data if set
if (test $fh -gt 0) {
temp=/mnt/shmsensors/bme680-$pid
dn=`{date -n}
cod=`{echo $dn - $fh | bc}
cat /mnt/shmsensors/bme680 | awk -v 'cod='$cod -v 'dn='$dn '{if ($1 > cod) print $0;}' > $temp
mv $temp $lf
}