/* Surveille la température de la cuve de mineurs et déclenche différentes alarmes */ #include #include #include #include #include #include #include #include #include using namespace std; /* Obtient le statut de la température de la cuve et la différence de température avec la température problème */ static string cuve_checktemp(const char *hangar, const char *port, double &diff, bool &failcon) { glocal string ret; glocal diff; glocal bool ok = false; glocal failcon; failcon = false; (hangar,port,10); send ("checklowtemp\n"); if (!splitline(line,glocal.ret,glocal.diff)){ tlmp_warning ("Ligne invalide: %s\n",line); } glocal.ok = true; end = true; glocal.failcon = true; if (!glocal.ok) tlmp_error ("Connexion imcomplète\n"); return glocal.ret; } int main (int argc, char *argv[]) { glocal int ret = -1; glocal const char *hangar = "hangar"; glocal const char *port = "43"; glocal const char *unixpath = "/var/run/cuvealert.sock"; glocal bool silent = false; glocal unsigned delai = 60; glocal bool control = false; glocal const char *command = "status"; glocal.ret = (argc,argv); setproginfo ("cuvealert","0.0","Surveillance de la cuve des mineurs"); setarg ('h',"hangar","Serveur gérant les mineurs",glocal.hangar,false); setarg ('p',"port","Port TCP du serveur",glocal.port,false); setarg ('s',"silent","Aucun affichage",glocal.silent,false); setarg ('u',"unixpath","Port de contrôle",glocal.unixpath,false); setgrouparg ("Controle"); setarg ('c',"control","Communique avec le serveur",glocal.control,false); setarg (' ',"command","Commande à envoyer au serveur",glocal.command,false); setgrouparg ("Misc."); setarg (' ',"delai","Test la température de la cuve à toutes les X secondes",glocal.delai,false); int ret = 0; if (glocal.control){ ("unix:",glocal.unixpath,5); sendf ("%s\n",glocal.command); printf ("%s\n",line); }else{ glocal string last; glocal bool waitgood = false; glocal bool nosound = false; glocal bool noalarm = false; { double diff; bool failcon; string status = cuve_checktemp (glocal.hangar,glocal.port,diff,failcon); if (status == "low"){ glocal.waitgood = true; glocal.nosound = true; printf ("Attend que la température soit rétablie\n"); } } (string_f("unix:%s",glocal.unixpath),5); if (is_eq(line,"quit")){ endserver = true; }else if (is_eq(line,"testok")){ system ("/home/pi/testok.sh"); }else if (is_eq(line,"alarm-sound")){ system ("/home/pi/alarm-sound"); }else if (is_eq(line,"alarm")){ system ("/home/pi/alarm test du serveur"); }else if (is_eq(line,"noalarm")){ glocal.noalarm = true; }else{ sendf ("waitgood = %d\n",glocal.waitgood); sendf ("noalarm = %d\n",glocal.noalarm); sendf ("Dernière lecture = %s\n",glocal.last.c_str()); } endclient = true; if (o.is_ok()){ (glocal.delai); double diff; bool failcon; string status = cuve_checktemp (glocal.hangar,glocal.port,diff,failcon); time_t ti = time(nullptr); auto tt = localtime(&ti); glocal.last = string_f ("%04d-%02d-%02d %02d:%02d:%02d " ,tt->tm_year+1900,tt->tm_mon+1,tt->tm_mday ,tt->tm_hour,tt->tm_min,tt->tm_sec); if (status == "low"){ glocal.last += string_f (" %s %6.2lf ",status.c_str(),diff); if (!glocal.noalarm){ if (!glocal.nosound){ system("/home/pi/alarm-sound"); } if (!glocal.waitgood){ system ("/home/pi/alarm basse température"); glocal.waitgood = true; } } }else{ glocal.last += string_f (" %s %6.2lf %s %s ",status.c_str(),diff,glocal.waitgood ? "attend" : "", glocal.nosound ? "silencieux" : ""); if (status == ""){ if (failcon) tlmp_error ("Échec de connexion\n"); }else if (diff > 5){ glocal.noalarm = false; if (glocal.waitgood){ printf ("\nalarm réactivée\n"); glocal.waitgood = false; glocal.nosound = false; } } } if (!glocal.silent){ printf ("\r%s",glocal.last.c_str()); fflush (stdout); } netevent_loop(o,idle); }else{ tlmp_error ("Ne peut établir le socket (%s)\n",strerror(errno)); } } return ret; return glocal.ret; }