-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGPIOServer.sh
executable file
·136 lines (115 loc) · 3.45 KB
/
GPIOServer.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
#!/bin/bash
{
# Initial Script created by Daniel Curzon (http://www.instructables.com/member/drcurzon).
# Initial version created 10th June 2012.
# Initial Version: 1.0.
# Heavily modified script by linwiz.
# January 7th 2015
# Set working directory.
dir="$(dirname "$0")"
# Read config file (relative).
source "$dir/GPIOServer.conf.sh"
checkMySQL() {
if ! nc -z "$dbhostname" "$dbport"; then
echo "MySQL is down.";
sudo service mysql start
sleep 5
if ! nc -z "$dbhostname" "$dbport"; then
echo "MySQL is down. Exiting";
sudo service gpioserver stop
exit;
fi
fi
}
checkMySQL
dbExec() {
mysql -B --host="$dbhostname" --port="$dbport" --disable-column-names --user="$dbusername" --password="$dbpassword" "$dbdatabase" -e "$1"
}
addLogItem() {
dbExec "INSERT INTO log (data) VALUES (\"$1\");"
}
dbQuery() {
if ! dbExec "$1"; then
checkMySQL
addLogItem "$dbtype ERROR. Waiting 5 seconds to try again."
sleep 5
dbExec "$1"
fi
}
# Retrieve revision information.
revision=`dbQuery "SELECT piRevision FROM config WHERE configVersion=1"`
addLogItem "Starting GPIO Server"
trap "addLogItem Stopping GPIO Server" EXIT
# Retreive all GPIO pins.
pins=`dbQuery "SELECT pinNumberBCM FROM pinRevision$revision WHERE concat('',pinNumberBCM * 1) = pinNumberBCM order by pinID"`
# Start loop.
while true; do
# Retrieve pinDelay.
pinDelay=`dbQuery "SELECT pinDelay FROM config WHERE configVersion=1"`
# Retrieve logging information.
logging=`dbQuery "SELECT enableLogging FROM config WHERE configVersion=1"`
for PIN in $pins ;
do
# Select current pin details.
currPIN[$PIN]=`dbQuery "SELECT pinID,pinEnabled,pinStatus,pinDirection FROM pinRevision$revision WHERE pinNumberBCM='$PIN'"`
this_pin=${currPIN[$PIN]}
currPIN=($this_pin)
# Populate variables from query.
pinID=${currPIN[0]}
pinEnabled=${currPIN[1]}
pinStatus=${currPIN[2]}
pinDirection=${currPIN[3]}
if [ "$pinEnabled" == "1" ]; then
if [ ! -d "/sys/class/gpio/gpio$PIN" ]
then
echo $PIN > /sys/class/gpio/export
if [ "$logging" == "1" ]; then addLogItem "Enabled Pin $PIN"; fi
fi
else
if [ -d "/sys/class/gpio/gpio$PIN" ]
then
echo $PIN > /sys/class/gpio/unexport
if [ "$logging" == "1" ]; then addLogItem "Disabled Pin $PIN"; fi
fi
fi
# Skip disabled pins.
if [ -d "/sys/class/gpio/gpio$PIN" ]; then
# Read pin directions.
direction2=`cat /sys/class/gpio/gpio$PIN/direction`
# Read pin status'.
status2=`cat /sys/class/gpio/gpio$PIN/value`
# Change pin status'.
if [ "$pinDirection" != "$direction2" ]; then
if [ -n $PIN ]; then
if [ -n $pinDirection ]; then
echo $pinDirection > /sys/class/gpio/gpio$PIN/direction
if [ "$logging" == "1" ]; then
addLogItem "Pin $PIN direction to: $pinDirection"
fi
elif [ -z $pinDirection ]; then
addLogItem "Pin $PIN direction zero"
fi
elif [ -z $PIN ]; then
addLogItem "Pin $PIN value zero"
fi
fi
if [ "$pinStatus" != "$status2" ]; then
if [ -n $PIN ]; then
if [ -n $pinStatus ]; then
echo $pinStatus > /sys/class/gpio/gpio$PIN/value
if [ "$logging" == "1" ]; then
addLogItem "Pin $PIN changed to: $pinStatus"
fi
elif [ -z $pinStatus ]; then
addLogItem "Pin $PIN status zero"
fi
elif [ -z $PIN ]; then
addLogItem "Pin $PIN value zero"
fi
fi
fi
done
# Complete loop.
sleep $pinDelay
done
} >> /var/log/GPIOServer.log