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