-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmopi
executable file
·303 lines (278 loc) · 9.34 KB
/
mopi
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/bin/bash
#
# mopi - configuration tool for http://pi.gate.ac.uk/mopi
#
# Author: Hamish Cunningham <hamish@gate.ac.uk>
# This code is copyright Hamish Cunningham and the University of Sheffield
# and is licenced under GPL 3 or any later version.
#
# Ideas and idioms borrowed from
# https://github.com/asb/raspi-config/blob/master/raspi-config
# (thanks Alex & co!)
# standard locals
P="$0"
USAGE="`basename ${P}` [-h(elp)] [-d(ebug)] [-v(ersion)] [-l(og entries)]"
OPTIONSTRING=hdvl
alias cd='builtin cd'
DBG=:
# specific locals
DNAME=simbamond
CONFIG=
INST_DIR=`dirname ${P}`
CLI=${INST_DIR}/mopicli
LOG_STRING=simbamon
# message & exit if exit num present
usage() { echo -e Usage: $USAGE; [ ! -z "$1" ] && exit $1; }
# needs to be run as root
if [ $(id -u) -ne 0 ]
then
echo "${P} must be run as root; try 'sudo mopi'?"
usage 1
fi
# load current config
if [ -r ${INST_DIR}/${DNAME}.default ] # development mode
then
CONFIG=${INST_DIR}/${DNAME}.default
elif [ -r /etc/default/$DNAME ]
then
CONFIG=/etc/default/$DNAME
else
echo "${P}: cannot find ${DNAME} config" >&2
usage 2
fi
source $CONFIG
# get recent log entries
do_log_grep() { grep -i $LOG_STRING /var/log/syslog |tail -${WT_HEIGHT}; }
# process options
while getopts $OPTIONSTRING OPTION
do
case $OPTION in
h) usage 0 ;;
v) echo $P is at version $VERSION; exit 0 ;;
d) DBG=echo; CLI=echo ;;
l) do_log_grep; exit 0 ;;
*) usage 3 ;;
esac
done
shift `expr $OPTIND - 1`
# does mopicli work?
${CLI} -s >/dev/null || {
COUT=`${CLI} -s 2>&1`
echo "${P}: ${CLI} not working; installation problem? (${COUT})" >&2
usage 4
}
# does bc work?
echo 1 |bc >/dev/null || {
echo "${P}: bc not working; installation problem?" >&2
usage 5
}
# figure out optimal size for whiptail
calc_wt_size() {
WT_HEIGHT=24
WT_WIDTH=$(tput cols)
if [ -z "$WT_WIDTH" ] || [ "$WT_WIDTH" -lt 60 ]; then
WT_WIDTH=80
fi
if [ "$WT_WIDTH" -gt 178 ]; then
WT_WIDTH=120
fi
WT_MENU_HEIGHT=$(($WT_HEIGHT-6))
}
calc_wt_size
# menu actions etc.
do_about() {
whiptail --title "About" --msgbox "\
This is a configuration tool for MoPi
(mobile and 24/7 power for the Raspberry Pi).
Version ${VERSION}.
See http://pi.gate.ac.uk/mopi for details.\
" $WT_HEIGHT $(( $WT_WIDTH / 2 )) $WT_MENU_HEIGHT
}
do_supply_config() {
# process options
SETTING=both
case $1 in
-b) SETTING="both supplies"; S_FLAG=wc; ;;
-1) SETTING="supply 1"; S_FLAG=wc1; ;;
-2) SETTING="supply 2"; S_FLAG=wc2; ;;
esac
# what type of supply?
BACKTITLE="Configuring MoPi power supplies (${SETTING})..."
TITLE='Specify Supply Type'
C="whiptail --title \"${TITLE}\" --backtitle \"${BACKTITLE}\" \
--radiolist \"Specify type (for ${SETTING}):\" \
$WT_HEIGHT $(( $WT_WIDTH / 2 + 9 )) $WT_MENU_HEIGHT \
--cancel-button \"Cancel\" --ok-button \"Next\" \
"${SUPPLY_TYPE_A[@]}" "
SUPPLY_TYPE=`bash -c "${C} 3>&1 1>&2 2>&3"`
RET=$?
[ $RET -eq 1 ] && return 0
[ $RET -eq 0 ] && \
[ $SUPPLY_TYPE -ge 1 -a $SUPPLY_TYPE -le `supply_type_max` ] || {
whiptail --msgbox "Error: unrecognized option ${SUPPLY_TYPE}" \
--title "Oops!" --backtitle "${BACKTITLE}" \
$WT_HEIGHT $(( $WT_WIDTH / 2 )) $WT_MENU_HEIGHT
return 1
}
# get number of cells
TITLE='Specify Number of Cells'
C="whiptail --title \"${TITLE}\" --backtitle \"${BACKTITLE}\" \
--radiolist \"Specify number of cells (for ${SETTING}):\" \
$WT_HEIGHT $(( $WT_WIDTH / 2 + 9 )) $WT_MENU_HEIGHT \
--cancel-button \"Cancel\" --ok-button \"Next\" \
"${NUM_CELLS_A[@]}" "
NUM_CELLS=`bash -c "${C} 3>&1 1>&2 2>&3"`
RET=$?
[ $RET -eq 1 ] && return 0
[ $RET -eq 0 ] && \
[ $NUM_CELLS -ge 1 -a $NUM_CELLS -le `num_cells_max` ] || {
whiptail --msgbox "Error: unrecognized option ${NUM_CELLS}" \
--title "Oops!" --backtitle "${BACKTITLE}" \
$WT_HEIGHT $(( $WT_WIDTH / 2 )) $WT_MENU_HEIGHT
return 1
}
# get chemistry
TITLE='Specify Battery Chemistry'
C="whiptail --title \"${TITLE}\" --backtitle \"${BACKTITLE}\" \
--radiolist \"Specify battery chemistry (for ${SETTING}):\" \
$WT_HEIGHT $(( $WT_WIDTH / 2 + 9 )) $WT_MENU_HEIGHT \
--cancel-button \"Cancel\" --ok-button \"Next\" \
"${BAT_CHEMISTRY_A[@]}" "
CHEMISTRY=`bash -c "${C} 3>&1 1>&2 2>&3"`
RET=$?
[ $RET -eq 1 ] && return 0
[ $RET -eq 0 ] && \
[ $CHEMISTRY -ge 1 -a $CHEMISTRY -le `bat_chemistry_max` ] || {
whiptail --msgbox "Error: unrecognized option ${CHEMISTRY}" \
--title "Oops!" --backtitle "${BACKTITLE}" \
$WT_HEIGHT $(( $WT_WIDTH / 2 )) $WT_MENU_HEIGHT
return 1
}
# hokus pokus abracadabra, etc.
calculate_config $SUPPLY_TYPE $NUM_CELLS $CHEMISTRY
# validate config
TITLE="Oops!"
if [ `echo "$BAT_FULL>$OPERATING_CEILING" |bc` -eq 1 ]
then
whiptail --title "${TITLE}" --backtitle "${BACKTITLE}" \
--msgbox "Configuration error!
The full level (${BAT_FULL}V) is greater than the safe
operating ceiling (${OPERATING_CEILING}V). Please try again!" 20 70 1
return 0
elif [ `echo "$BAT_FLOOR>$BAT_GOOD" |bc` -eq 1 ]
then
whiptail --title "${TITLE}" --backtitle "${BACKTITLE}" \
--msgbox "Configuration error!
The discharge floor (${BAT_FLOOR}V) is greater than
the good charge level (${BAT_GOOD}V). Please try again!" 20 70 1
return 0
elif [ `echo "$BAT_GOOD<$OPERATING_FLOOR" |bc` -eq 1 ]
then
whiptail --title "${TITLE}" --backtitle "${BACKTITLE}" \
--msgbox "Configuration error!
The good charge level (${BAT_GOOD}V) is less
than the minimum operating level (${OPERATING_FLOOR}V).
Please try again!" 20 70 1
return 0
elif [ `echo "$BAT_GOOD>$BAT_FULL" |bc` -eq 1 ]
then
whiptail --title "${TITLE}" --backtitle "${BACKTITLE}" \
--msgbox "Configuration error!
The good charge level (${BAT_GOOD}V) is greater than
the full level (${BAT_FULL}V). Please try again!" 20 70 1
return 0
elif [ `echo "$BAT_FLOOR<$OPERATING_FLOOR" |bc` -eq 1 ]
then
TITLE="Warning!"
whiptail --title "${TITLE}" --backtitle "${BACKTITLE}" \
--msgbox "Note:
The minimum safe discharge level (${BAT_FLOOR}V) is less
than the minimum operating level (${OPERATING_FLOOR}V).
Discharge will stop at ${OPERATING_FLOOR}V." 20 70 1
fi
# confirm config and offer to write to MoPi
TITLE="Finalising configuration..."
whiptail --title "${TITLE}" --backtitle "${BACKTITLE}" --yesno "
The configuration for your supply has been calculated as:
minimum safe discharge level: ${BAT_FLOOR}V
low charge level: ${BAT_LOW}V
good charge level: ${BAT_GOOD}V
full charge level: ${BAT_FULL}V
Do you want to write this configuration to MoPi?
(Supply type:
$(supply_type ${SUPPLY_TYPE})
Number of cells:
$(num_cells ${NUM_CELLS})
Chemistry:
$(bat_chemistry ${CHEMISTRY}))
" 25 70 1 || {
whiptail --title "Cancelled" --backtitle "${BACKTITLE}" \
--msgbox "Configuration cancelled" 20 70 1
return 0
}
# write to mopi (and save locally)
while :
do
F=`v2mv $BAT_FULL`; G=`v2mv $BAT_GOOD`
L=`v2mv $BAT_LOW`; C=`v2mv $BAT_FLOOR`
FLAGS="-${S_FLAG} $SUPPLY_TYPE $F $G $L $C"
COM="${CLI} ${FLAGS}"
if OUT=`${COM}`
then
whiptail --title "Configuration written successfully" \
--backtitle "${BACKTITLE}" \
--msgbox "MoPi has been configured :-)\n\n(${COM})" 20 70 1
save_local_config ${S_FLAG} "$FLAGS"
break
else
whiptail --title "Retry?" --backtitle "${BACKTITLE}" \
--yesno "Problem writing config -- retry?\n\n(${COM}\n ${OUT})" \
25 70 1 || {
whiptail --title ":-(" --backtitle "${BACKTITLE}" \
--msgbox "Configuration failed" 20 70 1
break
}
fi
done
}
# main loop
clear
while true; do
SEL=$(whiptail --title "MoPi Configuration Tool (mopi)" \
--menu "Configure MoPi" \
$WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT \
--cancel-button Finish --ok-button Select \
"1 Configure Both" "Configure both power supplies" \
"2 Configure Supply 1" "Configure supply 1" \
"3 Configure Supply 2" "Configure supply 2" \
"4 Status" "Show current status from your MoPi board" \
"5 Show Local Config" "Show local configuration" \
"6 Clear Local Config" "Clear local configuration" \
"7 Show Log Entries" "Show the most recent log entries" \
"8 About MoPi" "Information about this configuration tool" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
exit 0
elif [ $RET -eq 0 ]; then
case "$SEL" in
1\ *) do_supply_config -b ;;
2\ *) do_supply_config -1 ;;
3\ *) do_supply_config -2 ;;
4\ *) whiptail --title "MoPi Status" --msgbox \
"`${CLI} -e |pr -e -t2 -w76 |expand`" $WT_HEIGHT 78 1 ;;
5\ *) whiptail --title "MoPi Local Config" --msgbox \
"`get_local_config`" 20 60 1 ;;
6\ *) whiptail --title "Clear Local Config" --yesno \
"This will DELETE all local configuration -- are you sure?!" \
24 70 1 && \
delete_local_config ;;
7\ *) whiptail --title "simbamon Log Entries" --msgbox \
"`do_log_grep`" $WT_HEIGHT $WT_WIDTH 1 ;;
8\ *) do_about ;;
*) whiptail --msgbox "Error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $SEL" 20 60 1
else
exit 5
fi
done