-
Notifications
You must be signed in to change notification settings - Fork 0
/
piGardenSched.sh
executable file
·219 lines (208 loc) · 6.51 KB
/
piGardenSched.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/bin/bash
#
# pigarden sched
# main script "piGardenSched.sh"
# Author: androtto
# VERSION=0.3.6
VERSION=0
SUB_VERSION=3
RELEASE_VERSION=6
# 2019/09/13: TMPDIR changed
# 2019/08/13: irrigation option has improved
# 2021/06/15: boot option for cleanup at startup added - need to add following line in crontab manually at the moment:
# @reboot /home/pi/piGardenSched/piGardenSched.sh boot >> /home/pi/piGardenSched/log/piGardenSched.log 2>&1
##### MAIN
now=$(date +%s )
# for testing only
if [[ $1 = "run" ]] ; then
now=$2
echo "DEBUG: now is overrided with $2 $(date --date="@$now")"
fi
DIR_SCRIPT="$(cd `dirname $0` ; pwd )"
NAME_SCRIPT=${0##*/}
FUNCTIONS=$DIR_SCRIPT/include/functions.include.sh
RAINFUNCT=$DIR_SCRIPT/include/rain.include.sh
CRONFUNCT=$DIR_SCRIPT/include/cron.include.sh
LOGDIR="$DIR_SCRIPT/log" ; [ ! -d "$LOGDIR" ] && mkdir $LOGDIR
TMPDIR="/tmp/${NAME_SCRIPT/.sh/}" ; [ ! -d "$TMPDIR" ] && mkdir $TMPDIR
STATDIR="$DIR_SCRIPT/state" ; [ ! -d "$STATDIR" ] && mkdir $STATDIR
BCKDIR="$DIR_SCRIPT/bck" ; [ ! -d "$BCKDIR" ] && mkdir $BCKDIR
irrigating=$DIR_SCRIPT/include/irrigating.sh
cfgfiles=$DIR_SCRIPT/.cfgfiles
. $cfgfiles || { echo "ERROR: while executing $cfgfiles or not found" ; exit 1 ; }
for sourcefile in $PIGARDENSCHED_CONF $FUNCTIONS $RAINFUNCT $CRONFUNCT $PIGARDEN_CONFIG_ETC
do
. $sourcefile || { echo "ERROR: while executing $sourcefile or not found" ; exit 1 ; }
done
# help:
if [[ $1 = "help" || $1 = "-help" || $1 = "-h" ]] ; then
help
exit 0
fi
# corregge l'orario al secondo 00
now=$(echo $now | $JQ './60' | $JQ 'floor' | $JQ '.*60' )
date_now=$(date --date="@$now")
time_now="$(date --date="@$now" '+%R')"
day_now="$(date --date="@$now" '+%d')"
declare -a EV LONG TIME_SCHED DAYFREQ EVLABEL
declare -g maxline
pigardensched=$(basename $PIGARDENSCHED )
PIGARDENSCHED_save=$TMPDIR/${pigardensched}_save$$
PIGARDENSCHED_TMP=$TMPDIR/$pigardensched ; > $PIGARDENSCHED_TMP
#cp -p $PIGARDENSCHED $PIGARDENSCHED_save
if [ ! -f $PIGARDENSCHED ] ; then
echo "WARNING: piGarden_sched Config file not found in $PIGARDENSCHED"
echo -e "\tcreating empty file"
update_cfgfile
fi
#### body script
parsingfilesched "$PIGARDENSCHED"
case $1 in
stat|status)
status
;;
reset) en_echo "NORMAL: resetting irrigation status for all EVs"
tar cvf $BCKDIR/bck_STATDIR_$$.tar $STATDIR
rm $STATDIR/*
;;
add) shift
[[ $# -ne 4 ]] && { en_echo "ERROR: 4 parameters needed\n$NAME_SCRIPT add EV? duration time frequency" ; exit 1 ; }
check_evalias $1 || { en_echo "ERROR: $1 is not a valid EV" ; exit 1 ; }
check_number $2 || { echo "ERROR: $2 is not a valid duration in minutes" ; exit 1 ; }
check_timeformat $3 || { en_echo "ERROR: $3 is not a valid time" ; exit 1 ; }
check_number $4 || { echo "ERROR: $4 is not a valid frequency in days" ; exit 1 ; }
if insert $1 $2 $3 $4 ; then
en_echo "SUCCESS adding new schedule for $1"
exit 0
else
en_echo "ERROR adding new schedule for $1"
exit 1
fi
;;
seq) shift
[[ $# -eq 1 ]] && { echo "ERROR: at least 2 parameters needed" ; exit 1 ; }
if sequential $* ; then
en_echo "SUCCESS creating sequence $*"
else
exit 1
fi
;;
noseq) if convert ; then
en_echo "NORMAL: change done"
else
en_echo "$err_msg"
exit 1
fi
;;
change_dur) shift
[[ $# -ne 2 ]] && { echo "ERROR: 2 parameters needed after change_dur option" ; exit 1 ; }
check_evalias $1 || { echo "ERROR: $1 is not a valid EV" ; exit 1 ; }
check_number $2 || { echo "ERROR: $2 is not a valid duration in minutes" ; exit 1 ; }
if modify duration $1 $2 ; then
en_echo "NORMAL: change done"
else
en_echo "ERROR: action aborted"
exit 1
fi
;;
change_freq) shift
[[ $# -ne 2 ]] && { echo "ERROR: 2 parameters needed after change_freq option" ; exit 1 ; }
check_evalias $1 || { echo "ERROR: $1 is not a valid EV" ; exit 1 ; }
check_number $2 || { echo "ERROR: $2 is not a valid frequency in days" ; exit 1 ; }
if modify freq $1 $2 ; then
en_echo "NORMAL: change done"
else
en_echo $err_msg
exit 1
fi
;;
change_time) shift
[[ $# -ne 2 ]] && { echo "ERROR: 2 parameters needed after change_time option" ; exit 1 ; }
check_evalias $1 || { echo "ERROR: $1 is not a valid EV" ; exit 1 ; }
check_timeformat $2 || { echo "ERROR: $2 is not a valid time" ; exit 1 ; }
if modify time $1 $2 ; then
en_echo "NORMAL: change done"
else
en_echo $err_msg
exit 1
fi
;;
del_time) shift
[[ $# -ne 2 ]] && { echo "ERROR: 2 parameters needed after change_time option" ; exit 1 ; }
check_evalias $1 || { echo "ERROR: $1 is not a valid EV" ; exit 1 ; }
check_timeformat $2 || { echo "ERROR: $2 is not a valid time" ; exit 1 ; }
if remove_time $1 $2 ; then
en_echo "NORMAL: $2 removed from $1 entry"
else
en_echo "$err_msg"
exit 1
fi
;;
add_time) shift
[[ $# -ne 2 ]] && { echo "ERROR: 2 parameters needed after add_time option" ; exit 1 ; }
check_evalias $1 || { echo "ERROR: $1 is not a valid EV" ; exit 1 ; }
check_timeformat $2 || { echo "ERROR: $2 is not a valid time" ; exit 1 ; }
if add_time $1 $2 ; then
en_echo "NORMAL: $2 added to $1 entry"
else
en_echo "$err_msg"
exit 1
fi
;;
show|sched) grep -v ^# $PIGARDENSCHED
;;
del) shift
[[ $# -ne 1 ]] && { echo "ERROR: 1 parameter needed" ; exit 1 ; }
check_evalias $1 || { echo "error check_evalias" ; exit 1 ; }
if remove $1 ; then
en_echo "$msg"
else
en_echo "$msg"
exit 1
fi
;;
history) history ;;
irrigation) shift
case $# in
2)
check_evalias $2 || { echo "ERROR: $2 is not a valid EV" ; exit 1 ; }
[[ $1 =~ ^[0-9]*$ ]] || { echo "ERROR: irrigation history parameter must be an integer" ; exit 1 ; }
irrigation_history $1 $2
;;
1)
[[ $1 =~ ^[0-9]*$ ]] || { echo "ERROR: irrigation history parameter must be an integer" ; exit 1 ; }
irrigation_history $1
;;
0) irrigation_history
;;
esac
;;
enable) shift
[[ $# -ne 1 ]] && { echo "ERROR: 1 parameter needed after enable option" ; exit 1 ; }
check_evalias $1 || { echo "ERROR: $1 is not a valid EV" ; exit 1 ; }
if modify enable $1 ; then
en_echo "NORMAL: change done"
else
en_echo $err_msg
exit 1
fi
;;
disable) shift
[[ $# -ne 1 ]] && { echo "ERROR: 1 parameter needed after disable option" ; exit 1 ; }
check_evalias $1 || { echo "ERROR: $1 is not a valid EV" ; exit 1 ; }
if modify disable $1 ; then
en_echo "NORMAL: change done"
else
en_echo $err_msg
exit 1
fi
;;
cronadd) cron_del ; cron_add ;;
crondel) cron_del ;;
croncheck) cron_check && exit 0 || exit 1 ;;
boot) cleanup ;;
run|*)
schedule
;;
esac
exit 0