Subversion Repositories HomeAutomation

Rev

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

Rev 999 Rev 1007
Line 85... Line 85...
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));
86
    myGlobal->Set(String::New("startSocketThread"), FunctionTemplate::New(VirtualMachine::_startSocketThread));
87
    myGlobal->Set(String::New("stopSocketThread"), FunctionTemplate::New(VirtualMachine::_stopSocketThread));
87
    myGlobal->Set(String::New("stopSocketThread"), FunctionTemplate::New(VirtualMachine::_stopSocketThread));
88
    myGlobal->Set(String::New("sendToSocketThread"), FunctionTemplate::New(VirtualMachine::_sendToSocketThread));
88
    myGlobal->Set(String::New("sendToSocketThread"), FunctionTemplate::New(VirtualMachine::_sendToSocketThread));
89
    myGlobal->Set(String::New("uint2hex"), FunctionTemplate::New(VirtualMachine::_uint2hex));
89
    myGlobal->Set(String::New("uint2hex"), FunctionTemplate::New(VirtualMachine::_uint2hex));
-
 
90
    myGlobal->Set(String::New("loadDataStore"), FunctionTemplate::New(VirtualMachine::_loadDataStore));
90
    myContext = Context::New(NULL, myGlobal);
91
    myContext = Context::New(NULL, myGlobal);
91
 
92
 
92
 
93
 
93
    if (loadScript("System/Base.js"))
94
    if (loadScript("System/Base.js"))
94
    {
95
    {
Line 176... Line 177...
176
 
177
 
177
        result += underline + "\n";
178
        result += underline + "\n";
178
    }
179
    }
179
 
180
 
180
    return result;
181
    return result;
-
 
182
}
-
 
183
 
-
 
184
bool VirtualMachine::loadDataStore(string storeName)
-
 
185
{
-
 
186
    SyslogStream &slog = SyslogStream::getInstance();
-
 
187
 
-
 
188
    string basePath = Settings::get("BasePath") + "Services/DataStores/";
-
 
189
    string scriptFileName = basePath + storeName + ".js";
-
 
190
 
-
 
191
    if (!file_exists(scriptFileName))
-
 
192
    {
-
 
193
        slog << "Failed to load datastore " + scriptFileName + ", file does not exist.\n";
-
 
194
        return false;
-
 
195
    }
-
 
196
 
-
 
197
    string scriptSource = file_get_contents(scriptFileName);
-
 
198
 
-
 
199
    scriptSource = "DataStore.addStore('" + storeName + "', " + str_replace("\n", "", scriptSource) + ");";
-
 
200
 
-
 
201
    Handle<String> source =  String::New(scriptSource.c_str(), scriptSource.length());
-
 
202
    Handle<String> name = String::New(scriptFileName.c_str(), scriptFileName.length());
-
 
203
 
-
 
204
    Context::Scope contextScope(myContext);
-
 
205
 
-
 
206
    TryCatch tryCatch;
-
 
207
 
-
 
208
    Handle<Script> script = Script::Compile(source, name);
-
 
209
 
-
 
210
    if (script.IsEmpty())
-
 
211
    {
-
 
212
        slog << formatException(&tryCatch);
-
 
213
        return false;
-
 
214
    }
-
 
215
    else
-
 
216
    {
-
 
217
        Handle<Value> result = script->Run();
-
 
218
 
-
 
219
        if (result.IsEmpty())
-
 
220
        {
-
 
221
            slog << formatException(&tryCatch);
-
 
222
            return false;
-
 
223
        }
-
 
224
 
-
 
225
        slog << "Loaded datastore " + storeName + "\n";
-
 
226
    }
-
 
227
 
-
 
228
    return true;
181
}
229
}
182
 
230
 
