/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ /* Populate_Datalogger.pc */ /* Load to the NCEDC database the datalogger */ /* information */ /* Errors can be found on Error.log */ /* */ /* Zuzlewski Stephane @1998 */ /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ #include #include #include EXEC SQL INCLUDE sqlca.h; typedef char asciz[20]; typedef char vc2_arr[11]; EXEC SQL BEGIN DECLARE SECTION; /* User-defined type for null-terminated strings */ EXEC SQL TYPE asciz IS STRING(20) REFERENCE; /* User-defined type for a VARCHAR array element */ EXEC SQL TYPE vc2_arr IS VARCHAR2(11) REFERENCE; asciz username; asciz password; char user_pwd[80]; int i; int j; char type[32]; /* Datalogger type */ char sn[32]; /* Serial number */ char firm[32]; /* Firmware number */ char s_dat[32]; /* Start date */ char e_dat[32]; /* End date */ int nb_ndm; /* Number of boards */ char msn[32]; /* Board serial number */ int nb_nch; /* Number of modules */ float sens; /* Sensitivity */ EXEC SQL END DECLARE SECTION; long SQLCODE; FILE* f_err; /* Error File descriptor */ void sql_error(); /* handles unrecoverable errors */ /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ /* Function to convert a julian date to a Oracle date */ /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ char* Convert_Date (d) char d[16]; { EXEC SQL BEGIN DECLARE SECTION; char nd[32]; int day; EXEC SQL END DECLARE SECTION; char hour[5]; char year[5]; char sday[4]; sprintf (year, "%.4s", d); sprintf (sday, "%.3s", d+5); sprintf (hour, "%.4s", d+9); if (!strcmp (year, "2599")) { strcpy (nd, "3000/01/01 00:00:00"); return (nd); } else if (!strcmp (year, "????")) { strcpy (nd, "0001/01/01 00:00:00"); return (nd); } day = atoi (sday); EXEC SQL SELECT TO_CHAR (TO_DATE (:day, 'DDD'), 'YYYY/MM/DD HH24:MI:SS') INTO :nd FROM DUAL; sprintf (nd, "%.4s/%.2s/%.2s %.2s:%.2s:00", year, nd+5, nd+8, hour, hour+2); return (nd); } /*-*-*-*-*-*-*-*-*/ /* Main function */ /*-*-*-*-*-*-*-*-*/ main (argc, argv) int argc; char* argv[]; { FILE* f_datlog; /* Datalogger file descriptor */ char line[1025]; /* Line information */ char header[7]; /* Header */ char sdat[16]; /* Start date */ char edat[16]; /* End date */ /* Opening the error file */ if ((f_err = fopen ("Error.log", "w+t")) == NULL) { printf ("\n Error [File (Error.log) could not be opened].\n\n"); exit (0); } /* Opening the datalogger file */ if ((f_datlog = fopen ("datlog.tbl", "rt")) == NULL) { printf ("\n Error [File (datlog.tbl) could not be opened].\n\n"); exit (0); } /* Connect to ORACLE. */ EXEC SQL WHENEVER SQLERROR DO sql_error(); strcpy (username, "stephane"); strcpy (password, "steph98"); strcpy (user_pwd, "stephane/steph98@ncedc_test"); EXEC SQL CONNECT :username IDENTIFIED BY :password; printf ("\n Connected to ORACLE as user: %s\n", username); /* Parsing datalogger file */ fgets (line, 1024, f_datlog); while (!feof (f_datlog)) { strcpy (header, ""); sscanf (line, "%s", header); if (!strcmp (header, "DATLOG")) { /* Reading datalogger information */ sscanf (line, "%*s %s %s %s %s %s %d", type, sn, firm, sdat, edat, &nb_ndm); /* Converting dates format */ strcpy (s_dat, Convert_Date (sdat)); strcpy (e_dat, Convert_Date (edat)); /* Inserting datalogger information into the database */ EXEC SQL INSERT INTO Datalogger (data_id, data_type, serial_nb, firmware_nb, ondate, offdate, nb_board, word_32, word_16, lddate) VALUES (dataseq.NEXTVAL, :type, :sn, :firm, :s_dat, :e_dat, :nb_ndm, 3210, 10, SYSDATE); printf ("\n Datalogger : %s\t%s\t%s\t%s\t%s\t%d", type, sn, firm, sdat, edat, nb_ndm); for (i=1;i<=nb_ndm;i++) { fgets (line, 1024, f_datlog); /* Reading board information */ sscanf (line, "%*s %*s %s %d", msn, &nb_nch); /* Inserting board information into the database */ EXEC SQL INSERT INTO Datalogger_Board (data_id, board_nb, serial_nb, ondate, offdate, nb_module, lddate) VALUES (dataseq.CURRVAL, :i, :msn, NULL, NULL, :nb_nch, SYSDATE); printf ("\n\t Board : %s\t%d", msn, nb_nch); for (j=1;j<=nb_nch;j++) { fgets (line, 1024, f_datlog); /* Reading module information */ sscanf (line, "%*s %*s %f", &sens); /* Inserting board information into the database */ EXEC SQL INSERT INTO Datalogger_Module (data_id, board_nb, module_nb, serial_nb, ondate, offdate, sensitivity, lddate) VALUES (dataseq.CURRVAL, :i, :j, NULL, NULL, NULL, :sens, SYSDATE); printf ("\n\t Module : %f", sens); } } } fgets (line, 1024, f_datlog); } fprintf (f_err, "\n"); printf ("\n\n"); /* Closing error file */ fclose (f_err); /* Closing datalogger file */ fclose (f_datlog); /* Disconnect from the database */ printf ("\n Disconnecting from ORACLE ...\n\n"); EXEC SQL COMMIT WORK RELEASE; exit(0); } /*************************************/ /* Handle errors. Exit on any error. */ /*************************************/ void sql_error() { char msg[512]; size_t buf_len, msg_len; EXEC SQL WHENEVER SQLERROR CONTINUE; buf_len = sizeof(msg); sqlglm(msg, &buf_len, &msg_len); fprintf(f_err, "\nORACLE error detected:"); fprintf(f_err, "\n%.*s \n", msg_len, msg); printf("\nORACLE error detected:"); printf("\n%.*s \n", msg_len, msg); /*EXEC SQL ROLLBACK WORK RELEASE; exit(1);*/ }