Subversion Repositories HomeAutomation

Rev

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

Rev 1314 Rev 1323
Line 8... Line 8...
8
#include "Message.h"
8
#include "Message.h"
9
 
9
 
10
namespace atom {
10
namespace atom {
11
namespace message {
11
namespace message {
12
 
12
 
13
Message::Message(string moduletype, unsigned int moduleId, string type)
13
Message::Message(Header header)
14
{
14
{
15
    LOG.setName("Message");
15
    LOG.setName("Message");
16
 
16
 
17
    this->myModuletype = moduletype;
-
 
18
    this->myModuleId = moduleId;
-
 
19
    this->myType = type;
17
    this->myHeader = header;
20
 
18
 
21
    xml::Node moduleNode = Protocol::getInstance()->getRootNode().selectFirst("modules").selectFirst("module", xml::Node::attributePair("name", moduletype));
-
 
22
    xml::Node messageNode = Protocol::getInstance()->getRootNode().selectFirst("messagetypes").selectFirst("messagetype", xml::Node::attributePair("name", type));
19
    xml::Node messageNode = Protocol::getInstance()->getRootNode().selectFirst("messagetypes").selectFirst("messagetype", xml::Node::attributePair("name", this->myHeader.getMessageType()));
23
 
20
 
-
 
21
    // Load variables
24
    xml::Node::nodeList variableNodes = messageNode.select("variable");
22
    xml::Node::nodeList variableNodes = messageNode.select("variable");
25
 
23
 
26
    for (unsigned int n = 0; n < variableNodes.size(); n++)
24
    for (unsigned int n = 0; n < variableNodes.size(); n++)
27
    {
25
    {
28
        Variable variable(variableNodes[n]["name"], variableNodes[n]["datatype"], convert::string2bool(variableNodes[n]["required"]), variableNodes[n]["unit"]);
26
        Variable variable(variableNodes[n]);
29
        this->myVariables[variableNodes[n]["name"]] = variable;
27
        this->myVariables[variableNodes[n]["name"]] = variable;
30
    }
28
    }
31
 
29
 
32
    if (moduleNode.selectFirst("in").select("message", xml::Node::attributePair("name", type)).size() > 0)
-
 
33
    {
-
 
34
        this->myFromModule = false;
-
 
35
    }
-
 
36
    else if (moduleNode.selectFirst("out").select("message", xml::Node::attributePair("name", type)).size() > 0)
-
 
37
    {
-
 
38
        this->myFromModule = true;
-
 
39
    }
-
 
40
    else
-
 
41
    {
-
 
42
        LOG.error("This message is not supported by this module type, something is wrong; protocol.xml is old, module code is old or a Atom is trying to send and illegal message.");
-
 
43
        // TODO throw exception
30
    // Load responses
44
    }
-
 
45
 
-
 
46
    xml::Node::nodeList responseNodes = messageNode.select("response");
31
    xml::Node::nodeList responseNodes = messageNode.select("response");
47
 
32
 
48
    for (unsigned int n = 0; n < responseNodes.size(); n++)
33
    for (unsigned int n = 0; n < responseNodes.size(); n++)
49
    {
34
    {
50
        this->myResponses.push_back(responseNodes[n]["code"]); // TODO Validate the code
35
        this->myResponses.push_back(responseNodes[n]["code"]); // TODO Validate the code
51
    }
36
    }
-
 
37
 
-
 
38
    // Check if valid for this module
-
 
39
    xml::Node moduleNode = Protocol::getInstance()->getRootNode().selectFirst("modules").selectFirst("module", xml::Node::attributePair("name", this->myHeader.getModuleType()));
52
 
40
 
-
 
41
    if (moduleNode.selectFirst("in").select("message", xml::Node::attributePair("name", this->myHeader.getModuleType())).size() == 0 &&
-
 
42
        moduleNode.selectFirst("out").select("message", xml::Node::attributePair("name", this->myHeader.getModuleType())).size() == 0)
-
 
43
    {
-
 
44
        LOG.error("This message is not supported by this module type, something is wrong; protocol.xml is old, module code is old or a Atom is trying to send and illegal message.");
-
 
45
        // TODO throw exception
-
 
46
    }
53
}
47
}
54
 
48
 
55
Message::Message()
49
Message::Message()
56
{
50
{
-
 
51
    LOG.setName("Message");
57
}
52
}
58
 
53
 
59
Message::~Message()
54
Message::~Message()
60
{
55
{
61
}
56
}
62
 
57
 
63
void Message::setOrigin(const void *origin)
58
void Message::setOrigin(const void *origin)
64
{
59
{
65
    //LOG.debug("setOrigin(" + itos((unsigned long) origin) + ")");
60
    //LOG.debug("setOrigin(" + itos((unsigned long) origin) + ")");
66
    this->myOrigin = origin;
61
    this->myOrigin = origin;
67
}
62
}
68
 
63
 
69
bool Message::isOrigin(const void *origin)
64
bool Message::isOrigin(const void *origin)
Line 71... Line 66...
71
    //LOG.debug("isOrigin(" + itos((unsigned long)origin) + "==" + itos((unsigned long) this->myOrigin) + ")");
66
    //LOG.debug("isOrigin(" + itos((unsigned long)origin) + "==" + itos((unsigned long) this->myOrigin) + ")");
72
    return this->myOrigin == origin;
67
    return this->myOrigin == origin;
73
}
68
}
74
 
69
 
75
 
70
 
76
bool Message::isFromModule()
71
Header & Message::getHeader()
77
{
72
{
78
    return this->myFromModule;
-
 
79
}
-
 
80
 
-
 
81
string Message::getModuletype()
-
 
82
{
-
 
83
    return this->myModuletype;
-
 
84
}
-
 
85
 
-
 
86
unsigned int Message::getModuleId()
-
 
87
{
-
 
88
    return this->myModuleId;
-
 
89
}
-
 
90
 
-
 
91
string Message::getType()
-
 
92
{
-
 
93
    return this->myType;
73
    return this->myHeader;
94
}
74
}
95
 
75
 
96
Variable & Message::getVariable(string name)
76
Variable & Message::getVariable(string name)
97
{
77
{
98
    return this->myVariables[name];
78
    return this->myVariables[name];
99
 
79
 
100
    // TODO Check if it does not exist, throw exception?
80
    // TODO Check if it does not exist, throw exception?
101
}
81
}
-
 
82
 
-
 
83
Message::responseList Message::getResponses()
-
 
84
{
-
 
85
    return this->myResponses;
-
 
86
}
-
 
87
 
102
 
88
 
103
void Message::readBits(BitBuffer & buffer)
89
void Message::readBits(BitBuffer & buffer)
104
{
90
{
105
    for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
91
    for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
106
    {
92
    {
107
        iter->second.readBits(buffer);
93
        iter->second.readBits(buffer);
108
    }
94
    }
109
}
95
}
110
 
-
 
111
 
96
 
112
Message::responseList Message::getResponses()
97
void Message::writeBits(BitBuffer & buffer)
113
{
98
{
-
 
99
    for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
-
 
100
    {
114
    return this->myResponses;
101
        iter->second.writeBits(buffer);
115
}
102
    }
-
 
103
}
-
 
104
 
116
 
105
 
117
}
106
}
118
}
107
}