Subversion Repositories HomeAutomation

Rev

Rev 976 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 976 Rev 981
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) November 28, 2008 by Mattias Runge                             *
2
 *   Copyright (C) November 28, 2008 by Mattias Runge                             *
3
 *   mattias@runge.se                                                      *
3
 *   mattias@runge.se                                                      *
4
 *   canidtranslator.cpp                                            *
4
 *   canidtranslator.cpp                                            *
5
 *                                                                         *
5
 *                                                                         *
6
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   This program is free software; you can redistribute it and/or modify  *
7
 *   it under the terms of the GNU General Public License as published by  *
7
 *   it under the terms of the GNU General Public License as published by  *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 *   (at your option) any later version.                                   *
9
 *   (at your option) any later version.                                   *
10
 *                                                                         *
10
 *                                                                         *
11
 *   This program is distributed in the hope that it will be useful,       *
11
 *   This program is distributed in the hope that it will be useful,       *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU General Public License for more details.                          *
14
 *   GNU General Public License for more details.                          *
15
 *                                                                         *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU General Public License     *
16
 *   You should have received a copy of the GNU General Public License     *
17
 *   along with this program; if not, write to the                         *
17
 *   along with this program; if not, write to the                         *
18
 *   Free Software Foundation, Inc.,                                       *
18
 *   Free Software Foundation, Inc.,                                       *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20
 ***************************************************************************/
20
 ***************************************************************************/
21
 
21
 
22
#include "canidtranslator.h"
22
#include "canidtranslator.h"
23
 
23
 
24
CanIdTranslator* CanIdTranslator::myInstance = NULL;
24
CanIdTranslator* CanIdTranslator::myInstance = NULL;
25
 
25
 
26
CanIdTranslator& CanIdTranslator::getInstance()
26
CanIdTranslator& CanIdTranslator::getInstance()
27
{
27
{
28
    if (myInstance == NULL)
28
    if (myInstance == NULL)
29
    {
29
    {
30
        myInstance = new CanIdTranslator();
30
        myInstance = new CanIdTranslator();
31
    }
31
    }
32
 
32
 
33
    return *myInstance;
33
    return *myInstance;
34
}
34
}
35
 
35
 
36
void CanIdTranslator::deleteInstance()
36
void CanIdTranslator::deleteInstance()
37
{
37
{
38
    delete myInstance;
38
    delete myInstance;
39
    myInstance = NULL;
39
    myInstance = NULL;
40
}
40
}
41
 
41
 
42
CanIdTranslator::CanIdTranslator()
42
CanIdTranslator::CanIdTranslator()
43
{
43
{
44
    SyslogStream &slog = SyslogStream::getInstance();
44
    SyslogStream &slog = SyslogStream::getInstance();
45
 
45
 
46
    string filename = Settings::get("CanIdXMLFile");
46
    string filename = Settings::get("CanIdXMLFile");
47
 
47
 
48
    xmlNode.load(filename.c_str());
48
    xmlNode.load(filename.c_str());
49
 
49
 
50
    slog << "Loading definitions from " + filename + ".\n";
50
    slog << "Loading definitions from " + filename + ".\n";
51
}
51
}
52
 
52
 
53
string CanIdTranslator::lookupClassName(int classId)
53
string CanIdTranslator::lookupClassName(int classId)
54
{
54
{
55
    XmlNode classesNode = xmlNode.findChild("classes");
55
    XmlNode classesNode = xmlNode.findChild("classes");
56
    vector<XmlNode> classNodes = classesNode.getChildren();
56
    vector<XmlNode> classNodes = classesNode.getChildren();
57
 
57
 
58
    for (int n = 0; n < classNodes.size(); n++)
58
    for (int n = 0; n < classNodes.size(); n++)
59
    {
59
    {
60
        map<string, string> attributes = classNodes[n].getAttributes();
60
        map<string, string> attributes = classNodes[n].getAttributes();
61
 
61
 
62
        if (stoi(attributes["id"]) == classId)
62
        if (stoi(attributes["id"]) == classId)
63
        {
63
        {
64
            return attributes["name"];
64
            return attributes["name"];
65
        }
65
        }
66
    }
66
    }
67
 
67
 
68
    return "";
68
    return "";
69
}
69
}
70
 
