Statistics
| Revision:

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

History | View | Annotate | Download (8.18 KB)

1
#include "scenariolist.h"
2
#include "../logics/conditions.h"
3
#include <string.h>
4
#include <mxml.h>
5

    
6
short NUMBER_OF_SCENARIOS = 0;
7

    
8
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

    
34
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

    
45
        /** 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

    
55
        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
}
78

    
79
scenario *setNewScenarioList() // ony called on startup
80
{
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
        if(fp = fopen("./scenariotree.xml", "r")) //TODO !hardcoded
89
        {
90
        //printf("File read!\n");
91
        //TODO replace with syslog error
92
        }
93
        else
94
        printf("Error file not found!");
95
        //TODO replace with syslog error
96

    
97
        tree = mxmlLoadFile(NULL, fp, MXML_IGNORE_CALLBACK);
98
        node = tree;
99
        fclose(fp);
100

    
101
    //getting to the correct level of the xml tree (descend 2 levels) TODO add null check
102
    node=node->child->child;
103

    
104
    // fill the list of scenarios
105
    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
        //printf("id:\t %s\n", curr->scen_id );
113

    
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
        //printf("scenario name:\t %s\n", curr->scen_name);
118

    
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
        //printf("inDevice_id:\t %s\n", curr->inDevice_id );
128

    
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
        //printf("outDevice_id:\t %s\n", curr->outDevice_id );
134

    
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
        //printf("func:\t %s\n", curr->func );
140

    
141
        /// int     idleChkItv;
142
        curr->idleChkItv = atoi(mxmlElementGetAttr(mxml_scenarioNode, "val"));
143
            mxml_scenarioNode= mxml_scenarioNode->next;
144
        //printf("idleChkItv:\t %d s\n", curr->idleChkItv );
145

    
146
        /// int     actvChkItv;
147
        curr->idleChkItv = atoi(mxmlElementGetAttr(mxml_scenarioNode, "val"));
148
            mxml_scenarioNode= mxml_scenarioNode->next;
149
        //printf("idleChkItv:\t %d s\n", curr->idleChkItv );
150

    
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
        //printf("params_val:\t %s\n", curr->params_val );
156

    
157
        /// int     minval;
158
        curr->minval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "minval"));
159
        //printf("minval:\t %d\n", curr->minval );
160

    
161
        /// int     maxval;
162
        curr->maxval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "maxval"));
163
        //printf("maxval:\t %d\n", curr->maxval );
164

    
165
        /// int     optval;
166
        curr->optval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "optval"));
167
        //printf("optval:\t %d\n", curr->optval );
168

    
169
        /// int     tolval;
170
        curr->tolval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "tolerance"));
171
            mxml_scenarioNode = mxml_scenarioNode->next;
172
        //printf("tolval:\t %d\n", curr->tolval );
173

    
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
        //printf("action:\t %s\n", curr->action );
179

    
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
        //printf("param:\t %s\n", curr->param );
185

    
186
        /// conditions
187
        curr->cond_l = getScenCond(mxml_scenarioNode);
188
        //printf("----------------------------------------------\n");
189

    
190
        //** TEST conditions
191
        getConditionsResult(curr->cond_l);
192
        /**/
193

    
194
        curr->running = 0;
195
        // done
196
        // moving to the next XML node
197
        node=node->next;
198
        NUMBER_OF_SCENARIOS++  ;
199
        /// struct  scenario_s *nxt;
200
        curr->nxt = head;
201
        head = curr;
202
    }
203
    /*
204
    hScenario=head; // handle to scenario list
205
    head=hScenario;
206

207
    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
    //delete obsolete xml tree
218
    mxmlDelete(tree);
219
    mxmlDelete(node);
220
    /**/
221
    return head;
222
}
223

    
224
int getNumberOfScenarios()
225
{
226
    return NUMBER_OF_SCENARIOS;
227
}
228

    
229
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
/*
245
void updateDeviceList(device_d *deviceList)
246
{
247

248
}
249
/**/