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
|
#Mise à jour de la dernière détection de capteur
$requete = "UPDATE capteurs SET timestamp_detection = \"$utc_timestamp\" WHERE id_capteur = \"$id_capteur\"";
$sth = $dbh->do($requete) or die "Echec Requete $requete : $DBI::errstr\n";
#Executer la fonction conditions_actions
&conditions_actions();
#Function to test conditions in conditions table
sub conditions_actions {
my @tab = ();
my $result = ();
my $id_action = ();
my $id_capteur = $_;
my $action = ();
my $msg = ();
my $i = 0;
my $requete = "SELECT * FROM Conditions";
my $sth = $dbh->prepare($requete);
$sth->execute() or die "Echec Requete $requete : $DBI::errstr\n";
if ($id_capteur eq $tab[$i]->{id_action}) {
#Execute the loop until the array is not empty
while (my @result = $sth->fetchrow_array()) {
$tab[$i] = { id_condition => $result[0], capteur => $result[1], type_mesure => $result[2], condition => $result[3], valeur => $result[4], id_action => $result[5]};
$i ++;
}
$sth->finish();
#Test, for each line in $tab, if conditions are ok or not
for $i (0 .. $#tab) {
#Store instantaneous value of sensor in variable
$requete = "SELECT valeur_mesure, timestamp_mesure FROM releves WHERE id_capteur = \"$tab[$i]->{capteur}\" AND type_mesure = \"$tab[$i]->{type_mesure}\" ORDER BY timestamp_mesure DESC LIMIT 1";
$sth = $dbh->prepare($requete);
$sth->execute() or die "Echec Requete $requete : $DBI::errstr";
my @result = $sth->fetchrow_array();
$result = $result[0];
$sth->finish();
print "$id_capteur";
print "$result$tab[$i]->{condition}$tab[$i]->{valeur}\n";
if (eval"$result $tab[$i]->{condition} $tab[$i]->{valeur}") {
print "condition est vraie\n";
#Execute actions when condition is true
$id_action = ($tab[$i]->{id_action});
#Query to return action to execute
$requete = "SELECT action FROM Actions WHERE id_action = \"$id_action\"";
$sth = $dbh->prepare($requete);
$sth->execute() or die "Echec Requete $requete : $DBI::errstr";
my @result = $sth->fetchrow_array();
$action = $result[0];
$sth->finish();
#Execute action
system $action;
print " Action executée\n";
return 0;
}
else
{
print "condition est fausse\n" ;
}
}
return 0;
}
}; |
Partager