-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClockSync.sh
145 lines (144 loc) · 3.38 KB
/
ClockSync.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
#!/bin/bash
# Execution: ./ClockSync.sh
# This code check and output sensor readings synchronizing with the local computer's clock time interval of 15,30,45,59th minutes
# According the local computer's clock, the system inserts sensor data into the local database, and pushes the data to the remote server
# Written by Yuttanant Suwansiri 6 Dec. 2012
# Start the program
while [ 1 ]
do
#functions
# Get flow meter information
{ function getMeter() {
meter=`quick-Flow.sh 265`
meter=`echo ${meter#0}` #Ignore 0 before the decimal point Ex. 0.75 -> .75, 7.5 -> 7.5
until [[ $meter =~ ^[0-9]+([.][0-9]+)?$ && $meter != 0 ]] #Convert . to 0. & string to #Ex. .70 will be 0.70 or string to 0.7
do
meter=`quick-Flow.sh 265`
meter=`echo ${meter#0}`
sleep 1
done
echo $meter
}
# Get tank information
function getTank() {
tankIn=`quick-VEGA.sh`
tankIn=`echo ${tankIn#0}`
tankIn=`echo "scale=6; (20.70913 - $tankIn) * 1.665 *12*4"|bc | sed 's/^\./0./'` #Calculation to convert level to bbls
until [[ $tankIn =~ ^[0-9]+([.][0-9]+)?$ && $tankIn != 0 ]]
do
tankIn=`quick-VEGA.sh`
tankIn=`echo ${tankIn#0}`
tankIn=`echo "scale=6; (20.70913 - $tankIn) * 1.665 *12*4"|bc | sed 's/^\./0./'`
sleep 1
done
echo $tankIn
}
# Check clock minute
function checkMin() {
until (( $min >= $1 ))
do
sleep 5
echo "Checking"
min=`date +%M`
min=`echo ${min#0}`
echo "$min $1"
done
dt3=`date +%s`
}
# Insert data into local database
function insertData() {
sqlite3 /xxx/xxx/xxx/xxx/xxx.db "INSERT INTO xxx_results VALUES ($1,0,0,0,0,0,0,0,0,$2,$3)"
}
# Gerating CSV file
function genCSV() {
uniqueid=1111
fileid="$uniqueid$dt2.csv"
echo "generating csv file"
echo "$1 $2 $3"
echo "$1| | | | | | | | |$2|$3" >> $fileid
}
function pushData() {
while [[ ! `ping -c 1 "google.com"` ]] #Check to see if there's internet connection by pinging google.com
do
echo "no internet"
sleep 5
done
echo "Uploading to servers"
scp $fileid [email protected]:/xxx/asuwansiri/data/xxx
ssh [email protected] "cd /xxx/asuwansiri/data/xxx; scp $fileid [email protected]:/var/www/xxx/xxx/www/data_files; ssh [email protected] "/usr/bin/curl -silent -o test.text https://xxx.xxx.com/xxx-update.php""
}
function operation() {
# Set date
dt2=`date "+%m%d%H$minLogic%y"`
{ checkMin $minLogic #Check to see if it's 0,15,30,45
}
{ getMeter #Get meter and tank barrels
getTank
echo "inside loop meter and tank: $meter $tankIn"
}
{ insertData $dt2 $meter $tankIn #Insert date/time, meter, tank barrel into database
genCSV $dt3 $meter $tankIn #Output a csv file
pushData #push data to servers
sleep 1
echo "removing csv file"
rm $fileid
echo "finish the process"
}
}
#get hr and min
{ hr=`date +%H`
hr=`echo ${hr#0}`
echo $hr
min=`date +%M`
min=`echo ${min#0}`
echo $min
}
# Mail program, start the logic
{ if (( 0 <= $min && $min <= 15 ))
then {
minLogic=15
operation
}
}
elif (( 15 < $min && $min <= 30 ))
then {
minLogic=30
operation
}
}
elif (( 30 < $min && $min <= 45 ))
then {
minLogic=45
operation
}
}
elif (( 45 < $min && $min <= 59 ))
then {
min4=00
minLogic=59
if (( $hr == 23 ))
then {
hr=00
operation
}
}
elif (( $hr < 9 ))
then {
((hr= $hr+1))
operation
}
}
else {
((hr= $hr+1))
operation
}
}
fi
}
else {
echo "no matches"
}
fi
}
sleep 2m
done