Statistics
| Revision:

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

History | View | Annotate | Download (8.7 KB)

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

    
6

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

    
9
short NUMBER_OF_SCENARIOS = 0;
10

    
11
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

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

    
42
    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

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

    
58
        if(node_t->child)
59
        {
60
            //printf("create child\n");
61
            cl->sub_lst = getScenCond(node_t->child);
62
        }
63
        else
64
        {
65
            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
        }
72
        node_t=node_t->next;
73

    
74
        cl->nxt_lst = head;
75
        head = cl;
76
    }
77
    return cl;
78
}
79

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

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

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

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

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

    
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
        curr->inDev = getDevice(dev, curr->inDevice_id);
130
        //printf("inDev:\t %s\n", curr->inDev->name );
131

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

    
138
        /// 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
            mxml_scenarioNode = mxml_scenarioNode->next;
152
        //printf("func:\t %s\n", curr->func );
153

    
154
        /// int     idleChkItv;
155
        curr->idleChkItv = atoi(mxmlElementGetAttr(mxml_scenarioNode, "val"));
156
            mxml_scenarioNode= mxml_scenarioNode->next;
157
        //printf("idleChkItv:\t %d s\n", curr->idleChkItv );
158

    
159
        /// int     actvChkItv;
160
        curr->actvChkItv = atoi(mxmlElementGetAttr(mxml_scenarioNode, "val"));
161
            mxml_scenarioNode= mxml_scenarioNode->next;
162
        //printf("actvChkItv:\t %d s\n", curr->actvChkItv );
163

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

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

    
179
        /// int     maxval;
180
        curr->maxval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "maxval"));
181
        //printf("maxval:\t %d\n", curr->maxval );
182

    
183
        /// int     optval;
184
        curr->optval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "optval"));
185
        //printf("optval:\t %d\n", curr->optval );
186

    
187
        /// int     tolval;
188
        curr->tolval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "tolerance"));
189
            mxml_scenarioNode = mxml_scenarioNode->next;
190
        //printf("tolval:\t %d\n", curr->tolval );
191

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

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

    
204
        /// conditions
205
        curr->cond_l = getScenCond(mxml_scenarioNode);
206
        //printf("----------------------------------------------\n");
207

    
208
        curr->running = 0;
209
        // done
210
        // moving to the next XML node
211
        node=node->next;
212
        NUMBER_OF_SCENARIOS++  ;
213
        /// struct  scenario_s *nxt;
214
        curr->nxt = head;
215
        head = curr;
216
    }
217
    /*
218
    hScenario=head; // handle to scenario list
219
    head=hScenario;
220

221
    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
    //delete obsolete xml tree
232
    mxmlDelete(tree);
233
    mxmlDelete(node);
234
    /**/
235
    return head;
236
}
237

    
238
int getNumberOfScenarios()
239
{
240
    return NUMBER_OF_SCENARIOS;
241
}
242

    
243
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
/*
259
void updateDeviceList(device_d *deviceList)
260
{
261

262
}
263
/**/