70
 
71
int CanIdTranslator::resolveClassId(string className)
71
int CanIdTranslator::resolveClassId(string className)
72
{
72
{
73
    XmlNode classesNode = xmlNode.findChild("classes");
73
    XmlNode classesNode = xmlNode.findChild("classes");
74
    vector<XmlNode> classNodes = classesNode.getChildren();
74
    vector<XmlNode> classNodes = classesNode.getChildren();
75
 
75
 
76
    for (int n = 0; n < classNodes.size(); n++)
76
    for (int n = 0; n < classNodes.size(); n++)
77
    {
77
    {
78
        map<string, string> attributes = classNodes[n].getAttributes();
78
        map<string, string> attributes = classNodes[n].getAttributes();
79
 
79
 
80
        if (attributes["name"].compare(className) == 0)
80
        if (attributes["name"].compare(className) == 0)
81
        {
81
        {
82
            return stoi(attributes["id"]);
82
            return stoi(attributes["id"]);
83
        }
83
        }
84
    }
84
    }
85
 
85
 
86
    return -1;
86
    return -1;
87
}
87
}
88
 
88
 
89
string CanIdTranslator::lookupCommandName(int commandId, string moduleName)
89
string CanIdTranslator::lookupCommandName(int commandId, string moduleName)
90
{
90
{
91
    XmlNode commandsNode = xmlNode.findChild("commands");
91
    XmlNode commandsNode = xmlNode.findChild("commands");
92
    vector<XmlNode> commandNodes = commandsNode.getChildren();
92
    vector<XmlNode> commandNodes = commandsNode.getChildren();
93
 
93
 
94
    for (int n = 0; n < commandNodes.size(); n++)
94
    for (int n = 0; n < commandNodes.size(); n++)
95
    {
95
    {
96
        map<string, string> attributes = commandNodes[n].getAttributes();
96
        map<string, string> attributes = commandNodes[n].getAttributes();
97
 
97
 
98
        if (commandId > 128)
98
        if (commandId >= 128)
99
        {
99
        {
100
            if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)
100
            if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)
101
            {
101
            {
102
                return attributes["name"];
102
                return attributes["name"];
103
            }
103
            }
104
        }
104
        }
105
        else if (stoi(attributes["id"]) == commandId)
105
        else if (stoi(attributes["id"]) == commandId)
106
        {
106
        {
107
            return attributes["name"];
107
            return attributes["name"];
108
        }
108
        }
109
    }
109
    }
110
 
110
 
111
    return "";
111
    return "";
112
}
112
}
113
 
113
 
114
int CanIdTranslator::resolveCommandId(string commandName, string moduleName)
114
int CanIdTranslator::resolveCommandId(string commandName, string moduleName)
115
{
115
{
116
    XmlNode commandsNode = xmlNode.findChild("commands");
116
    XmlNode commandsNode = xmlNode.findChild("commands");
117
    vector<XmlNode> commandNodes = commandsNode.getChildren();
117
    vector<XmlNode> commandNodes = commandsNode.getChildren();
118
 
118
 
119
    for (int n = 0; n < commandNodes.size(); n++)
119
    for (int n = 0; n < commandNodes.size(); n++)
120
    {
120
    {
121
        map<string, string> attributes = commandNodes[n].getAttributes();
121
        map<string, string> attributes = commandNodes[n].getAttributes();
122
 
122
 
123
        if (attributes["name"].compare(commandName) == 0)
123
        if (attributes["name"].compare(commandName) == 0)
124
        {
124
        {
125
            int commandId = stoi(attributes["id"]);
125
            int commandId = stoi(attributes["id"]);
126
 
126
 
127
            if (commandId > 128)
127
            if (commandId >= 128)
128
            {
128
            {
129
                if (attributes["module"] == moduleName)
129
                if (attributes["module"] == moduleName)
130
                {
130
                {
131
                    return commandId;
131
                    return commandId;
132
                }
132
                }
133
            }
133
            }
134
            else
134
            else
135
            {
135
            {
136
                return commandId;
136
                return commandId;
137
            }
137
            }
138
        }
138
        }
139
    }
139
    }
140
 
140
 
141
    return -1;
141
    return -1;
142
}
142
}
143
 
