Rev 1633 | Rev 1644 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1633 | Rev 1642 | ||
|---|---|---|---|
| Line 22... | Line 22... | ||
| 22 | 22 | ||
| 23 | #include <boost/lexical_cast.hpp> |
23 | #include <boost/lexical_cast.hpp> |
| 24 | 24 | ||
| 25 | #include "broker/Manager.h" |
25 | #include "broker/Manager.h" |
| 26 | #include "broker/Message.h" |
26 | #include "broker/Message.h" |
| 27 | #include " |
27 | #include "common/common.h" |
| 28 | #include "timer/Manager.h" |
28 | #include "timer/Manager.h" |
| 29 | #include "storage/Manager.h" |
29 | #include "storage/Manager.h" |
| 30 | 30 | ||
| 31 | #include "Message.h" |
31 | #include "can/Message.h" |
| 32 | 32 | ||
| 33 | namespace atom { |
33 | namespace atom { |
| 34 | namespace |
34 | namespace control { |
| 35 | 35 | ||
| 36 | Manager::Pointer Manager::instance_; |
36 | Manager::Pointer Manager::instance_; |
| 37 | 37 | ||
| 38 | Manager::Manager() : broker::Subscriber(false), LOG(" |
38 | Manager::Manager() : broker::Subscriber(false), LOG("control::Manager") |
| 39 | { |
39 | { |
| 40 | Node::SetupStateMachine(); |
40 | Node::SetupStateMachine(); |
| 41 | 41 | ||
| 42 | this->active_programming_node_id_ = 0; |
42 | this->active_programming_node_id_ = 0; |
| 43 | 43 | ||
| Line 80... | Line 80... | ||
| 80 | 80 | ||
| 81 | for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++) |
81 | for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++) |
| 82 | { |
82 | { |
| 83 | if (!it->second->CheckTimeout()) |
83 | if (!it->second->CheckTimeout()) |
| 84 | { |
84 | { |
| 85 | LOG.Info("Node " + |
85 | LOG.Info("Node " + common::ToHex(it->second->GetId()) + " has not sent anything in a long time, setting offline."); |
| 86 | this->RemoveModules(it->second->GetId()); |
86 | this->RemoveModules(it->second->GetId()); |
| 87 | 87 | ||
| 88 | storage::Manager::Instance()->FlushStore("NodeList"); |
88 | storage::Manager::Instance()->FlushStore("NodeList"); |
| 89 | storage::Manager::Instance()->FlushStore("ModuleList"); |
89 | storage::Manager::Instance()->FlushStore("ModuleList"); |
| 90 | } |
90 | } |
| Line 94... | Line 94... | ||
| 94 | void Manager::SlotOnMessageHandler(broker::Message::Pointer message) |
94 | void Manager::SlotOnMessageHandler(broker::Message::Pointer message) |
| 95 | { |
95 | { |
| 96 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
96 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
| 97 | { |
97 | { |
| 98 | //LOG.Debug("Message received"); |
98 | //LOG.Debug("Message received"); |
| 99 | Message* |
99 | can::Message* payload = static_cast<can::Message*>(message->GetPayload().get()); |
| 100 | Node::Pointer node; |
100 | Node::Pointer node; |
| 101 | 101 | ||
| 102 | if (payload->GetClassName() == "nmt") |
102 | if (payload->GetClassName() == "nmt") |
| 103 | { |
103 | { |
| 104 | if (payload->GetCommandName() == "Bios_Start") |
104 | if (payload->GetCommandName() == "Bios_Start") |
| Line 113... | Line 113... | ||
| 113 | } |
113 | } |
| 114 | else if (payload->GetCommandName() == "Heartbeat") |
114 | else if (payload->GetCommandName() == "Heartbeat") |
| 115 | { |
115 | { |
| 116 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
116 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 117 | node->Trigger(Node::EVENT_HEARTBEAT, payload->GetVariables()); |
117 | node->Trigger(Node::EVENT_HEARTBEAT, payload->GetVariables()); |
| - | 118 | } |
|
| - | 119 | else if (payload->GetCommandName() == "Pgm_Ack") |
|
| - | 120 | { |
|
| - | 121 | node = this->GetNode(this->active_programming_node_id_); |
|
| - | 122 | node->Trigger(Node::EVENT_PGM_ACK, payload->GetVariables()); |
|
| - | 123 | } |
|
| - | 124 | else if (payload->GetCommandName() == "Pgm_Nack") |
|
| - | 125 | { |
|
| - | 126 | node = this->GetNode(this->active_programming_node_id_); |
|
| - | 127 | node->Trigger(Node::EVENT_PGM_NACK, payload->GetVariables()); |
|
| 118 | } |
128 | } |
| 119 | } |
129 | } |
| 120 | else if (payload->GetDirectionName() == "From_Owner") |
130 | else if (payload->GetDirectionName() == "From_Owner") |
| 121 | { |
131 | { |
| 122 | Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName()); |
132 | Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName()); |
| Line 131... | Line 141... | ||
| 131 | if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
141 | if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
| 132 | { |
142 | { |
| 133 | node->Trigger(Node::EVENT_LIST_DONE, payload->GetVariables()); |
143 | node->Trigger(Node::EVENT_LIST_DONE, payload->GetVariables()); |
| 134 | } |
144 | } |
| 135 | 145 | ||
| 136 | LOG.Info("Module " + module->GetFullId() + " is available on node " + |
146 | LOG.Info("Module " + module->GetFullId() + " is available on node " + common::ToHex(node->GetId()) + "."); |
| 137 | this->signal_on_module_change_(module->GetFullId(), true); |
147 | this->signal_on_module_change_(module->GetFullId(), true); |
| 138 | } |
148 | } |
| 139 | else |
149 | else |
| 140 | { |
150 | { |
| 141 | node = this->GetNode(module->GetNodeId()); |
151 | node = this->GetNode(module->GetNodeId()); |
| Line 150... | Line 160... | ||
| 150 | void Manager::SlotOnNewState(Node::Id node_id, Node::State current_state, Node::State target_state) |
160 | void Manager::SlotOnNewState(Node::Id node_id, Node::State current_state, Node::State target_state) |
| 151 | { |
161 | { |
| 152 | if (target_state != Node::STATE_NORM_INITIALIZED) |
162 | if (target_state != Node::STATE_NORM_INITIALIZED) |
| 153 | { |
163 | { |
| 154 | this->RemoveModules(node_id); |
164 | this->RemoveModules(node_id); |
| 155 | } |
165 | } |
| 156 | 166 | ||
| 157 | if (target_state == Node::STATE_BPGM_OFFLINE || target_state == Node::STATE_APGM_OFFLINE) |
167 | if (target_state == Node::STATE_BPGM_OFFLINE || target_state == Node::STATE_APGM_OFFLINE) |
| 158 | { |
168 | { |
| 159 | this->active_programming_node_id_ = node_id; |
169 | this->active_programming_node_id_ = node_id; |
| 160 | } |
170 | } |
| Line 162... | Line 172... | ||
| 162 | { |
172 | { |
| 163 | this->active_programming_node_id_ = 0; |
173 | this->active_programming_node_id_ = 0; |
| 164 | } |
174 | } |
| 165 | } |
175 | } |
| 166 | 176 | ||
| 167 | void Manager::SendMessageHandler(std::string full_id, std::string command, |
177 | void Manager::SendMessageHandler(std::string full_id, std::string command, common::StringMap variables) |
| 168 | { |
178 | { |
| 169 | ModuleList::iterator it = this->modules_.find(full_id); |
179 | ModuleList::iterator it = this->modules_.find(full_id); |
| 170 | 180 | ||
| 171 | if (it == this->modules_.end()) |
181 | if (it == this->modules_.end()) |
| 172 | { |
182 | { |
| Line 174... | Line 184... | ||
| 174 | return; |
184 | return; |
| 175 | } |
185 | } |
| 176 | 186 | ||
| 177 | try |
187 | try |
| 178 | { |
188 | { |
| 179 | Message* |
189 | can::Message* payload = new can::Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command); |
| 180 | payload->SetVariables(variables); |
190 | payload->SetVariables(variables); |
| 181 | 191 | ||
| 182 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
192 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
| 183 | //LOG.Debug("Command " + command + " sent to " + it->second->GetName()); |
193 | //LOG.Debug("Command " + command + " sent to " + it->second->GetName()); |
| 184 | } |
194 | } |
| Line 186... | Line 196... | ||
| 186 | { |
196 | { |
| 187 | LOG.Error("Failed to send message, " + std::string(e.what())); |
197 | LOG.Error("Failed to send message, " + std::string(e.what())); |
| 188 | } |
198 | } |
| 189 | } |
199 | } |
| 190 | 200 | ||
| 191 | void Manager::SendMessage(std::string full_id, std::string command, |
201 | void Manager::SendMessage(std::string full_id, std::string command, common::StringMap variables) |
| 192 | { |
202 | { |
| 193 | this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables)); |
203 | this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables)); |
| 194 | } |
204 | } |
| 195 | 205 | ||
| 196 | bool Manager::IsModuleAvailable(std::string full_id) |
206 | bool Manager::IsModuleAvailable(std::string full_id) |
| 197 | { |
207 | { |
| 198 | return this->modules_.find(full_id) != this->modules_.end(); |
208 | return this->modules_.find(full_id) != this->modules_.end(); |
| - | 209 | } |
|
| - | 210 | ||
| - | 211 | bool Manager::ProgramNode(Node::Id node_id, bool is_bios, std::string filename) |
|
| - | 212 | { |
|
| - | 213 | NodeList::iterator it = this->nodes_.find(node_id); |
|
| - | 214 | ||
| - | 215 | if (it == this->nodes_.end()) |
|
| - | 216 | { |
|
| - | 217 | LOG.Error("Could not find node " + common::ToHex(node_id)); |
|
| - | 218 | return false; |
|
| - | 219 | } |
|
| - | 220 | ||
| - | 221 | Code::Pointer code = Code::Pointer(new Code()); |
|
| - | 222 | if (!code->LoadIntelHexFile(filename)) |
|
| - | 223 | { |
|
| - | 224 | LOG.Error("Failed to parse " + filename); |
|
| - | 225 | return false; |
|
| - | 226 | } |
|
| - | 227 | ||
| - | 228 | if (is_bios) |
|
| - | 229 | { |
|
| - | 230 | LOG.Debug("is_bios true"); |
|
| - | 231 | it->second->ProgramBios(code); |
|
| - | 232 | } |
|
| - | 233 | else |
|
| - | 234 | { |
|
| - | 235 | LOG.Debug("is_bios false"); |
|
| - | 236 | it->second->ProgramApplication(code); |
|
| - | 237 | } |
|
| - | 238 | ||
| - | 239 | return true; |
|
| 199 | } |
240 | } |
| 200 | 241 | ||
| 201 | Node::Pointer Manager::GetNode(Node::Id node_id) |
242 | Node::Pointer Manager::GetNode(Node::Id node_id) |
| 202 | { |
243 | { |
| 203 | Node::Pointer node; |
244 | Node::Pointer node; |
| Line 206... | Line 247... | ||
| 206 | if (it == this->nodes_.end()) |
247 | if (it == this->nodes_.end()) |
| 207 | { |
248 | { |
| 208 | node = Node::Pointer(new Node(node_id)); |
249 | node = Node::Pointer(new Node(node_id)); |
| 209 | node->ConnectSlots(Node::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(this->tracker_)); |
250 | node->ConnectSlots(Node::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(this->tracker_)); |
| 210 | this->nodes_[node_id] = node; |
251 | this->nodes_[node_id] = node; |
| 211 | } |
252 | } |
| 212 | else |
253 | else |
| 213 | { |
254 | { |
| 214 | node = it->second; |
255 | node = it->second; |
| 215 | } |
256 | } |
| 216 | 257 | ||
| 217 | return node; |
258 | return node; |
| Line 245... | Line 286... | ||
| 245 | this->signal_on_module_change_(it->second->GetFullId(), false); |
286 | this->signal_on_module_change_(it->second->GetFullId(), false); |
| 246 | 287 | ||
| 247 | this->modules_.erase(it); |
288 | this->modules_.erase(it); |
| 248 | } |
289 | } |
| 249 | } |
290 | } |
| 250 | } |
291 | } |
| 251 | 292 | ||
| 252 | unsigned int Manager::GetNumberOfModules(Node::Id node_id) |
293 | unsigned int Manager::GetNumberOfModules(Node::Id node_id) |
| 253 | { |
294 | { |
| 254 | unsigned int number_of_modules = 0; |
295 | unsigned int number_of_modules = 0; |
| 255 | 296 | ||
| Line 262... | Line 303... | ||
| 262 | } |
303 | } |
| 263 | 304 | ||
| 264 | return number_of_modules; |
305 | return number_of_modules; |
| 265 | } |
306 | } |
| 266 | 307 | ||
| 267 | - | ||
| 268 | }; // namespace |
308 | }; // namespace control |
| 269 | }; // namespace atom |
309 | }; // namespace atom |