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, 0, SEEK_SET);
37 amount_read = fread(&buffer, sizeof(DataPoint), QUERY_BUFSIZ, file);
38 s->read_offset += amount_read;
39 Log(LOGDEBUG, "Read %d data points; offset now at %d", amount_read, s->read_offset);
44 pthread_mutex_unlock(&(s->mutex));
45 //End critical section
47 // So... we have a buffer
48 // I guess we'll want to JSON it or something?
49 // Just print it out for now
50 for (int i = 0; i < amount_read; ++i)
52 printf("%f\t%f\n", buffer[i].time, buffer[i].value);
55 // Will want to handle case where there actually wasn't anything new to respond with
56 // (In case we have a sensor that is slower than the rate of jQuery requests)
59 Log(LOGWARN, "No data points read from sensor%s file");
60 printf("# No data\n");
65 * Test function to simulate responding to HTTP requests
66 * @param args - IGNORED (void* required to pass function to pthread_create)
67 * @returns NULL (void* required to pass function to pthread_create)
69 void * Query_Main(void * args)
71 while (true) //TODO: Exit condition
74 for (int i = 0; i < NUMSENSORS; ++i)
76 printf("# Sensor %d\n", i);