143
 
144
string CanIdTranslator::getDefineId(string name, string group)
144
string CanIdTranslator::getDefineId(string name, string group)
145
{
145
{
146
    XmlNode definesNode = xmlNode.findChild("defines");
146
    XmlNode definesNode = xmlNode.findChild("defines");
147
    vector<XmlNode> defineNodes = definesNode.getChildren();
147
    vector<XmlNode> defineNodes = definesNode.getChildren();
148
 
148
 
149
    for (int n = 0; n < defineNodes.size(); n++)
149
    for (int n = 0; n < defineNodes.size(); n++)
150
    {
150
    {
151
        map<string, string> attributes = defineNodes[n].getAttributes();
151
        map<string, string> attributes = defineNodes[n].getAttributes();
152
 
152
 
153
        if (attributes["group"] == group && attributes["name"] == name)
153
        if (attributes["group"] == group && attributes["name"] == name)
154
        {
154
        {
155
            return attributes["id"];
155
            return attributes["id"];
156
        }
156
        }
157
    }
157
    }
158
 
158
 
159
    return "";
159
    return "";
160
}
160
}
161
 
161
 
162
string CanIdTranslator::lookupDirectionFlag(int directionFlag)
162
string CanIdTranslator::lookupDirectionFlag(int directionFlag)
163
{
163
{
164
    XmlNode definesNode = xmlNode.findChild("defines");
164
    XmlNode definesNode = xmlNode.findChild("defines");
165
    vector<XmlNode> defineNodes = definesNode.getChildren();
165
    vector<XmlNode> defineNodes = definesNode.getChildren();
166
 
166
 
167
    for (int n = 0; n < defineNodes.size(); n++)
167
    for (int n = 0; n < defineNodes.size(); n++)
168
    {
168
    {
169
        map<string, string> attributes = defineNodes[n].getAttributes();
169
        map<string, string> attributes = defineNodes[n].getAttributes();
170
 
170
 
171
        if (attributes["group"].compare("DirectionFlag") == 0 && stoi(attributes["id"]) == directionFlag)
171
        if (attributes["group"].compare("DirectionFlag") == 0 && stoi(attributes["id"]) == directionFlag)
172
        {
172
        {
173
            return attributes["name"];
173
            return attributes["name"];
174
        }
174
        }
175
    }
175
    }
176
 
176
 
177
    return "";
177
    return "";
178
}
178
}
179
 
179
 
180
int CanIdTranslator::resolveDirectionFlag(string directionName)
180
int CanIdTranslator::resolveDirectionFlag(string directionName)
181
{
181
{
182
    XmlNode definesNode = xmlNode.findChild("defines");
182
    XmlNode definesNode = xmlNode.findChild("defines");
183
    vector<XmlNode> defineNodes = definesNode.getChildren();
183
    vector<XmlNode> defineNodes = definesNode.getChildren();
184
 
184
 
185
    for (int n = 0; n < defineNodes.size(); n++)
185
    for (int n = 0; n < defineNodes.size(); n++)
186
    {
186
    {
187
        map<string, string> attributes = defineNodes[n].getAttributes();
187
        map<string, string> attributes = defineNodes[n].getAttributes();
188
 
188
 
189
        if (attributes["group"].compare("DirectionFlag") == 0 && attributes["name"].compare(directionName) == 0)
189
        if (attributes["group"].compare("DirectionFlag") == 0 && attributes["name"].compare(directionName) == 0)
190
        {
190
        {
191
            return stoi(attributes["id"]);
191
            return stoi(attributes["id"]);
192
        }
192
        }
193
    }
193
    }
194
 
194
 
195
    return -1;
195
    return -1;
196
}
196
}
197
 
