Statistics
| Revision:

root / logic / trunk / src / init / scenariolist.c @ 36

History | View | Annotate | Download (8.18 KB)

1 30 Janez1
#include "scenariolist.h"
2
#include "../logics/conditions.h"
3
#include <string.h>
4
#include <mxml.h>
5
6 34 Janez1
short NUMBER_OF_SCENARIOS = 0;
7 30 Janez1
8 34 Janez1
int getOperatorType(char *val)
9
{
10
    if(0 == strcmp(val, "conditions"))
11
        return TOP_NODE;
12
    else if(0 == strcmp(val, "and"))
13
        return AND;
14
    else if(0 == strcmp(val, "nand"))
15
        return NOT_AND;
16
    else if(0 == strcmp(val, "or"))
17
        return OR;
18
    else if(0 == strcmp(val, "xor"))
19
        return XOR;
20
    else if(0 == strcmp(val, "greater"))
21
        return GREATER;
22
    else if(0 == strcmp(val, "greater_or_equal"))
23
        return GREATER_OR_EQUAL;
24
    else if(0 == strcmp(val, "less"))
25
        return LESS;
26
    else if(0 == strcmp(val, "less_or_equal"))
27
        return LESS_OR_EQUAL;
28
    else
29
        printf("Unidentified condition \"%s\".\n", val);
30
    return NULL;
31
    /**/
32
}
33 30 Janez1
34 34 Janez1
cond_lst *getScenCond(mxml_node_t *node_t)
35
{
36
    cond_lst *cl, *head;
37
    head = NULL;
38
    //cl->OPERATOR_TYPE =
39
    while(node_t) // same level
40
    {
41
        //printf("operator name: %s\n", node_t->value.opaque);
42
        //*
43
        cl = (cond_lst *)malloc(sizeof(cond_lst));
44 30 Janez1
45 34 Janez1
        /** possible BUG, random pointer values (uncomment this block)
46
        cl->OPERATOR_TYPE = NULL;
47
        cl->dev = NULL;
48
        cl->nxt_lst = NULL;
49
        cl->sub_lst = NULL;
50
        cl->value = NULL;
51
        /**/
52
        (*cl).OPERATOR_TYPE = getOperatorType(node_t->value.opaque);
53
        //printf("cl->OPERATOR_TYPE %d\n", cl->OPERATOR_TYPE);
54 30 Janez1
55 34 Janez1
        if(node_t->child)
56
        {
57
            //printf("create child\n");
58
            cl->sub_lst = getScenCond(node_t->child);
59
        }
60
        else
61
        {
62
            //printf("create leaf\n");
63
            cl->dev = (char *)malloc(sizeof(node_t->value.element.attrs->value)*strlen(mxmlElementGetAttr(node_t, "inDeviceId")));
64
            cl->dev = mxmlElementGetAttr(node_t, "inDeviceId");
65
            //printf("cl->dev %s\n", cl->dev);
66
67
            (*cl).value = atoi(mxmlElementGetAttr(node_t, "val"));
68
            //printf("cl->value %d\n", (*cl).value);
69
        }
70
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
        curr->idleChkItv = atoi(mxmlElementGetAttr(mxml_scenarioNode, "val"));
148
            mxml_scenarioNode= mxml_scenarioNode->next;
149 34 Janez1
        //printf("idleChkItv:\t %d s\n", curr->idleChkItv );
150 30 Janez1
151
        /// char    *params_val;    // TBD kaj je to?
152
        curr->params_val = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "val")));
153
        curr->params_val = mxmlElementGetAttr(mxml_scenarioNode, "val");
154
            mxml_scenarioNode = mxml_scenarioNode->next;
155 34 Janez1
        //printf("params_val:\t %s\n", curr->params_val );
156 30 Janez1
157
        /// int     minval;
158
        curr->minval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "minval"));
159 34 Janez1
        //printf("minval:\t %d\n", curr->minval );
160 30 Janez1
161
        /// int     maxval;
162
        curr->maxval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "maxval"));
163 34 Janez1
        //printf("maxval:\t %d\n", curr->maxval );
164 30 Janez1
165
        /// int     optval;
166
        curr->optval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "optval"));
167 34 Janez1
        //printf("optval:\t %d\n", curr->optval );
168 30 Janez1
169
        /// int     tolval;
170
        curr->tolval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "tolerance"));
171
            mxml_scenarioNode = mxml_scenarioNode->next;
172 34 Janez1
        //printf("tolval:\t %d\n", curr->tolval );
173 30 Janez1
174
        /// char    *action;
175
        curr->action = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "act")));
176
        curr->action = mxmlElementGetAttr(mxml_scenarioNode, "act");
177
            mxml_scenarioNode = mxml_scenarioNode->next;
178 34 Janez1
        //printf("action:\t %s\n", curr->action );
179 30 Janez1
180
        /// char    *param;
181
        curr->param = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "par")));
182
        curr->param = mxmlElementGetAttr(mxml_scenarioNode, "par");
183
            mxml_scenarioNode = mxml_scenarioNode->next;
184 34 Janez1
        //printf("param:\t %s\n", curr->param );
185 30 Janez1
186 34 Janez1
        /// conditions
187
        curr->cond_l = getScenCond(mxml_scenarioNode);
188
        //printf("----------------------------------------------\n");
189 30 Janez1
190 34 Janez1
        //** TEST conditions
191
        getConditionsResult(curr->cond_l);
192
        /**/
193
194
        curr->running = 0;
195 30 Janez1
        // done
196
        // moving to the next XML node
197
        node=node->next;
198 34 Janez1
        NUMBER_OF_SCENARIOS++  ;
199 30 Janez1
        /// struct  scenario_s *nxt;
200
        curr->nxt = head;
201
        head = curr;
202
    }
203 34 Janez1
    /*
204
    hScenario=head; // handle to scenario list
205
    head=hScenario;
206 30 Janez1

207 34 Janez1
    printf("hScenario %d\n", hScenario);
208
    printf("head %d\n", head);
209

210

211
    while(head)
212
    {
213
        printf("scenario id %s\n", head->scen_id);
214
        printf("scenario name %s\n", head->scen_name);
215
        head=head->nxt;
216
    }
217 30 Janez1
    //delete obsolete xml tree
218
    mxmlDelete(tree);
219
    mxmlDelete(node);
220 34 Janez1
    /**/
221
    return head;
222
}
223 30 Janez1
224 34 Janez1
int getNumberOfScenarios()
225
{
226
    return NUMBER_OF_SCENARIOS;
227 30 Janez1
}
228
229 34 Janez1
scenario *getAvailableScenario(scenario *scen)
230
{
231
    while(scen)
232
    {
233
        if(!scen->running)
234
        {
235
            scen->running = 1;
236
            printf("activating scenario %s with status %d\n", scen->scen_id, scen->running);
237
            return scen;
238
        }
239
        scen=scen->nxt;
240
    }
241
242
    return NULL;
243
}
244 30 Janez1
/*
245
void updateDeviceList(device_d *deviceList)
246
{
247

248
}
249
/**/