#include #include #include #include #include #include "ow-functions.h" #include "dev-functions.h" #include "pq-wrapper.h" #define ADAPTER_SETTINGS_FILE "DS2490-settings.dat" #define MAX_QUERY 1024 #define MAX_TIME_STRING 80 extern int sleepSeconds (int sec); static volatile sig_atomic_t interrupted = 0; void handler (int signal) { interrupted = 1; return; } int main (int argc, char *argv[]) { owDevice devList[MAX_DEVICES]; long numDevices, n, channelID[MAX_DEVICES][MAX_CHANNELS]; char query[MAX_QUERY], *ptr; PGresult *result; if (signal (SIGINT, &handler) == SIG_ERR) { fprintf (stderr, "ERROR: Signal handler could not be initialized\n"); return EXIT_FAILURE; } // Read adapter settings from file readAdapterSettings (ADAPTER_SETTINGS_FILE); // Acquire adapter if ( acquireAdapter() < 0 ) { return EXIT_FAILURE; } // Reset adapter if ( resetAdapter() < 0 ) { releaseAdapter(); return EXIT_FAILURE; } // Connect to database if (my_PQconnectdb() < 0) { releaseAdapter(); return EXIT_FAILURE; } // Find Devices numDevices = makeDeviceList (devList); if (numDevices <= 0) { releaseAdapter(); return EXIT_FAILURE; } // Search for channel in OneWireID table for (n = 0; n < numDevices; n++) { int i, owsubch; for (owsubch = 0; owsubch < devList[n].channels; owsubch++) { ptr = query; ptr += sprintf (ptr, "SELECT id FROM OneWireID WHERE owid = x'"); for (i = 7; i >= 0; i--) { ptr += sprintf (ptr, "%02X", devList[n].SN[i]); } ptr += sprintf (ptr, "'::bigint"); if (devList[n].channels > 1) { sprintf (ptr, " AND owsubch = %d", owsubch); } result = my_PQexec (query, 1); if (result && PQntuples(result) >= 1 && PQnfields(result) >= 1) { channelID[n][owsubch] = atol( PQgetvalue(result, 0, 0) ); } else { fprintf (stderr, "ERROR: Could not find device "); for (i = 7; i >= 0; i--) { fprintf (stderr, "%02X", devList[n].SN[i]); } if (devList[n].channels > 1) { fprintf (stderr, ", channel %d", owsubch); } fprintf (stderr, " in OneWireID table\n"); my_PQfinish(); releaseAdapter(); return EXIT_FAILURE; } } } while (!interrupted) { // Set up devices if needed for (n = 0; n < numDevices; n++) { if (devList[n].configError | devList[n].alarmError) { setupDevice (&devList[n]); } } // Initiate conversions for (n = 0; n < numDevices; n++) { if ( !(devList[n].configError) ) { doConversion (&devList[n]); } } // Sleep 1 second if (sleepSeconds(1) < 0) { if (interrupted) { break; } fprintf (stderr, "ERROR: Sleep failed\n"); errPrintDet (NULL); fprintf (stderr, "\n"); for (n = 0; n < numDevices; n++) { devList[n].convertError = 1; } } for (n = 0; n < numDevices; n++) { if (devList[n].channels != 0) { devList[n].tries++; } } // Read devices for (n = 0; n < numDevices; n++) { if ( !(devList[n].configError | devList[n].convertError) ) { float data[MAX_CHANNELS]; if ( readDevice(&devList[n], data) < 0 ) { fprintf (stderr, "\n"); } else { int i; for (i = 0; i < devList[n].channels; i++) { char sTime[MAX_TIME_STRING]; strftime (sTime, MAX_TIME_STRING, "%Y-%m-%d %T", localtime(&(devList[n].convertTime))); sprintf ( query, "INSERT INTO ReadoutHistory_%ld (time, value) " "VALUES ('%s', %.8e)", channelID[n][i], sTime, data[i] ); if ( my_PQexec(query, 0) == NULL ) { fprintf (stderr, "Could not insert data read into database\n"); if (devList[n].channels > 1) { fprintf (stderr, " Channel: %d\n", i); } fprintf (stderr, " Data: %.8e\n", data[i]); errPrintDet (devList[n].SN); fprintf (stderr, "\n"); } } } } } // Search for alarming devices alarmSearch (devList, numDevices); } printSuccessRate (devList, numDevices); my_PQfinish(); releaseAdapter(); return 0; }