197
 
198
string CanIdTranslator::lookupModuleName(int moduleId)
198
string CanIdTranslator::lookupModuleName(int moduleId)
199
{
199
{
200
    XmlNode modulesNode = xmlNode.findChild("modules");
200
    XmlNode modulesNode = xmlNode.findChild("modules");
201
    vector<XmlNode> moduleNodes = modulesNode.getChildren();
201
    vector<XmlNode> moduleNodes = modulesNode.getChildren();
202
 
202
 
203
    for (int n = 0; n < moduleNodes.size(); n++)
203
    for (int n = 0; n < moduleNodes.size(); n++)
204
    {
204
    {
205
        map<string, string> attributes = moduleNodes[n].getAttributes();
205
        map<string, string> attributes = moduleNodes[n].getAttributes();
206
 
206
 
207
        if (stoi(attributes["id"]) == moduleId)
207
        if (stoi(attributes["id"]) == moduleId)
208
        {
208
        {
209
            return attributes["name"];
209
            return attributes["name"];
210
        }
210
        }
211
    }
211
    }
212
 
212
 
213
    return "";
213
    return "";
214
}
214
}
215
 
215
 
216
int CanIdTranslator::resolveModuleId(string moduleName)
216
int CanIdTranslator::resolveModuleId(string moduleName)
217
{
217
{
218
    XmlNode modulesNode = xmlNode.findChild("modules");
218
    XmlNode modulesNode = xmlNode.findChild("modules");
219
    vector<XmlNode> moduleNodes = modulesNode.getChildren();
219
    vector<XmlNode> moduleNodes = modulesNode.getChildren();
220
 
220
 
221
    for (int n = 0; n < moduleNodes.size(); n++)
221
    for (int n = 0; n < moduleNodes.size(); n++)
222
    {
222
    {
223
        map<string, string> attributes = moduleNodes[n].getAttributes();
223
        map<string, string> attributes = moduleNodes[n].getAttributes();
224
 
224
 
225
        if (attributes["name"].compare(moduleName) == 0)
225
        if (attributes["name"].compare(moduleName) == 0)
226
        {
226
        {
227
            return stoi(attributes["id"]);
227
            return stoi(attributes["id"]);
228
        }
228
        }
229
    }
229
    }
230
 
230
 
231
    return -1;
231
    return -1;
232
}
232
}
233
 
233
 
