Change FCGI_Authorize* to FCGI_*Control. Add actuator test code.
[matches/MCTX3420.git] / server / control.c
1 #include "common.h"
2 #include "control.h"
3
4 /**
5  * Handles control of the actuators.
6  */
7 void ActuatorHandler(FCGIContext *context, int id, const char *set_value) {
8         char *ptr;
9         
10         switch(id) { //Add new actuators here
11                 case ACT_PREG: //Suppose is pressure regulator. 0-700 input (kPa)
12                 {
13                         int value = strtol(set_value, &ptr, 10);
14                         if (*ptr == '\0' && value >= 0 && value <= 700) {
15                                 FCGI_BeginJSON(context, STATUS_OK);
16                                 FCGI_JSONKey("description");
17                                 FCGI_JSONValue("\"Set pressure to %d kPa!\"", value);
18                                 FCGI_EndJSON();
19                         } else {
20                                 FCGI_RejectJSONEx(context, 
21                                         STATUS_ERROR, "Invalid pressure specified.");
22                         }
23                 } break;
24                 case ACT_SOLENOID1:
25                 {
26                         int value = strtol(set_value, &ptr, 10);
27                         if (*ptr == '\0') {
28                                 const char *state = "off";
29                                 if (value)
30                                         state = "on";
31                                 FCGI_BeginJSON(context, STATUS_OK);
32                                 FCGI_JSONKey("description");
33                                 FCGI_JSONValue("\"Solenoid 1 turned %s!\"", state);
34                                 FCGI_EndJSON();
35                         } else {
36                                 FCGI_RejectJSON(context);
37                         }
38                 } break;
39                 default:
40                         FCGI_RejectJSONEx(context, 
41                                 STATUS_ERROR, "Invalid actuator id specified.");
42         }
43 }
44
45 /**
46  * System control handler. This covers control over all aspects of the system.
47  * E.g: Actuators, system commands (start/stop experiment/recording) etc
48  * @param context The context to work in
49  * @param params The input parameters
50  */
51 void Control_Handler(FCGIContext *context, char *params) {
52         const char *key, *value, *control_key = NULL;
53         const char *action = NULL, *set_value = NULL;
54         bool force = false;
55         char *ptr;
56         int id = ACT_NONE;
57         
58         while ((params = FCGI_KeyPair(params, &key, &value))) {
59                 if (!strcmp(key, "action"))
60                         action = value;
61                 else if (!strcmp(key, "key"))
62                         control_key = value;
63                 else if (!strcmp(key, "force"))
64                         force = !force;
65                 else if (!strcmp(key, "id") && *value) { //Ensure non-empty value
66                         int parsed = strtol(value, &ptr, 10);
67                         if (*ptr == '\0') {
68                                 id = parsed;
69                         }
70                 } else if (!strcmp(key, "value")) {
71                         set_value = value;
72                 }
73         }
74         
75         if (action == NULL) { //Must have an action
76                 FCGI_RejectJSON(context);
77         } else if (!strcmp(action, "start")) {
78                 FCGI_BeginControl(context, force);
79         } else if (!strcmp(action, "stop")) { //Don't require control key to stop...
80                 //EMERGENCY STOP!! TODO - replace!
81                 FCGI_BeginJSON(context, STATUS_OK);
82                 FCGI_JSONPair("description", "stopped! (not)");
83                 FCGI_EndJSON();
84         } else { //Under this section, the user must have the current control key.
85                 if (!FCGI_HasControl(context, control_key)) {
86                         FCGI_RejectJSONEx(context, 
87                                 STATUS_UNAUTHORIZED, "Invalid control key specified.");
88                 } else if (!strcmp(action, "end")) {
89                         FCGI_EndControl(context);
90                 } else if (!strcmp(action, "set")) {
91                         if (set_value == NULL || *set_value == '\0') {
92                                 FCGI_RejectJSONEx(context, 
93                                         STATUS_ERROR, "Set called but no value specified.");
94                         } else {
95                                 ActuatorHandler(context, id, set_value);
96                         }
97                 }
98         }
99 }

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