Add semi working control code
[matches/MCTX3420.git] / server / actuator.c
1 /**
2  * @file actuator.c
3  * @purpose Implementation of Actuator related functionality
4  */
5
6 #include "actuator.h"
7 #include "control.h"
8 #include "options.h"
9
10 /** Array of Actuators (global to this file) initialised by Actuator_Init **/
11 static Actuator g_actuators[NUMACTUATORS];
12
13 /** Human readable names for the Actuators **/
14 const char * g_actuator_names[NUMACTUATORS] = { 
15         "actuator_test0", "actuator_test1"
16 };
17
18 /**
19  * One off initialisation of *all* Actuators
20  */
21 void Actuator_Init()
22 {
23         for (int i = 0; i < NUMACTUATORS; ++i)
24         {
25                 g_actuators[i].id = i;
26                 Data_Init(&(g_actuators[i].data_file));
27                 pthread_mutex_init(&(g_actuators[i].mutex), NULL);
28         }
29 }
30
31 /**
32  * Start an Actuator
33  * @param a - The Actuator to start
34  * @param experiment_name - Prepended to DataFile filename
35  */
36 void Actuator_Start(Actuator * a, const char * experiment_name)
37 {
38         // Set filename
39         char filename[BUFSIZ];
40         if (sprintf(filename, "%s_a%d", experiment_name, a->id) >= BUFSIZ)
41         {
42                 Fatal("Experiment name \"%s\" too long (>%d)", experiment_name, BUFSIZ);
43         }
44
45         Log(LOGDEBUG, "Actuator %d with DataFile \"%s\"", a->id, filename);
46         // Open DataFile
47         Data_Open(&(a->data_file), filename);
48
49         a->activated = true; // Don't forget this
50         
51         a->control_changed = false;
52
53         // Create the thread
54         pthread_create(&(a->thread), NULL, Actuator_Loop, (void*)(a));
55 }
56
57 void Actuator_Pause(Actuator *a)
58 {
59         if (a->activated)
60         {
61                 a->activated = false;
62                 Actuator_SetControl(a, NULL);
63                 pthread_join(a->thread, NULL); // Wait for thread to exit       
64         }
65 }
66
67 void Actuator_Resume(Actuator *a)
68 {
69         if (!a->activated)
70         {
71                 a->activated = true; 
72                 pthread_create(&(a->thread), NULL, Actuator_Loop, (void*)(a));
73         }
74 }
75
76 /**
77  * Stop an Actuator
78  * @param s - The Actuator to stop
79  */
80 void Actuator_Stop(Actuator * a)
81 {
82         // Stop
83         Actuator_Pause(a);
84         Data_Close(&(a->data_file)); // Close DataFile
85
86 }
87
88 void Actuator_PauseAll()
89 {
90         for (int i = 0; i < NUMACTUATORS; ++i)
91                 Actuator_Pause(g_actuators+i);  
92 }
93
94 void Actuator_ResumeAll()
95 {
96         for (int i = 0; i < NUMACTUATORS; ++i)
97                 Actuator_Resume(g_actuators+i); 
98 }
99
100 /**
101  * Stop all Actuators
102  */
103 void Actuator_StopAll()
104 {
105         for (int i = 0; i < NUMACTUATORS; ++i)
106                 Actuator_Stop(g_actuators+i);
107 }
108
109 /**
110  * Start all Actuators
111  */
112 void Actuator_StartAll(const char * experiment_name)
113 {
114         for (int i = 0; i < NUMACTUATORS; ++i)
115                 Actuator_Start(g_actuators+i, experiment_name);
116 }
117
118 /**
119  * Actuator control thread
120  * @param arg - Cast to an Actuator*
121  * @returns NULL to keep pthreads happy
122  */
123 void * Actuator_Loop(void * arg)
124 {
125         Actuator * a = (Actuator*)(arg);
126         
127         // Loop until stopped
128         while (a->activated)
129         {
130                 pthread_mutex_lock(&(a->mutex));
131                 while (!a->control_changed)
132                 {
133                         pthread_cond_wait(&(a->cond), &(a->mutex));
134                 }
135                 a->control_changed = false;
136                 pthread_mutex_unlock(&(a->mutex));
137                 if (!a->activated)
138                         break;
139
140                 Actuator_SetValue(a, a->control.value);
141         }
142
143         //TODO: Cleanup?
144         
145         // Keep pthreads happy
146         return NULL;
147 }
148
149 /**
150  * Set an Actuators control variable
151  * @param a - Actuator to control 
152  * @param c - Control to set to
153  */
154 void Actuator_SetControl(Actuator * a, ActuatorControl * c)
155 {
156         pthread_mutex_lock(&(a->mutex));
157         if (c != NULL)
158                 a->control = *c;
159         a->control_changed = true;
160         pthread_cond_broadcast(&(a->cond));
161         pthread_mutex_unlock(&(a->mutex));
162         
163 }
164
165 /**
166  * Set an Actuator value
167  * @param a - The Actuator
168  * @param value - The value to set
169  */
170 void Actuator_SetValue(Actuator * a, double value)
171 {
172         // Set time stamp
173         struct timeval t;
174         gettimeofday(&t, NULL);
175
176         DataPoint d = {TIMEVAL_DIFF(t, *Control_GetStartTime()), value};
177         //TODO: Set actuator
178         switch (a->id)
179         {
180                 case ACTUATOR_TEST0:
181                         break;
182                 case ACTUATOR_TEST1:
183                         break;
184         }
185
186         Log(LOGDEBUG, "Actuator %s set to %f", g_actuator_names[a->id], value);
187
188         // Record the value
189         Data_Save(&(a->data_file), &d, 1);
190 }
191
192 /**
193  * Helper: Begin Actuator response in a given format
194  * @param context - the FCGIContext
195  * @param format - Format
196  * @param id - ID of Actuator
197  */
198 void Actuator_BeginResponse(FCGIContext * context, ActuatorId id, DataFormat format)
199 {
200         // Begin response
201         switch (format)
202         {
203                 case JSON:
204                         FCGI_BeginJSON(context, STATUS_OK);
205                         FCGI_JSONLong("id", id);
206                         break;
207                 default:
208                         FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
209                         break;
210         }
211 }
212
213 /**
214  * Helper: End Actuator response in a given format
215  * @param context - the FCGIContext
216  * @param id - ID of the Actuator
217  * @param format - Format
218  */
219 void Actuator_EndResponse(FCGIContext * context, ActuatorId id, DataFormat format)
220 {
221         // End response
222         switch (format)
223         {
224                 case JSON:
225                         FCGI_EndJSON();
226                         break;
227                 default:
228                         break;
229         }
230 }
231
232
233
234
235 /**
236  * Handle a request for an Actuator
237  * @param context - FCGI context
238  * @param params - Parameters passed
239  */
240 void Actuator_Handler(FCGIContext * context, char * params)
241 {
242         struct timeval now;
243         gettimeofday(&now, NULL);
244         double current_time = TIMEVAL_DIFF(now, *Control_GetStartTime());
245         int id = 0;
246         double set = 0;
247         double start_time = 0;
248         double end_time = current_time;
249         char * fmt_str;
250
251         // key/value pairs
252         FCGIValue values[] = {
253                 {"id", &id, FCGI_REQUIRED(FCGI_INT_T)}, 
254                 {"set", &set, FCGI_DOUBLE_T},
255                 {"start_time", &start_time, FCGI_DOUBLE_T},
256                 {"end_time", &end_time, FCGI_DOUBLE_T},
257                 {"format", &fmt_str, FCGI_STRING_T}
258         };
259
260         // enum to avoid the use of magic numbers
261         typedef enum {
262                 ID,
263                 SET,
264                 START_TIME,
265                 END_TIME,
266                 FORMAT
267         } ActuatorParams;
268         
269         // Fill values appropriately
270         if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
271         {
272                 // Error occured; FCGI_RejectJSON already called
273                 return;
274         }       
275
276         // Get the Actuator identified
277         Actuator * a = NULL;
278         if (id < 0 || id >= NUMACTUATORS)
279         {
280                 FCGI_RejectJSON(context, "Invalid Actuator id");
281                 return;
282         }
283         
284         a = g_actuators+id;
285
286         DataFormat format = Data_GetFormat(&(values[FORMAT]));
287
288         // Begin response
289         Actuator_BeginResponse(context, id, format);
290
291         // Set?
292         if (FCGI_RECEIVED(values[SET].flags))
293         {
294                 if (format == JSON)
295                         FCGI_JSONDouble("set", set);
296         
297                 ActuatorControl c;
298                 c.value = set;
299
300                 Actuator_SetControl(a, &c);
301         }
302
303         // Print Data
304         Data_Handler(&(a->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time);
305         
306         // Finish response
307         Actuator_EndResponse(context, id, format);
308 }

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