234
string CanIdTranslator::translateDataToHex(int commandId, string moduleName, map<string, CanVariable> data)
234
string CanIdTranslator::translateDataToHex(int commandId, string moduleName, map<string, CanVariable> data)
235
{
235
{
236
    XmlNode commandsNode = xmlNode.findChild("commands");
236
    XmlNode commandsNode = xmlNode.findChild("commands");
237
    vector<XmlNode> commandNodes = commandsNode.getChildren();
237
    vector<XmlNode> commandNodes = commandsNode.getChildren();
238
 
238
 
239
    map<string, CanVariable> sortedData;
239
    map<string, CanVariable> sortedData;
240
 
240
 
241
    for (int n = 0; n < commandNodes.size(); n++)
241
    for (int n = 0; n < commandNodes.size(); n++)
242
    {
242
    {
243
        map<string, string> attributes = commandNodes[n].getAttributes();
243
        map<string, string> attributes = commandNodes[n].getAttributes();
244
 
244
 
245
        bool correct = false;
245
        bool correct = false;
246
 
246
 
247
        if (commandId > 128)
247
        if (commandId >= 128)
248
        {
248
        {
249
            if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)
249
            if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)
250
            {
250
            {
251
                correct = true;
251
                correct = true;
252
            }
252
            }
253
        }
253
        }
254
        else if (stoi(attributes["id"]) == commandId)
254
        else if (stoi(attributes["id"]) == commandId)
255
        {
255
        {
256
            correct = true;
256
            correct = true;
257
        }
257
        }
258
 
258
 
259
        if (correct)
259
        if (correct)
260
        {
260
        {
261
            XmlNode varablesNode = commandNodes[n].findChild("variables");
261
            XmlNode varablesNode = commandNodes[n].findChild("variables");
262
 
262
 
263
            vector<XmlNode> variableNodes = varablesNode.getChildren();
263
            vector<XmlNode> variableNodes = varablesNode.getChildren();
264
 
264
 
265
            for (int c = 0; c < variableNodes.size(); c++)
265
            for (int c = 0; c < variableNodes.size(); c++)
266
            {
266
            {
267
                map<string, string> attributes = variableNodes[c].getAttributes();
267
                map<string, string> attributes = variableNodes[c].getAttributes();
268
 
268
 
269
                if (data.find(attributes["name"]) != data.end())
269
                if (data.find(attributes["name"]) != data.end())
270
                {
270
                {
271
                    int bitStart = stoi(attributes["start_bit"]);///FIXME: This is not a good way
271
                    int bitStart = stoi(attributes["start_bit"]);///FIXME: This is not a good way
272
                    int bitLength = stoi(attributes["bit_length"]);
272
                    int bitLength = stoi(attributes["bit_length"]);
273
 
273
 
274
                    sortedData[attributes["name"]].setType(attributes["type"]);
274
                    sortedData[attributes["name"]].setType(attributes["type"]);
275
                    sortedData[attributes["name"]].setUnit(attributes["unit"]);
275
                    sortedData[attributes["name"]].setUnit(attributes["unit"]);
276
                    sortedData[attributes["name"]].setBitLength(bitLength);
276
                    sortedData[attributes["name"]].setBitLength(bitLength);
277
                    sortedData[attributes["name"]].setBitLength(bitLength);
277
                    sortedData[attributes["name"]].setBitLength(bitLength);
278
                    sortedData[attributes["name"]].setStartBit(bitStart);
278
                    sortedData[attributes["name"]].setStartBit(bitStart);
279
                    sortedData[attributes["name"]].setName(attributes["name"]);
279
                    sortedData[attributes["name"]].setName(attributes["name"]);
280
                    sortedData[attributes["name"]].setValue(data[attributes["name"]].getValue());
280
                    sortedData[attributes["name"]].setValue(data[attributes["name"]].getValue());
281
                }
281
                }
282
            }
282
            }
283
        }
283
        }
284
    }
284
    }
285
 
285
 
286
    string bin = "0000000000000000000000000000000000000000000000000000000000000000";
286
    string bin = "0000000000000000000000000000000000000000000000000000000000000000";
287
    int heighestBit = 0;
287
    int heighestBit = 0;
288
 
288
 
289
    map<string, CanVariable>::iterator iter;
289
    map<string, CanVariable>::iterator iter;
290
 
290
 
291
    for (iter = sortedData.begin(); iter != sortedData.end(); iter++)///FIXME: We should check order and length
291
    for (iter = sortedData.begin(); iter != sortedData.end(); iter++)///FIXME: We should check order and length
292
    {
292
    {
293
        if (iter->second.getType() == "uint")
293
        if (iter->second.getType() == "uint")
294
        {
294
        {
295
            if (iter->second.getStartBit() + iter->second.getBitLength() > heighestBit)
295
            if (iter->second.getStartBit() + iter->second.getBitLength() > heighestBit)
296
                heighestBit = iter->second.getStartBit() + iter->second.getBitLength();
296
                heighestBit = iter->second.getStartBit() + iter->second.getBitLength();
297
 
297
 
298
            //cout << iter->first << ":" <<  stoi(iter->second.getValue()) << endl;
298
            //cout << iter->first << ":" <<  stoi(iter->second.getValue()) << endl;
299
            int a = stoi(iter->second.getValue());
299
            unsigned int a = stoi(iter->second.getValue());
300
           
300
           
301
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength()));
301
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength()));
302
        }
