Subversion Repositories HomeAutomation

Rev

Rev 973 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 973 Rev 976
Line 63... Line 63...
63
    string scriptName = "Base.js";
63
    string scriptName = "Base.js";
64
    string scriptFileName = basePath + "System/" + scriptName;
64
    string scriptFileName = basePath + "System/" + scriptName;
65
 
65
 
66
    if (!file_exists(scriptFileName))
66
    if (!file_exists(scriptFileName))
67
    {
67
    {
68
        slog << "Failed to load :" << scriptFileName << "\n";
68
        slog << "Failed to load :" + scriptFileName + "\n";
69
    }
69
    }
70
 
70
 
71
    string scriptSource = file_get_contents(scriptFileName);
71
    string scriptSource = file_get_contents(scriptFileName);
72
 
72
 
73
    Handle<String> source =  String::New(scriptSource.c_str(), scriptSource.length());
73
    Handle<String> source =  String::New(scriptSource.c_str(), scriptSource.length());
Line 81... Line 81...
81
    myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine_log));
81
    myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine_log));
82
    myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine_sendCanMessage));
82
    myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine_sendCanMessage));
83
    myGlobal->Set(String::New("loadScript"), FunctionTemplate::New(VirtualMachine_loadScript));
83
    myGlobal->Set(String::New("loadScript"), FunctionTemplate::New(VirtualMachine_loadScript));
84
    myGlobal->Set(String::New("startIntervalThread"), FunctionTemplate::New(VirtualMachine_startIntervalThread));
84
    myGlobal->Set(String::New("startIntervalThread"), FunctionTemplate::New(VirtualMachine_startIntervalThread));
85
    myGlobal->Set(String::New("stopIntervalThread"), FunctionTemplate::New(VirtualMachine_stopIntervalThread));
85
    myGlobal->Set(String::New("stopIntervalThread"), FunctionTemplate::New(VirtualMachine_stopIntervalThread));
-
 
86
    myGlobal->Set(String::New("startSocketThread"), FunctionTemplate::New(VirtualMachine_startSocketThread));
-
 
87
    myGlobal->Set(String::New("stopSocketThread"), FunctionTemplate::New(VirtualMachine_stopSocketThread));
-
 
88
    myGlobal->Set(String::New("sendToSocketThread"), FunctionTemplate::New(VirtualMachine_sendToSocketThread));
86
 
89
 
87
    //create context for the script
90
    //create context for the script
88
    myContext = Context::New(NULL, myGlobal);
91
    myContext = Context::New(NULL, myGlobal);
89
 
92
 
90
 
93
 
Line 99... Line 102...
99
    if (script.IsEmpty())
102
    if (script.IsEmpty())
100
    {
103
    {
101
 
104
 
102
        // Print errors that happened during compilation.
105
        // Print errors that happened during compilation.
103
        String::AsciiValue error(try_catch.Exception());
106
        String::AsciiValue error(try_catch.Exception());
-
 
107
        string sError = *error;
104
        slog << "Failed to compile script: " << *error << "\n";
108
        slog << "Failed to compile script: " + sError + "\n";
105
    }
109
    }
106
    else
110
    else