183
bool VirtualMachine::loadScript(string scriptName)
231
bool VirtualMachine::loadScript(string scriptName)
184
{
232
{
185
    SyslogStream &slog = SyslogStream::getInstance();
233
    SyslogStream &slog = SyslogStream::getInstance();
Line 255... Line 303...
255
 
303
 
256
    return socketThread->getId();
304
    return socketThread->getId();
257
}
305
}
258
 
306
 
259
bool VirtualMachine::stopSocketThread(unsigned int id)
307
bool VirtualMachine::stopSocketThread(unsigned int id)
260
{
308
{
261
    if (mySocketThreads.find(id) != mySocketThreads.end())
309
    if (mySocketThreads.find(id) != mySocketThreads.end())
262
    {
310
    {
263
        delete mySocketThreads[id];
311
        delete mySocketThreads[id];
264
        mySocketThreads.erase(id);
312
        mySocketThreads.erase(id);
265
        return true;
313
        return true;
266
    }
314
    }
267
 
315
 
268
    return false;
316
    return false;
269
}
317
}
270
 
318
 
271
void VirtualMachine::sendToSocketThread(unsigned int id, string data)
319
void VirtualMachine::sendToSocketThread(unsigned int id, string data)
272
{
320
{
273
    if (mySocketThreads.find(id) != mySocketThreads.end())
321
    if (mySocketThreads.find(id) != mySocketThreads.end())
274
    {
322
    {
275
        mySocketThreads[id]->send(data);
323
        mySocketThreads[id]->send(data);
276
    }
324
    }
277
}
325
}
278
 
326
 
279
void VirtualMachine::disconnectCommandClient(unsigned int id)
327
void VirtualMachine::disconnectCommandClient(unsigned int id)
280
{
328
{
281
    myCommandThread.disconnectClient(id);
329
    myCommandThread.disconnectClient(id);
282
}
330
}
283
 
331
 
284
void VirtualMachine::sendToCommandClient(unsigned int id, string data)
332
void VirtualMachine::sendToCommandClient(unsigned int id, string data)
285
{
333
{
286
    myCommandThread.sendTo(id, data);
334
    myCommandThread.sendTo(id, data);
287
}
335
}
288
 
336
 
289
void VirtualMachine::print(unsigned int id, string data)
337
void VirtualMachine::print(unsigned int id, string data)
290
{
338
{
291
    if (id == 0)
339
    if (id == 0)
292
    {
340
    {
293
        SyslogStream &slog = SyslogStream::getInstance();
341
        SyslogStream &slog = SyslogStream::getInstance();
294
        slog << data;
342
        slog << data;
295
    }
343
    }
296
    else
344
    else
297
    {
345
    {
298
        VirtualMachine &vm = VirtualMachine::getInstance();
346
        VirtualMachine &vm = VirtualMachine::getInstance();
299
        vm.sendToCommandClient(id, data);
347
        vm.sendToCommandClient(id, data);
300
    }
348
    }
301
}
349
}
302
 
350
 
303
bool VirtualMachine::runExpression(Expression expression)
351
bool VirtualMachine::runExpression(Expression expression)
304
{
352
{
305
    string scriptName = "runExpression";
353
    string scriptName = "runExpression";
306
 
354
 
307
    string scriptData = expression.getScript();
355
    string scriptData = expression.getScript();
Line 312... Line 360...
312
    Handle<String> name = String::New(scriptName.c_str(), scriptName.length());
360
    Handle<String> name = String::New(scriptName.c_str(), scriptName.length());
313
 
361
 
314
    Context::Scope contextScope(myContext);
362
    Context::Scope contextScope(myContext);
315
 
363
 
316
    TryCatch tryCatch;
364
    TryCatch tryCatch;
317
 
365
 
318
    Handle<Script> script = Script::Compile(source, name);
366
    Handle<Script> script = Script::Compile(source, name);
319
 
367
 
320
    if (script.IsEmpty())
368
    if (script.IsEmpty())
321
    {
369
    {
322
        print(expression.getTargetId(), formatException(&tryCatch));
370
        print(expression.getTargetId(), formatException(&tryCatch));
323
        return false;
371
        return false;
324
    }
372
    }
325
    else
373
    else
326
    {
374
    {
327
        Handle<Value> result = script->Run();
375
        Handle<Value> result = script->Run();
Line 341... Line 389...
341
                print(expression.getTargetId(), resultString + "\n");
389
                print(expression.getTargetId(), resultString + "\n");
342
             }
390
             }
343
        }
391
        }
