Code cleanup and partial modification to control 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 const char * g_actuator_names[NUMACTUATORS] = { 
9         "Pressure regulator", "Solenoid 1" 
10 };
11
12 /**
13  * Handles control of the actuators.
14  */
15 void ActuatorHandler(FCGIContext *context, ActuatorId id, const char *set_value) {
16         char *ptr;
17         
18         switch(id) { //Add new actuators here
19                 case ACT_PRESSURE: //Suppose is pressure regulator. 0-700 input (kPa)
20                 {
21                         int value = strtol(set_value, &ptr, 10);
22                         if (*ptr == '\0' && value >= 0 && value <= 700) {
23                                 FCGI_BeginJSON(context, STATUS_OK);
24                                 FCGI_JSONKey("description");
25                                 FCGI_JSONValue("\"Set pressure to %d kPa!\"", value);
26                                 FCGI_EndJSON();
27                         } else {
28                                 FCGI_RejectJSONEx(context, 
29                                         STATUS_ERROR, "Invalid pressure specified.");
30                         }
31                 } break;
32                 case ACT_SOLENOID1:
33                 {
34                         int value = strtol(set_value, &ptr, 10);
35                         if (*ptr == '\0') {
36                                 const char *state = "off";
37                                 if (value)
38                                         state = "on";
39                                 FCGI_BeginJSON(context, STATUS_OK);
40                                 FCGI_JSONKey("description");
41                                 FCGI_JSONValue("\"Solenoid 1 turned %s!\"", state);
42                                 FCGI_EndJSON();
43                         } else {
44                                 FCGI_RejectJSON(context, "Invalid actuator value specified");
45                         }
46                 } break;
47                 default:
48                         FCGI_RejectJSONEx(context, 
49                                 STATUS_ERROR, "Invalid actuator id specified.");
50         }
51 }
52
53 /**
54  * System control handler. This covers control over all aspects of the system.
55  * E.g: Actuators, system commands (start/stop experiment/recording) etc
56  * @param context The context to work in
57  * @param params The input parameters
58  */
59 void Control_Handler(FCGIContext *context, char *params) {
60         const char *action, *key = "", *mode = "", *name = "";
61         bool force = false;
62
63         FCGIValue values[5] = {
64                 {"action", &action, FCGI_REQUIRED(FCGI_STRING_T)},
65                 {"key", &key, FCGI_STRING_T},
66                 {"force", &force, FCGI_BOOL_T},
67                 {"mode", &mode, FCGI_STRING_T},
68                 {"name", &name, FCGI_STRING_T}
69         };
70
71         if (!FCGI_ParseRequest(context, params, values, 5))
72                 return;
73
74         if (!strcmp(action, "gain")) {
75                 FCGI_BeginControl(context, force);
76         } else { 
77                 if (!FCGI_HasControl(context, key)) {
78                         FCGI_RejectJSONEx(context, 
79                                 STATUS_UNAUTHORIZED, "Invalid control key specified.");
80                         
81                 } else if (!strcmp(action, "release")) {
82                         FCGI_EndControl(context);
83                 } else if (!strcmp(action, "experiment")) {
84                         if (!strcmp(mode, "start")) {
85                                 FCGI_BeginJSON(context, STATUS_OK);
86                                 FCGI_JSONPair("description", mode);
87                                 FCGI_EndJSON();
88                         } else if (!strcmp(mode, "pause")) {
89                                 FCGI_BeginJSON(context, STATUS_OK);
90                                 FCGI_JSONPair("description", mode);
91                                 FCGI_EndJSON();
92                         } else if (!strcmp(mode, "stop")) {
93                                 FCGI_BeginJSON(context, STATUS_OK);
94                                 FCGI_JSONPair("description", mode);
95                                 FCGI_EndJSON();
96                         } else {
97                                 FCGI_RejectJSON(context, "Unknown experiment mode specified");
98                         }
99                 } else {
100                         FCGI_RejectJSON(context, "Unknown action specified");
101                 }
102         }
103 }

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