Rev 983 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 983 | Rev 986 | ||
|---|---|---|---|
| Line 75... | Line 75... | ||
| 75 | myGlobal = ObjectTemplate::New(); |
75 | myGlobal = ObjectTemplate::New(); |
| 76 | 76 | ||
| 77 | //associates "print" on script to the Print function |
77 | //associates "print" on script to the Print function |
| 78 | myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine_log)); |
78 | myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine_log)); |
| 79 | myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine_sendCanMessage)); |
79 | myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine_sendCanMessage)); |
| - | 80 | myGlobal->Set(String::New("sendCanNMTMessage"), FunctionTemplate::New(VirtualMachine_sendCanNMTMessage)); |
|
| 80 | myGlobal->Set(String::New("loadScript"), FunctionTemplate::New(VirtualMachine_loadScript)); |
81 | myGlobal->Set(String::New("loadScript"), FunctionTemplate::New(VirtualMachine_loadScript)); |
| 81 | myGlobal->Set(String::New("startIntervalThread"), FunctionTemplate::New(VirtualMachine_startIntervalThread)); |
82 | myGlobal->Set(String::New("startIntervalThread"), FunctionTemplate::New(VirtualMachine_startIntervalThread)); |
| 82 | myGlobal->Set(String::New("stopIntervalThread"), FunctionTemplate::New(VirtualMachine_stopIntervalThread)); |
83 | myGlobal->Set(String::New("stopIntervalThread"), FunctionTemplate::New(VirtualMachine_stopIntervalThread)); |
| 83 | myGlobal->Set(String::New("startSocketThread"), FunctionTemplate::New(VirtualMachine_startSocketThread)); |
84 | myGlobal->Set(String::New("startSocketThread"), FunctionTemplate::New(VirtualMachine_startSocketThread)); |
| 84 | myGlobal->Set(String::New("stopSocketThread"), FunctionTemplate::New(VirtualMachine_stopSocketThread)); |
85 | myGlobal->Set(String::New("stopSocketThread"), FunctionTemplate::New(VirtualMachine_stopSocketThread)); |
| Line 125... | Line 126... | ||
| 125 | if (file_exists(basePath + "Autostart.js")) |
126 | if (file_exists(basePath + "Autostart.js")) |
| 126 | { |
127 | { |
| 127 | loadScript("Autostart.js"); |
128 | loadScript("Autostart.js"); |
| 128 | } |
129 | } |
| 129 | 130 | ||
| 130 |
|
131 | myFunctionHandleNMTMessage = Handle<Function>::Cast(myContext->Global()->Get(String::New("handleNMTMessage"))); |
| 131 | myFunctionOfflineCheck = Handle<Function>::Cast(myContext->Global()->Get(String::New("offlineCheck"))); |
132 | myFunctionOfflineCheck = Handle<Function>::Cast(myContext->Global()->Get(String::New("offlineCheck"))); |
| 132 | myFunctionHandleMessage = Handle<Function>::Cast(myContext->Global()->Get(String::New("handleMessage"))); |
133 | myFunctionHandleMessage = Handle<Function>::Cast(myContext->Global()->Get(String::New("handleMessage"))); |
| 133 | myFunctionStartup = Handle<Function>::Cast(myContext->Global()->Get(String::New("startup"))); |
134 | myFunctionStartup = Handle<Function>::Cast(myContext->Global()->Get(String::New("startup"))); |
| 134 | } |
135 | } |
| 135 | } |
136 | } |
| Line 156... | Line 157... | ||
| 156 | 157 | ||
| 157 | while (myCanMessages.size() > 0) |
158 | while (myCanMessages.size() > 0) |
| 158 | { |
159 | { |
| 159 | canMessage = myCanMessages.pop(); |
160 | canMessage = myCanMessages.pop(); |
| 160 | 161 | ||
| 161 | if (canMessage. |
162 | if (canMessage.getClassName() == "nmt") |
| 162 | { |
163 | { |
| 163 |
|
164 | callHandleNMTMessage(canMessage); |
| 164 | } |
165 | } |
| 165 | else |
166 | else |
| 166 | { |
167 | { |
| 167 | callHandleMessage(canMessage); |
168 | callHandleMessage(canMessage); |
| 168 | } |
169 | } |
| Line 240... | Line 241... | ||
| 240 | } |
241 | } |
| 241 | 242 | ||
| 242 | return true; |
243 | return true; |
| 243 | } |
244 | } |
| 244 | 245 | ||
| 245 | void VirtualMachine:: |
246 | void VirtualMachine::callHandleNMTMessage(CanMessage canMessage) |
| 246 | { |
247 | { |
| 247 | const int argc = |
248 | const int argc = 3; |
| 248 | Handle<Value> argv[argc] = { Integer::New(hardwareId) }; |
- | |
| 249 | 249 | ||
| - | 250 | string jsonData = canMessage.getJSONData(); |
|
| - | 251 | ||
| - | 252 | Handle<Value> argv[argc] = {String::New(canMessage.getClassName().c_str()), |
|
| - | 253 | String::New(canMessage.getCommandName().c_str()), |
|
| - | 254 | String::New(jsonData.c_str()) }; |
|
| - | 255 | ||
| 250 | Handle<Value> result = |
256 | Handle<Value> result = myFunctionHandleNMTMessage->Call(myFunctionHandleNMTMessage, argc, argv); // argc and argv are your standard arguments to a function |
| 251 | } |
257 | } |
| 252 | 258 | ||
| 253 | void VirtualMachine::callHandleMessage(CanMessage canMessage) |
259 | void VirtualMachine::callHandleMessage(CanMessage canMessage) |
| 254 | { |
260 | { |
| 255 | const int argc = 6; |
261 | const int argc = 6; |
| Line 286... | Line 292... | ||
| 286 | 292 | ||
| 287 | return false; |
293 | return false; |
| 288 | } |
294 | } |
| 289 | 295 | ||
| 290 | unsigned int VirtualMachine::startSocketThread(string address, int port, unsigned int reconnectTimeout) |
296 | unsigned int VirtualMachine::startSocketThread(string address, int port, unsigned int reconnectTimeout) |
| 291 | { |
297 | { |
| 292 | SocketThread *socketThread = new SocketThread(address, port, reconnectTimeout); |
298 | SocketThread *socketThread = new SocketThread(address, port, reconnectTimeout); |
| 293 | mySocketThreads[socketThread->getId()] = socketThread; |
299 | mySocketThreads[socketThread->getId()] = socketThread; |
| 294 | mySocketThreads[socketThread->getId()]->start(); |
300 | mySocketThreads[socketThread->getId()]->start(); |
| 295 | 301 | ||
| 296 | return socketThread->getId(); |
302 | return socketThread->getId(); |
| 297 | } |
303 | } |
| 298 | 304 | ||
| 299 | bool VirtualMachine::stopSocketThread(unsigned int id) |
305 | bool VirtualMachine::stopSocketThread(unsigned int id) |
| 300 | { |
306 | { |
| 301 | 307 | ||
| 302 | 308 | ||
| 303 | if (mySocketThreads.find(id) != mySocketThreads.end()) |
309 | if (mySocketThreads.find(id) != mySocketThreads.end()) |
| 304 | { |
310 | { |
| 305 | //mySocketThreads[id]->stop(); |
311 | //mySocketThreads[id]->stop(); |
| 306 | delete mySocketThreads[id]; |
312 | delete mySocketThreads[id]; |
| 307 | 313 | ||
| 308 | mySocketThreads.erase(id); |
314 | mySocketThreads.erase(id); |
| 309 | return true; |
315 | return true; |
| 310 | } |
316 | } |
| 311 | 317 | ||
| 312 | return false; |
318 | return false; |
| 313 | } |
319 | } |
| 314 | 320 | ||
| 315 | void VirtualMachine::sendToSocketThread(unsigned int id, string data) |
321 | void VirtualMachine::sendToSocketThread(unsigned int id, string data) |
| 316 | { |
322 | { |
| Line 367... | Line 373... | ||
| 367 | } |
373 | } |
| 368 | 374 | ||
| 369 | Handle<Value> VirtualMachine_log(const Arguments& args) |
375 | Handle<Value> VirtualMachine_log(const Arguments& args) |
| 370 | { |
376 | { |
| 371 | SyslogStream &slog = SyslogStream::getInstance(); |
377 | SyslogStream &slog = SyslogStream::getInstance(); |
| 372 | 378 | ||
| 373 | String::AsciiValue str(args[0]); |
379 | String::AsciiValue str(args[0]); |
| 374 | 380 | ||
| 375 | slog << *str; |
381 | slog << *str; |
| 376 | 382 | ||
| 377 | return Undefined(); |
383 | return Undefined(); |
| 378 | } |
384 | } |
| 379 | 385 | ||
| 380 | Handle<Value> VirtualMachine_sendCanMessage(const Arguments& args) |
386 | Handle<Value> VirtualMachine_sendCanMessage(const Arguments& args) |
| - | 387 | { |
|
| - | 388 | //SyslogStream &slog = SyslogStream::getInstance(); |
|
| - | 389 | CanNetManager &canMan = CanNetManager::getInstance(); |
|
| - | 390 | ||
| - | 391 | CanMessage canMessage; |
|
| - | 392 | ||
| - | 393 | String::AsciiValue className(args[0]); |
|
| - | 394 | canMessage.setClassName(*className); |
|
| - | 395 | String::AsciiValue directionFlag(args[1]); |
|
| - | 396 | canMessage.setDirectionFlag(*directionFlag); |
|
| - | 397 | String::AsciiValue moduleName(args[2]); |
|
| - | 398 | canMessage.setModuleName(*moduleName); |
|
| - | 399 | canMessage.setModuleId(args[3]->Uint32Value()); |
|
| - | 400 | String::AsciiValue commandName(args[4]); |
|
| - | 401 | canMessage.setCommandName(*commandName); |
|
| - | 402 | ||
| - | 403 | String::AsciiValue dataString(args[5]); |
|
| - | 404 | ||
| - | 405 | vector<string> parts = explode(",", trim(*dataString, ',')); |
|
| - | 406 | map<string, CanVariable> data; |
|
| - | 407 | ||
| - | 408 | for (int n = 0; n < parts.size(); n++) |
|
| - | 409 | { |
|
| - | 410 | vector<string> keyAndValue = explode(":", parts[n]); |
|
| - | 411 | ||
| - | 412 | //string key = trim(keyAndValue[0], ' '); |
|
| - | 413 | //string value = trim(keyAndValue[1], ' '); |
|
| - | 414 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
|
| - | 415 | } |
|
| - | 416 | ||
| - | 417 | canMessage.setData(data); |
|
| - | 418 | ||
| - | 419 | canMan.sendMessage(canMessage); |
|
| - | 420 | ||
| - | 421 | return Undefined(); |
|
| - | 422 | } |
|
| - | 423 | ||
| - | 424 | Handle<Value> VirtualMachine_sendCanNMTMessage(const Arguments& args) |
|
| 381 | { |
425 | { |
| 382 | //SyslogStream &slog = SyslogStream::getInstance(); |
426 | //SyslogStream &slog = SyslogStream::getInstance(); |
| 383 | CanNetManager &canMan = CanNetManager::getInstance(); |
427 | CanNetManager &canMan = CanNetManager::getInstance(); |
| 384 | 428 | ||
| 385 | CanMessage canMessage; |
429 | CanMessage canMessage; |
| 386 | 430 | ||
| 387 | String::AsciiValue className(args[0]); |
431 | String::AsciiValue className(args[0]); |
| 388 | canMessage.setClassName(*className); |
432 | canMessage.setClassName(*className); |
| 389 | String::AsciiValue directionFlag(args[1]); |
- | |
| 390 | canMessage.setDirectionFlag(*directionFlag); |
- | |
| 391 | String::AsciiValue moduleName(args[2]); |
- | |
| 392 | canMessage.setModuleName(*moduleName); |
- | |
| 393 | canMessage.setModuleId(args[3]->Uint32Value()); |
- | |
| 394 | String::AsciiValue commandName(args[ |
433 | String::AsciiValue commandName(args[1]); |
| 395 | canMessage.setCommandName(*commandName); |
434 | canMessage.setCommandName(*commandName); |
| 396 | 435 | ||
| 397 | String::AsciiValue dataString(args[ |
436 | String::AsciiValue dataString(args[2]); |
| 398 | 437 | ||
| 399 | vector<string> parts = explode(",", trim(*dataString, ',')); |
438 | vector<string> parts = explode(",", trim(*dataString, ',')); |
| 400 | map<string, CanVariable> data; |
439 | map<string, CanVariable> data; |
| 401 | 440 | ||
| 402 | for (int n = 0; n < parts.size(); n++) |
441 | for (int n = 0; n < parts.size(); n++) |
| 403 | { |
442 | { |
| Line 407... | Line 446... | ||
| 407 | //string value = trim(keyAndValue[1], ' '); |
446 | //string value = trim(keyAndValue[1], ' '); |
| 408 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
447 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
| 409 | } |
448 | } |
| 410 | 449 | ||
| 411 | canMessage.setData(data); |
450 | canMessage.setData(data); |
| 412 | 451 | ||
| 413 | canMan.sendMessage(canMessage); |
452 | canMan.sendMessage(canMessage); |
| 414 | 453 | ||
| 415 | return Undefined(); |
454 | return Undefined(); |
| 416 | } |
455 | } |
| 417 | 456 | ||