Subversion Repositories HomeAutomation

Rev

Rev 1606 | Rev 1608 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1606 Rev 1607
Line 163... Line 163...
163
        }
163
        }
164
       
164
       
165
        for (unsigned int n = 0; n < variable_nodes.size(); n++)
165
        for (unsigned int n = 0; n < variable_nodes.size(); n++)
166
        {
166
        {
167
            value = payload->GetVariable(variable_nodes[n].GetAttributeValue("name"));
167
            value = payload->GetVariable(variable_nodes[n].GetAttributeValue("name"));
168
           
168
            //LOG.Debug(variable_nodes[n].GetAttributeValue("name") + "=" + value); 
169
            if (value == "")
169
            if (value == "")
170
            {
170
            {
171
                continue;
171
                continue;
172
            }
172
            }
173
           
173
           
174
            start_bit = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("start_bit"));
174
            start_bit = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("start_bit"));
175
            bit_length = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("bit_length"));
175
            bit_length = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("bit_length"));
176
            type = variable_nodes[n].GetAttributeValue("type");
176
            type = variable_nodes[n].GetAttributeValue("type");
177
           
177
           
178
            if (highest_bit < start_bit + bit_length - 1)
178
            if (highest_bit < start_bit + bit_length - 1)
179
            {
179
            {
180
                highest_bit = start_bit + bit_length - 1;
180
                highest_bit = start_bit + bit_length - 1;
181
            }
181
            }
182
           
182
           
183
            if (type == "int")
183
            if (type == "int")
184
            {
184
            {
185
                Protocol::Instance()->EncodeInt(databits, start_bit, bit_length, value);
185
                Protocol::Instance()->EncodeInt(databits, start_bit, bit_length, value);
186
            }
186
            }
187
            else if (type == "float")
187
            else if (type == "float")
188
            {
188
            {
189
                Protocol::Instance()->EncodeFloat(databits, start_bit, bit_length, value);
189
                Protocol::Instance()->EncodeFloat(databits, start_bit, bit_length, value);
190
            }
190
            }
191
            else if (type == "ascii")
191
            else if (type == "ascii")
192
            {
192
            {
193
                Protocol::Instance()->EncodeAscii(databits, start_bit, bit_length, value);
193
                Protocol::Instance()->EncodeAscii(databits, start_bit, bit_length, value);
194
            }
194
            }
195
            else if (type == "hexstring")
195
            else if (type == "hexstring")
196
            {
196
            {
197
                Protocol::Instance()->EncodeHexstring(databits, start_bit, bit_length, value);
197
                Protocol::Instance()->EncodeHexstring(databits, start_bit, bit_length, value);
198
            }
198
            }
199
            else if (type == "enum")
199
            else if (type == "enum")
200
            {
200
            {
201
                value = variable_nodes[n].SelectChild("name", value).GetAttributeValue("id");
201
                value = variable_nodes[n].SelectChild("name", value).GetAttributeValue("id");
202
                Protocol::Instance()->EncodeUint(databits, start_bit, bit_length, value);
202
                Protocol::Instance()->EncodeUint(databits, start_bit, bit_length, value);
203
            }
203
            }
204
            else// if (type == "uint")
204
            else// if (type == "uint")
205
            {
205
            {
206
                Protocol::Instance()->EncodeUint(databits, start_bit, bit_length, value);
206
                Protocol::Instance()->EncodeUint(databits, start_bit, bit_length, value);
207
            }
207
            }
208
        }
208
        }
209
       
209
       
210
        //LOG.Debug(databits.ToDebugString());
210
        //LOG.Debug(databits.ToDebugString());
211
       
211
       
212
        unsigned int length = ceil((float)highest_bit / 8.0f);
212
        unsigned int length = ceil((float)highest_bit / 8.0f);
213
       
213
       
214
        data[5] = 1;
214
        data[5] = 1;
215
        data[6] = 0;
215
        data[6] = 0;
216
       
216
       
217
        data[7] = length;
217
        data[7] = length;
218
       
218
       
219
        for (unsigned int n = 0; n < 8; n++)
219
        for (unsigned int n = 0; n < 8; n++)
220
        {
220
        {
221
            data[8 + n] = databits.GetBytes()[n];
221
            data[8 + n] = databits.GetBytes()[n];
222
        }
222
        }
223
       
223
       
224
        data[16] = PACKET_END;
224
        data[16] = PACKET_END;
225
       
225
       
226
        data.SetSize(17);
226
        data.SetSize(17);
227
       
227
       
Line 231... Line 231...
231
}
231
}
232
 
232
 
