root / logic / trunk / src / init / scenariolist.c @ 42
History | View | Annotate | Download (8.29 KB)
1 | 30 | Janez1 | #include "scenariolist.h" |
---|---|---|---|
2 | 40 | Janez1 | #include "devicelist.h" |
3 | 30 | Janez1 | #include "../logics/conditions.h" |
4 | #include <mxml.h> |
||
5 | |||
6 | 40 | Janez1 | //extern device *hDevice; //device list
|
7 | |||
8 | 34 | Janez1 | short NUMBER_OF_SCENARIOS = 0; |
9 | 30 | Janez1 | |
10 | 34 | Janez1 | int getOperatorType(char *val) |
11 | { |
||
12 | if(0 == strcmp(val, "conditions")) |
||
13 | return TOP_NODE;
|
||
14 | else if(0 == strcmp(val, "and")) |
||
15 | return AND;
|
||
16 | else if(0 == strcmp(val, "nand")) |
||
17 | return NOT_AND;
|
||
18 | else if(0 == strcmp(val, "or")) |
||
19 | return OR;
|
||
20 | else if(0 == strcmp(val, "xor")) |
||
21 | return XOR;
|
||
22 | else if(0 == strcmp(val, "greater")) |
||
23 | return GREATER;
|
||
24 | else if(0 == strcmp(val, "greater_or_equal")) |
||
25 | return GREATER_OR_EQUAL;
|
||
26 | else if(0 == strcmp(val, "less")) |
||
27 | return LESS;
|
||
28 | else if(0 == strcmp(val, "less_or_equal")) |
||
29 | return LESS_OR_EQUAL;
|
||
30 | else
|
||
31 | printf("Unidentified condition \"%s\".\n", val);
|
||
32 | return NULL; |
||
33 | /**/
|
||
34 | } |
||
35 | 30 | Janez1 | |
36 | 34 | Janez1 | cond_lst *getScenCond(mxml_node_t *node_t) |
37 | { |
||
38 | cond_lst *cl, *head; |
||
39 | head = NULL;
|
||
40 | 40 | Janez1 | |
41 | 34 | Janez1 | while(node_t) // same level |
42 | { |
||
43 | //printf("operator name: %s\n", node_t->value.opaque);
|
||
44 | //*
|
||
45 | cl = (cond_lst *)malloc(sizeof(cond_lst));
|
||
46 | 30 | Janez1 | |
47 | 40 | Janez1 | //** //possible BUG, random pointer values (uncomment this block)
|
48 | 34 | Janez1 | cl->OPERATOR_TYPE = NULL;
|
49 | cl->dev = NULL;
|
||
50 | cl->nxt_lst = NULL;
|
||
51 | cl->sub_lst = NULL;
|
||
52 | cl->value = NULL;
|
||
53 | /**/
|
||
54 | (*cl).OPERATOR_TYPE = getOperatorType(node_t->value.opaque); |
||
55 | //printf("cl->OPERATOR_TYPE %d\n", cl->OPERATOR_TYPE);
|
||
56 | 30 | Janez1 | |
57 | 34 | Janez1 | if(node_t->child)
|
58 | { |
||
59 | //printf("create child\n");
|
||
60 | cl->sub_lst = getScenCond(node_t->child); |
||
61 | } |
||
62 | else
|
||
63 | { |
||
64 | 40 | Janez1 | if(mxmlElementGetAttr(node_t, "inDeviceId")) |
65 | { |
||
66 | cl->dev = (char *)malloc(sizeof(node_t->value.element.attrs->value)*strlen(mxmlElementGetAttr(node_t, "inDeviceId"))); |
||
67 | cl->dev = mxmlElementGetAttr(node_t, "inDeviceId");
|
||
68 | (*cl).value = atoi(mxmlElementGetAttr(node_t, "val"));
|
||
69 | } |
||
70 | 34 | Janez1 | } |
71 | node_t=node_t->next; |
||
72 | |||
73 | cl->nxt_lst = head; |
||
74 | head = cl; |
||
75 | } |
||
76 | return cl;
|
||
77 | 30 | Janez1 | } |
78 | |||
79 | 34 | Janez1 | scenario *setNewScenarioList() // ony called on startup
|
80 | 30 | Janez1 | { |
81 | //TODO vedno preverjaj, ce so vse naprave, ki jih uporabljas, prisotne! npr. termometer mora biti v device list-u
|
||
82 | scenario *curr, *head; |
||
83 | head = NULL;
|
||
84 | |||
85 | FILE *fp; |
||
86 | mxml_node_t *tree, *node; |
||
87 | |||
88 | 34 | Janez1 | if(fp = fopen("./scenariotree.xml", "r")) //TODO !hardcoded |
89 | 30 | Janez1 | { |
90 | 34 | Janez1 | //printf("File read!\n");
|
91 | 30 | Janez1 | //TODO replace with syslog error
|
92 | } |
||
93 | else
|
||
94 | 34 | Janez1 | printf("Error file not found!");
|
95 | 30 | Janez1 | //TODO replace with syslog error
|
96 | |||
97 | tree = mxmlLoadFile(NULL, fp, MXML_IGNORE_CALLBACK);
|
||
98 | node = tree; |
||
99 | fclose(fp); |
||
100 | |||
101 | 34 | Janez1 | //getting to the correct level of the xml tree (descend 2 levels) TODO add null check
|
102 | 30 | Janez1 | node=node->child->child; |
103 | |||
104 | 34 | Janez1 | // fill the list of scenarios
|
105 | 30 | Janez1 | while(node!=NULL) |
106 | { |
||
107 | curr = (scenario *)malloc(sizeof(scenario));
|
||
108 | |||
109 | /// char *scen_id;
|
||
110 | curr->scen_id = (char *)malloc(sizeof(node->value.element.attrs->value)*strlen(mxmlElementGetAttr(node, "id"))); |
||
111 | curr->scen_id = mxmlElementGetAttr(node, "id"); // possible BUG, perhaps use strcpy here! |
||
112 | 34 | Janez1 | //printf("id:\t %s\n", curr->scen_id );
|
113 | 30 | Janez1 | |
114 | /// char *scen_name;
|
||
115 | curr->scen_name = (char *)malloc(sizeof(node->value.element.attrs->value)*strlen(mxmlElementGetAttr(node, "name"))); |
||
116 | curr->scen_name = mxmlElementGetAttr(node, "name"); // possible BUG, perhaps use strcpy here! |
||
117 | 34 | Janez1 | //printf("scenario name:\t %s\n", curr->scen_name);
|
118 | 30 | Janez1 | |
119 | // getting data for individual scenario
|
||
120 | mxml_node_t *mxml_scenarioNode; |
||
121 | mxml_scenarioNode=node->child; |
||
122 | |||
123 | /// char *inDevice_id;
|
||
124 | curr->inDevice_id = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "id"))); |
||
125 | curr->inDevice_id = mxmlElementGetAttr(mxml_scenarioNode, "id");
|
||
126 | mxml_scenarioNode = mxml_scenarioNode->next; |
||
127 | 34 | Janez1 | //printf("inDevice_id:\t %s\n", curr->inDevice_id );
|
128 | 30 | Janez1 | |
129 | /// char *outDevice_id;
|
||
130 | curr->outDevice_id = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "id"))); |
||
131 | curr->outDevice_id = mxmlElementGetAttr(mxml_scenarioNode, "id");
|
||
132 | mxml_scenarioNode = mxml_scenarioNode->next; |
||
133 | 34 | Janez1 | //printf("outDevice_id:\t %s\n", curr->outDevice_id );
|
134 | 30 | Janez1 | |
135 | /// char *func;
|
||
136 | curr->func = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "func"))); |
||
137 | curr->func = mxmlElementGetAttr(mxml_scenarioNode, "func");
|
||
138 | mxml_scenarioNode = mxml_scenarioNode->next; |
||
139 | 34 | Janez1 | //printf("func:\t %s\n", curr->func );
|
140 | 30 | Janez1 | |
141 | /// int idleChkItv;
|
||
142 | curr->idleChkItv = atoi(mxmlElementGetAttr(mxml_scenarioNode, "val"));
|
||
143 | mxml_scenarioNode= mxml_scenarioNode->next; |
||
144 | 34 | Janez1 | //printf("idleChkItv:\t %d s\n", curr->idleChkItv );
|
145 | 30 | Janez1 | |
146 | /// int actvChkItv;
|
||
147 | 40 | Janez1 | curr->actvChkItv = atoi(mxmlElementGetAttr(mxml_scenarioNode, "val"));
|
148 | 30 | Janez1 | mxml_scenarioNode= mxml_scenarioNode->next; |
149 | 40 | Janez1 | //printf("actvChkItv:\t %d s\n", curr->actvChkItv );
|
150 | 30 | Janez1 | |
151 | 40 | Janez1 | /// float alpha;
|
152 | curr->alpha = atof(mxmlElementGetAttr(mxml_scenarioNode, "val"));
|
||
153 | mxml_scenarioNode= mxml_scenarioNode->next; |
||
154 | //printf("actvChkItv:\t %f s\n", curr->alpha );
|
||
155 | |||
156 | 30 | Janez1 | /// char *params_val; // TBD kaj je to?
|
157 | curr->params_val = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "val"))); |
||
158 | curr->params_val = mxmlElementGetAttr(mxml_scenarioNode, "val");
|
||
159 | mxml_scenarioNode = mxml_scenarioNode->next; |
||
160 | 34 | Janez1 | //printf("params_val:\t %s\n", curr->params_val );
|
161 | 30 | Janez1 | |
162 | /// int minval;
|
||
163 | curr->minval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "minval"));
|
||
164 | 34 | Janez1 | //printf("minval:\t %d\n", curr->minval );
|
165 | 30 | Janez1 | |
166 | /// int maxval;
|
||
167 | curr->maxval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "maxval"));
|
||
168 | 34 | Janez1 | //printf("maxval:\t %d\n", curr->maxval );
|
169 | 30 | Janez1 | |
170 | /// int optval;
|
||
171 | curr->optval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "optval"));
|
||
172 | 34 | Janez1 | //printf("optval:\t %d\n", curr->optval );
|
173 | 30 | Janez1 | |
174 | /// int tolval;
|
||
175 | curr->tolval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "tolerance"));
|
||
176 | mxml_scenarioNode = mxml_scenarioNode->next; |
||
177 | 34 | Janez1 | //printf("tolval:\t %d\n", curr->tolval );
|
178 | 30 | Janez1 | |
179 | /// char *action;
|
||
180 | curr->action = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "act"))); |
||
181 | curr->action = mxmlElementGetAttr(mxml_scenarioNode, "act");
|
||
182 | mxml_scenarioNode = mxml_scenarioNode->next; |
||
183 | 34 | Janez1 | //printf("action:\t %s\n", curr->action );
|
184 | 30 | Janez1 | |
185 | /// char *param;
|
||
186 | curr->param = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "par"))); |
||
187 | curr->param = mxmlElementGetAttr(mxml_scenarioNode, "par");
|
||
188 | mxml_scenarioNode = mxml_scenarioNode->next; |
||
189 | 34 | Janez1 | //printf("param:\t %s\n", curr->param );
|
190 | 30 | Janez1 | |
191 | 34 | Janez1 | /// conditions
|
192 | curr->cond_l = getScenCond(mxml_scenarioNode); |
||
193 | //printf("----------------------------------------------\n");
|
||
194 | 30 | Janez1 | |
195 | 34 | Janez1 | curr->running = 0;
|
196 | 30 | Janez1 | // done
|
197 | // moving to the next XML node
|
||
198 | node=node->next; |
||
199 | 34 | Janez1 | NUMBER_OF_SCENARIOS++ ; |
200 | 30 | Janez1 | /// struct scenario_s *nxt;
|
201 | curr->nxt = head; |
||
202 | head = curr; |
||
203 | } |
||
204 | 34 | Janez1 | /*
|
205 | hScenario=head; // handle to scenario list
|
||
206 | head=hScenario;
|
||
207 | 30 | Janez1 | |
208 | 34 | Janez1 | printf("hScenario %d\n", hScenario);
|
209 | printf("head %d\n", head);
|
||
210 | |||
211 | |||
212 | while(head)
|
||
213 | {
|
||
214 | printf("scenario id %s\n", head->scen_id);
|
||
215 | printf("scenario name %s\n", head->scen_name);
|
||
216 | head=head->nxt;
|
||
217 | }
|
||
218 | 30 | Janez1 | //delete obsolete xml tree
|
219 | mxmlDelete(tree);
|
||
220 | mxmlDelete(node);
|
||
221 | 34 | Janez1 | /**/
|
222 | return head;
|
||
223 | } |
||
224 | 30 | Janez1 | |
225 | 34 | Janez1 | int getNumberOfScenarios()
|
226 | { |
||
227 | return NUMBER_OF_SCENARIOS;
|
||
228 | 30 | Janez1 | } |
229 | |||
230 | 34 | Janez1 | scenario *getAvailableScenario(scenario *scen) |
231 | { |
||
232 | while(scen)
|
||
233 | { |
||
234 | if(!scen->running)
|
||
235 | { |
||
236 | scen->running = 1;
|
||
237 | printf("activating scenario %s with status %d\n", scen->scen_id, scen->running);
|
||
238 | return scen;
|
||
239 | } |
||
240 | scen=scen->nxt; |
||
241 | } |
||
242 | |||
243 | return NULL; |
||
244 | } |
||
245 | 30 | Janez1 | /*
|
246 | void updateDeviceList(device_d *deviceList)
|
||
247 | {
|
||
248 | |||
249 | }
|
||
250 | /**/ |