107
    {
111
    {
108
        //no errors , let's continue
112
        //no errors , let's continue
109
        Handle<Value> result = script->Run();
113
        Handle<Value> result = script->Run();
Line 111... Line 115...
111
        //check if execution ended with errors
115
        //check if execution ended with errors
112
        if (result.IsEmpty())
116
        if (result.IsEmpty())
113
        {
117
        {
114
            // Print errors that happened during execution.
118
            // Print errors that happened during execution.
115
            String::AsciiValue error(try_catch.Exception());
119
            String::AsciiValue error(try_catch.Exception());
-
 
120
            string sError = *error;
116
            slog << "Constructor: Failed to run script: " << *error << "\n";
121
            slog << "Constructor: Failed to run script: " << sError << "\n";
117
        }
122
        }
118
        else
123
        else
119
        {
124
        {
120
            loadScript("System/Startup.js");
125
            loadScript("System/Startup.js");
121
 
126
 
Line 190... Line 195...
190
    string basePath = Settings::get("BasePath") + "/Services/";
195
    string basePath = Settings::get("BasePath") + "/Services/";
191
    string scriptFileName = basePath + scriptName;
196
    string scriptFileName = basePath + scriptName;
192
 
197
 
193
    if (!file_exists(scriptFileName))
198
    if (!file_exists(scriptFileName))
194
    {
199
    {
195
        slog << "Failed to load :" << scriptFileName << "\n";
200
        slog << "Failed to load :" + scriptFileName + "\n";
196
        return false;
201
        return false;
197
    }
202
    }
198
 
203
 
199
    string scriptSource = file_get_contents(scriptFileName);
204
    string scriptSource = file_get_contents(scriptFileName);
200
 
205
 
Line 212... Line 217...
212
    //check if we got problems on compilation
217
    //check if we got problems on compilation
213
    if (script.IsEmpty())
218
    if (script.IsEmpty())
214
    {
219
    {
215
        // Print errors that happened during compilation.
220
        // Print errors that happened during compilation.
216
        String::AsciiValue error(try_catch.Exception());
221
        String::AsciiValue error(try_catch.Exception());
-
 
222
        string sError = *error;
217
        slog << "Failed to compile script: " << *error << "\n";
223
        slog << "loadScript: Failed to compile script: " + sError + "\n";
218
        return false;
224
        return false;
219
    }
225
    }
220
    else
226
    else
221
    {
227
    {
222
        //no errors , let's continue
228
        //no errors , let's continue
Line 225... Line 231...
225
        //check if execution ended with errors
231
        //check if execution ended with errors
226
        if (result.IsEmpty())
232
        if (result.IsEmpty())
227
        {
233
        {
228
            // Print errors that happened during execution.
234
            // Print errors that happened during execution.
229
            String::AsciiValue error(try_catch.Exception());
235
            String::AsciiValue error(try_catch.Exception());
-
 
236
            string sError = *error;
230
            slog << "loadScript: Failed to run script: " << *error << "\n";
237
            slog << "loadScript: Failed to run script: " + sError + "\n";
231
            return false;
238
            return false;
232
        }
239
        }
233
    }
240
    }
234
 
241
 
235
    return true;
242
    return true;
Line 273... Line 280...
273
        myIntervalThreads.erase(id);
280
        myIntervalThreads.erase(id);
274
        return true;
281
        return true;
275
    }
282
    }
276
 
283
 
277
    return false;
284
    return false;
-
 
285
}
-
 
286
 
-
 
287
unsigned int VirtualMachine::startSocketThread(string address, int port, unsigned int reconnectTimeout)
-
 
288
{
-
 
289
    SocketThread socketThread(address, port, reconnectTimeout);
-
 
290
    mySocketThreads[socketThread.getId()] = socketThread;
-
 
291
    mySocketThreads[socketThread.getId()].start();
-
 
292
 
-
 
293
    return socketThread.getId();
-
 
294
}
-
 
295
 
-
 
296
bool VirtualMachine::stopSocketThread(unsigned int id)
-
 
297
{
-
 
298
    if (mySocketThreads.find(id) != mySocketThreads.end())
-
 
299
    {
-
 
300
        mySocketThreads.erase(id);
-
 
301
        return true;
-
 
302
    }
-
 
303
 
-
 
304
    return false;
-
 
305
}
-
 
306
 
-
 
307
void VirtualMachine::sendToSocketThread(unsigned int id, string data)
-
 
308
{
-
 
309
    if (mySocketThreads.find(id) != mySocketThreads.end())
-
 
310
    {
-
 
311
        mySocketThreads[id].send(data);
-
 
312
    }
278
}
313
}
279
 
314
 
280
bool VirtualMachine::runExpression(string expression)
315
bool VirtualMachine::runExpression(string expression)
281
{
316
{
282
    SyslogStream &slog = SyslogStream::getInstance();
317
    SyslogStream &slog = SyslogStream::getInstance();
Line 297... Line 332...
297
    //check if we got problems on compilation
332
    //check if we got problems on compilation
298
    if (script.IsEmpty())
333
    if (script.IsEmpty())
299
    {
334
    {
300
        // Print errors that happened during compilation.
335
        // Print errors that happened during compilation.
301
        String::AsciiValue error(try_catch.Exception());
336
        String::AsciiValue error(try_catch.Exception());
-
 
337
        string sError = *error;
302
        slog << "Failed to compile script: " << *error << "\n";
338
        slog << "runExpression: Failed to compile script: " + sError + "\n";
303
       
339
        slog << expression << "\n\n";
304
        return false;
340
        return false;
305
    }
341
    }
306
    else
342
    else
307
    {
343
    {
308
        //no errors , let's continue
344
        //no errors , let's continue
309
        Handle<Value> result = script->Run();
345
        Handle<Value> result = script->Run();
310
 
346
 
311
        //check if execution ended with errors
347
        //check if execution ended with errors
312
        if (result.IsEmpty())
348
        if (result.IsEmpty())
313
        {
349
        {
314
            // Print errors that happened during execution.
350
            // Print errors that happened during execution.
315
            String::AsciiValue error(try_catch.Exception());
351
            String::AsciiValue error(try_catch.Exception());
-
 
352
            string sError = *error;
316
            slog << "runExpression: Failed to run script: " << *error << "\n";
353
            slog << "runExpression: Failed to run script: " + sError + "\n";
317
            return false;
354
            return false;
318
        }
355
        }
319
    }
356
    }
320
 
357
 
321
    return true;
358
    return true;
Line 332... Line 369...
332
    return Undefined();
369
    return Undefined();
333
}
370
}
334
 
