*/
#include "actuator.h"
+#include "control.h"
#include "options.h"
/** Array of Actuators (global to this file) initialised by Actuator_Init **/
DataFormat format = Data_GetFormat(&(values[FORMAT]));
- // Begin response
- Actuator_BeginResponse(context, id, format);
-
- // Set?
- if (FCGI_RECEIVED(values[SET].flags))
+ if (Control_Lock())
{
- if (format == JSON)
- FCGI_JSONDouble("set", set);
-
- ActuatorControl c;
- c.value = set;
+ // Begin response
+ Actuator_BeginResponse(context, id, format);
- Actuator_SetControl(a, &c);
- }
+ // Set?
+ if (FCGI_RECEIVED(values[SET].flags))
+ {
+ if (format == JSON)
+ FCGI_JSONDouble("set", set);
+
+ ActuatorControl c;
+ c.value = set;
- // Print Data
- Data_Handler(&(a->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time);
-
- // Finish response
- Actuator_EndResponse(context, id, format);
+ Actuator_SetControl(a, &c);
+ }
+
+ // Print Data
+ Data_Handler(&(a->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time);
+
+ // Finish response
+ Actuator_EndResponse(context, id, format);
+
+ Control_Unlock();
+ }
+ else
+ {
+ FCGI_RejectJSON(context, "Experiment is not running.");
+ }
}
*/
#include "common.h"
#include "control.h"
-
-
-const char * g_actuator_names[NUMACTUATORS] = {
- "Pressure regulator", "Solenoid 1"
-};
-
-/*
-void ActuatorHandler(FCGIContext *context, ActuatorId id, const char *set_value) {
- char *ptr;
-
- switch(id) { //Add new actuators here
- case ACT_PRESSURE: //Suppose is pressure regulator. 0-700 input (kPa)
- {
- int value = strtol(set_value, &ptr, 10);
- if (*ptr == '\0' && value >= 0 && value <= 700) {
- FCGI_BeginJSON(context, STATUS_OK);
- FCGI_JSONKey("description");
- FCGI_JSONValue("\"Set pressure to %d kPa!\"", value);
- FCGI_EndJSON();
- } else {
- FCGI_RejectJSONEx(context,
- STATUS_ERROR, "Invalid pressure specified.");
- }
- } break;
- case ACT_SOLENOID1:
- {
- int value = strtol(set_value, &ptr, 10);
- if (*ptr == '\0') {
- const char *state = "off";
- if (value)
- state = "on";
- FCGI_BeginJSON(context, STATUS_OK);
- FCGI_JSONKey("description");
- FCGI_JSONValue("\"Solenoid 1 turned %s!\"", state);
- FCGI_EndJSON();
- } else {
- FCGI_RejectJSON(context, "Invalid actuator value specified");
- }
- } break;
- default:
- FCGI_RejectJSONEx(context,
- STATUS_ERROR, "Invalid actuator id specified.");
- }
-}*/
+#include "sensor.h"
+#include "actuator.h"
typedef enum ControlState {
STATE_STOPPED,
}
bool Control_Start(const char *experiment_name) {
+ bool ret = false;
+
pthread_mutex_lock(&(g_controls.mutex));
if (g_controls.state == STATE_STOPPED) {
gettimeofday(&(g_controls.start_time), NULL);
Sensor_StartAll(experiment_name);
+ Actuator_StartAll(experiment_name);
g_controls.state = STATE_RUNNING;
-
- pthread_mutex_unlock(&(g_controls.mutex));
- return true;
+ ret = true;
}
- return false;
pthread_mutex_unlock(&(g_controls.mutex));
+ return ret;
}
+
void Control_Pause() {
pthread_mutex_lock(&(g_controls.mutex));
pthread_mutex_unlock(&(g_controls.mutex));
}
-bool Control_End() {
+bool Control_Stop() {
+ bool ret = false;
+
pthread_mutex_lock(&(g_controls.mutex));
if (g_controls.state != STATE_STOPPED) {
+ Actuator_StopAll();
Sensor_StopAll();
g_controls.state = STATE_STOPPED;
-
- pthread_mutex_unlock(&(g_controls.mutex));
- return true;
+ ret = true;
}
pthread_mutex_unlock(&(g_controls.mutex));
+ return ret;
+}
+
+bool Control_Lock() {
+ pthread_mutex_lock(&(g_controls.mutex));
+ if (g_controls.state == STATE_RUNNING)
+ return true;
+ pthread_mutex_unlock(&(g_controls.mutex));
return false;
}
+
+void Control_Unlock() {
+ pthread_mutex_unlock(&(g_controls.mutex));
+}
\ No newline at end of file
extern void Control_Handler(FCGIContext *context, char *params);
extern bool Control_Start(const char *experiment_name);
extern void Control_Pause();
-extern bool Control_End();
+extern bool Control_Stop();
+extern bool Control_Lock();
+extern void Control_Unlock();
#endif
*/
DataFormat Data_GetFormat(FCGIValue * fmt)
{
- char * fmt_str = *(char**)(fmt->value);
+ const char * fmt_str = *(const char**)(fmt->value);
// Check if format type was specified
if (FCGI_RECEIVED(fmt->flags))
{
/**
- * @file datapoint.h
+ * @file data.h
* @purpose Declaration of data handling functions; saving, loading, displaying, selecting.
*/
}
/**
- * Aids in parsing request parameters. Expected keys along with their type
- * and whether or not they're required are provided. This function will then
- * parse the parameter string to find these keys.
+ * Aids in parsing request parameters.
+ * Input: The expected keys along with their type and whether or not
+ * they're required.
* @param context The context to work in
* @param params The parameter string to be parsed
* @param values An array of FCGIValue's that specify expected keys
}
if (FCGI_TYPE(val->flags) == FCGI_INT_T)
- *((int*) val->value) = parsed;
+ *((int*) val->value) = (int) parsed;
else
*((long*) val->value) = parsed;
} break;
#include "options.h"
#include "sensor.h"
#include "actuator.h"
+#include "control.h"
// --- Standard headers --- //
#include <signal.h> // for signal handling
*/
Sensor_Init();
Actuator_Init();
- Sensor_StartAll("test");
- Actuator_StartAll("test");
+ //Sensor_StartAll("test");
+ //Actuator_StartAll("test");
+ Control_Start("test");
// run request thread in the main thread
FCGI_RequestLoop(NULL);
- Sensor_StopAll();
- Actuator_StopAll();
+ Control_Stop();
+ //Sensor_StopAll();
+ //Actuator_StopAll();
Cleanup();
return 0;
*/
#include "common.h"
+#include "control.h"
#include "sensor.h"
#include "options.h"
#include <math.h>
{1,-1,1,-1}, // ANALOG_TEST0
{500,0,499,0}, // ANALOG_TEST1
{5,-5,4,-4}, // ANALOG_FAIL0
- {1,0,1,0}, // DIGITAL_TEST0
- {1,0,1,0}, // DIGITAL_TEST1
- {1,0,1,0} // DIGITAL_FAIL0
+ {1,0,1,0}, // DIGITAL_TEST0
+ {1,0,1,0}, // DIGITAL_TEST1
+ {1,0,1,0} // DIGITAL_FAIL0
};
/** Human readable names for the sensors **/
return;
}
Sensor * s = g_sensors+id;
-
+
DataFormat format = Data_GetFormat(&(values[FORMAT]));
- // Begin response
- Sensor_BeginResponse(context, id, format);
+ if (Control_Lock())
+ {
+ // Begin response
+ Sensor_BeginResponse(context, id, format);
- // Print Data
- Data_Handler(&(s->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time);
-
- // Finish response
- Sensor_EndResponse(context, id, format);
-
+ // Print Data
+ Data_Handler(&(s->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time);
+
+ // Finish response
+ Sensor_EndResponse(context, id, format);
+
+ Control_Unlock();
+ }
+ else
+ {
+ FCGI_RejectJSON(context, "Experiment is not running.");
+ }
}