X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Fcontrol.c;h=70fc2ee4938d06569f451faedd563b568322812e;hb=a12a70d4f860bafb6268a20616fbaa4ca7694af6;hp=7f5ea34c0509fc59eb1c5b5af96de9387fd12622;hpb=0a7664de84e25474991ead0d76dc9aea58e61aba;p=matches%2FMCTX3420.git diff --git a/server/control.c b/server/control.c index 7f5ea34..70fc2ee 100644 --- a/server/control.c +++ b/server/control.c @@ -1,6 +1,55 @@ +/** + * @file control.c + * @brief Handles all client control requests (admin/actuator related) + */ #include "common.h" #include "control.h" +const char * g_actuator_names[NUMACTUATORS] = { + "Pressure regulator", "Solenoid 1" +}; + +/** + * Handles control of the actuators. + */ +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."); + } +} + /** * System control handler. This covers control over all aspects of the system. * E.g: Actuators, system commands (start/stop experiment/recording) etc @@ -8,43 +57,47 @@ * @param params The input parameters */ void Control_Handler(FCGIContext *context, char *params) { - const char *key, *value, *loginkey = NULL, *action = NULL; + const char *action, *key = "", *mode = "", *name = ""; bool force = false; - - while ((params = FCGI_KeyPair(params, &key, &value))) { - if (!strcmp(key, "action")) - action = value; - else if (!strcmp(key, "key")) - loginkey = value; - else if (!strcmp(key, "force")) - force = !force; - else if (!strcmp(key, "id")) { - - } - else if (!strcmp(key, "value")) { - - } - } - - if (!strcmp(action, "start")) { - FCGI_Authorize(context, force); - } else if (!strcmp(action, "stop")) { //Don't require control key to stop... - //EMERGENCY STOP!! - FCGI_BeginJSON(context, STATUS_OK); - FCGI_JSONPair("description", "stopped!"); //Not really - FCGI_EndJSON(); - } else { - if (!FCGI_Authorized(context, loginkey)) { - FCGI_BeginJSON(context, STATUS_UNAUTHORIZED); - FCGI_JSONPair("description", "Invalid key specified."); - FCGI_EndJSON(); - return; - } else if (!strcmp(action, "end")) { - FCGI_AuthorizeEnd(context); - } else if (!strcmp(action, "set")) { - FCGI_BeginJSON(context, STATUS_OK); - FCGI_JSONPair("description", "actuated!"); - FCGI_EndJSON(); + + FCGIValue values[5] = { + {"action", &action, FCGI_REQUIRED(FCGI_STRING_T)}, + {"key", &key, FCGI_STRING_T}, + {"force", &force, FCGI_BOOL_T}, + {"mode", &mode, FCGI_STRING_T}, + {"name", &name, FCGI_STRING_T} + }; + + if (!FCGI_ParseRequest(context, params, values, 5)) + return; + + if (!strcmp(action, "gain")) { + FCGI_BeginControl(context, force); + } else { + if (!FCGI_HasControl(context, key)) { + FCGI_RejectJSONEx(context, + STATUS_UNAUTHORIZED, "Invalid control key specified."); + + } else if (!strcmp(action, "release")) { + FCGI_EndControl(context); + } else if (!strcmp(action, "experiment")) { + if (!strcmp(mode, "start")) { + FCGI_BeginJSON(context, STATUS_OK); + FCGI_JSONPair("description", mode); + FCGI_EndJSON(); + } else if (!strcmp(mode, "pause")) { + FCGI_BeginJSON(context, STATUS_OK); + FCGI_JSONPair("description", mode); + FCGI_EndJSON(); + } else if (!strcmp(mode, "stop")) { + FCGI_BeginJSON(context, STATUS_OK); + FCGI_JSONPair("description", mode); + FCGI_EndJSON(); + } else { + FCGI_RejectJSON(context, "Unknown experiment mode specified"); + } + } else { + FCGI_RejectJSON(context, "Unknown action specified"); } } }