Statistics
| Revision:

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

History | View | Annotate | Download (5.84 KB)

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

    
6

    
7

    
8
scenario *hScenario; //scenario list
9

    
10
//parseScenCondLeaf()
11
//parseScenCondNode()
12

    
13
cond_lst *parseScenCond()
14
{
15
    return NULL;
16
}
17

    
18
int setNewScenarioList() // ony called on startup
19
{
20
    //TODO vedno preverjaj, ce so vse naprave, ki jih uporabljas, prisotne! npr. termometer mora biti v device list-u
21
    scenario *curr, *head;
22
    head = NULL;
23

    
24
    FILE *fp;
25
    mxml_node_t *tree, *node;
26

    
27
        if(fp = fopen("./src/init/scenariotree.xml", "r")) //TODO !hardcoded
28
        {
29
        printf("File read!\n");
30
        //TODO replace with syslog error
31
        }
32
        else
33
        printf("Error!");
34
        //TODO replace with syslog error
35

    
36
        tree = mxmlLoadFile(NULL, fp, MXML_IGNORE_CALLBACK);
37
        node = tree;
38
        fclose(fp);
39

    
40
    /* //structure of XML tree
41
    ?xml
42
      |
43
    devicetree
44
      |
45
    dev1 - dev2 - ...
46
      |      |
47
      |      |
48
      |     ID - name - minvalue - maxvalue - action - param
49
    ID - name - minvalue - maxvalue - action - param
50
    /**/
51
    //getting to the correct level of the xml tree (descend 2 levels)
52
    node=node->child->child;
53
    // printf("hDevice %d\n", hDevice);
54

    
55
    // fill the list of known devices
56
    //      sorry for the ugly chunk of code, but I had to allocate memory
57
    // for each string separately in order to keep it scalable
58
    while(node!=NULL)
59
    {
60
        curr = (scenario *)malloc(sizeof(scenario));
61

    
62
        /// char    *scen_id;
63
        curr->scen_id = (char *)malloc(sizeof(node->value.element.attrs->value)*strlen(mxmlElementGetAttr(node, "id")));
64
        curr->scen_id = mxmlElementGetAttr(node, "id"); // possible BUG, perhaps use strcpy here!
65
        printf("id:\t %s\n", curr->scen_id );
66

    
67
        /// char    *scen_name;
68
        curr->scen_name = (char *)malloc(sizeof(node->value.element.attrs->value)*strlen(mxmlElementGetAttr(node, "name")));
69
        curr->scen_name = mxmlElementGetAttr(node, "name"); // possible BUG, perhaps use strcpy here!
70
        printf("scenario name:\t %s\n", curr->scen_name);
71

    
72
        // getting data for individual scenario
73
        mxml_node_t *mxml_scenarioNode;
74
        mxml_scenarioNode=node->child;
75

    
76
        /// char    *inDevice_id;
77
        curr->inDevice_id = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "id")));
78
        curr->inDevice_id = mxmlElementGetAttr(mxml_scenarioNode, "id");
79
            mxml_scenarioNode = mxml_scenarioNode->next;
80
        printf("inDevice_id:\t %s\n", curr->inDevice_id );
81

    
82
        /// char    *outDevice_id;
83
        curr->outDevice_id = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "id")));
84
        curr->outDevice_id = mxmlElementGetAttr(mxml_scenarioNode, "id");
85
            mxml_scenarioNode = mxml_scenarioNode->next;
86
        printf("outDevice_id:\t %s\n", curr->outDevice_id );
87

    
88
        /// char    *func;
89
        curr->func = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "func")));
90
        curr->func = mxmlElementGetAttr(mxml_scenarioNode, "func");
91
            mxml_scenarioNode = mxml_scenarioNode->next;
92
        printf("func:\t %s\n", curr->func );
93

    
94
        /// int     idleChkItv;
95
        curr->idleChkItv = atoi(mxmlElementGetAttr(mxml_scenarioNode, "val"));
96
            mxml_scenarioNode= mxml_scenarioNode->next;
97
        printf("idleChkItv:\t %d s\n", curr->idleChkItv );
98

    
99
        /// int     actvChkItv;
100
        curr->idleChkItv = atoi(mxmlElementGetAttr(mxml_scenarioNode, "val"));
101
            mxml_scenarioNode= mxml_scenarioNode->next;
102
        printf("idleChkItv:\t %d s\n", curr->idleChkItv );
103

    
104
        /// char    *params_val;    // TBD kaj je to?
105
        curr->params_val = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "val")));
106
        curr->params_val = mxmlElementGetAttr(mxml_scenarioNode, "val");
107
            mxml_scenarioNode = mxml_scenarioNode->next;
108
        printf("params_val:\t %s\n", curr->params_val );
109

    
110
        /// int     minval;
111
        curr->minval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "minval"));
112
        printf("minval:\t %d\n", curr->minval );
113

    
114
        /// int     maxval;
115
        curr->maxval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "maxval"));
116
        printf("maxval:\t %d\n", curr->maxval );
117

    
118
        /// int     optval;
119
        curr->optval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "optval"));
120
        printf("optval:\t %d\n", curr->optval );
121

    
122
        /// int     tolval;
123
        curr->tolval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "tolerance"));
124
            mxml_scenarioNode = mxml_scenarioNode->next;
125
        printf("tolval:\t %d\n", curr->tolval );
126

    
127
        /// char    *action;
128
        curr->action = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "act")));
129
        curr->action = mxmlElementGetAttr(mxml_scenarioNode, "act");
130
            mxml_scenarioNode = mxml_scenarioNode->next;
131
        printf("action:\t %s\n", curr->action );
132

    
133
        /// char    *param;
134
        curr->param = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "par")));
135
        curr->param = mxmlElementGetAttr(mxml_scenarioNode, "par");
136
            mxml_scenarioNode = mxml_scenarioNode->next;
137
        printf("param:\t %s\n", curr->param );
138

    
139
        // TODO parse struct cond
140

    
141
        printf("----------------------------------------------\n");
142
        // done
143
        // moving to the next XML node
144
        node=node->next;
145

    
146
        /// struct  scenario_s *nxt;
147
        curr->nxt = head;
148
        head = curr;
149
    }
150

    
151
    hScenario=head; // handle to device list
152
    //delete obsolete xml tree
153
    mxmlDelete(tree);
154
    mxmlDelete(node);
155

    
156
    return 1;
157
}
158

    
159
/*
160
void updateDeviceList(device_d *deviceList)
161
{
162

163
}
164
/**/