5 * Handles control of the actuators.
7 void ActuatorHandler(FCGIContext *context, int id, const char *set_value) {
10 switch(id) { //Add new actuators here
11 case ACT_PREG: //Suppose is pressure regulator. 0-700 input (kPa)
13 int value = strtol(set_value, &ptr, 10);
14 if (*ptr == '\0' && value >= 0 && value <= 700) {
15 FCGI_BeginJSON(context, STATUS_OK);
16 FCGI_JSONKey("description");
17 FCGI_JSONValue("\"Set pressure to %d kPa!\"", value);
20 FCGI_RejectJSONEx(context,
21 STATUS_ERROR, "Invalid pressure specified.");
26 int value = strtol(set_value, &ptr, 10);
28 const char *state = "off";
31 FCGI_BeginJSON(context, STATUS_OK);
32 FCGI_JSONKey("description");
33 FCGI_JSONValue("\"Solenoid 1 turned %s!\"", state);
36 FCGI_RejectJSON(context);
40 FCGI_RejectJSONEx(context,
41 STATUS_ERROR, "Invalid actuator id specified.");
46 * System control handler. This covers control over all aspects of the system.
47 * E.g: Actuators, system commands (start/stop experiment/recording) etc
48 * @param context The context to work in
49 * @param params The input parameters
51 void Control_Handler(FCGIContext *context, char *params) {
52 const char *key, *value, *control_key = NULL;
53 const char *action = NULL, *set_value = NULL;
58 while ((params = FCGI_KeyPair(params, &key, &value))) {
59 if (!strcmp(key, "action"))
61 else if (!strcmp(key, "key"))
63 else if (!strcmp(key, "force"))
65 else if (!strcmp(key, "id") && *value) { //Ensure non-empty value
66 int parsed = strtol(value, &ptr, 10);
70 } else if (!strcmp(key, "value")) {
75 if (action == NULL) { //Must have an action
76 FCGI_RejectJSON(context);
77 } else if (!strcmp(action, "start")) {
78 FCGI_BeginControl(context, force);
79 } else if (!strcmp(action, "stop")) { //Don't require control key to stop...
80 //EMERGENCY STOP!! TODO - replace!
81 FCGI_BeginJSON(context, STATUS_OK);
82 FCGI_JSONPair("description", "stopped! (not)");
84 } else { //Under this section, the user must have the current control key.
85 if (!FCGI_HasControl(context, control_key)) {
86 FCGI_RejectJSONEx(context,
87 STATUS_UNAUTHORIZED, "Invalid control key specified.");
88 } else if (!strcmp(action, "end")) {
89 FCGI_EndControl(context);
90 } else if (!strcmp(action, "set")) {
91 if (set_value == NULL || *set_value == '\0') {
92 FCGI_RejectJSONEx(context,
93 STATUS_ERROR, "Set called but no value specified.");
95 ActuatorHandler(context, id, set_value);