/******************************************* * Look the parameters in the CAEN Mail_box * * * *******************************************/ #include #include #include #include #include #include #include #include "a200.h" /* definitions relative to the CAEN system */ #include "a200.hmn" #include "../lib/mylib1.h" #include "../dbase/hv_dbase.h" #define SLEEP_MAX 6 /* nr of cycles for which a chan. is put to sleep */ #define SECONDS #define MINUTES * 60 #define FTP_TIMEOUT 1010 /* timeout signal code */ #define TWENTY_S 20 /* timeout value ( in seconds ) */ SY227_MBOX *sy227_mod; int sem_227mod_id; SY127_MBOX *sy127_mod; hv_mod_struct *mid; int sem_127mod_id; char *caller; char line[80], cline[80], tmpfile[64], tmpfile_h[64]; int n_order, alarm[40], sleep_count[40]; int cpu_nr, tcp_ip_nr; FILE *fp, *fp1, *fp2, *fopen(); char *outfile = "tubi_sitter.log"; char *outfile_h = "tubi_sitter_hosp.log"; char *outfile_m = "tubi_sitter.msg"; char *tmpfile_0 = "t_s_temp"; char *tmpfile_h_0 = "t_s_h_temp"; char *remfile = "tubi_sitter.log"; char *remfile_h = "tubi_sitter_hosp.log"; int pout; char *scatola_nera = "scatola_nera"; int hosp; int nout1, nout2, nout3; int err1, err2, err3; int to_be_sent, to_be_sent_h; int ftp_ok; sighand(signal) register int signal; { if ( signal == FTP_TIMEOUT ){ ftp_ok = 0; printf(" signal %d received\n", signal); } else if ( signal >= 2 && signal <= 3){ /* ^C or ^E ? */ A200_end(); sem_unl_del(sem_127mod_id, "sy127mod", caller); munlink(sy127_mod); sem_unl(sem_227mod_id, "sy227mod", caller); munlink(sy227_mod); munlink(mid); exit(0); } } main(argc, argv) int argc; char *argv[]; { char *chamber_code; char *mod_name0 = "hv_config0"; char *mod_name1 = "hv_config1"; char *mod_name2 = "hv_config2"; char *mod_name, *hv_config[3]; int dis, sl; int ch1, ch2, first_print; int nwrite, nerror, par_val; short ndata, data[45], prot_word; short i, ir, j, k, chan, value, code; int n_good, n_bad; float imean, vmean; short ch_imax, imax, d_vmon[32], d_imon[32], d_status[32]; short hv_ch, on_off, hv_in; int sy127_mod_vim_tim[32]; int sy127_mod_sta_tim[32]; int sy227_mod_vim_tim[32]; int sy227_mod_sta_tim[32]; int sy127_mod_nvim; int sy127_mod_nvim_err; int sy127_mod_nsta; int sy127_mod_nsta_err; int sy227_mod_nvim; int sy227_mod_nvim_err; int sy227_mod_nsta; int sy227_mod_nsta_err; short status_hv_ch; short v0_hv_ch; short vmon_hv_ch; short i0_hv_ch; short imon_hv_ch; int yy, mm, dd, hh, min, sec; int jtime1, jdate1; int jtime2, jdate2; int tick; short day; int time_lost, time_to_sleep; int ncycle; intercept(sighand); if ((pout = create(scatola_nera, 0x03, 0x03)) == -1) if ((pout = open(scatola_nera, 0x03)) == -1) exit(_errmsg(errno," tubi_sitter: error opening %s\n",scatola_nera)); close(pout); if((sy127_mod = (SY127_MBOX *)modlink("sy127mod", 0)) == (SY127_MBOX *) -1) exit(_errmsg(errno,"tubi_sitter: err. link data module\n")); sem_127mod_id = sem_link_cr("SY127mod", argv[0]); if((sy227_mod = (SY227_MBOX *)modlink("sy227mod", 0)) == (SY227_MBOX *) -1) exit(_errmsg(errno,"tubi_sitter: err. link data module\n")); sem_227mod_id = sem_link_cr("SY227mod", argv[0]); get_config(&tcp_ip_nr, &cpu_nr, hv_config); mod_name = hv_config[cpu_nr]; if ((mid = (hv_mod_struct *)modlink(mod_name, 0)) == (hv_mod_struct *)-1) if ((mid = (hv_mod_struct *)modload(mod_name, 0)) == (hv_mod_struct *)-1) exit(_errmsg(errno, "%s: error linking module %s\n", argv[0], mod_name)); chamber_code = "0"; for (i = 0; i < 40; i++) alarm[i] = sleep_count[i] = 0; ncycle = 0; while(1){ ncycle++; if ((pout = open(scatola_nera, 0x03)) == -1) exit(_errmsg(errno," tubi_sitter: error opening %s\n",scatola_nera)); if (lseek(pout, 0, 0) == -1) exit(_errmsg(errno," error rewinding %s\n",scatola_nera)); data_ora(&yy, &mm, &dd, &hh, &min, &sec); sprintf(cline, "%2d/%2d/%2d %2d:%2d Sittering starts\n",dd, mm, yy, hh, min); write(pout, cline, strlen(cline)); to_be_sent = 0; to_be_sent_h = 0; n_order = -1; for(dis = 0; dis < mid->n_crates; dis++) for(sl = 0; sl < 4; sl++) for(k = 0; k < 2; k++) /* successivamente si potrebbe chiedere soltanto una camera */ if((strcmp(chamber_code, mid->distr[dis].slot[sl].cable[k].chamber) == 0 || strcmp(chamber_code, mid->distr[dis].slot[sl].cable[k].cable_name) == 0 || ( strcmp("0", chamber_code) == 0 && mid->distr[dis].slot[sl].hv_ch >= 0 && mid->distr[dis].slot[sl].cable[k].first_ch >= 0) ) && !(mid->distr[dis].slot[sl].cond IS_CONDITIONING) ){ /* not cond */ data_ora(&yy, &mm, &dd, &hh, &min, &sec); sprintf(cline, "%2d/%2d/%2d %2d:%2d ",dd, mm, yy, hh, min); write(pout, cline, strlen(cline)); sprintf(cline, "cr %2d, sl %2d : ",dis +1 , sl); write(pout, cline, strlen(cline)); if (strcmp(mid->distr[dis].slot[sl].cable[k].chamber,"00") == 0) hosp = 1; else hosp = 0; if (mid->distr[dis].slot[sl].d_used_ch){ hv_ch = mid->distr[dis].slot[sl].hv_ch; sem_wait(sem_127mod_id, argv[0]); status_hv_ch = sy127_mod->hv_par[1].status[hv_ch]; v0_hv_ch = sy127_mod->hv_par[1].v0[hv_ch]; vmon_hv_ch = sy127_mod->hv_par[1].vmon[hv_ch]; i0_hv_ch = sy127_mod->hv_par[1].i0[hv_ch]; imon_hv_ch = sy127_mod->hv_par[1].imon[hv_ch]; sem_free(sem_127mod_id, argv[0]); /* distributors */ sem_wait(sem_227mod_id, argv[0]); on_off = sy227_mod->sl_par[dis][sl].onoff; hv_in = sy227_mod->sl_par[dis][sl].hvin; for ( i = 0; i < 32; i++){ d_vmon[i] = sy227_mod->sl_par[dis][sl].vmon[i]; d_imon[i] = sy227_mod->sl_par[dis][sl].imon[i]; d_status[i] = sy227_mod->sl_par[dis][sl].status[i]; } sem_free(sem_227mod_id, argv[0]); n_bad = 0; n_good = 0; ch_imax = -1; imax = -1; imean = 0.; vmean = 0.; for (i = 0; i < 32; i++){ if (mid->distr[dis].slot[sl].d_used_ch & ( 1 << i ) ) { if ( ! (d_status[i] D_IS_OK ) ) n_bad++; else { imean += (float) d_imon[i]; vmean += (float) d_vmon[i]; n_good++; } if ( d_imon[i] > imax ){ ch_imax = i; imax = d_imon[i]; } } } if ( n_good > 0 ){ imean = imean / (float)(n_good); vmean = vmean / (float)(n_good); } else { imean = -1.; vmean = -1.; } n_order++; if ( alarm[n_order] && !( mid->distr[dis].slot[sl].cond IS_SITTERING ) ) alarm[n_order] = 0; if (!alarm[n_order] && !(mid->distr[dis].slot[sl].cond IS_SITTERING && ncycle == 1) ) { sprintf(cline, "127_st: %2d; imean %4.0f, n_bad %2d ", status_hv_ch, imean, n_bad); write(pout, cline, strlen(cline)); if ( status_hv_ch IS_OVERV || status_hv_ch IS_TRIP || status_hv_ch IS_OVERC || ((status_hv_ch IS_ON) && !(on_off D_RELAY_IS_ON) ) || ( imax > 3000 || n_bad > 5 ) ) { sprintf(cline, "-> OFF\n"); write(pout, cline, strlen(cline)); alarm[n_order] = 1; mid->distr[dis].slot[sl].cond |= SITTERING; strcpy(line, "hv_switch "); strcat(line, mid->distr[dis].slot[sl].cable[k].cable_name); strcat(line, " off "); strcat(line, argv[0]); strcat(line, " >>>/nil"); data_ora(&yy, &mm, &dd, &hh, &min, &sec); if (hosp){ if( ( fp = ( FILE *) fopen(outfile_h, "a+")) == NULL){ sprintf(cline, "%s cannot open %s", argv[0], outfile_h); write(pout, cline, strlen(cline)); } } else { if( ( fp = ( FILE *) fopen(outfile, "a+")) == NULL){ sprintf(cline, "%s cannot open %s", argv[0], outfile); write(pout, cline, strlen(cline)); } } nout1 = fprintf(fp, "%2d/%2d %2d:%2d %s OFF", dd, mm, hh, min, mid->distr[dis].slot[sl].cable[k].cable_name); err1 = ferror(fp); nout2 = fprintf(fp, " V=%4d,st=0x%2x;=%4.0f,imax=%4d(ch%2d),", vmon_hv_ch, status_hv_ch, imean, imax, ch_imax); err2 = ferror(fp); nout3 = fprintf(fp, "n_bad=%2d\n", n_bad); err3 = ferror(fp); fclose(fp); if (err1 || err2 || err3 ){ sprintf(cline, " err1, err2, err3 : %d, %d, %d \n", err1, err2, err3); write(pout, cline, strlen(cline)); } if (hosp){ strcpy(tmpfile_h, tmpfile_h_0); /* strcat(tmpfile_h, time_str()); */ fp1 = fopen( tmpfile_h, "a+"); to_be_sent_h++; } else { strcpy(tmpfile, tmpfile_0); /* strcat(tmpfile, time_str()); */ fp1 = fopen( tmpfile, "a+"); to_be_sent++; } fprintf(fp1, "%2d/%2d %2d:%2d %s OFF", dd, mm, hh, min, mid->distr[dis].slot[sl].cable[k].cable_name); fprintf(fp1, " V=%4d,st=0x%2x;=%4.0f,imax=%4d(ch%2d),", vmon_hv_ch, status_hv_ch, imean, imax, ch_imax); fprintf(fp1, "n_bad=%2d\n", n_bad); fclose(fp1); system(line); } else { sprintf(cline, " \n"); write(pout, cline, strlen(cline)); } } else if( (sleep_count[n_order] > SLEEP_MAX && mid->distr[dis].slot[sl].cond IS_SITTERING) || (mid->distr[dis].slot[sl].cond IS_SITTERING && ncycle == 1) ) { mid->distr[dis].slot[sl].cond &= ~SITTERING; sprintf(cline, " -> ON\n"); write(pout, cline, strlen(cline)); alarm[n_order] = 0; sleep_count[n_order] = 0; strcpy(line, "hv_switch "); strcat(line, mid->distr[dis].slot[sl].cable[k].cable_name); strcat(line, " on "); strcat(line, argv[0]); strcat(line, " >>>/nil"); data_ora(&yy, &mm, &dd, &hh, &min, &sec); system(line); } else { sleep_count[n_order]++; if ( mid->distr[dis].slot[sl].cond IS_SITTERING ) { sprintf(cline, " SLEEPING\n"); write(pout, cline, strlen(cline)); } else { sprintf(cline, " \n"); write(pout, cline, strlen(cline)); } } } } time_lost = 0; if (to_be_sent || to_be_sent_h){ _sysdate(1, &jtime1, &jdate1, &day, &tick); data_ora(&yy, &mm, &dd, &hh, &min, &sec); sprintf(cline, "%2d/%2d/%2d %2d:%2d ",dd, mm, yy, hh, min); write(pout, cline, strlen(cline)); sprintf(cline, " calling to_vax() to send command \n"); write(pout, cline, strlen(cline)); to_vax(); if (to_be_sent){ printf(" going to delete tempfile \n"); strcpy(cline, "del "); strcat(cline, tmpfile); system(cline); } if (to_be_sent_h){ printf(" going to delete tempfile_h 1\n"); strcpy(cline, "del "); printf(" going to delete tempfile_h 2\n"); strcat(cline, tmpfile_h); printf(" going to delete tempfile_h 3 : '%s'\n", cline); /* system("procs"); */ system(cline); } data_ora(&yy, &mm, &dd, &hh, &min, &sec); sprintf(cline, "%2d/%2d/%2d %2d:%2d ",dd, mm, yy, hh, min); write(pout, cline, strlen(cline)); sprintf(cline, " to_vax() called \n"); write(pout, cline, strlen(cline)); _sysdate(1, &jtime2, &jdate2, &day, &tick); time_lost = ( jdate2 - jdate1) * 86399 + ( jtime2 - jtime1 ); sprintf(cline, " time lost: %d s \n", time_lost); write(pout, cline, strlen(cline)); } time_to_sleep = 60 - time_lost; /* time in seconds */ if (time_to_sleep < 1) time_to_sleep = 1; data_ora(&yy, &mm, &dd, &hh, &min, &sec); sprintf(cline, "%2d/%2d/%2d %2d:%2d *** CYCLE FINISHED *******\n\n\n", dd, mm, yy, hh, min); write(pout, cline, strlen(cline)); sprintf(cline, " Ignore following lines \n\n\n"); write(pout, cline, strlen(cline)); close(pout); sleep( time_to_sleep); } sem_unl_del(sem_127mod_id, "SY127mod", caller); sem_unl_del(sem_227mod_id, "SY127mod", caller); munlink(sy127_mod); munlink(sy227_mod); munlink(mid); } to_vax() { extern int os9forkc(); extern char **environ; char *argblk[5]; int pid; char *cline; char *pipe_0 ="/pipe/t_s"; char str[256], pipe[64]; int ntry; int alarm_id, ftp_id; unsigned ftp_status; int ftp_in, ftp_out, ftp_err; int pathin1, pathout1, patherr1; int pathin2, pathout2, patherr2; strcpy( str, "dagostin\n"); strcat( str, "zeus148\n"); strcat( str, "chd bm$hwc\n"); if (cpu_nr == 1) strcat( str, "chd [.reports.balc1]\n"); else if (cpu_nr == 2) strcat( str, "chd [.reports.balc2]\n"); else strcat( str, "chd [.reports.lab]\n"); if (to_be_sent){ strcat(str, "append "); strcat(str, tmpfile); strcat(str, " "); strcat(str, remfile); strcat(str, "\n"); } if (to_be_sent_h){ strcat(str, "append "); strcat(str, tmpfile_h); strcat(str, " "); strcat(str, remfile_h); strcat(str, "\n"); } strcat( str, "quit\n"); strcpy(pipe, pipe_0); strcat(pipe, time_str()); if((pid = create(pipe, 0x03+S_ISIZE, 0x03, strlen(str))) == -1){ printf(" tubi_sitter : errno %d\n", errno); exit(_errmsg(errno," error creating %s\n",pipe)); } write(pid, str, strlen(str)); close(pid); printf(" before return \n"); return ftp_ok; }