Revision 71 logic/trunk/src/startup/main.c
main.c | ||
---|---|---|
11 | 11 |
#include <unistd.h> |
12 | 12 |
#include "../hci_comm/hciChanged.h" |
13 | 13 |
#include "../hci_comm/hci_comm.h" |
14 |
#include "../drv_comm/drv_comm.h" |
|
14 | 15 |
#include "../init/scenariolist.h" |
15 | 16 |
#include "../init/devicelist.h" |
16 | 17 |
#include "../logics/device_threads.h" |
... | ... | |
55 | 56 |
int reinitialiseLogic(); // on HCI request |
56 | 57 |
int shutDownLogic(); |
57 | 58 |
|
59 |
// devices |
|
60 |
const char *dev1="/dev/dev_thermometer_01"; |
|
58 | 61 |
// Application entry point |
59 | 62 |
int main(int argc, char *argv[]) { |
60 |
//** //DAEMON
|
|
63 |
/** //DAEMON |
|
61 | 64 |
pid_t pid, sid; |
62 | 65 |
|
63 | 66 |
pid = fork(); |
... | ... | |
167 | 170 |
scenario *scen; |
168 | 171 |
device *inDev; |
169 | 172 |
int active = 0; |
170 |
float scenarioBuffer; |
|
171 | 173 |
//printf("My scen ID> %ld\n",pthread_self()); |
172 | 174 |
|
173 | 175 |
/*load scenario*/ |
... | ... | |
177 | 179 |
// TODO add pointer to this device (all of them actually) to scenario structure - problems with segfault |
178 | 180 |
//indev = scen->inDev; |
179 | 181 |
inDev = getDevice(hDevice, scen->inDevice_id); |
180 |
scen->buf=inDev->buf;
|
|
181 |
printf("Scenario %s buffer updated to %.2f!\n", scen->scen_id, inDev->buf);
|
|
182 |
scen->buf=inDev->buf/10; //TODO make sure buffer values make sense!
|
|
183 |
//printf("Scenario %s buffer updated to %.2f!\n", scen->scen_id, inDev->buf/10);
|
|
182 | 184 |
//set its value |
183 | 185 |
//* |
184 | 186 |
while(1) //TODO signal == true |
185 | 187 |
{ |
186 | 188 |
/// main scenario loop |
187 | 189 |
//printf("inDev->buf %.2f\n", 1.0f*inDev->buf); |
190 |
printf("scen->buf %.2f\n", 1.0f*scen->buf); |
|
188 | 191 |
if(active) |
189 | 192 |
sleep(scen->actvChkItv); |
190 | 193 |
else |
191 | 194 |
sleep(scen->idleChkItv); |
195 |
|
|
192 | 196 |
//checking primary condition |
193 |
if(scen->buf) // is not null |
|
197 |
scen->buf = scen->buf*scen->alpha+inDev->buf*(1-scen->alpha)/10; |
|
198 |
//printf("Scenario %s buffer updated to %.2f, alpha: %.2f!\n", scen->scen_id, scen->buf, scen->alpha); |
|
199 |
syslog(LOG_MAIL, "%s:%.2f", scen->inDevice_id, scen->buf); |
|
200 |
active = 1; |
|
201 |
|
|
202 |
if((scen->buf > scen->optval + scen->tolval) || |
|
203 |
(scen->buf < scen->optval - scen->tolval)) |
|
194 | 204 |
{ |
195 |
scen->buf = scen->buf*scen->alpha+inDev->buf*(1-scen->alpha); |
|
196 |
//printf("Scenario %s buffer updated to %f, alpha: %.2f!\n", scen->scen_id, scenarioBuffer, scen->alpha); |
|
197 |
syslog(LOG_MAIL, "%s:%.2f", scen->inDevice_id, scen->buf); |
|
198 |
active = 1; |
|
199 |
|
|
200 |
if((scen->buf > scen->optval + scen->tolval) || |
|
201 |
(scen->buf < scen->optval - scen->tolval)) |
|
205 |
if(evaluateAdditionalConditions(scen->cond_l, hDevice)) |
|
202 | 206 |
{ |
203 |
if(evaluateAdditionalConditions(scen->cond_l, hDevice)) |
|
204 |
{ |
|
205 |
// TODO syslog - "action:scen_ID:power_percentage" |
|
206 |
int power = getPowerPercentage(scen->minval, scen->maxval, scen->optval, &scen->buf, scen->func); |
|
207 |
// TODO syslog - "action:scen_ID:power_percentage" |
|
208 |
int power = getPowerPercentage(scen->minval, scen->maxval, scen->optval, &scen->buf, scen->func); |
|
207 | 209 |
|
208 |
/// Forking to execute action!
|
|
209 |
pid_t pid, sid;
|
|
210 |
/// Forking to execute action! |
|
211 |
pid_t pid;
|
|
210 | 212 |
|
211 |
pid = fork(); |
|
212 |
if (pid < 0) { |
|
213 |
exit(EXIT_FAILURE); |
|
214 |
} |
|
215 |
// ignore parent |
|
216 |
if (pid > 0) {} |
|
217 |
else |
|
218 |
{ |
|
219 |
/*TODO execute action with parameter(s)*/ |
|
220 |
syslog(LOG_MAIL, "%s:%s:%.2f", scen->action, scen->scen_id, power); |
|
213 |
pid = fork(); |
|
214 |
if (pid < 0) { |
|
215 |
exit(EXIT_FAILURE); |
|
216 |
} |
|
217 |
// ignore parent |
|
218 |
if (pid > 0) { |
|
219 |
} |
|
220 |
else |
|
221 |
{ |
|
222 |
/*TODO execute action with parameter(s)*/ |
|
223 |
syslog(LOG_MAIL, "%s:%s:%.2f", scen->action, scen->scen_id, power); |
|
221 | 224 |
|
222 |
execl(scen->action, power);
|
|
223 |
printf("Zaganjam napravo %s z mocjo %d\n", scen->scen_id, power);
|
|
225 |
execl(scen->action, power); |
|
226 |
printf("Zaganjam napravo %s z mocjo %d\n", scen->scen_id, power); |
|
224 | 227 |
|
225 |
shutDownLogic();
|
|
226 |
}
|
|
228 |
shutDownLogic(); |
|
229 |
} |
|
227 | 230 |
|
228 |
} |
|
229 |
else |
|
230 |
printf("Scenario %s not updated!\n", scen->scen_id); |
|
231 | 231 |
} |
232 | 232 |
else |
233 |
active = 0;
|
|
233 |
printf("Scenario %s not updated, additional conditions false!\n", scen->scen_id);
|
|
234 | 234 |
} |
235 |
else |
|
236 |
active = 0; |
|
235 | 237 |
/* |
236 | 238 |
if(active) |
237 | 239 |
printf("%s main buffer %.2f, sleeping %d\n",scen->scen_id, scen->buf, scen->actvChkItv); |
... | ... | |
254 | 256 |
//set its value |
255 | 257 |
while(1) //TODO signal == true |
256 | 258 |
{ |
257 |
dev->buf=random()%10+20;// random 20-30 |
|
259 |
dev->buf=get_device_value(dev1); |
|
260 |
//dev->buf=random()%10+20;// random 20-30 |
|
258 | 261 |
//printf("device %s buf = %d\n", dev->id, dev->buf); |
259 | 262 |
sleep(dev->readitv); |
260 | 263 |
} |
Also available in: Unified diff