#!/usr/bin/webif-page
<?
. "/usr/lib/webif/webif.sh" 
###################################################################
# Powercontrol Webinterface
#
# Description:
#        This Page allows you to control the ports of a serial
#	 connected µC like the Microchip 16F819
# Author:
#        Matthias Beckert <m.beckert@tu-bs.de>
#
# Configuration files referenced:
#   none
#

header "System" "Powercontrol" "@TR<<Powercontrol>>" '' ''
i=0
### Check if the "Set" button was pressed and set the new output
if [ "$FORM_set" -gt "0" ]; then
	while [ "$i" -lt "$FORM_set" ]
	do
		eval state="\$FORM_port_$i"
		p=`echo $(($i+65)) | awk '{printf "%c\n", $1}'`
		echo "S" >>/dev/ttyS0
		echo $p >>/dev/ttyS0
		echo $state >>/dev/ttyS0
		usleep 100
		echo "Port "$p" set to "$state"<br>"
		i=$(($i+1))
	done
echo "<br>"
fi
### Check if the "Reset" button was pressed and reset the Ports
if [ "$FORM_reset" -gt "0" ]; then
	while [ "$i" -lt "$FORM_reset" ]
	do
		eval checked="\$FORM_reset_$i"
		p=`echo $(($i+65)) | awk '{printf "%c\n", $1}'`
		if [ "$checked" -eq "1" ]; then
			eval time="\$FORM_time_$i"
			echo "Resetting Port "$p" for "$time" seconds<br>"
			./resetport.sh $p $time &
			usleep 500000
		fi
		i=$(($i+1))
	done
echo "Do not change the states of the resetted ports, while the ports are set to 0 <br><br>"
fi
### Check if the "Name" button was pressed
if [ "$FORM_portname" -gt "0" ]; then
	if [ -f "./portnames" ]; then
		rm portnames
	fi
	while [ "$i" -lt "$FORM_portname" ]
	do
		eval name="\$FORM_portname_$i"
		echo $name >>portnames
		i=$(($i+1))
	done
fi
### Read the portstate from the µC into a textfile (is needed, because of some tricky timing problem with the µC)		
if [ -f "./output" ]; then 
	rm output
fi
touch output
echo "G">>/dev/ttyS0
while [ "$LINE" != "9" ]
do
read LINE < /dev/ttyS0
    echo $LINE >>output
done
### Read the textfile and count the number of the ports
i=0
while
read line
do
	eval name$i=\"$line\"
	read line
	eval port$i=\"$line\"
	i=$(($i+1))
done < ./output
i=$(($i-1))

### Read the port discribtion
n=0
if [ ! -f "./portnames" ]; then
	touch portnames
	while [ "$n" -lt "$i" ]
	do
		eval line=\"Port $n\"
		eval disc$n=\"$line\"
		echo $line>>portnames
		n=$(($n+1))
	done
else
	while
	read line
	do
		eval disc$n=\"$line\"
		n=$(($n+1))
	done < ./portnames
fi
### Start the HTML Form i is the number of connected ports
x=0
echo "<form method=\"get\" action=\"./system-powercontrol.sh\" name=\"config\">"
display_form <<EOF
start_form|@TR<<Port Configuration>>
EOF
### Generate the controlpanel
echo "<tr><td width=\"20%\"><b>Port</b></td><td><b>Discription</b></td><td width=\"50%\"><b>Control</b></td></tr>"
while [ $x -lt $i ]
do
	eval line="\$port$x"
	eval ident="\$name$x"
	eval pn="\$disc$x"
	echo "<tr><td width=\"20%\">Port "$ident"</td><td><input size=\"10\" name=\"portname_"$x"\" value=\""$pn"\"></td><td width=\"50%\">"
	if [ "$line" -eq "1" ]; then
		echo "<input type=\"radio\" name=\"port_"$x"\" value=\"1\" checked=\"checked\" />
		On
		<input type=\"radio\" name=\"port_"$x"\" value=\"0\"  />
		Off
		<input name=\"reset_"$x"\" value=\"1\" type=\"checkbox\"/>
		Reset
		<select name=\"time_"$x"\" size=\"1\">"
		t=1
		while [ $t -le 60 ]
		do
			echo "<option>"$t"</option>"
			t=$(($t+1))
		done 
		echo "</select></td></tr>"
	fi
	if [ "$line" -eq "0" ]; then
		echo "<input type=\"radio\" name=\"port_"$x"\" value=\"1\" />
	      	On                                                                     
	       	<input type=\"radio\" name=\"port_"$x"\" value=\"0\" checked=\"checked\"  />                  
	       	Off 
		<input disabled=\"disabled\" name=\"reset_"$x"\" value=\"1\" type=\"checkbox\"/>
		</td></tr>"		
	fi
	x=$(($x+1))
done
display_form <<EOF
helpitem|Port Configuration
helptext|HelpText Port Configuration#Use the radiobuttons to set the state of the appending port. Every time you load this page the radiobuttons will show you the current state for every port.
end_form
EOF
echo "<button title=\"set\" value=\""$i"\" name=\"set\">Set</button>  "
echo "<button title=\"reset\" value=\""$i"\" name=\"reset\">Reset</button>  <button title=\"portname\" value=\""$i"\" name=\"portname\">Names</button> </form>"
footer ?>
<!--
##WEBIF:name:System:950:Powercontrol
-->


