Add Actuator related code
[matches/MCTX3420.git] / server / sensor.c
index 8914817..55a9eaa 100644 (file)
@@ -38,13 +38,20 @@ void Sensor_Init()
  */
 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);
        }
+
+       Log(LOGDEBUG, "Sensor %d with DataFile \"%s\"", s->id, filename);
+       // Open DataFile
        Data_Open(&(s->data_file), filename);
-               
+
+       s->record_data = true; // Don't forget this!
+
+       // Create the thread
        pthread_create(&(s->thread), NULL, Sensor_Loop, (void*)(s));
 }
 
@@ -54,11 +61,14 @@ void Sensor_Start(Sensor * s, const char * experiment_name)
  */
 void Sensor_Stop(Sensor * s)
 {
+       // Stop
        if (s->record_data)
        {
                s->record_data = false;
-               pthread_join(s->thread, NULL);
-               Data_Close(&(s->data_file));
+               pthread_join(s->thread, NULL); // Wait for thread to exit
+               Data_Close(&(s->data_file)); // Close DataFile
+               s->newest_data.time_stamp = 0;
+               s->newest_data.value = 0;
        }
 }
 
@@ -127,6 +137,7 @@ bool Sensor_Read(Sensor * s, DataPoint * d)
        if (result)
        {
                s->newest_data.time_stamp = d->time_stamp;
+               s->newest_data.value = d->value;
        }
        return result;
 }
@@ -168,18 +179,23 @@ void Sensor_CheckData(SensorId id, DataPoint * d)
 void * Sensor_Loop(void * arg)
 {
        Sensor * s = (Sensor*)(arg);
+       Log(LOGDEBUG, "Sensor %d starts", s->id);
 
        // Until the sensor is stopped, record data points
        while (s->record_data)
        {
                DataPoint d;
+               //Log(LOGDEBUG, "Sensor %d reads data [%f,%f]", s->id, d.time_stamp, d.value);
                if (Sensor_Read(s, &d)) // If new DataPoint is read:
                {
+                       //Log(LOGDEBUG, "Sensor %d saves data [%f,%f]", s->id, d.time_stamp, d.value);
                        Data_Save(&(s->data_file), &d, 1); // Record it
                }
        }
        
        // Needed to keep pthreads happy
+
+       Log(LOGDEBUG, "Sensor %d finished", s->id);
        return NULL;
 }
 
@@ -220,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");
@@ -280,61 +295,28 @@ void Sensor_Handler(FCGIContext *context, char * params)
        } SensorParams;
        
        // Fill values appropriately
-       if (!FCGI_ParseRequest(context, params, values, sizeof(values)))
+       if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
        {
                // 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);
+               FCGI_RejectJSON(context, "Invalid sensor id");
+               return;
        }
-
-       // Get Sensor
        Sensor * s = g_sensors+id;
        
+       DataFormat format = Data_GetFormat(&(values[FORMAT]));
+
        // 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);
-       }
+       // Print Data
+       Data_Handler(&(s->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time);
        
        // Finish response
        Sensor_EndResponse(context, id, format);

UCC git Repository :: git.ucc.asn.au