233
void Network::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, type::Byteset data)
233
void Network::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, type::Byteset data)
234
{
234
{
235
    if (client_id != this->client_id_)
235
    if (client_id != this->client_id_)
236
    {
236
    {
237
            return;
237
            return;
238
    }
238
    }
239
   
239
   
240
    static bool have_start = false;
240
    static bool have_start = false;
241
   
241
   
242
    for (unsigned int n = 0; n < data.GetSize(); n++)
242
    for (unsigned int n = 0; n < data.GetSize(); n++)
243
    {
243
    {
244
        if (have_start)
244
        if (have_start)
245
        {
245
        {
246
            if (data[n] == PACKET_END && this->buffer_.GetSize() == 15)
246
            if (data[n] == PACKET_END && this->buffer_.GetSize() == 15)
247
            {
247
            {
248
                this->ProcessBuffer();
248
                this->ProcessBuffer();
249
                have_start = false;
249
                have_start = false;
250
            }
250
            }
251
            else
251
            else
252
            {
252
            {
253
                this->buffer_.Append(data[n]);
253
                this->buffer_.Append(data[n]);
254
            }
254
            }
255
        }
255
        }
Line 311... Line 311...
311
        std::string module_name = "";
311
        std::string module_name = "";
312
        unsigned int id = 0;
312
        unsigned int id = 0;
313
        std::string command_name = "";
313
        std::string command_name = "";
314
       
314
       
315
        unsigned int class_id = (this->buffer_[3] >> 1) & 0x0F;
315
        unsigned int class_id = (this->buffer_[3] >> 1) & 0x0F;
-
 
316
        //LOG.Debug("class_id=" + boost::lexical_cast<std::string>(class_id));
316
        class_name = Protocol::Instance()->LookupClassName(class_id);
317
        class_name = Protocol::Instance()->LookupClassName(class_id);
317
       
318
       
318
        if (class_name == "nmt")
319
        if (class_name == "nmt")
319
        {
320
        {
320
            unsigned int command_id = this->buffer_[2];
321
            unsigned int command_id = this->buffer_[2];
-
 
322
            //LOG.Debug("command_id=" + boost::lexical_cast<std::string>(command_id));
321
            command_name = Protocol::Instance()->LookupNMTCommandName(command_id);
323
            command_name = Protocol::Instance()->LookupNMTCommandName(command_id);
322
        }
324
        }
323
        else
325
        else
324
        {
326
        {
325
            unsigned int direction_flag = this->buffer_[3] & 0x01;
327
            unsigned int direction_flag = this->buffer_[3] & 0x01;
-
 
328
            //LOG.Debug("direction_flag=" + boost::lexical_cast<std::string>(direction_flag));
326
            direction_name = Protocol::Instance()->LookupDirectionFlag(direction_flag);
329
            direction_name = Protocol::Instance()->LookupDirectionFlag(direction_flag);
327
           
330
           
328
            unsigned int module_id = this->buffer_[2];
331
            unsigned int module_id = this->buffer_[2];
-
 
332
            //LOG.Debug("module_id=" + boost::lexical_cast<std::string>(module_id));
329
            module_name = Protocol::Instance()->LookupModuleName(module_id);
333
            module_name = Protocol::Instance()->LookupModuleName(module_id);
330
           
334
           
331
            id = this->buffer_[1];
335
            id = this->buffer_[1];
332
           
336
           
333
            unsigned int command_id = this->buffer_[0];
337
            unsigned int command_id = this->buffer_[0];
-
 
338
            //LOG.Debug("command_id=" + boost::lexical_cast<std::string>(command_id));
334
            command_name = Protocol::Instance()->LookupCommandName(command_id, module_name);
339
            command_name = Protocol::Instance()->LookupCommandName(command_id, module_name);
335
        }
340
        }
336
 
341
 
337
        Message* payload = new Message(class_name, direction_name, module_name, id, command_name);
342
        Message* payload = new Message(class_name, direction_name, module_name, id, command_name);
338
 
343
 
Line 346... Line 351...
346
        }
351
        }
347
 
352
 
348
        type::Bitset databits(data_set);
353
        type::Bitset databits(data_set);
349
        xml::Node::NodeList variable_nodes;
354
        xml::Node::NodeList variable_nodes;
350
        unsigned int start_bit;
355
        unsigned int start_bit;
351
        unsigned int bit_length;
356
        int bit_length;
352
        std::string type;
357
        std::string type;
353
        std::string value;
358
        std::string value;
-
 
359
        std::string name;
354
       
360
       
355
        if (class_name == "nmt")
361
        if (class_name == "nmt")
356
        {
362
        {
357
            variable_nodes = Protocol::Instance()->GetNMTCommandVariables(command_name);
363
            variable_nodes = Protocol::Instance()->GetNMTCommandVariables(command_name);
358
        }
364
        }
Line 361... Line 367...
361
            variable_nodes = Protocol::Instance()->GetCommandVariables(command_name, module_name);
367
            variable_nodes = Protocol::Instance()->GetCommandVariables(command_name, module_name);
362
        }
368
        }