302
        }
303
        else if (iter->second.getType() == "float")
303
        else if (iter->second.getType() == "float")
304
        {
304
        {
305
            if (iter->second.getStartBit() + iter->second.getBitLength() > heighestBit)
305
            if (iter->second.getStartBit() + iter->second.getBitLength() > heighestBit)
306
                heighestBit = iter->second.getStartBit() + iter->second.getBitLength();
306
                heighestBit = iter->second.getStartBit() + iter->second.getBitLength();
307
 
307
 
308
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength()));
308
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength()));
309
        }
309
        }
310
        else if (iter->second.getType() == "ascii")
310
        else if (iter->second.getType() == "ascii")
311
        {
311
        {
312
            //cout << iter->second.getValue() << endl;
312
            //cout << iter->second.getValue() << endl;
313
            for (int n = 0; n < iter->second.getValue().length(); n++)
313
            for (int n = 0; n < iter->second.getValue().length(); n++)
314
            {
314
            {
315
                if (iter->second.getStartBit()+n*8 + 8 > heighestBit)
315
                if (iter->second.getStartBit()+n*8 + 8 > heighestBit)
316
                    heighestBit = iter->second.getStartBit()+n*8 + 8;
316
                    heighestBit = iter->second.getStartBit()+n*8 + 8;
317
 
317
 
318
                //cout << iter->second.getValue()[n] << ":" << (unsigned int)iter->second.getValue()[n] << endl;
318
                //cout << iter->second.getValue()[n] << ":" << (unsigned int)iter->second.getValue()[n] << endl;
319
                bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8));
319
                bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8));
320
            }
320
            }
321
        }
321
        }
-
 
322
        else if (iter->second.getType() == "hexstring")
-
 
323
        {
-
 
324
            for (int n = 0; n < iter->second.getValue().length(); n++)
-
 
325
            {
-
 
326
                if (iter->second.getStartBit()+n*4 + 4 > heighestBit)
-
 
327
                    heighestBit = iter->second.getStartBit()+n*4 + 4;
-
 
328
 
-
 
329
                string character;
-
 
330
                character += iter->second.getValue()[n];
-
 
331
 
-
 
332
                bin.replace(iter->second.getStartBit()+n*4, 4, hex2bin(character));
-
 
333
            }
-
 
334
        }
322
    }
335
    }
323
 
336
 
324
    bin = bin.substr(0, heighestBit);
337
    bin = bin.substr(0, heighestBit);
325
 
338
 
326
    //cout << bin2hex(bin) << endl;
339
    //cout << bin2hex(bin) << endl;
327
 
340
 
328
    return bin2hex(bin);
341
    return bin2hex(bin);
329
}
342
}
330
 
343
 
