-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelay.sh
149 lines (123 loc) · 3.04 KB
/
relay.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
146
147
148
149
# Author: Peter Finn - https://github.com/peter2233finn/ linux controler for GeekTechesNC1601
# Set the IP address and port here:
ip="192.168.1.4"
portDir="30000"
function compileMode(){
clear
echo "Compile mode:"
echo "s = sleep"
echo "n o/f = turn on/off"
echo "e = execute"
echo "c = cancel"
function execute(){
clear
printf "Executing:\n\n"
IFS=',' read -r -a cmdArr <<< "$*"
x=1
for i in ${cmdArr[@]}; do
echo "$x $i" | tr "-" " " | sed 's/doAction/Set/g'
((x++))
done
x=0
printf "Completed:\n"
for i in ${cmdArr[@]}; do
printf "\r\r\r\r\r"
cmd=$(echo "$i" | tr "-" " ")
$cmd
((x++))
printf "...$x"
done
printf "\n"
}
function preCompile(){
execute $(echo "$@"|tr "-" " "|tr "," ";" | sed 's/do;/do /g'|sh |tr " " "-"|tr "\n" ",")
}
function halt(){
function doSleep(){
i=0
while [[ $i -le $1 ]]; do
#for i in {0..$1}; do
printf "$i out of $1 sec "
sleep 1
printf "\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r"
((i+=1))
done
}
if [[ "$1" == *"m" ]]; then
doSleep $((${1::-1}*60))
elif [[ "$1" == *"h" ]]; then
doSleep $((${1::-1}*60*60))
else
doSleep $1
fi
}
comp=""
while true; do
read zzz
# cancel
if [[ "$zzz" == "c" ]]; then
break
elif [[ "$zzz" == "ef" ]]; then
comp=$comp"done,"
elif [[ "$zzz" == "f"* ]]; then
loops=${zzz#?}
comp=$comp"for-i-in-{1..$loops};-do,"
elif [[ "$zzz" == "e" ]]; then
preCompile "$comp"
comp=""
break
# this will execute during sleep due to number
elif [[ $zzz =~ [0-9] ]] && [[ "$zzz" != *"s"* ]]; then
comp=$comp"echo-doAction-$zzz,"
elif [[ "$zzz" == *"s"* ]]; then
comp=$comp"echo-halt-${zzz:1},"
fi
done
}
function doAction() {
rrr=$1
# add 0 for on, 1 for off
# Also -1 because it starts at 0
lastChar="${rrr: -1}"
number="${rrr: : -1}"
number=$((number-1))
# Multiply by 2 because each relay needs two numbers
# too turn on and off
number=$((number*2))
if [[ "$lastChar" == "o" ]];then
# echo "Relay on"
number=$((number+1))
elif [[ "$lastChar" == "f" ]];then
number=$number
# echo "Relay off"
elif [[ "$rrr" == "c" ]];then
compileMode
elif [[ "$rrr" == "ss" ]];then
function showStatus() {
for i in {1..4};do
curl --silent http://$ip/$portDir/43 |sed 's/ <font color="#FF0000">//g'|sed 's/  <\/font>//g'|grep -oh "Relay-......."|sed 's/<fo/ON/g'| grep -v Relay-ALL
done
}
showStatus | sort
else
echo "Error. Enter a valid command. eg 10, ss or c"
fi
# Get length of string. add a 0 of it on less than 10
if [[ $(echo "$number" | wc -m) == "2" ]]; then
# echo "Adding 0 as single digit"
number="0"$number
fi
curl "$ip/$portDir/$number" 2> /dev/null 1> /dev/null
}
if [[ "$*" == *"a"* ]];then
echo all XX
for i in {1..16}; do
# skip 1,9
doAction "$i""$2"
done
else
# parse and do actions
for i in "$@" ; do
doAction $i
done
fi