Subversion Repositories HomeAutomation

Rev

Rev 983 | Rev 995 | Go to most recent revision | Show entire file | Regard 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
            myFunctionHandleHeartbeat = Handle<Function>::Cast(myContext->Global()->Get(String::New("handleHeartbeat")));
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.isHeartbeat())
162
            if (canMessage.getClassName() == "nmt")
162
            {
163
            {
163
                callHandleHeartbeat(stoi(canMessage.getData()["HardwareId"].getValue()));
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::callHandleHeartbeat(int hardwareId)
246
void VirtualMachine::callHandleNMTMessage(CanMessage canMessage)
246
{
247
{
247
    const int argc = 1;
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 = myFunctionHandleHeartbeat->Call(myFunctionHandleHeartbeat, argc, argv); // argc and argv are your standard arguments to a function
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 279... Line 285...
279
bool VirtualMachine::stopIntervalThread(unsigned int id)
285
bool VirtualMachine::stopIntervalThread(unsigned int id)
280
{
286
{
281
    if (myIntervalThreads.find(id) != myIntervalThreads.end())
287
    if (myIntervalThreads.find(id) != myIntervalThreads.end())
282
    {
288
    {
283
        myIntervalThreads.erase(id);
289
        myIntervalThreads.erase(id);
284
        return true;
290
        return true;
285
    }
291
    }
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);
Line 319... Line 325...
319
        mySocketThreads[id]->send(data);
325
        mySocketThreads[id]->send(data);
320
    }
326
    }
321
}
327
}
322
 
328
 
323
bool VirtualMachine::runExpression(string expression)
329
bool VirtualMachine::runExpression(string expression)
324
{
330
{
325
    SyslogStream &slog = SyslogStream::getInstance();
331
    SyslogStream &slog = SyslogStream::getInstance();
326
 
332
 
327
    string scriptName = "runExpression";
333
    string scriptName = "runExpression";
328
 
334
 
329
    Handle<String> source =  String::New(expression.c_str(), expression.length());
335
    Handle<String> source =  String::New(expression.c_str(), expression.length());
Line 337... Line 343...
337
    //compile script to binary code - JIT
343
    //compile script to binary code - JIT
338
    Handle<Script> script = Script::Compile(source, name);
344
    Handle<Script> script = Script::Compile(source, name);
339
 
345
 
340
    //check if we got problems on compilation
346
    //check if we got problems on compilation
341
    if (script.IsEmpty())
347
    if (script.IsEmpty())
342
    {
348
    {
343
        // Print errors that happened during compilation.
349
        // Print errors that happened during compilation.
344
        String::AsciiValue error(try_catch.Exception());
350
        String::AsciiValue error(try_catch.Exception());
345
        string sError = *error;
351
        string sError = *error;
346
        slog << "runExpression: Failed to compile script: " + sError + "\n";
352
        slog << "runExpression: Failed to compile script: " + sError + "\n";
347
        slog << expression << "\n\n";
353
        slog << expression << "\n\n";
Line 375... Line 381...
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[4]);
433
    String::AsciiValue commandName(args[1]);
395
    canMessage.setCommandName(*commandName);
434
    canMessage.setCommandName(*commandName);
396
 
435
 
397
    String::AsciiValue dataString(args[5]);
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++)