29d28 < digitemp -e Log all sensors to MySQL table 322,323d320 < #include < 378,384c375 < option_list[40], < db_conf_file[1024], /* MySQL Configuration File*/ < dbuser[16], /* MySQL user name */ < dbhost[16], /* MySQL hostname */ < dbpass[16], /* MySQL password */ < dbname[16]; /* MySQL database name */ < --- > option_list[40]; 418d408 < printf(" -e Log all sensors to MySQL table\n"); 781c771 < int log_temp( int sensor, float temp_c, unsigned char *sn, MYSQL *conn, float temp_f ) --- > int log_temp( int sensor, float temp_c, unsigned char *sn ) 783d772 < /*dtf = date and time format */ 785,787c774 < time_format[160], < dtf[160], < tempd[1024]; --- > time_format[160]; 791d777 < 793,823c779,782 < if( mytime ) { < /* mysql db stuff, this actually INSERTs the data in the db */ < /* all the mysql mods by Nick */ < /* the info can be found at http://illx.org/digitemp */ < /* MySQL code has now been implemented on digitemp 3.2.0 */ < if(log_type == 4) { < char query[1024], < snt[1024]; < < sprintf(snt, "%02X%02X%02X%02X%02X%02X%02X%02X", < sn[0],sn[1],sn[2],sn[3],sn[4],sn[5],sn[6],sn[7]); < strcpy(dtf, "%Y-%m-%d %H:%M:%S"); < strftime( tempd, 1024, dtf, localtime( &mytime ) ); < < /* I am only going to be putting temp_c in the DB now */ < /* instead of both temp_c, and temp_f */ < /* build the mysql SQL query */ < sprintf(query, "insert into temps values('%d','%f','%f','%s','%s')", < sensor, temp_c, temp_f, snt, tempd); < < /* execute it */ < if(mysql_query (conn, query) != 0) { < printf("Error!! MySQL INSERT failed!\n"); < } < else { < return 0; < } < } < else { < /* Build the time format string from log_format */ < build_tf( time_format, temp_format, sensor, temp_c, sn ); --- > if( mytime ) > { > /* Build the time format string from log_format */ > build_tf( time_format, temp_format, sensor, temp_c, sn ); 825,826c784,785 < /* Handle the time format tokens */ < strftime( temp, 1024, time_format, localtime( &mytime ) ); --- > /* Handle the time format tokens */ > strftime( temp, 1024, time_format, localtime( &mytime ) ); 828,829c787 < strcat( temp, "\n" ); < } --- > strcat( temp, "\n" ); 997c955 < int read_temperature( int sensor_family, int sensor, MYSQL *conn ) --- > int read_temperature( int sensor_family, int sensor ) 1127,1131d1084 < /* mysql testing log_type */ < case 4: owSerialNum( 0, &TempSN[0], TRUE ); < log_temp( sensor, temp_c, TempSN, conn, c2f(temp_c) ); < break; < 1133c1086 < log_temp( sensor, temp_c, TempSN, 0, 0 ); --- > log_temp( sensor, temp_c, TempSN ); 1229c1182 < int read_device( struct _roms *sensor_list, int sensor, MYSQL *conn ) --- > int read_device( struct _roms *sensor_list, int sensor ) 1317c1270 < status = read_temperature( sensor_family, sensor, conn ); --- > status = read_temperature( sensor_family, sensor ); 1343c1296 < read_device( sensor_list, x, 0 ); --- > read_device( sensor_list, x ); 1349,1361d1301 < /* This routine reads all the sensors and logs to mysql */ < int read_all_and_dblog( struct _roms *sensor_list, MYSQL *conn ) < { < int x; < < for( x = 0; x < (num_cs+sensor_list->max); x++ ) < { < /*printf();*/ < read_device( sensor_list, x, conn ); < } < < return 0; < } 1581,1641d1520 < /* ----------------------------------------------------------------------- < Read a .digitemprc_mysql file from the current directory < < The rc file contains: < < DBNAME database_name < DBUSER database_user_name < DBPASS database_password < DBHOST localhost (or whichever host) < */ < < int read_rcdbfile( char *fname ) < { < FILE *fp; < char temp[80]; < char *ptr; < int sensors; < struct _coupler *c_ptr, *coupler_end; < < sensors = 0; < num_cs = 0; < c_ptr = coupler_top; < coupler_end = coupler_top; < < if( ( fp = fopen( fname, "r" ) ) == NULL ) < { < /* No rcfile to read, could be part of an -i so don't die */ < return 1; < } < < while( fgets( temp, 80, fp ) != 0 ) < { < if( (temp[0] == '\n') || (temp[0] == '#') ) < continue; < < ptr = strtok( temp, " \t\n" ); < < if( strncasecmp( "DBUSER", ptr, 6 ) == 0 ) < { < ptr = strtok( NULL, " \t\n" ); < strcpy( dbuser, ptr ); < } else if( strncasecmp( "DBPASS", ptr, 6 ) == 0 ) { < ptr = strtok( NULL, " \t\n"); < strcpy( dbpass, ptr ); < } else if( strncasecmp( "DBHOST", ptr, 6 ) == 0 ) { < ptr = strtok( NULL, " \t\n"); < strcpy( dbhost, ptr ); < } else if( strncasecmp( "DBNAME", ptr, 6 ) == 0 ) { < ptr = strtok( NULL, "\"\n"); < strcpy( dbname, ptr ); < } else { < fprintf( stderr, "Error reading %s file\n", fname ); < fclose( fp ); < return -1; < } < } < < fclose( fp ); < < return 0; < } 2261,2262c2140 < strcpy( db_conf_file, ".digitemprc_mysql" ); < strcpy( option_list, "?hqiavwr:f:s:l:t:d:n:o:c:O:e" ); --- > strcpy( option_list, "?hqiavwr:f:s:l:t:d:n:o:c:O:" ); 2283,2284d2160 < case 'e': opts |= OPT_DBLOG; /* Log to MySQL */ < break; 2371c2247 < if ((opts & (OPT_WALK|OPT_INIT|OPT_SINGLE|OPT_ALL|OPT_DBLOG)) == 0 ) --- > if ((opts & (OPT_WALK|OPT_INIT|OPT_SINGLE|OPT_ALL)) == 0 ) 2373c2249 < fprintf( stderr, "Error! You need 1 of the following action commands, -w -a -i -t -e\n"); --- > fprintf( stderr, "Error! You need 1 of the following action commands, -w -a -i -t\n"); 2381,2384d2256 < if ( read_rcdbfile( db_conf_file ) < 0 ) { < exit(EXIT_NORC); < } < 2512c2384 < read_device( &sensor_list, sensor, 0 ); --- > read_device( &sensor_list, sensor ); 2521,2533d2392 < /* database testing stuff */ < if( opts & OPT_DBLOG ) < { < /* open the mysql database */ < MYSQL *conn; < conn = mysql_init(NULL); < mysql_real_connect (conn,dbhost,dbuser,dbpass, < dbname,0,NULL,0); < log_type = 4; < read_all_and_dblog( &sensor_list, conn ); < mysql_close(conn); < } <