3 * @purpose Temporary file to run a test thread that will query a sensors thread
4 * Code will probably be combined with Jeremy's FastCGI API
14 static DataPoint buffer[QUERY_BUFSIZ];
17 * Query sensor with id
18 * @param id - The index of the sensor in g_sensors
20 void QuerySensor(int id) //TODO: This code will form the SensorHandler FastCGI function (I think?)
22 Sensor * s = g_sensors+id;
25 //CRITICAL SECTION (Don't access file while sensor thread is writing to it!)
26 pthread_mutex_lock(&(s->mutex));
28 FILE * file = fopen(s->filename, "rb");
31 Log(LOGWARN, "Couldn't open file \"%s\" mode rb - %s", s->filename, strerror(errno));
35 fseek(file, (s->read_offset)*sizeof(DataPoint), SEEK_SET);
36 amount_read = fread(&buffer, sizeof(DataPoint), QUERY_BUFSIZ, file);
37 s->read_offset += amount_read;
38 Log(LOGDEBUG, "Read %d data points; offset now at %d", amount_read, s->read_offset);
43 pthread_mutex_unlock(&(s->mutex));
44 //End critical section
46 // So... we have a buffer
47 // I guess we'll want to JSON it or something?
48 // Just print it out for now
49 for (int i = 0; i < amount_read; ++i)
51 printf("%f\t%f\n", buffer[i].time, buffer[i].value);
54 // Will want to handle case where there actually wasn't anything new to respond with
55 // (In case we have a sensor that is slower than the rate of jQuery requests)
58 Log(LOGWARN, "No data points read from sensor%s file");
59 printf("# No data\n");
64 * Test function to simulate responding to HTTP requests
65 * @param args - IGNORED (void* required to pass function to pthread_create)
66 * @returns NULL (void* required to pass function to pthread_create)
68 void * Query_Main(void * args)
70 while (true) //TODO: Exit condition
73 for (int i = 0; i < NUMSENSORS; ++i)
75 printf("# Sensor %d\n", i);