+
+/**
+ * Get a Sensor given an ID string
+ * @param id_str ID string
+ * @returns Sensor* identified by the string; NULL on error
+ */
+Sensor * Sensor_Identify(const char * id_str)
+{
+ char * end;
+ // Parse string as integer
+ int id = strtol(id_str, &end, 10);
+ if (*end != '\0')
+ {
+ return NULL;
+ }
+ // Bounds check
+ if (id < 0 || id >= NUMSENSORS)
+ return NULL;
+
+
+ Log(LOGDEBUG, "Sensor \"%s\" identified", g_sensor_names[id]);
+ return g_sensors+id;
+}
+
+/**
+ * Helper: Begin sensor response in a given format
+ * @param context - the FCGIContext
+ * @param format - Format
+ * @param id - ID of sensor
+ */
+void Sensor_BeginResponse(FCGIContext * context, SensorId id, DataFormat format)
+{
+ // Begin response
+ switch (format)
+ {
+ case JSON:
+ FCGI_BeginJSON(context, STATUS_OK);
+ FCGI_JSONLong("id", id);
+ FCGI_JSONKey("data");
+ break;
+ default:
+ FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
+ break;
+ }
+}
+
+/**
+ * Helper: End sensor response in a given format
+ * @param context - the FCGIContext
+ * @param id - ID of the sensor
+ * @param format - Format
+ */
+void Sensor_EndResponse(FCGIContext * context, SensorId id, DataFormat format)
+{
+ // End response
+ switch (format)
+ {
+ case JSON:
+ FCGI_EndJSON();
+ break;
+ default:
+ break;
+ }
+}
+
+/**
+ * Handle a request to the sensor module
+ * @param context - The context to work in
+ * @param params - Parameters passed
+ */
+void Sensor_Handler(FCGIContext *context, char * params)
+{
+ struct timeval now;
+ gettimeofday(&now, NULL);
+ double current_time = TIMEVAL_DIFF(now, g_options.start_time);
+
+ int id = 0;
+ double start_time = 0;
+ double end_time = current_time;
+ char * fmt_str;
+
+ // key/value pairs
+ FCGIValue values[] = {
+ {"id", &id, FCGI_REQUIRED(FCGI_LONG_T)},
+ {"format", &fmt_str, FCGI_STRING_T},
+ {"start_time", &start_time, FCGI_DOUBLE_T},
+ {"end_time", &end_time, FCGI_DOUBLE_T},
+ };
+
+ // enum to avoid the use of magic numbers
+ typedef enum {
+ ID,
+ FORMAT,
+ START_TIME,
+ END_TIME,
+ } SensorParams;
+
+ // Fill values appropriately
+ if (!FCGI_ParseRequest(context, params, values, sizeof(values)))
+ {
+ // Error occured; FCGI_RejectJSON already called
+ return;
+ }
+
+ // Error checking on sensor id
+ if (id < 0 || id >= NUMSENSORS)
+ {
+ Log(LOGERR, "Invalid id %d", id);
+ FCGI_RejectJSON(context, "Invalid id"); // Whoops, I do still need this!
+ }
+
+ DataFormat format = JSON;
+
+ // Check if format type was specified
+ if (FCGI_RECEIVED(values[FORMAT].flags))
+ {
+ if (strcmp(fmt_str, "json") == 0)
+ format = JSON;
+ else if (strcmp(fmt_str, "tsv") == 0)
+ format = TSV;
+ else
+ Log(LOGERR, "Unknown format type \"%s\"", fmt_str);
+ }
+
+ // Get Sensor
+ Sensor * s = g_sensors+id;
+
+ // Begin response
+ Sensor_BeginResponse(context, id, format);
+
+ // If a time was specified
+ if (FCGI_RECEIVED(values[START_TIME].flags) || FCGI_RECEIVED(values[END_TIME].flags))
+ {
+ // Wrap times relative to the current time
+ if (start_time < 0)
+ start_time += current_time;
+ if (end_time < 0)
+ end_time += current_time;
+
+ // Print points by time range
+ Data_PrintByTimes(&(s->data_file), start_time, end_time, format);
+
+ }
+ else // No time was specified; just return a recent set of points
+ {
+ pthread_mutex_lock(&(s->data_file.mutex));
+ int start_index = s->data_file.num_points-DATA_BUFSIZ;
+ int end_index = s->data_file.num_points-1;
+ pthread_mutex_unlock(&(s->data_file.mutex));
+
+ // Print points by indexes
+ Data_PrintByIndexes(&(s->data_file), start_index, end_index, format);
+ }
+
+ // Finish response
+ Sensor_EndResponse(context, id, format);
+
+}
+
+