Add Actuator related code
[matches/MCTX3420.git] / server / control.c
1 /**
2  * @file control.c
3  * @brief Handles all client control requests (admin/actuator related)
4  */
5 #include "common.h"
6 #include "control.h"
7
8
9 /**
10  * Handles control of the actuators.
11  *
12 void ActuatorHandler(FCGIContext *context, ActuatorId id, const char *set_value) {
13         char *ptr;
14         
15         switch(id) { //Add new actuators here
16                 case ACT_PRESSURE: //Suppose is pressure regulator. 0-700 input (kPa)
17                 {
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);
23                                 FCGI_EndJSON();
24                         } else {
25                                 FCGI_RejectJSONEx(context, 
26                                         STATUS_ERROR, "Invalid pressure specified.");
27                         }
28                 } break;
29                 case ACT_SOLENOID1:
30                 {
31                         int value = strtol(set_value, &ptr, 10);
32                         if (*ptr == '\0') {
33                                 const char *state = "off";
34                                 if (value)
35                                         state = "on";
36                                 FCGI_BeginJSON(context, STATUS_OK);
37                                 FCGI_JSONKey("description");
38                                 FCGI_JSONValue("\"Solenoid 1 turned %s!\"", state);
39                                 FCGI_EndJSON();
40                         } else {
41                                 FCGI_RejectJSON(context, "Invalid actuator value specified");
42                         }
43                 } break;
44                 default:
45                         FCGI_RejectJSONEx(context, 
46                                 STATUS_ERROR, "Invalid actuator id specified.");
47         }
48 }
49 */
50
51 /**
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
56  */
57 void Control_Handler(FCGIContext *context, char *params) {
58         const char *key, *value, *control_key = NULL;
59         const char *action = NULL, *set_value = NULL;
60         bool force = false;
61         char *ptr;
62         int id = -1;
63         
64         while ((params = FCGI_KeyPair(params, &key, &value))) {
65                 if (!strcmp(key, "action"))
66                         action = value;
67                 else if (!strcmp(key, "key"))
68                         control_key = value;
69                 else if (!strcmp(key, "force"))
70                         force = !force;
71                 else if (!strcmp(key, "id") && *value) { //Ensure non-empty value
72                         int parsed = strtol(value, &ptr, 10);
73                         if (*ptr == '\0') {
74                                 id = parsed;
75                         }
76                 } else if (!strcmp(key, "value")) {
77                         set_value = value;
78                 }
79         }
80         Log(LOGDEBUG, "Id %d", id); // to stop compiler complaining for now
81         
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)");
90                 FCGI_EndJSON();
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.");
101                         } else 
102                         {
103 //                              ActuatorHandler(context, id, set_value);
104                         }
105                 }
106         }
107 }

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