Statistics
| Revision:

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

History | View | Annotate | Download (8.29 KB)

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

    
6
//extern device *hDevice;   //device list
7

    
8
short NUMBER_OF_SCENARIOS = 0;
9

    
10
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

    
36
cond_lst *getScenCond(mxml_node_t *node_t)
37
{
38
    cond_lst *cl, *head;
39
    head = NULL;
40

    
41
    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

    
47
        //** //possible BUG, random pointer values (uncomment this block)
48
        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

    
57
        if(node_t->child)
58
        {
59
            //printf("create child\n");
60
            cl->sub_lst = getScenCond(node_t->child);
61
        }
62
        else
63
        {
64
            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
        }
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->actvChkItv = atoi(mxmlElementGetAttr(mxml_scenarioNode, "val"));
148
            mxml_scenarioNode= mxml_scenarioNode->next;
149
        //printf("actvChkItv:\t %d s\n", curr->actvChkItv );
150

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

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

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

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

    
174
        /// int     tolval;
175
        curr->tolval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "tolerance"));
176
            mxml_scenarioNode = mxml_scenarioNode->next;
177
        //printf("tolval:\t %d\n", curr->tolval );
178

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

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

    
191
        /// conditions
192
        curr->cond_l = getScenCond(mxml_scenarioNode);
193
        //printf("----------------------------------------------\n");
194

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

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

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

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

249
}
250
/**/