Revision 34 logic/trunk/src/init/scenariolist.c

View differences:

scenariolist.c
3 3
#include <string.h>
4 4
#include <mxml.h>
5 5

  
6
short NUMBER_OF_SCENARIOS = 0;
6 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
}
7 33

  
8
scenario *hScenario; //scenario list
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));
9 44

  
10
//parseScenCondLeaf()
11
//parseScenCondNode()
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);
12 54

  
13
cond_lst *parseScenCond()
14
{
15
    return NULL;
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;
16 77
}
17 78

  
18
int setNewScenarioList() // ony called on startup
79
scenario *setNewScenarioList() // ony called on startup
19 80
{
20 81
    //TODO vedno preverjaj, ce so vse naprave, ki jih uporabljas, prisotne! npr. termometer mora biti v device list-u
21 82
    scenario *curr, *head;
......
24 85
    FILE *fp;
25 86
    mxml_node_t *tree, *node;
26 87

  
27
	if(fp = fopen("./src/init/scenariotree.xml", "r")) //TODO !hardcoded
88
	if(fp = fopen("./scenariotree.xml", "r")) //TODO !hardcoded
28 89
	{
29
        printf("File read!\n");
90
        //printf("File read!\n");
30 91
        //TODO replace with syslog error
31 92
	}
32 93
	else
33
        printf("Error!");
94
        printf("Error file not found!");
34 95
        //TODO replace with syslog error
35 96

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

  
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)
101
    //getting to the correct level of the xml tree (descend 2 levels) TODO add null check
52 102
    node=node->child->child;
53
    // printf("hDevice %d\n", hDevice);
54 103

  
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
104
    // fill the list of scenarios
58 105
    while(node!=NULL)
59 106
    {
60 107
        curr = (scenario *)malloc(sizeof(scenario));
......
62 109
        /// char    *scen_id;
63 110
        curr->scen_id = (char *)malloc(sizeof(node->value.element.attrs->value)*strlen(mxmlElementGetAttr(node, "id")));
64 111
        curr->scen_id = mxmlElementGetAttr(node, "id"); // possible BUG, perhaps use strcpy here!
65
        printf("id:\t %s\n", curr->scen_id );
112
        //printf("id:\t %s\n", curr->scen_id );
66 113

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

  
72 119
        // getting data for individual scenario
73 120
        mxml_node_t *mxml_scenarioNode;
......
77 124
        curr->inDevice_id = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "id")));
78 125
        curr->inDevice_id = mxmlElementGetAttr(mxml_scenarioNode, "id");
79 126
            mxml_scenarioNode = mxml_scenarioNode->next;
80
        printf("inDevice_id:\t %s\n", curr->inDevice_id );
127
        //printf("inDevice_id:\t %s\n", curr->inDevice_id );
81 128

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

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

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

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

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

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

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

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

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

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

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

  
139
        // TODO parse struct cond
186
        /// conditions
187
        curr->cond_l = getScenCond(mxml_scenarioNode);
188
        //printf("----------------------------------------------\n");
140 189

  
141
        printf("----------------------------------------------\n");
190
        //** TEST conditions
191
        getConditionsResult(curr->cond_l);
192
        /**/
193

  
194
        curr->running = 0;
142 195
        // done
143 196
        // moving to the next XML node
144 197
        node=node->next;
145

  
198
        NUMBER_OF_SCENARIOS++  ;
146 199
        /// struct  scenario_s *nxt;
147 200
        curr->nxt = head;
148 201
        head = curr;
149 202
    }
203
    /*
204
    hScenario=head; // handle to scenario list
205
    head=hScenario;
150 206

  
151
    hScenario=head; // handle to device list
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
    }
152 217
    //delete obsolete xml tree
153 218
    mxmlDelete(tree);
154 219
    mxmlDelete(node);
220
    /**/
221
    return head;
222
}
155 223

  
156
    return 1;
224
int getNumberOfScenarios()
225
{
226
    return NUMBER_OF_SCENARIOS;
157 227
}
158 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
}
159 244
/*
160 245
void updateDeviceList(device_d *deviceList)
161 246
{

Also available in: Unified diff