344
    }
392
    }
345
 
393
 
346
    return true;
394
    return true;
347
}
395
}
348
 
396
 
349
 
397
 
350
Handle<Value> VirtualMachine::_quit(const Arguments& args)
398
Handle<Value> VirtualMachine::_quit(const Arguments& args)
351
{
399
{
352
    VirtualMachine &vm = VirtualMachine::getInstance();
400
    VirtualMachine &vm = VirtualMachine::getInstance();
353
 
401
 
354
    vm.disconnectCommandClient(args[0]->Uint32Value());
402
    vm.disconnectCommandClient(args[0]->Uint32Value());
355
 
403
 
356
    return Undefined();
404
    return Undefined();
357
}
405
}
358
 
406
 
359
Handle<Value> VirtualMachine::_log(const Arguments& args)
407
Handle<Value> VirtualMachine::_log(const Arguments& args)
360
{
408
{
361
    SyslogStream &slog = SyslogStream::getInstance();
409
    SyslogStream &slog = SyslogStream::getInstance();
362
 
410
 
363
    String::AsciiValue str(args[0]);
411
    String::AsciiValue str(args[0]);
364
 
412
 
365
    slog << *str;
413
    slog << *str;
366
 
414
 
367
    return Undefined();
415
    return Undefined();
368
}
416
}
369
 
417
 
370
Handle<Value> VirtualMachine::_print(const Arguments& args)
418
Handle<Value> VirtualMachine::_print(const Arguments& args)
371
{
419
{
372
    VirtualMachine &vm = VirtualMachine::getInstance();
420
    VirtualMachine &vm = VirtualMachine::getInstance();
373
 
421
 
374
    String::AsciiValue str(args[1]);
422
    String::AsciiValue str(args[1]);
375
 
423
 
376
    vm.print(args[0]->Uint32Value(), *str);
424
    vm.print(args[0]->Uint32Value(), *str);
377
 
425
 
378
    return Undefined();
426
    return Undefined();
379
}
427
}
380
 
428
 
381
Handle<Value> VirtualMachine::_sendCanMessage(const Arguments& args)
429
Handle<Value> VirtualMachine::_sendCanMessage(const Arguments& args)
382
{
430
{
383
    CanNetManager &canMan = CanNetManager::getInstance();
431
    CanNetManager &canMan = CanNetManager::getInstance();
384
 
432
 
385
    CanMessage canMessage;
433
    CanMessage canMessage;
386
 
434
 
387
    String::AsciiValue className(args[0]);
435
    String::AsciiValue className(args[0]);
Line 448... Line 496...
448
    VirtualMachine &vm = VirtualMachine::getInstance();
496
    VirtualMachine &vm = VirtualMachine::getInstance();
449
 
497
 
450
    String::AsciiValue str(args[0]);
498
    String::AsciiValue str(args[0]);
451
 
499
 
452
    return Boolean::New(vm.loadScript(*str));
500
    return Boolean::New(vm.loadScript(*str));
-
 
501
}
-
 
502
 
-
 
503
Handle<Value> VirtualMachine::_loadDataStore(const Arguments& args)
-
 
504
{
-
 
505
    VirtualMachine &vm = VirtualMachine::getInstance();
-
 
506
 
-
 
507
    String::AsciiValue str(args[0]);
-
 
508
 
-
 
509
    return Boolean::New(vm.loadDataStore(*str));
453
}
510
}
454
 
511
 
