28a29 > digitemp -e Log all sensors to MySQL table 376a378,379 > #include > 457c460,466 < option_list[40]; --- > option_list[41], > 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 */ > 481c490 < printf("\nUsage: digitemp [-s -i -U -l -r -v -t -a -d -n -o -c]\n"); --- > printf("\nUsage: digitemp [-s -i -U -l -r -v -t -a -d -n -o -c -e]\n"); 490a500 > printf(" -e Log all sensors to MySQL table\n"); 882c892 < int log_temp( int sensor, float temp_c, unsigned char *sn ) --- > int log_temp( int sensor, float temp_c, unsigned char *sn, MYSQL *conn, float temp_f ) 885c895,897 < time_format[160]; --- > time_format[160], > dtf[160], > tempd[1024]; 890,893c902,934 < if( mytime ) < { < /* Build the time format string from log_format */ < build_tf( time_format, temp_format, sensor, temp_c, -1, sn ); --- > 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.3.2 */ > 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 ) ); > > /* In the future I will only put temp_c in the DB */ > /* instead of both temp_c, and temp_f */ > /* build the mysql SQL query */ > /*printf("INSERT INTO temps VALUES('%d','%f','%f','%s','%s')", > sensor, temp_c, temp_f, snt, tempd);*/ > sprintf(query, "INSERT INTO temps VALUES('%d','%f','%f','%s','%s')", > sensor, temp_c, temp_f, snt, tempd); > /*printf("query string: '%s'\n", query);*/ > /* 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, -1, sn ); 895,896c936,937 < /* Handle the time format tokens */ < strftime( temp, 1024, time_format, localtime( &mytime ) ); --- > /* Handle the time format tokens */ > strftime( temp, 1024, time_format, localtime( &mytime ) ); 898c939,940 < strcat( temp, "\n" ); --- > strcat( temp, "\n" ); > } 1098c1140 < int read_temperature( int sensor_family, int sensor ) --- > int read_temperature( int sensor_family, int sensor, MYSQL *conn ) 1224a1267,1271 > /* mysql log_type */ > case 4: owSerialNum( 0, &TempSN[0], TRUE ); > log_temp( sensor, temp_c, TempSN, conn, c2f(temp_c) ); > break; > 1226c1273 < log_temp( sensor, temp_c, TempSN ); --- > log_temp( sensor, temp_c, TempSN, 0, 0 ); 1463c1510 < int read_device( struct _roms *sensor_list, int sensor ) --- > int read_device( struct _roms *sensor_list, int sensor, MYSQL *conn ) 1551c1598 < status = read_temperature( sensor_family, sensor ); --- > status = read_temperature( sensor_family, sensor, conn ); 1586c1633 < read_device( sensor_list, x ); --- > read_device( sensor_list, x, 0 ); 1591a1639,1651 > /* 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; > } 1828a1889,1949 > /* ----------------------------------------------------------------------- > 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 ); > printf("Error reading %s file\n", fname ); > fclose( fp ); > return -1; > } > } > fclose( fp ); > return 0; > } > 2466c2587,2588 < strcpy( option_list, "?hqiaAvwr:f:s:l:t:d:n:o:c:O:H:" ); --- > strcpy( db_conf_file, ".digitemprc_mysql" ); > strcpy( option_list, "?hqiaAvwr:f:s:l:t:d:n:o:c:O:H:e" ); 2486a2609,2611 > case 'e': opts |= OPT_DBLOG; /* Log to MySQL */ > break; > 2585c2710 < if ((opts & (OPT_WALK|OPT_INIT|OPT_SINGLE|OPT_ALL)) == 0 ) --- > if ((opts & (OPT_WALK|OPT_INIT|OPT_SINGLE|OPT_ALL|OPT_DBLOG)) == 0 ) 2587c2712 < fprintf( stderr, "Error! You need 1 of the following action commands, -w -a -i -t\n"); --- > fprintf( stderr, "Error! You need 1 of the following action commands, -w -a -i -t -e\n"); 2594a2720,2723 > if ( read_rcdbfile( db_conf_file ) < 0 ) { > exit(EXIT_NORC); > } > 2806c2935 < read_device( &sensor_list, sensor ); --- > read_device( &sensor_list, sensor, 0 ); 2812a2942,2964 > } > > /* MySQL database stuff */ > if( opts & OPT_DBLOG ) { > /* open the mysql database */ > MYSQL *conn; > conn = mysql_init(NULL); > if(conn == NULL) { > fprintf(stderr, "mysql_init() failed... probably out of memory\n"); > exit(1); > } > > if( mysql_real_connect (conn,dbhost,dbuser,dbpass, > dbname,0,NULL,0) == NULL) { > fprintf(stderr, "mysql_real_connect() failed!\nError: (%u) '%s'\n", > mysql_errno(conn), mysql_error(conn)); > mysql_close(conn); > exit(1); > } > > log_type = 4; > read_all_and_dblog( &sensor_list, conn ); > mysql_close(conn);