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