363
       
369
       
364
        for (unsigned int n = 0; n < variable_nodes.size(); n++)
370
        for (unsigned int n = 0; n < variable_nodes.size(); n++)
365
        {
371
        {
-
 
372
            name = variable_nodes[n].GetAttributeValue("name");
366
            start_bit = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("start_bit"));
373
            start_bit = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("start_bit"));
367
            bit_length = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("bit_length"));
374
            bit_length = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("bit_length"));
-
 
375
           
-
 
376
            if (databits.GetCount() < start_bit + bit_length)
-
 
377
            {
-
 
378
                bit_length = databits.GetCount() - start_bit;
-
 
379
               
-
 
380
                if (bit_length <= 0)
-
 
381
                {
-
 
382
                    LOG.Warning("Can not read variable " + name + " for command " + command_name + ", message is to short, it there a match between the module and the protocol XML file?");
-
 
383
                    //LOG.Debug("start_bit=" + boost::lexical_cast<std::string>(start_bit) + ", bit_length=" + boost::lexical_cast<std::string>(bit_length) + ", databits.GetCount()=" + boost::lexical_cast<std::string>(databits.GetCount()));
-
 
384
                    continue;
-
 
385
                }
-
 
386
            }
-
 
387
           
368
            type = variable_nodes[n].GetAttributeValue("type");
388
            type = variable_nodes[n].GetAttributeValue("type");
369
           
389
           
370
            if (type == "int")
390
            if (type == "int")
371
            {
391
            {
372
                value = Protocol::Instance()->DecodeInt(databits, start_bit, bit_length);
392
                value = Protocol::Instance()->DecodeInt(databits, start_bit, bit_length);
373
            }
393
            }
374
            else if (type == "float")
394
            else if (type == "float")
375
            {
395
            {
376
                value = Protocol::Instance()->DecodeFloat(databits, start_bit, bit_length);
396
                value = Protocol::Instance()->DecodeFloat(databits, start_bit, bit_length);
377
            }
397
            }
378
            else if (type == "ascii")
398
            else if (type == "ascii")
379
            {
399
            {
380
                value = Protocol::Instance()->DecodeAscii(databits, start_bit, bit_length);
400
                value = Protocol::Instance()->DecodeAscii(databits, start_bit, bit_length);
381
            }
401
            }
382
            else if (type == "hexstring")
402
            else if (type == "hexstring")
383
            {
403
            {
384
                value = Protocol::Instance()->DecodeHexstring(databits, start_bit, bit_length);
404
                value = Protocol::Instance()->DecodeHexstring(databits, start_bit, bit_length);
385
            }
405
            }
386
            else if (type == "enum")
406
            else if (type == "enum")
387
            {
407
            {
-
 
408
                //LOG.Debug("Enum: command name=" + command_name);
388
                value = Protocol::Instance()->DecodeUint(databits, start_bit, bit_length);
409
                value = Protocol::Instance()->DecodeUint(databits, start_bit, bit_length);
-
 
410
                //LOG.Debug("Enum: value=" + boost::lexical_cast<std::string>(value));
-
 
411
                //LOG.Debug("start_bit=" + boost::lexical_cast<std::string>(start_bit) + ", bit_length=" + boost::lexical_cast<std::string>(bit_length));
-
 
412
               
389
                value = variable_nodes[n].SelectChild("id", value).GetAttributeValue("name");
413
                value = variable_nodes[n].SelectChild("id", value).GetAttributeValue("name");
390
            }
414
            }
391
            else// if (type == "uint")
415
            else// if (type == "uint")
392
            {
416
            {
-
 
417
                //LOG.Debug("type: type=" + type);
393
                value = Protocol::Instance()->DecodeUint(databits, start_bit, bit_length);
418
                value = Protocol::Instance()->DecodeUint(databits, start_bit, bit_length);
394
            }
419
            }
395
           
420
           
396
            payload->SetVariable(variable_nodes[n].GetAttributeValue("name"), value);
421
            payload->SetVariable(name, value);
397
        }
422
        }
398
       
423
       
399
        broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
424
        broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
400
    }
425
    }
401
    catch (std::runtime_error& e)
426
    catch (std::runtime_error& e)