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

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