Parte 1
Premessa :
io lavoro come root , se lo fate da utenti inserite sudo prima di ogni comando.
mi sembra che bc non sia presente di default, da terminale date il comando bc -v , se
risponde con comando non trovato dovere dare un 'apt install bc'
In pratica ho creato un programma bash così fatto :
Codice: Seleziona tutto
#!/bin/bash
#
LIM3ORE=3300 # 3300 limite su 3 ore pari a 3kw + 10%
LIM2MIN=4191 # 4191 limite 2minuti pari 3.3kw + 27%
FILEPOWER=/run/shm/power
FILEPOWERHI=/run/shm/pow_hi
FILEENERGYHI=/run/shm/ene_hi
POW=0
IP1=`cat /run/shm/123s_LIVEMEMORY.json | cut -d, -f16 | cut -d: -f2`
CP1=`cat /run/shm/metern2.txt | grep '*W)'`
CP2=`echo $CP1 | cut -d'(' -f2 | cut -d* -f1`
POW=`echo "$CP2 - $IP1" | bc`
# potenza nell'istante di lettura
POW1=${POW/.*}
CE1=`cat /run/shm/metern2.txt | grep '*Wh)'`
CE2=`echo $CE1 | cut -d'(' -f2 | cut -d* -f1`
CEOLD=`cat $FILEENERGYHI`
POE=`echo "($CE2 -$CEOLD)*60 - $IP1" | bc`
# potenza media in un minuto
POE1=${POE/.*}
#echo $POE1
#echo $POW1
# prendo il maggiore tra i due dati
if [ $POE1 -lt $POW1 ]; then
POW1=$POE1
fi
# il minimo è zero per non avere valori negativi
if [ $POW1 -lt 0 ]; then
POW1=0
fi
# inserisco l'ultimo dato e cancello il piu' vecchio
echo $POW1 >> $FILEPOWER
NDATA=`wc -l < $FILEPOWER`
if [ $NDATA -gt 180 ]; then
sed -i '1d' $FILEPOWER
fi
#
# parte potenza assorbita oltre i limiti
#
PLAST=`cat $FILEPOWERHI`
if [ $PLAST -gt $LIM2MIN ] && [ $POW1 -gt $LIM2MIN ]; then
# comandi da eseguire in caso di assorbimento istantaneo oltre soglia
/var/www/comapps/telegram.sh "Allarme Distacco Contatore picco potenza $POW1 W"
fi
PMED=`awk '{s+=$1} END {if(NR>0) print s/NR}' $FILEPOWER | cut -d. -f1`
if [ $PMED -gt $LIM3ORE ]; then
# comandi da eseguire in caso di assorbimento prolungato oltre il 10%
/var/www/comapps/telegram.sh "Allarme Distacco Contatore media ultime 3 ore $PMED W"
fi
echo $POW1 > $FILEPOWERHI
echo $CE2 > $FILEENERGYHI
#
create il file ed inserite il codice sopra :
nano /var/www/comapps/power_now.sh
date i permessi di esecuzione:
chmod +x /var/www/comapps/power_now.sh
lo script viene lanciato ogni minuto da cron , aggiungere quindi la seguente riga :
* * * * * /var/www/comapps/power_now.sh >/dev/null 2>&1
nel file /var/spool/cron/crontabs/root
che, salvo altre vostre modifiche deve risultare così :
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.lGg0If/crontab installed on Sun Apr 30 14:30:25 2017)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * /var/www/comapps/power_now.sh >/dev/null 2>&1
ok, ben fatto, se tutto è ok potete testare con i seguenti comandi:
cat /run/shm/pow_hi
cat /run/shm/ene_hi
che vi ritornano dei numeri.
Fine parte 1