Changeset 15043


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.
Location:
trunk/FACT++/src
Files:
2 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();
  • trunk/FACT++/src/InterpreterV8.h

    r14983 r15043  
    1313
    1414#include "State.h"
     15#include "Service.h"
    1516#include "Description.h"
    1617#include "EventImp.h"
     
    7879    v8::Handle<v8::Value> FuncGetState(const v8::Arguments& args);
    7980    v8::Handle<v8::Value> FuncGetStates(const v8::Arguments& args);
     81    v8::Handle<v8::Value> FuncGetService(const v8::Arguments& args);
     82    v8::Handle<v8::Value> FuncGetServices(const v8::Arguments& args);
    8083    v8::Handle<v8::Value> FuncNewState(const v8::Arguments& args);
    8184    //v8::Handle<v8::Value> FuncOpen(const v8::Arguments& args);
     
    129132    static v8::Handle<v8::Value> WrapGetState(const v8::Arguments &args) { if (This) return This->FuncGetState(args); else return v8::Undefined(); }
    130133    static v8::Handle<v8::Value> WrapGetStates(const v8::Arguments &args){ if (This) return This->FuncGetStates(args);else return v8::Undefined(); }
     134    static v8::Handle<v8::Value> WrapGetService(const v8::Arguments &args){ if (This) return This->FuncGetService(args);else return v8::Undefined(); }
     135    static v8::Handle<v8::Value> WrapGetServices(const v8::Arguments &args){ if (This) return This->FuncGetServices(args);else return v8::Undefined(); }
    131136    //static v8::Handle<v8::Value> WrapOpen(const v8::Arguments &args)     { if (This) return This->FuncOpen(args);     else return v8::Undefined(); }
    132137    static v8::Handle<v8::Value> WrapSubscription(const v8::Arguments &args){ if (This) return This->FuncSubscription(args);else return v8::Undefined(); }
     
    185190    virtual State JsGetCurrentState() const { return State(); }
    186191    virtual std::vector<State> JsGetStates(const std::string &) { return std::vector<State>(); }
     192    virtual std::set<Service> JsGetServices() { return std::set<Service>(); }
     193    virtual std::vector<Description> JsGetDescription(const std::string &) { return std::vector<Description>(); }
    187194
    188195    virtual std::vector<Description> JsDescription(const std::string &) { return std::vector<Description>(); };
Note: See TracChangeset for help on using the changeset viewer.