331
map<string, CanVariable> CanIdTranslator::translateData(int commandId, string moduleName, string dataHex)
344
map<string, CanVariable> CanIdTranslator::translateData(int commandId, string moduleName, string dataHex)
332
{
345
{
333
    map<string, CanVariable> variables;
346
    map<string, CanVariable> variables;
334
    XmlNode commandsNode = xmlNode.findChild("commands");
347
    XmlNode commandsNode = xmlNode.findChild("commands");
335
    vector<XmlNode> commandNodes = commandsNode.getChildren();
348
    vector<XmlNode> commandNodes = commandsNode.getChildren();
336
 
349
 
337
    string dataBin = hex2bin(dataHex);
350
    string dataBin = hex2bin(dataHex);
338
    //string dataString = "{";
351
    //string dataString = "{";
339
 
352
 
340
    for (int n = 0; n < commandNodes.size(); n++)
353
    for (int n = 0; n < commandNodes.size(); n++)
341
    {
354
    {
342
        map<string, string> attributes = commandNodes[n].getAttributes();
355
        map<string, string> attributes = commandNodes[n].getAttributes();
343
 
356
 
344
        bool correct = false;
357
        bool correct = false;
345
 
358
 
346
        if (commandId > 128)
359
        if (commandId >= 128)
347
        {
360
        {
348
            if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)
361
            if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)
349
            {
362
            {
350
                correct = true;
363
                correct = true;
351
            }
364
            }
352
        }
365
        }
353
        else if (stoi(attributes["id"]) == commandId)
366
        else if (stoi(attributes["id"]) == commandId)
354
        {
367
        {
355
            correct = true;
368
            correct = true;
356
        }
369
        }
357
 
370
 
358
        if (correct)
371
        if (correct)
359
        {
372
        {
360
            XmlNode varablesNode = commandNodes[n].findChild("variables");
373
            XmlNode varablesNode = commandNodes[n].findChild("variables");
361
 
374
 
362
            vector<XmlNode> variableNodes = varablesNode.getChildren();
375
            vector<XmlNode> variableNodes = varablesNode.getChildren();
363
 
376
 
364
            for (int c = 0; c < variableNodes.size(); c++)
377
            for (int c = 0; c < variableNodes.size(); c++)
365
            {
378
            {
366
                map<string, string> attributes = variableNodes[c].getAttributes();
379
                map<string, string> attributes = variableNodes[c].getAttributes();
367
 
380
 
368
                int bitStart = stoi(attributes["start_bit"]);
381
                int bitStart = stoi(attributes["start_bit"]);
369
                int bitLength = stoi(attributes["bit_length"]);
382
                int bitLength = stoi(attributes["bit_length"]);
370
 
383
 
371
                string bits = dataBin.substr(bitStart, bitLength);
384
                string bits = dataBin.substr(bitStart, bitLength);
372
               
385
               
373
                CanVariable variable;
386
                CanVariable variable;
374
               
387
               
375
                variable.setName(attributes["name"]);
388
                variable.setName(attributes["name"]);
376
                variable.setType(attributes["type"]);
389
                variable.setType(attributes["type"]);
377
                variable.setUnit(attributes["unit"]);
390
                variable.setUnit(attributes["unit"]);
378
                variable.setBitLength(bitLength);
391
                variable.setBitLength(bitLength);
379
 
392
 
380
                //dataString += " " + attributes["name"] + " : { value : ";
393
                //dataString += " " + attributes["name"] + " : { value : ";
381
                //dataString += attributes["name"] + "=";
394
                //dataString += attributes["name"] + "=";
382
                //dataString += attributes["name"] + ":" + attributes["type"] + ":" + attributes["bit_length"] + "=\"";
395
                //dataString += attributes["name"] + ":" + attributes["type"] + ":" + attributes["bit_length"] + "=\"";
383
 
396
 
384
                if (attributes["type"] == "uint")
397
                if (attributes["type"] == "uint")
385
                {
398
                {
386
                    variable.setValue(itos(bin2uint(bits)));
399
                    variable.setValue(itos(bin2uint(bits)));
387
                    //dataString += itos(bin2uint(bits));
400
                    //dataString += itos(bin2uint(bits));
388
                }
401
                }
389
                else if (attributes["type"] == "float")
402
                else if (attributes["type"] == "float")
390
                {
403
                {
391
                    variable.setValue(ftos(bin2float(bits)));
404
                    variable.setValue(ftos(bin2float(bits)));
392
                    //dataString += ftos(bin2float(bits));
405
                    //dataString += ftos(bin2float(bits));
393
                }
406
                }
394
                else if (attributes["type"] == "ascii")
407
                else if (attributes["type"] == "ascii")
395
                {
408
                {
396
                    string str;
409
                    string str;
397
                    //dataString += "'";
410
                    //dataString += "'";
398
 
411
 
399
                    for (int k = 0; k < bits.length(); k += 8)
412
                    for (int k = 0; k < bits.length(); k += 8)
400
                    {
413
                    {
401
                        str += (char)bin2uint(bits.substr(k, 8));
414
                        str += (char)bin2uint(bits.substr(k, 8));
402
                        //dataString += (char)bin2uint(bits.substr(k, 4));
415
                        //dataString += (char)bin2uint(bits.substr(k, 4));
403
                    }
416
                    }
404
 
417
 
405
                    variable.setValue(str);
418
                    variable.setValue(str);
406
                    //cout << "Parsed ascii: " << str << "\n";
419
                    //cout << "Parsed ascii: " << str << "\n";
407
                    //dataString += "'";
420
                    //dataString += "'";
-
 
421
                }
-
 
422
                else if (attributes["type"] == "hexstring")
-
 
423
                {
-
 
424
                    string str;
-
 
425
 
-
 
426
                    for (int k = 0; k < bits.length(); k += 4)
-
 
427
                    {
-
 
428
                        str += bin2hex(bits.substr(k, 4));
-
 
429
                        //dataString += (char)bin2uint(bits.substr(k, 4));
-
 
430
                    }
-
 
431
 
-
 
432
                    variable.setValue(str);
408
                }
433
                }
409
 
434
 
410
                variables[attributes["name"]] = variable;
435
                variables[attributes["name"]] = variable;
411
 
436
 
412
                //dataString += ", type : '" + attributes["type"] + "', bits : " + attributes["bit_length"] + ", unit : '" + attributes["unit"] + "' },";
437
                //dataString += ", type : '" + attributes["type"] + "', bits : " + attributes["bit_length"] + ", unit : '" + attributes["unit"] + "' },";
413
 
438
 
414
                //dataString += ",";
439
                //dataString += ",";
415
                //dataString += "\",";
440
                //dataString += "\",";
416
            }
441
            }
417
 
442
 
418
            //dataString = trim(dataString, ',');
443
            //dataString = trim(dataString, ',');
419
 
444
 
420
            //dataString += " }";
445
            //dataString += " }";
421
 
446
 
422
            //return dataString;
447
            //return dataString;
423
 
448
 
424
 
449
 
425
            return variables;
450
            return variables;
426
        }
451
        }
427
    }
452
    }
