#include #include #include #include "ow-functions.h" #include "pq-wrapper.h" #define MAX_QUERY 1024 #define MAX_DEVICES 65536 #define SEARCH_ROM_CMD 0xF0 #define NUM_TYPES_DEFINED 4 // Family codes #define FIXED_RES_THERMOMETER 0x10 #define A_TO_D_CONVERTER 0x20 #define ECONO_THERMOMETER 0x22 #define PROG_RES_THERMOMETER 0x28 int main() { PGresult *result; unsigned char serialNum[MAX_DEVICES][8]; unsigned char familyCode[NUM_TYPES_DEFINED] = { FIXED_RES_THERMOMETER, ECONO_THERMOMETER, PROG_RES_THERMOMETER, A_TO_D_CONVERTER }; const char *typeName[NUM_TYPES_DEFINED] = { "Test DS18S20 thermometer", "Test DS1822 thermometer", "Test DS18B20 thermometer", "Test A-to-D converter" }; const char *unit[NUM_TYPES_DEFINED] = { "degrees Celsius", "degrees Celsius", "degrees Celsius", "volts" }; char query[MAX_QUERY], *ptr; int devType, owsubch, j; int numChannels[NUM_TYPES_DEFINED] = {1, 1, 1, 4}; long numDevices, i; long id = 0, num[NUM_TYPES_DEFINED] = {0}; // Connect to database if (my_PQconnectdb() < 0) { goto failure; } // Create ChannelID table if not present result = my_PQexec ("SELECT * FROM pg_tables WHERE tablename = 'channelid'", 1); if (result == NULL) { goto failure; } if (PQntuples(result) == 0) { if ( my_PQexec("CREATE TABLE ChannelID (id int, name text, unit text)", 0) == NULL ) { goto failure; } } // Create OneWireID table if not present result = my_PQexec ("SELECT * FROM pg_tables WHERE tablename = 'onewireid'", 1); if (result == NULL) { goto failure; } if (PQntuples(result) == 0) { if ( my_PQexec("CREATE TABLE OneWireID (id int, owid bigint, owsubch int, install_time timestamp, uninstall_time timestamp)", 0) == NULL ) { goto failure; } } // Find one-wire devices if (acquireAdapter() < 0) { goto failure; } if (resetAdapter() < 0) { releaseAdapter(); goto failure; } numDevices = findDevices (SEARCH_ROM_CMD, serialNum, MAX_DEVICES); if (numDevices < 0) { releaseAdapter(); goto failure; } if (releaseAdapter() < 0) { goto failure; } // Determine last used ID number result = my_PQexec ("SELECT id FROM ChannelID ORDER BY id DESC LIMIT 1", 1); if (result && PQntuples(result) >= 1 && PQnfields(result) >= 1) { id = atol ( PQgetvalue(result, 0, 0) ); } // Loop over devices found for (i = 0; i < numDevices; i++) { // Search for device in OneWireID table ptr = query; ptr += sprintf (ptr, "SELECT * FROM OneWireID WHERE owid = x'"); for (j = 7; j >= 0; j--) { ptr += sprintf (ptr, "%02X", serialNum[i][j]); } sprintf (ptr, "'::bigint"); result = my_PQexec (query, 1); if (result && PQntuples(result)) { continue; } // Determine type of device for (devType = 0; devType < NUM_TYPES_DEFINED; devType++) { if (serialNum[i][0] == familyCode[devType]) { goto typeFound; } } continue; typeFound: // Determine next number for name if (num[devType] == 0) { sprintf ( query, "SELECT substring(" "name " "from position('#' in name) + 1 " "for position(',' in name || ',') - position('#' in name) - 1" ")::int " "AS num FROM ChannelID " "WHERE name like '%s #%%' " "ORDER BY num DESC LIMIT 1", typeName[devType] ); result = my_PQexec (query, 1); if (result && PQntuples(result) >= 1 && PQnfields(result) >= 1) { num[devType] = atol( PQgetvalue(result, 0, 0) ); } } num[devType]++; for (owsubch = 0; owsubch < numChannels[devType]; owsubch++) { // Increment ID number id++; // Add entry in ChannelID table ptr = query; ptr += sprintf ( ptr, "INSERT INTO ChannelID (id, name, unit) " "VALUES (%ld, '%s #%ld", id, typeName[devType], num[devType] ); if (numChannels[devType] > 1) { ptr += sprintf (ptr, ", channel %d", owsubch); } sprintf (ptr, "', '%s')", unit[devType]); my_PQexec (query, 0); // Add entry in OneWireID table ptr = query; ptr += sprintf (ptr, "INSERT INTO OneWireID (id, owid"); if (numChannels[devType] > 1) { ptr += sprintf (ptr, ", owsubch"); } ptr += sprintf (ptr, ") VALUES (%ld, x'", id); for (j = 7; j >= 0; j--) { ptr += sprintf (ptr, "%02X", serialNum[i][j]); } ptr += sprintf (ptr, "'::bigint"); if (numChannels[devType] > 1) { ptr += sprintf (ptr, ", %d", owsubch); } sprintf (ptr, ")"); my_PQexec (query, 0); // Create ReadoutHistory_N table sprintf ( query, "CREATE TABLE ReadoutHistory_%ld " "(time timestamp, value real, status int)", id ); my_PQexec (query, 0); } } my_PQfinish(); return 0; failure: { my_PQfinish(); return EXIT_FAILURE; } }