3 * @brief Handles all client control requests (admin/actuator related)
9 * Handles control of the actuators.
11 void ActuatorHandler(FCGIContext *context, int id, const char *set_value) {
14 switch(id) { //Add new actuators here
15 case ACT_PREG: //Suppose is pressure regulator. 0-700 input (kPa)
17 int value = strtol(set_value, &ptr, 10);
18 if (*ptr == '\0' && value >= 0 && value <= 700) {
19 FCGI_BeginJSON(context, STATUS_OK);
20 FCGI_JSONKey("description");
21 FCGI_JSONValue("\"Set pressure to %d kPa!\"", value);
24 FCGI_RejectJSONEx(context,
25 STATUS_ERROR, "Invalid pressure specified.");
30 int value = strtol(set_value, &ptr, 10);
32 const char *state = "off";
35 FCGI_BeginJSON(context, STATUS_OK);
36 FCGI_JSONKey("description");
37 FCGI_JSONValue("\"Solenoid 1 turned %s!\"", state);
40 FCGI_RejectJSON(context);
44 FCGI_RejectJSONEx(context,
45 STATUS_ERROR, "Invalid actuator id specified.");
50 * System control handler. This covers control over all aspects of the system.
51 * E.g: Actuators, system commands (start/stop experiment/recording) etc
52 * @param context The context to work in
53 * @param params The input parameters
55 void Control_Handler(FCGIContext *context, char *params) {
56 const char *key, *value, *control_key = NULL;
57 const char *action = NULL, *set_value = NULL;
62 while ((params = FCGI_KeyPair(params, &key, &value))) {
63 if (!strcmp(key, "action"))
65 else if (!strcmp(key, "key"))
67 else if (!strcmp(key, "force"))
69 else if (!strcmp(key, "id") && *value) { //Ensure non-empty value
70 int parsed = strtol(value, &ptr, 10);
74 } else if (!strcmp(key, "value")) {
79 if (action == NULL) { //Must have an action
80 FCGI_RejectJSON(context);
81 } else if (!strcmp(action, "start")) {
82 FCGI_BeginControl(context, force);
83 } else if (!strcmp(action, "stop")) { //Don't require control key to stop...
84 //EMERGENCY STOP!! TODO - replace!
85 FCGI_BeginJSON(context, STATUS_OK);
86 FCGI_JSONPair("description", "stopped! (not)");
88 } else { //Under this section, the user must have the current control key.
89 if (!FCGI_HasControl(context, control_key)) {
90 FCGI_RejectJSONEx(context,
91 STATUS_UNAUTHORIZED, "Invalid control key specified.");
92 } else if (!strcmp(action, "end")) {
93 FCGI_EndControl(context);
94 } else if (!strcmp(action, "set")) {
95 if (set_value == NULL || *set_value == '\0') {
96 FCGI_RejectJSONEx(context,
97 STATUS_ERROR, "Set called but no value specified.");
99 ActuatorHandler(context, id, set_value);