Change FCGI_Authorize* to FCGI_*Control. Add actuator test code.
[matches/MCTX3420.git] / server / control.c
index 7f5ea34..12e340a 100644 (file)
@@ -1,6 +1,47 @@
 #include "common.h"
 #include "control.h"
 
+/**
+ * Handles control of the actuators.
+ */
+void ActuatorHandler(FCGIContext *context, int id, const char *set_value) {
+       char *ptr;
+       
+       switch(id) { //Add new actuators here
+               case ACT_PREG: //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);
+                       }
+               } 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
  * @param params The input parameters
  */
 void Control_Handler(FCGIContext *context, char *params) {
-       const char *key, *value, *loginkey = NULL, *action = NULL;
+       const char *key, *value, *control_key = NULL;
+       const char *action = NULL, *set_value = NULL;
        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"))
-                       loginkey = value;
+                       control_key = value;
                else if (!strcmp(key, "force"))
                        force = !force;
-               else if (!strcmp(key, "id")) {
-               
-               }
-               else if (!strcmp(key, "value")) {
-               
+               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 (!strcmp(action, "start")) {
-               FCGI_Authorize(context, force);
+       if (action == NULL) { //Must have an action
+               FCGI_RejectJSON(context);
+       } else if (!strcmp(action, "start")) {
+               FCGI_BeginControl(context, force);
        } else if (!strcmp(action, "stop")) { //Don't require control key to stop...
-               //EMERGENCY STOP!!
+               //EMERGENCY STOP!! TODO - replace!
                FCGI_BeginJSON(context, STATUS_OK);
-               FCGI_JSONPair("description", "stopped!"); //Not really
+               FCGI_JSONPair("description", "stopped! (not)");
                FCGI_EndJSON();
-       } else {
-               if (!FCGI_Authorized(context, loginkey)) {
-                       FCGI_BeginJSON(context, STATUS_UNAUTHORIZED);
-                       FCGI_JSONPair("description", "Invalid key specified.");
-                       FCGI_EndJSON();
-                       return;
+       } else { //Under this section, the user must have the current control key.
+               if (!FCGI_HasControl(context, control_key)) {
+                       FCGI_RejectJSONEx(context, 
+                               STATUS_UNAUTHORIZED, "Invalid control key specified.");
                } else if (!strcmp(action, "end")) {
-                       FCGI_AuthorizeEnd(context);
+                       FCGI_EndControl(context);
                } else if (!strcmp(action, "set")) {
-                       FCGI_BeginJSON(context, STATUS_OK);
-                       FCGI_JSONPair("description", "actuated!");
-                       FCGI_EndJSON();
+                       if (set_value == NULL || *set_value == '\0') {
+                               FCGI_RejectJSONEx(context, 
+                                       STATUS_ERROR, "Set called but no value specified.");
+                       } else {
+                               ActuatorHandler(context, id, set_value);
+                       }
                }
        }
 }

UCC git Repository :: git.ucc.asn.au