428
 
453
 
429
    //return dataString + " ]";
454
    //return dataString + " ]";
430
    return variables;
455
    return variables;
431
}
456
}
432
 
457
 
433
map<string, CanVariable> CanIdTranslator::translateHeartbeatData(string dataHex)
458
map<string, CanVariable> CanIdTranslator::translateHeartbeatData(string dataHex)
434
{
459
{
435
    map<string, CanVariable> variables;
460
    map<string, CanVariable> variables;
436
 
461
 
437
    string dataBin = hex2bin(dataHex);
462
    string dataBin = hex2bin(dataHex);
438
    string bits = dataBin.substr(0, 32);
463
    string bits = dataBin.substr(0, 32);
439
 
464
 
440
    CanVariable canVariable("HardwareId", itos(bin2uint(bits)), "uint", "");
465
    CanVariable canVariable("HardwareId", itos(bin2uint(bits)), "uint", "");
441
    canVariable.setBitLength(32);
466
    canVariable.setBitLength(32);
442
 
467
 
443
    variables["HardwareId"] = canVariable;
468
    variables["HardwareId"] = canVariable;
444
 
469
 
445
    return variables;
470
    return variables;
446
 
471
 
447
    //return "{ HardwareId : { value : " + itos(bin2uint(bits)) + ", type : 'uint', bits : 32, unit : '' }";
472
    //return "{ HardwareId : { value : " + itos(bin2uint(bits)) + ", type : 'uint', bits : 32, unit : '' }";
448
    //return "HardwareId:uint:32=\"" + itos(bin2uint(bits)) + "\"";
473
    //return "HardwareId:uint:32=\"" + itos(bin2uint(bits)) + "\"";
449
}
474
}
450
 
475