root / logic / trunk / src / init / devicelist.c @ 41
History | View | Annotate | Download (3.52 KB)
1 | 10 | Janez1 | #include "devicelist.h" |
---|---|---|---|
2 | #include <string.h> |
||
3 | #include <mxml.h> |
||
4 | 40 | Janez1 | #include <time.h> |
5 | 10 | Janez1 | |
6 | 34 | Janez1 | short NUMBER_OF_DEVICES = 0; |
7 | 10 | Janez1 | |
8 | 34 | Janez1 | device *setDeviceList() |
9 | 10 | Janez1 | { |
10 | device *curr, *head; |
||
11 | head = NULL;
|
||
12 | |||
13 | FILE *fp; |
||
14 | mxml_node_t *tree, *node; |
||
15 | |||
16 | 34 | Janez1 | if(fp = fopen("./devicetree.xml", "r")) //TODO !hardcoded |
17 | 19 | Janez1 | { |
18 | 34 | Janez1 | //printf("File read!\n");
|
19 | 19 | Janez1 | } |
20 | else
|
||
21 | printf("Error!");
|
||
22 | |||
23 | 10 | Janez1 | tree = mxmlLoadFile(NULL, fp, MXML_IGNORE_CALLBACK);
|
24 | node = tree; |
||
25 | fclose(fp); |
||
26 | |||
27 | 34 | Janez1 | /** //structure of XML tree
|
28 | 10 | Janez1 | ?xml
|
29 | |
|
||
30 | devicetree
|
||
31 | |
|
||
32 | dev1 - dev2 - ...
|
||
33 | | |
|
||
34 | | |
|
||
35 | | ID - name - minvalue - maxvalue - action - param
|
||
36 | ID - name - minvalue - maxvalue - action - param
|
||
37 | /**/
|
||
38 | 34 | Janez1 | |
39 | 10 | Janez1 | //getting to the correct level of the xml tree (descend 2 levels)
|
40 | node=node->child->child; |
||
41 | |||
42 | // fill the list of known devices
|
||
43 | while(node!=NULL) |
||
44 | { |
||
45 | curr = (device *)malloc(sizeof(device));
|
||
46 | |||
47 | 27 | Janez1 | curr->id = (char *)malloc(sizeof(node->value.element.attrs->value)*strlen(node->value.element.attrs->value)); |
48 | 10 | Janez1 | strcpy(curr->id, node->value.element.attrs->value); |
49 | 34 | Janez1 | //printf("id:\t %s\n", curr->id );
|
50 | |||
51 | 10 | Janez1 | // getting data for individual device
|
52 | mxml_node_t *mxml_deviceNode; |
||
53 | mxml_deviceNode=node->child; |
||
54 | //TODO malloc()
|
||
55 | 28 | Janez1 | curr->name = (char *)malloc(sizeof(mxml_deviceNode->value.element.attrs->value)*strlen(node->value.element.attrs->value)); |
56 | 10 | Janez1 | strcpy(curr->name, mxml_deviceNode->value.element.attrs->value); |
57 | mxml_deviceNode= mxml_deviceNode->next; |
||
58 | 34 | Janez1 | //printf("name:\t %s\n", curr->name );
|
59 | 10 | Janez1 | |
60 | 19 | Janez1 | curr->readitv = atoi(mxml_deviceNode->value.element.attrs->value); |
61 | 10 | Janez1 | mxml_deviceNode= mxml_deviceNode->next; |
62 | 34 | Janez1 | //printf("readitv:\t %d s\n", curr->readitv );
|
63 | 10 | Janez1 | |
64 | 28 | Janez1 | curr->action = (char *)malloc(sizeof(mxml_deviceNode->value.element.attrs->value)*strlen(node->value.element.attrs->value)); |
65 | 10 | Janez1 | strcpy(curr->action, mxml_deviceNode->value.element.attrs->value); |
66 | mxml_deviceNode= mxml_deviceNode->next; |
||
67 | 34 | Janez1 | //printf("action:\t %s\n", curr->action );
|
68 | 10 | Janez1 | |
69 | 28 | Janez1 | curr->param = (char *)malloc(sizeof(mxml_deviceNode->value.element.attrs->value)*strlen(node->value.element.attrs->value)); |
70 | 10 | Janez1 | strcpy(curr->param, mxml_deviceNode->value.element.attrs->value); |
71 | 34 | Janez1 | //printf("param:\t %s\n", curr->param );
|
72 | 40 | Janez1 | curr->buf = 0;
|
73 | 34 | Janez1 | curr->running = 0;
|
74 | 10 | Janez1 | // moving to the next XML node
|
75 | node=node->next; |
||
76 | |||
77 | curr->nxt = head; |
||
78 | head = curr; |
||
79 | 34 | Janez1 | |
80 | NUMBER_OF_DEVICES++; |
||
81 | 10 | Janez1 | } |
82 | 34 | Janez1 | |
83 | 10 | Janez1 | //delete obsolete xml tree
|
84 | mxmlDelete(tree); |
||
85 | mxmlDelete(node); |
||
86 | 34 | Janez1 | |
87 | return head;
|
||
88 | } |
||
89 | |||
90 | int getNumberOfDevices()
|
||
91 | { |
||
92 | return NUMBER_OF_DEVICES;
|
||
93 | } |
||
94 | |||
95 | 40 | Janez1 | device *getDevice1(char *devName)
|
96 | { |
||
97 | return NULL; |
||
98 | } |
||
99 | |||
100 | device *getDevice2(device *dev) |
||
101 | { |
||
102 | return NULL; |
||
103 | } |
||
104 | |||
105 | 34 | Janez1 | device *getDevice(device *dev, char *devName)
|
106 | { |
||
107 | while(dev)
|
||
108 | 10 | Janez1 | { |
109 | 34 | Janez1 | if(0==strcmp(dev->id,devName)) |
110 | { |
||
111 | return dev;
|
||
112 | } |
||
113 | dev=dev->nxt; |
||
114 | 10 | Janez1 | } |
115 | 34 | Janez1 | return NULL; |
116 | 10 | Janez1 | } |
117 | |||
118 | 34 | Janez1 | device *getAvailableDevice(device *dev) |
119 | { |
||
120 | while(dev)
|
||
121 | { |
||
122 | if(!dev->running)
|
||
123 | { |
||
124 | dev->running = 1;
|
||
125 | 40 | Janez1 | //printf("activating device %s with status %d\n", dev->id, dev->running);
|
126 | 34 | Janez1 | return dev;
|
127 | } |
||
128 | dev=dev->nxt; |
||
129 | } |
||
130 | |||
131 | return NULL; |
||
132 | } |
||
133 | 40 | Janez1 | |
134 | int getLocalFormattedTime()
|
||
135 | { |
||
136 | // desired format hhmm, 4 digit int; 1430 = 14:30
|
||
137 | time_t epoch_time; |
||
138 | struct tm *tm_p;
|
||
139 | int wntForm=0; |
||
140 | |||
141 | epoch_time = time( NULL );
|
||
142 | tm_p = localtime( &epoch_time ); |
||
143 | |||
144 | return tm_p->tm_hour*100+tm_p->tm_min; |
||
145 | } |
||
146 | |||
147 | 10 | Janez1 | /*
|
148 | void updateDeviceList(device_d *deviceList)
|
||
149 | {
|
||
150 | |||
151 | }
|
||
152 | /**/ |