X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Fcontrol.c;h=70fc2ee4938d06569f451faedd563b568322812e;hb=a12a70d4f860bafb6268a20616fbaa4ca7694af6;hp=59b5bf80d8ec582d53873c0184730418daca1527;hpb=8aa358c02e86aee0486c1951ee3c5634cb7586a1;p=matches%2FMCTX3420.git diff --git a/server/control.c b/server/control.c index 59b5bf8..70fc2ee 100644 --- a/server/control.c +++ b/server/control.c @@ -5,14 +5,18 @@ #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, int id, const char *set_value) { +void ActuatorHandler(FCGIContext *context, ActuatorId id, const char *set_value) { char *ptr; switch(id) { //Add new actuators here - case ACT_PREG: //Suppose is pressure regulator. 0-700 input (kPa) + 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) { @@ -37,7 +41,7 @@ void ActuatorHandler(FCGIContext *context, int id, const char *set_value) { FCGI_JSONValue("\"Solenoid 1 turned %s!\"", state); FCGI_EndJSON(); } else { - FCGI_RejectJSON(context); + FCGI_RejectJSON(context, "Invalid actuator value specified"); } } break; default: @@ -53,51 +57,47 @@ void ActuatorHandler(FCGIContext *context, int id, const char *set_value) { * @param params The input parameters */ void Control_Handler(FCGIContext *context, char *params) { - const char *key, *value, *control_key = NULL; - const char *action = NULL, *set_value = NULL; + const char *action, *key = "", *mode = "", *name = ""; bool force = false; - char *ptr; - int id = ACT_NONE; - - while ((params = FCGI_KeyPair(params, &key, &value))) { - if (!strcmp(key, "action")) - action = value; - else if (!strcmp(key, "key")) - control_key = value; - else if (!strcmp(key, "force")) - force = !force; - else if (!strcmp(key, "id") && *value) { //Ensure non-empty value - int parsed = strtol(value, &ptr, 10); - if (*ptr == '\0') { - id = parsed; - } - } else if (!strcmp(key, "value")) { - set_value = value; - } - } - - if (action == NULL) { //Must have an action - FCGI_RejectJSON(context); - } else if (!strcmp(action, "start")) { + + 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 (!strcmp(action, "stop")) { //Don't require control key to stop... - //EMERGENCY STOP!! TODO - replace! - FCGI_BeginJSON(context, STATUS_OK); - FCGI_JSONPair("description", "stopped! (not)"); - FCGI_EndJSON(); - } else { //Under this section, the user must have the current control key. - if (!FCGI_HasControl(context, control_key)) { + } else { + if (!FCGI_HasControl(context, key)) { FCGI_RejectJSONEx(context, STATUS_UNAUTHORIZED, "Invalid control key specified."); - } else if (!strcmp(action, "end")) { + + } else if (!strcmp(action, "release")) { FCGI_EndControl(context); - } else if (!strcmp(action, "set")) { - if (set_value == NULL || *set_value == '\0') { - FCGI_RejectJSONEx(context, - STATUS_ERROR, "Set called but no value specified."); + } 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 { - ActuatorHandler(context, id, set_value); + FCGI_RejectJSON(context, "Unknown experiment mode specified"); } + } else { + FCGI_RejectJSON(context, "Unknown action specified"); } } }