Ignore:
Timestamp:
03/13/13 10:49:50 (12 years ago)
Author:
tbretz
Message:
Added new dim functions to get access to the description of a single service or the list of all available services.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/InterpreterV8.cc

    r15003 r15043  
    442442
    443443    if (args.Length()==1 && !args[0]->IsString())
    444         return ThrowException(String::New("Argument must be an string."));
     444        return ThrowException(String::New("Argument must be a string."));
    445445
    446446    const string server = args.Length()==1 ? *String::AsciiValue(args[0]) : "DIM_CONTROL";
     
    465465
    466466    return handle_scope.Close(list);
     467}
     468
     469Handle<Value> InterpreterV8::FuncGetService(const Arguments& args)
     470{
     471    if (args.Length()!=1)
     472        return ThrowException(String::New("getService must take exactly one argument."));
     473
     474    if (args.Length()==1 && !args[0]->IsString())
     475        return ThrowException(String::New("Argument must be a string."));
     476
     477    const string service = *String::AsciiValue(args[0]);
     478
     479    const vector<Description> descriptions = JsGetDescription(service);
     480    const set<Service> services = JsGetServices();
     481
     482    auto is=services.begin();
     483    for (; is!=services.end(); is++)
     484        if (is->name==service)
     485            break;
     486
     487    HandleScope handle_scope;
     488
     489    Handle<Array> arr = Array::New();
     490    if (arr.IsEmpty())
     491        return Undefined();
     492
     493    auto it=descriptions.begin();
     494    arr->Set(String::New("name"), String::New(it->name.c_str()), ReadOnly);
     495    if (!it->comment.empty())
     496        arr->Set(String::New("description"), String::New(it->comment.c_str()), ReadOnly);
     497    if (is!=services.end())
     498    {
     499        arr->Set(String::New("server"), String::New(is->server.c_str()), ReadOnly);
     500        arr->Set(String::New("service"), String::New(is->service.c_str()), ReadOnly);
     501        arr->Set(String::New("command"), Boolean::New(is->iscmd), ReadOnly);
     502        if (!is->format.empty())
     503            arr->Set(String::New("format"), String::New(is->format.c_str()), ReadOnly);
     504    }
     505
     506    uint32_t i=0;
     507    for (it++; it!=descriptions.end(); it++)
     508    {
     509        Handle<Object> obj = Object::New();
     510        if (obj.IsEmpty())
     511            return Undefined();
     512
     513        obj->Set(String::New("name"), String::New(it->name.c_str()), ReadOnly);
     514        if (!it->comment.empty())
     515            obj->Set(String::New("description"), String::New(it->comment.c_str()), ReadOnly);
     516        if (!it->unit.empty())
     517            obj->Set(String::New("unit"), String::New(it->unit.c_str()), ReadOnly);
     518
     519        arr->Set(i++, obj);
     520    }
     521
     522    return handle_scope.Close(arr);
     523}
     524
     525Handle<Value> InterpreterV8::FuncGetServices(const Arguments& args)
     526{
     527    if (args.Length()>2)
     528        return ThrowException(String::New("getServices must not take more than two argument."));
     529
     530    if (args.Length()>=1 && !args[0]->IsString())
     531        return ThrowException(String::New("First argument must be a string."));
     532
     533    if (args.Length()==2 && !args[1]->IsBoolean())
     534        return ThrowException(String::New("Second argument must be a boolean."));
     535
     536    string arg0 = args.Length() ? *String::AsciiValue(args[0]) : "";
     537    if (arg0=="*")
     538        arg0=="";
     539
     540    const set<Service> services = JsGetServices();
     541
     542    HandleScope handle_scope;
     543
     544    Handle<Array> arr = Array::New();
     545    if (arr.IsEmpty())
     546        return Undefined();
     547
     548    uint32_t i=0;
     549    for (auto is=services.begin(); is!=services.end(); is++)
     550    {
     551        if (!arg0.empty() && is->name.find(arg0)!=0)
     552            continue;
     553
     554        if (args.Length()==2 && args[1]->BooleanValue()!=is->iscmd)
     555            continue;
     556
     557        Handle<Object> obj = Object::New();
     558        if (obj.IsEmpty())
     559            return Undefined();
     560
     561        obj->Set(String::New("name"), String::New(is->name.c_str()), ReadOnly);
     562        obj->Set(String::New("server"), String::New(is->server.c_str()), ReadOnly);
     563        obj->Set(String::New("service"), String::New(is->service.c_str()), ReadOnly);
     564        obj->Set(String::New("command"), Boolean::New(is->iscmd), ReadOnly);
     565        if (!is->format.empty())
     566            obj->Set(String::New("format"), String::New(is->format.c_str()), ReadOnly);
     567
     568        arr->Set(i++, obj);
     569    }
     570
     571    return handle_scope.Close(arr);
    467572}
    468573
     
    20302135    dim->Set(String::New("version"),   Integer::New(DIM_VERSION_NUMBER),     ReadOnly);
    20312136    dim->Set(String::New("getStates"), FunctionTemplate::New(WrapGetStates), ReadOnly);
     2137    dim->Set(String::New("getService"), FunctionTemplate::New(WrapGetService), ReadOnly);
     2138    dim->Set(String::New("getServices"), FunctionTemplate::New(WrapGetServices), ReadOnly);
    20322139
    20332140    Handle<ObjectTemplate> dimctrl = ObjectTemplate::New();
Note: See TracChangeset for help on using the changeset viewer.