455
Handle<Value> VirtualMachine::_startIntervalThread(const Arguments& args)
512
Handle<Value> VirtualMachine::_startIntervalThread(const Arguments& args)
456
{
513
{
457
    VirtualMachine &vm = VirtualMachine::getInstance();
514
    VirtualMachine &vm = VirtualMachine::getInstance();
458
    return Integer::New(vm.startIntervalThread(args[0]->Uint32Value()));
515
    return Integer::New(vm.startIntervalThread(args[0]->Uint32Value()));
459
}
516
}
460
 
517
 
461
Handle<Value> VirtualMachine::_stopIntervalThread(const Arguments& args)
518
Handle<Value> VirtualMachine::_stopIntervalThread(const Arguments& args)
462
{
519
{
463
    VirtualMachine &vm = VirtualMachine::getInstance();
520
    VirtualMachine &vm = VirtualMachine::getInstance();
464
    return Boolean::New(vm.stopIntervalThread(args[0]->Uint32Value()));
521
    return Boolean::New(vm.stopIntervalThread(args[0]->Uint32Value()));
465
}
522
}
466
 
523
 
467
Handle<Value> VirtualMachine::_startSocketThread(const Arguments& args)
524
Handle<Value> VirtualMachine::_startSocketThread(const Arguments& args)
468
{
525
{
469
    VirtualMachine &vm = VirtualMachine::getInstance();
526
    VirtualMachine &vm = VirtualMachine::getInstance();
470
 
527
 
471
    String::AsciiValue address(args[0]);
528
    String::AsciiValue address(args[0]);
472
 
529
 
473
    return Integer::New(vm.startSocketThread(*address, args[1]->Uint32Value(), args[2]->Uint32Value()));
530
    return Integer::New(vm.startSocketThread(*address, args[1]->Uint32Value(), args[2]->Uint32Value()));
474
}
531
}
475
 
532
 
476
Handle<Value> VirtualMachine::_stopSocketThread(const Arguments& args)
533
Handle<Value> VirtualMachine::_stopSocketThread(const Arguments& args)
477
{
534
{
478
    VirtualMachine &vm = VirtualMachine::getInstance();
535
    VirtualMachine &vm = VirtualMachine::getInstance();
479
 
536
 
480
    return Boolean::New(vm.stopSocketThread(args[0]->Uint32Value()));
537
    return Boolean::New(vm.stopSocketThread(args[0]->Uint32Value()));
481
}
538
}
482
 
539
 
483
Handle<Value> VirtualMachine::_sendToSocketThread(const Arguments& args)
540
Handle<Value> VirtualMachine::_sendToSocketThread(const Arguments& args)
484
{
541
{
485
    VirtualMachine &vm = VirtualMachine::getInstance();
542
    VirtualMachine &vm = VirtualMachine::getInstance();
486
 
543
 
487
    String::AsciiValue data(args[1]);
544
    String::AsciiValue data(args[1]);
488
 
545
 
489
    vm.sendToSocketThread(args[0]->Uint32Value(), *data);
546
    vm.sendToSocketThread(args[0]->Uint32Value(), *data);
490
 
547
 
491
    return Undefined();
548
    return Undefined();
492
}
549
}
493
 
550
 
494
Handle<Value> VirtualMachine::_uint2hex(const Arguments& args)
551
Handle<Value> VirtualMachine::_uint2hex(const Arguments& args)
495
{
552
{
496
    VirtualMachine &vm = VirtualMachine::getInstance();
553
    VirtualMachine &vm = VirtualMachine::getInstance();
497
 
554
 
498
    string hexId = uint2hex(args[0]->Uint32Value(), args[1]->Uint32Value());
555
    string hexId = uint2hex(args[0]->Uint32Value(), args[1]->Uint32Value());
499
 
556
 
500
    return String::New(hexId.c_str());
557
    return String::New(hexId.c_str());
501
}
558
}
502
 
-