3 * @brief Handles all client control requests (admin/actuator related)
10 * Handles control of the actuators.
12 void ActuatorHandler(FCGIContext *context, ActuatorId id, const char *set_value) {
15 switch(id) { //Add new actuators here
16 case ACT_PRESSURE: //Suppose is pressure regulator. 0-700 input (kPa)
18 int value = strtol(set_value, &ptr, 10);
19 if (*ptr == '\0' && value >= 0 && value <= 700) {
20 FCGI_BeginJSON(context, STATUS_OK);
21 FCGI_JSONKey("description");
22 FCGI_JSONValue("\"Set pressure to %d kPa!\"", value);
25 FCGI_RejectJSONEx(context,
26 STATUS_ERROR, "Invalid pressure specified.");
31 int value = strtol(set_value, &ptr, 10);
33 const char *state = "off";
36 FCGI_BeginJSON(context, STATUS_OK);
37 FCGI_JSONKey("description");
38 FCGI_JSONValue("\"Solenoid 1 turned %s!\"", state);
41 FCGI_RejectJSON(context, "Invalid actuator value specified");
45 FCGI_RejectJSONEx(context,
46 STATUS_ERROR, "Invalid actuator id specified.");
52 * System control handler. This covers control over all aspects of the system.
53 * E.g: Actuators, system commands (start/stop experiment/recording) etc
54 * @param context The context to work in
55 * @param params The input parameters
57 void Control_Handler(FCGIContext *context, char *params) {
58 const char *key, *value, *control_key = NULL;
59 const char *action = NULL, *set_value = NULL;
64 while ((params = FCGI_KeyPair(params, &key, &value))) {
65 if (!strcmp(key, "action"))
67 else if (!strcmp(key, "key"))
69 else if (!strcmp(key, "force"))
71 else if (!strcmp(key, "id") && *value) { //Ensure non-empty value
72 int parsed = strtol(value, &ptr, 10);
76 } else if (!strcmp(key, "value")) {
80 Log(LOGDEBUG, "Id %d", id); // to stop compiler complaining for now
82 if (action == NULL) { //Must have an action
83 FCGI_RejectJSON(context, "No action specified");
84 } else if (!strcmp(action, "start")) {
85 FCGI_BeginControl(context, force);
86 } else if (!strcmp(action, "stop")) { //Don't require control key to stop...
87 //EMERGENCY STOP!! TODO - replace!
88 FCGI_BeginJSON(context, STATUS_OK);
89 FCGI_JSONPair("description", "stopped! (not)");
91 } else { //Under this section, the user must have the current control key.
92 if (!FCGI_HasControl(context, control_key)) {
93 FCGI_RejectJSONEx(context,
94 STATUS_UNAUTHORIZED, "Invalid control key specified.");
95 } else if (!strcmp(action, "end")) {
96 FCGI_EndControl(context);
97 } else if (!strcmp(action, "set")) {
98 if (set_value == NULL || *set_value == '\0') {
99 FCGI_RejectJSONEx(context,
100 STATUS_ERROR, "Set called but no value specified.");
103 // ActuatorHandler(context, id, set_value);