371
 
335
Handle<Value> VirtualMachine_sendCanMessage(const Arguments& args)
372
Handle<Value> VirtualMachine_sendCanMessage(const Arguments& args)
336
{
373
{
337
    SyslogStream &slog = SyslogStream::getInstance();
374
    //SyslogStream &slog = SyslogStream::getInstance();
338
    CanNetManager &canMan = CanNetManager::getInstance();
375
    CanNetManager &canMan = CanNetManager::getInstance();
339
 
376
 
340
    CanMessage canMessage;
377
    CanMessage canMessage;
341
 
378
 
342
    String::AsciiValue className(args[0]);
379
    String::AsciiValue className(args[0]);
Line 364... Line 401...
364
    }
401
    }
365
 
402
 
366
    canMessage.setData(data);
403
    canMessage.setData(data);
367
   
404
   
368
    canMan.sendMessage(canMessage);
405
    canMan.sendMessage(canMessage);
369
 
406
 
370
    return Undefined();
407
    return Undefined();
371
}
408
}
372
 
409
 
373
Handle<Value> VirtualMachine_loadScript(const Arguments& args)
410
Handle<Value> VirtualMachine_loadScript(const Arguments& args)
374
{
411
{
375
    VirtualMachine &vm = VirtualMachine::getInstance();
412
    VirtualMachine &vm = VirtualMachine::getInstance();
376
 
413
 
377
    String::AsciiValue str(args[0]);
414
    String::AsciiValue str(args[0]);
378
 
415
 
379
    return Boolean::New(vm.loadScript(*str));
416
    return Boolean::New(vm.loadScript(*str));
380
}
417
}
381
 
418
 
382
Handle<Value> VirtualMachine_startIntervalThread(const Arguments& args)
419
Handle<Value> VirtualMachine_startIntervalThread(const Arguments& args)
383
{
420
{
384
    VirtualMachine &vm = VirtualMachine::getInstance();
421
    VirtualMachine &vm = VirtualMachine::getInstance();
385
 
422
 
386
    unsigned int timeout = args[0]->Uint32Value();
423
    unsigned int timeout = args[0]->Uint32Value();
387
 
424
 
388
    return Integer::New(vm.startIntervalThread(timeout));
425
    return Integer::New(vm.startIntervalThread(timeout));
389
}
426
}
390
 
427
 
391
Handle<Value> VirtualMachine_stopIntervalThread(const Arguments& args)
428
Handle<Value> VirtualMachine_stopIntervalThread(const Arguments& args)
392
{
429
{
393
    VirtualMachine &vm = VirtualMachine::getInstance();
430
    VirtualMachine &vm = VirtualMachine::getInstance();
394
 
431
 
395
    unsigned int id = args[0]->Uint32Value();
432
    unsigned int id = args[0]->Uint32Value();
396
 
433
 
397
    return Integer::New(vm.stopIntervalThread(id));
434
    return Boolean::New(vm.stopIntervalThread(id));
-
 
435
}
-
 
436
 
-
 
437
Handle<Value> VirtualMachine_startSocketThread(const Arguments& args)
-
 
438
{
-
 
439
    VirtualMachine &vm = VirtualMachine::getInstance();
-
 
440
 
-
 
441
    String::AsciiValue address(args[0]);
-
 
442
    int port = args[1]->Uint32Value();
-
 
443
    unsigned int reconnectTimeout = args[2]->Uint32Value();
-
 
444
 
-
 
445
    return Integer::New(vm.startSocketThread(*address, port, reconnectTimeout));
-
 
446
}
-
 
447
 
-
 
448
Handle<Value> VirtualMachine_stopSocketThread(const Arguments& args)
-
 
449
{
-
 
450
    VirtualMachine &vm = VirtualMachine::getInstance();
-
 
451
 
-
 
452
    unsigned int id = args[0]->Uint32Value();
-
 
453
 
-
 
454
    return Boolean::New(vm.stopSocketThread(id));
-
 
455
}
-
 
456
 
-
 
457
Handle<Value> VirtualMachine_sendToSocketThread(const Arguments& args)
-
 
458
{
-
 
459
    VirtualMachine &vm = VirtualMachine::getInstance();
-
 
460
 
-
 
461
    unsigned int id = args[0]->Uint32Value();
-
 
462
    String::AsciiValue data(args[1]);
-
 
463
 
-
 
464
    vm.sendToSocketThread(id, *data);
-
 
465
 
-
 
466
    return Undefined();
398
}
467
}
-
 
468