X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=sidebyside;f=server%2Fsensor.c;h=55a9eaad21eeb574a69b92fd5e4e616415da7125;hb=ea228f75c1f6d83f9a02a5ad5a7341caabac65c8;hp=227ea42bdcaaa0b4072082e042f9f91f5da28860;hpb=d0f77e15cfa58191a7683caf343037c25be9f31c;p=matches%2FMCTX3420.git diff --git a/server/sensor.c b/server/sensor.c index 227ea42..55a9eaa 100644 --- a/server/sensor.c +++ b/server/sensor.c @@ -40,7 +40,7 @@ void Sensor_Start(Sensor * s, const char * experiment_name) { // Set filename char filename[BUFSIZ]; - if (sprintf(filename, "%s_%d", experiment_name, s->id) >= BUFSIZ) + if (sprintf(filename, "%s_s%d", experiment_name, s->id) >= BUFSIZ) { Fatal("Experiment name \"%s\" too long (>%d)", experiment_name, BUFSIZ); } @@ -236,7 +236,6 @@ void Sensor_BeginResponse(FCGIContext * context, SensorId id, DataFormat 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"); @@ -302,67 +301,22 @@ void Sensor_Handler(FCGIContext *context, char * params) return; } - // Get Sensor - Sensor * s = NULL; // Error checking on sensor id if (id < 0 || id >= NUMSENSORS) { - Log(LOGERR, "Invalid id %d", id); - } - else - { - s = g_sensors+id; + FCGI_RejectJSON(context, "Invalid sensor id"); + return; } + Sensor * s = g_sensors+id; - DataFormat format = JSON; + DataFormat format = Data_GetFormat(&(values[FORMAT])); - // 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); - } - - - // Begin response Sensor_BeginResponse(context, id, format); - - // If a time was specified - if ((s != NULL) && (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 if (s != NULL) // 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)); - - // Bounds check - if (start_index < 0) - start_index = 0; - if (end_index < 0) - end_index = 0; - - // Print points by indexes - Log(LOGDEBUG, "Sensor %d file \"%s\" indexes %d->%d", s->id, s->data_file.filename, start_index, end_index); - Data_PrintByIndexes(&(s->data_file), start_index, end_index, format); - } + // Print Data + Data_Handler(&(s->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time); // Finish response Sensor_EndResponse(context, id, format);