30d29 < digitemp -e Log all sensors to MySQL table 283,284d281 < #include < 332,337c329 < 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]; 367d358 < printf(" -e Log all sensors to MySQL table\n"); 592c583 < int log_time( int sensor, float temp_c, float temp_f, unsigned char *sn, MYSQL *conn ) --- > int log_time( int sensor, float temp_c, float temp_f, unsigned char *sn ) 594d584 < /*dtf = date and time format */ 596,599c586 < time_format[160], < dtf[160], < tempd[1024]; < --- > time_format[160]; 606,632c593,594 < /* 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 */ < 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 ) ); < < /* 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, log_format, sensor, temp_c, temp_f, sn ); --- > /* Build the time format string from log_format */ > build_tf( time_format, log_format, sensor, temp_c, temp_f, sn ); 634,635c596,597 < /* Handle the time format tokens */ < strftime( temp, 1024, time_format, localtime( &mytime ) ); --- > /* Handle the time format tokens */ > strftime( temp, 1024, time_format, localtime( &mytime ) ); 637,638c599 < strcat( temp, "\n" ); < } --- > strcat( temp, "\n" ); 765c726 < int read_temp( struct _roms *sensor_list, int sensor, MYSQL *conn ) --- > int read_temp( struct _roms *sensor_list, int sensor ) 974,978c935 < /* mysql testing log_type */ < case 4: log_time( sensor, temp_c, c2f(temp_c), TempSN, conn ); < break; < < default: log_time( sensor, temp_c, c2f(temp_c), TempSN, 0 ); --- > default: log_time( sensor, temp_c, c2f(temp_c), TempSN ); 1023c980 < read_temp( sensor_list, x, 0 ); --- > read_temp( sensor_list, x ); 1029,1041d985 < /*testing*/ < 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_temp( sensor_list, x, conn ); < } < < return 0; < } 1257,1317d1200 < /* ----------------------------------------------------------------------- < 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, x; < 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; < } 1929,1930c1812 < strcpy( db_conf_file, ".digitemprc_mysql" ); < strcpy( option_list, "?hqiavwr:f:s:l:t:d:n:o:c:e" ); --- > strcpy( option_list, "?hqiavwr:f:s:l:t:d:n:o:c:" ); 1951,1952d1832 < case 'e': opts |= OPT_DBLOG; /* Log to MySQL */ < break; 2030c1910 < if ((opts & (OPT_WALK|OPT_INIT|OPT_SINGLE|OPT_ALL|OPT_DBLOG)) == 0 ) --- > if ((opts & (OPT_WALK|OPT_INIT|OPT_SINGLE|OPT_ALL)) == 0 ) 2032c1912 < 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"); 2040,2043d1919 < if ( read_rcdbfile( db_conf_file ) < 0 ) { < exit(EXIT_NORC); < } < 2167c2043 < read_temp( &sensor_list, sensor, 0 ); --- > read_temp( &sensor_list, sensor ); 2174,2187d2049 < } < < /* 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);