prosegue da sopra....
Quindi ho creato /www/cgi-bin/webif/centralina/temperature.sh
#!/usr/bin/webif-page
<?
. /usr/lib/webif/webif.sh
#
############################################################################
#
# Descrizione:
# Mostra i grafici delle temperature
#
#
# lupin73 --->Nabuk<----
#############################################################################
header "Info" "System" "<img src=\"/images/Temp.gif\">@TR<< Temperature >>"
indirizzo="$(uci get network.lan.ipaddr)"
#############################################################################
echo "temp" > /etc/centralina/storico_check.ini
img="/temperature.png"
pidowread=`pidof owread`
if equal $pidowread "";then
temp1=`/mnt/sda2/usb/usr/bin/owread /28.08A696030000/temperature`
temp1=$(printf "%1.1f" $temp1)
else
echo "<center><strong>Aggiornamento database in corso!</strong><br>Al termine verrà creato il grafico!"
while [ "$pidowread" != "" ]; do
sleep 1
pidowread=`pidof owread`
done
fi
pidowread=`pidof owread`
if equal $pidowread "";then
temp2=`/mnt/sda2/usb/usr/bin/owread /28.CBDE96030000/temperature`
temp2=$(printf "%1.1f" $temp2)
else
echo "<center><strong>Aggiornamento database in corso!</strong><br>Al termine verrà creato il grafico!"
while [ "$pidowread" != "" ]; do
sleep 1
pidowread=`pidof owread`
done
fi
pidowread=`pidof owread`
if equal $pidowread "";then
temp3=`/mnt/sda2/usb/usr/bin/owread /28.92AF96030000/temperature`
temp3=$(printf "%1.1f" $temp3)
else
echo "<center><strong>Aggiornamento database in corso!</strong><br>Al termine verrà creato il grafico!"
while [ "$pidowread" != "" ]; do
sleep 1
pidowread=`pidof owread`
done
fi
pidowread=`pidof owread`
if equal $pidowread "";then
temp4=`/mnt/sda2/usb/usr/bin/owread /28.D6CF96030000/temperature`
temp4=$(printf "%1.1f" $temp4)
else
echo "<center><strong>Aggiornamento database in corso!</strong><br>Al termine verrà creato il grafico!"
while [ "$pidowread" != "" ]; do
sleep 1
pidowread=`pidof owread`
done
fi
if equal $FORM_grafici "Seleziona_data" || equal $FORM_tipo_grafici "Seleziona_ambiente" ;then
echo "<center><strong>ATTENZIONE!</strong><br>Devi selezionare almeno una data e un tipo per generare il grafico!"
cat <<EOF
<script language="JavaScript" type="text/javascript">
setTimeout('top.location.href=\"/cgi-bin/webif/centralina/temperature.sh\"',"3000")
</script>
EOF
else
if equal $FORM_grafici "1_ora";then
echo $FORM_grafici > /etc/storico.ini
rm /mnt/sda2/usb/database_rrdtool/temperature.png
echo "<center><img src=\"../../../images/clessidra.gif\">"
echo "<center>Attendi per favore sto creando il grafico!"
/usr/sbin/sensori.sh 1 h 1 $FORM_tipo_grafici > /dev/null
cat <<EOF
<script language="JavaScript" type="text/javascript">
setTimeout('top.location.href=\"/cgi-bin/webif/centralina/temperature.sh\"',"1500")
</script>
EOF
fi
if equal $FORM_grafici "6_ore";then
echo $FORM_grafici > /etc/storico.ini
rm /mnt/sda2/usb/database_rrdtool/temperature.png
echo "<center><img src=\"../../../images/clessidra.gif\">"
echo "<center>Attendi per favore sto creando il grafico!"
/usr/sbin/sensori.sh 6 h 2 $FORM_tipo_grafici > /dev/null
cat <<EOF
<script language="JavaScript" type="text/javascript">
setTimeout('top.location.href=\"/cgi-bin/webif/centralina/temperature.sh\"',"1500")
</script>
EOF
fi
if equal $FORM_grafici "12_ore";then
echo $FORM_grafici > /etc/storico.ini
rm /mnt/sda2/usb/database_rrdtool/temperature.png
echo "<center><img src=\"../../../images/clessidra.gif\">"
echo "<center>Attendi per favore sto creando il grafico!"
/usr/sbin/sensori.sh 12 h 3 $FORM_tipo_grafici > /dev/null
cat <<EOF
<script language="JavaScript" type="text/javascript">
setTimeout('top.location.href=\"/cgi-bin/webif/centralina/temperature.sh\"',"1500")
</script>
EOF
fi
if equal $FORM_grafici "24_ore";then
echo $FORM_grafici > /etc/storico.ini
rm /mnt/sda2/usb/software/database_rrdtool/temperature.png
echo "<center><img src=\"../../../images/clessidra.gif\">"
echo "<center>Attendi per favore sto creando il grafico!"
/usr/sbin/sensori.sh 24 h 4 $FORM_tipo_grafici > /dev/null
cat <<EOF
<script language="JavaScript" type="text/javascript">
setTimeout('top.location.href=\"/cgi-bin/webif/centralina/temperature.sh\"',"1500")
</script>
EOF
fi
if equal $FORM_grafici "1_settimana";then
echo $FORM_grafici > /etc/storico.ini
rm /mnt/sda2/usb/database_rrdtool/temperature.png
echo "<center><img src=\"../../../images/clessidra.gif\">"
echo "<center>Attendi per favore sto creando il grafico!"
/usr/sbin/sensori.sh 1 w 5 $FORM_tipo_grafici > /dev/null
cat <<EOF
<script language="JavaScript" type="text/javascript">
setTimeout('top.location.href=\"/cgi-bin/webif/centralina/temperature.sh\"',"1500")
</script>
EOF
fi
if equal $FORM_grafici "1_mese";then
echo $FORM_grafici > /etc/storico.ini
rm /mnt/sda2/usb/database_rrdtool/temperature.png
echo "<center><img src=\"../../../images/clessidra.gif\">"
echo "<center>Attendi per favore sto creando il grafico!"
/usr/sbin/sensori.sh 1 m 6 $FORM_tipo_grafici > /dev/null
cat <<EOF
<script language="JavaScript" type="text/javascript">
setTimeout('top.location.href=\"/cgi-bin/webif/centralina/temperature.sh\"',"1500")
</script>
EOF
fi
if equal $FORM_grafici "1_anno";then
echo $FORM_grafici > /etc/storico.ini
rm /mnt/sda2/usb/database_rrdtool/temperature.png
echo "<center><img src=\"../../../images/clessidra.gif\">"
echo "<center>Attendi per favore sto creando il grafico!"
/usr/sbin/sensori.sh 1 y 7 $FORM_tipo_grafici > /dev/null
cat <<EOF
<script language="JavaScript" type="text/javascript">
setTimeout('top.location.href=\"/cgi-bin/webif/centralina/temperature.sh\"',"1500")
</script>
EOF
fi
cat <<EOF
<script type="text/javascript">
function show_confirm()
{
var r=confirm(" Questa operazione per intervalli superiori\n ad un mese può durare molti minuti!\n Vuoi continuare?");
if (r==true)
{
window.open('http://$indirizzo/cgi-bin/webif/centralina/storico.sh?id=`cat /etc/storico.ini`','finestra','width=600,height=400, scrollbars=yes,top=200,right=50');
}
else
{
alert("Se avevi altro da fare hai fatto una scelta saggia!\n:)");
}
}
</script>
<center><table>
<tr>
<td><a onclick="show_confirm()"><img src="$img" title="Fai click per visualizzare lo storico"></a></td>
</tr>
</table>
EOF
cat <<EOF
<form method="get" action="temperature.sh">
<select name="grafici" >
EOF
for n in 1_ora 6_ore 12_ore 24_ore 1_settimana 1_mese 1_anno Seleziona_data;do
cat <<EOF
<option value="$n" selected="selected">$n</option>
EOF
done
cat <<EOF
</select>
<select name="tipo_grafici">
EOF
for n in Tutti Interno Esterno Boiler Freezer Seleziona_ambiente;do
cat <<EOF
<option value="$n" selected="selected">$n</option>
EOF
done
cat <<EOF
<input type="submit" name="$n" value="Crea">
</select>
</form>
<br><br>
EOF
display_form <<EOF
start_form|@TR<<Lettura sensori>>
end_form
EOF
cat <<EOF
<tr><td><center><strong><font size="2">Interno=</td><font color="#0000FF">$temp4°</font><td>  Esterno=<font color="#FF4500">$temp3°</font>  Boiler=<font color="#00FF00">$temp2°</font>  Freezer=<font color="#FFBC14:">$temp1°</font></tr></td>
<br><br>
EOF
fi
footer
?>
che visualizza il grafico in base al tempo selezionato.
Volevo avere anche un controllo più particolareggiato dei dati del database e quindi ho creato una pagina html che mi visualizzi lo storico delle temperature cliccando sull'immagine del grafico e che potesse essere stampato.
Ho creato quindi /usr/sbin/crea_storico.sh che mi costruisce la tabella che verrà visualizzata nella webif (n.b. ho utilizzato bash per operare sugli array)
#!/bin/bash
#Pulisco la ram
ram_hot="$(free | grep "Mem" | awk '{ print $3 }')"
if [ $ram_hot -ge 25000 ];then
sync; echo 3 > /proc/sys/vm/drop_caches
fi
case $1 in
"1_ora")
R=5
S="-1h"
leg="ultima ora"
;;
"6_ore")
R=5
S="-6h"
leg="ultime 6 ore"
;;
"12_ore")
R=5
S="-12h"
leg="ultime 12 ore"
;;
"24_ore")
R=5
S="-24h"
leg="ultime 24 ore"
;;
"1_settimana")
R=60
S="-1w"
leg="ultima settimana"
;;
"1_mese")
R=60
S="-1m"
leg="ultimo mese"
;;
"1_anno")
R=60
S="-1y"
leg="ultimo anno"
esac
cal_exp ()
{
case $1 in
"+00")
S=$(echo "scale=3; $2*1.00" | bc)
;;
"+01")
S=$(echo "scale=3; $2*10.00" | bc)
;;
"+02")
S=$(echo "scale=3; $2*100.00" | bc)
;;
"+03")
S=$(echo "scale=3; $2*1000.00" | bc)
esac
return
}
case $2 in
"temp")
SOURCE="/mnt/sda2/usb/database_rrdtool/temperatura.rrd"
declare DATA=(`rrdtool fetch $SOURCE AVERAGE -r $R -s $S | awk '{ print $1 }' | sed -e 's/://' | cut -f 1 -de | tail -n +2`)
declare S1=(`rrdtool fetch $SOURCE AVERAGE -r $R -s $S | awk '{ print $2 }' | cut -f 1 -de | tail -n +2`)
declare S2=(`rrdtool fetch $SOURCE AVERAGE -r $R -s $S | awk '{ print $3 }' | cut -f 1 -de | tail -n +2`)
declare S3=(`rrdtool fetch $SOURCE AVERAGE -r $R -s $S | awk '{ print $4 }' | cut -f 1 -de | tail -n +2`)
declare S4=(`rrdtool fetch $SOURCE AVERAGE -r $R -s $S | awk '{ print $5 }' | cut -f 1 -de | tail -n +2`)
declare EXP1=(`rrdtool fetch $SOURCE AVERAGE -r $R -s $S | awk '{ print $2 }' | cut -f 2 -de | tail -n +2`)
declare EXP2=(`rrdtool fetch $SOURCE AVERAGE -r $R -s $S | awk '{ print $3 }' | cut -f 2 -de | tail -n +2`)
declare EXP3=(`rrdtool fetch $SOURCE AVERAGE -r $R -s $S | awk '{ print $4 }' | cut -f 2 -de | tail -n +2`)
declare EXP4=(`rrdtool fetch $SOURCE AVERAGE -r $R -s $S | awk '{ print $5 }' | cut -f 2 -de | tail -n +2`)
declare TESTA=(Data Interno Boiler Esterno Freezer)
ld=$((${#DATA[@]}-2))
start=0
end=5
echo "<TR>"
echo "<td colspan="5" align="center" valign="middle">"
echo "<b>Storico $leg</b>"
echo "</TR>"
echo "<TR>"
for n in $(seq 1 $end); do
echo "<TD><CENTER>${TESTA[$n-1]}</CENTER></TD>"
done
for i in $(seq $start $ld); do
if [ $n -eq $end ];then
echo "</TR>"
echo "<TR>"
fi
EPOCH=${DATA[$i]}
DATA=$(awk "BEGIN {print strftime(\"%c\",$EPOCH)}")
S1=$(printf "%1.3f" ${S1[$i]})
S2=$(printf "%1.3f" ${S2[$i]})
S3=$(printf "%1.3f" ${S3[$i]})
S4=$(printf "%1.3f" ${S4[$i]})
#for k in 1 2 3 4; do
# S[k]=$(printf "%1.3f" ${S[k][$i]})
#done
EX1=${EXP1[$i]}
EX2=${EXP2[$i]}
EX3=${EXP3[$i]}
EX4=${EXP4[$i]}
for n in $(seq 1 $end); do
if [ $n -eq 1 ] && [ "DATA" != "nan" ] && [ "$S1" != "nan" ] && [ "$S2" != "nan" ] && [ "$S3" != "nan" ] && [ "$S4" != "nan" ];then
echo "<TD><CENTER>$DATA</CENTER></TD>"
fi
if [ $n -eq 2 ] && [ "$S1" != "nan" ];then
S=$S1
cal_exp $EX1 $S
echo "<TD><CENTER>$S</CENTER></TD>"
fi
if [ $n -eq 3 ] && [ "$S2" != "nan" ];then
S=$S2
cal_exp $EX2 $S
echo "<TD><CENTER>$S</CENTER></TD>"
fi
if [ $n -eq 4 ] && [ "$S3" != "nan" ];then
S=$S3
cal_exp $EX3 $S
echo "<TD><CENTER>$S</CENTER></TD>"
fi
if [ $n -eq 5 ] && [ "$S4" != "nan" ];then
S=$S4
cal_exp $EX4 $S
echo "<TD><CENTER>$S</CENTER></TD>"
fi
done
if [ $i -eq $ld ];then
echo "</TR>"
fi
done
;;
"pot")
SOURCE="/mnt/sda2/usb/database_rrdtool/powerw.rrd"
declare DATA=(`rrdtool fetch $SOURCE AVERAGE -r $R -s $S | awk '{ print $1 }' | sed -e 's/://' | cut -f 1 -de | tail -n +2`)
declare S1=(`rrdtool fetch $SOURCE AVERAGE -r $R -s $S | awk '{ print $2 }' | cut -f 1 -de | tail -n +2`)
declare EXP1=(`rrdtool fetch $SOURCE AVERAGE -r $R -s $S | awk '{ print $2 }' | cut -f 2 -de | tail -n +2`)
declare TESTA=(Data Watt)
ld=$((${#DATA[@]}-2))
start=0
end=2
echo "<TR>"
echo "<td colspan="5" align="center" valign="middle">"
echo "<b>Storico $leg</b>"
echo "</TR>"
echo "<TR>"
for n in $(seq 1 $end); do
echo "<TD><CENTER>${TESTA[$n-1]}</CENTER></TD>"
done
for i in $(seq $start $ld); do
if [ $n -eq $end ];then
echo "</TR>"
echo "<TR>"
fi
EPOCH=${DATA[$i]}
DATA=$(awk "BEGIN {print strftime(\"%c\",$EPOCH)}")
S1=$(printf "%1.3f" ${S1[$i]})
EX1=${EXP1[$i]}
for n in $(seq 1 $end); do
if [ $n -eq 1 ] && [ "DATA" != "nan" ] && [ "$S1" != "nan" ] && [ "$S2" != "nan" ] && [ "$S3" != "nan" ] && [ "$S4" != "nan" ];then
echo "<TD><CENTER>$DATA</CENTER></TD>"
fi
if [ $n -eq 2 ] && [ "$S1" != "nan" ];then
S=$S1
cal_exp $EX1 $S
echo "<TD><CENTER>$S</CENTER></TD>"
fi
done
if [ $i -eq $ld ];then
echo "</TR>"
fi
done
esac
sync; echo 3 > /proc/sys/vm/drop_caches
exit 0
N.B Questo script mi crea anche lo storico dei kw ma questo lo vedremo prossimamente.
La pagina che visualizzerà lo storico:
#!/usr/bin/webif-page
<?
. /usr/lib/webif/webif.sh
cat <<EOF
Content-Type: text/html; charset=UTF-8
Pragma: no-cache
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<script type="text/javascript">
function load()
{
setTimeout('top.location.href=\"/cgi-bin/webif/centralina/storico.sh\"',"0")
}
function alertMsg()
{
document.write("<center><img src=\"../../../images/clessidra_storico.gif\">");
document.write("<center>Attendi per favore sto leggendo i dati!");
document.write("<center>Per intervalli di tempo superiori ad 1 mese può richiedere circa 15 minuti!");
document.write("<center>NON CHIUDERE LA FINESTRA!")
}
function timeMsg()
{
var t=setTimeout("alertMsg()",0);
}
</script>
</head>
EOF
indirizzo_test="$(echo $HTTP_REFERER | awk 'BEGIN { FS="/" } { print $3 }')"
if equal $indirizzo_test "127.0.0.1:12345" ; then
indirizzo="127.0.0.1"
else
indirizzo="$indirizzo_test"
fi
#check_s="`cat /etc/centralina/storico_check.ini`"
if equal $GET_id "1_ora"; then
cat <<EOF
<script language="JavaScript" type="text/javascript">
alertMsg()
</script>
<body onload="load()">
EOF
check_s="`cat /etc/centralina/storico_check.ini`"
/usr/sbin/crea_storico.sh $GET_id $check_s > /etc/tabella_storico
fi
if equal $GET_id "6_ore"; then
cat <<EOF
<script language="JavaScript" type="text/javascript">
alertMsg()
</script>
<body onload="load()">
EOF
check_s="`cat /etc/centralina/storico_check.ini`"
/usr/sbin/crea_storico.sh $GET_id $check_s > /etc/tabella_storico
fi
if equal $GET_id "12_ore"; then
cat <<EOF
<script language="JavaScript" type="text/javascript">
alertMsg()
</script>
<body onload="load()">
EOF
check_s="`cat /etc/centralina/storico_check.ini`"
/usr/sbin/crea_storico.sh $GET_id $check_s > /etc/tabella_storico
fi
if equal $GET_id "24_ore"; then
cat <<EOF
<script language="JavaScript" type="text/javascript">
alertMsg()
</script>
<body onload="load()">
EOF
check_s="`cat /etc/centralina/storico_check.ini`"
/usr/sbin/crea_storico.sh $GET_id $check_s > /etc/tabella_storico
fi
if equal $GET_id "1_settimana"; then
cat <<EOF
<script language="JavaScript" type="text/javascript">
alertMsg()
</script>
<body onload="load()">
EOF
/usr/sbin/crea_storico.sh $GET_id > /etc/tabella_storico
fi
if equal $GET_id "1_mese"; then
cat <<EOF
<script language="JavaScript" type="text/javascript">
alertMsg()
</script>
<body onload="load()">
EOF
check_s="`cat /etc/centralina/storico_check.ini`"
/usr/sbin/crea_storico.sh $GET_id $check_s > /etc/tabella_storico
fi
if equal $GET_id "1_anno"; then
cat <<EOF
<script language="JavaScript" type="text/javascript">
alertMsg()
</script>
<body onload="load()">
EOF
check_s="`cat /etc/centralina/storico_check.ini`"
/usr/sbin/crea_storico.sh $GET_id $check_s > /etc/tabella_storico
fi
cat <<EOF
<script type="text/javascript">
function printpage()
{
window.print();
}
</script>
<input type="button" value="Stampa" onclick="printpage()" /><br><br>
EOF
echo "<CENTER><TABLE BORDER=1 BORDERCOLOR="#000000" CELLPADDING=2 CELLSPACING=0>"
echo "`cat /etc/tabella_storico`"
echo "</TABLE>"
echo "</body>"
echo "</html>"
?>
<!--
-->
</html>
Scusate eventuali errori ma sono andato di fretta e comunque il progetto è ancora in fase di sviluppo, quando troverò un altro po' di tempo posterò la parte relativa al contatore elettrico.