- //TODO: Consider; is it better to give both tv_sec and tv_usec to the client seperately, instead of combining here?
- //NOTE: Must always use doubles; floats get rounded!
- double time = buffer[i].time_stamp.tv_sec + 1e-6*(buffer[i].time_stamp.tv_usec);
- FCGI_JSONValue("[%f, %f]", time, buffer[i].value);
- if (i+1 < amount_read)
- FCGI_JSONValue(",");
+ //Force download with content-disposition
+ FCGI_PrintRaw("Content-type: text/plain\r\n"
+ "Content-disposition: attachment;filename=%d.csv\r\n\r\n",
+ sensor->id);
+ //CRITICAL SECTION
+ pthread_mutex_lock(&(sensor->mutex));
+ fseek(sensor->file, 0, SEEK_SET);
+ int amount_read = 0;
+ do
+ {
+ amount_read = fread(buffer, sizeof(DataPoint), SENSOR_QUERYBUFSIZ, sensor->file);
+ for (int i = 0; i < amount_read; ++i)
+ {
+ FCGI_PrintRaw("%f\t%f\n", buffer[i].time_stamp, buffer[i].value);
+ }
+
+ }
+ while (amount_read == SENSOR_QUERYBUFSIZ);
+ pthread_mutex_unlock(&(sensor->mutex));
+ // end critical section
+ break;
+ }
+ default:
+ {
+ FCGI_BeginJSON(context, status);
+ FCGI_JSONPair(key, value); // should spit back sensor ID
+ //Log(LOGDEBUG, "Call Sensor_Query...");
+ int amount_read = Sensor_Query(sensor, buffer, SENSOR_QUERYBUFSIZ);
+ //Log(LOGDEBUG, "Read %d DataPoints", amount_read);
+ //Log(LOGDEBUG, "Produce JSON response");
+ FCGI_JSONKey("data");
+ FCGI_JSONValue("[");
+ for (int i = 0; i < amount_read; ++i)
+ {
+ //TODO: Consider; is it better to give both tv_sec and tv_usec to the client seperately, instead of combining here?
+
+ FCGI_JSONValue("[%f, %f]", buffer[i].time_stamp, buffer[i].value);
+ if (i+1 < amount_read)
+ FCGI_JSONValue(",");
+ }
+ FCGI_JSONValue("]");
+ //Log(LOGDEBUG, "Done producing JSON response");
+ FCGI_EndJSON();
+ break;