summaryrefslogtreecommitdiff
path: root/test/html-based-panel/node-cpp-lbs-modules/FuelStopAdvisorWrapper.cpp
blob: 3a8fa80a12f23761a4465bf28db3acc3b7b662ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* @licence app begin@
* SPDX-License-Identifier: MPL-2.0
*
* \copyright Copyright (C) 2016, PCA Peugeot Citroen
*
* \file FuelStopAdvisorWrapper.cpp
*
* \brief This file is part of the Navigation Web API proof of concept.
*
* \author Philippe Colliot <philippe.colliot@mpsa.com>
*
* \version 0.1
*
* This Source Code Form is subject to the terms of the
* Mozilla Public License (MPL), v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* For further information see http://www.genivi.org/.
*
* List of changes:
* <date>, <name>, <description of change>
*
* @licence end@
*/
#include <node.h>

#include "FuelStopAdvisorWrapper.hpp"

using namespace std;


v8::Persistent<v8::Function> FuelStopAdvisorWrapper::constructor;

v8::Persistent<v8::Function> FuelStopAdvisorWrapper::signalTripDataUpdated;

void FuelStopAdvisorWrapper::TripDataUpdated(const uint8_t& number)
{
    v8::Isolate* isolate = v8::Isolate::GetCurrent();
    v8::HandleScope handleScope(isolate);
    const unsigned argc = 1;
    v8::Local<v8::Value> argv[argc];

    argv[0]=v8::Local<v8::Value>::New(isolate,v8::Integer::New(isolate,number));

    v8::Local<v8::Function> fct = v8::Local<v8::Function>::New(isolate,signalTripDataUpdated);
    fct->Call(isolate->GetCurrentContext()->Global(), argc, argv);
}

void FuelStopAdvisorWrapper::SetTripDataUpdatedListener(const v8::FunctionCallbackInfo<v8::Value> &args)
{
    v8::Isolate* isolate = args.GetIsolate();

    if (!args[0]->IsFunction()) {
        isolate->ThrowException(
        v8::Exception::TypeError(v8::String::NewFromUtf8(isolate,"Requires a function as parameter"))
        );
    }
    v8::Local<v8::Function> fct = v8::Local<v8::Function>::Cast(args[0]);
    v8::Persistent<v8::Function> persfct(isolate,fct);
    signalTripDataUpdated.Reset(isolate,persfct);;

    v8::Local<v8::Object> ret = v8::Object::New(isolate);
    ret->Set( 0, v8::Boolean::New(isolate, v8::True) );

    args.GetReturnValue().Set(ret);
}

v8::Persistent<v8::Function> FuelStopAdvisorWrapper::signalFuelStopAdvisorWarning;

void FuelStopAdvisorWrapper::FuelStopAdvisorWarning(const bool &destinationCantBeReached)
{
    v8::Isolate* isolate = v8::Isolate::GetCurrent();
    v8::HandleScope handleScope(isolate);
    const unsigned argc = 1;
    v8::Local<v8::Value> argv[argc];

    argv[0]=v8::Local<v8::Value>::New(isolate,v8::Boolean::New(isolate,destinationCantBeReached));

    v8::Local<v8::Function> fct= v8::Local<v8::Function>::New(isolate,signalFuelStopAdvisorWarning);
    fct->Call(isolate->GetCurrentContext()->Global(), argc, argv);
}

void FuelStopAdvisorWrapper::SetFuelStopAdvisorWarningListener(const v8::FunctionCallbackInfo<v8::Value> &args)
{
    v8::Isolate* isolate = args.GetIsolate();

    if (!args[0]->IsFunction()) {
        isolate->ThrowException(
        v8::Exception::TypeError(v8::String::NewFromUtf8(isolate,"Requires a function as parameter"))
        );
    }
    v8::Local<v8::Function> fct = v8::Local<v8::Function>::Cast(args[0]);
    v8::Persistent<v8::Function> persfct(isolate,fct);
    signalFuelStopAdvisorWarning.Reset(isolate,persfct);;

    v8::Local<v8::Object> ret = v8::Object::New(isolate);
    ret->Set( 0, v8::Boolean::New(isolate, v8::True) );

    args.GetReturnValue().Set(ret);
}

v8::Persistent<v8::Function> FuelStopAdvisorWrapper::signalTripDataResetted;

void FuelStopAdvisorWrapper::TripDataResetted(const uint8_t& number)
{
    v8::Isolate* isolate = v8::Isolate::GetCurrent();
    v8::HandleScope handleScope(isolate);
    const unsigned argc = 1;
    v8::Local<v8::Value> argv[argc];

    argv[0]=v8::Local<v8::Value>::New(isolate,v8::Integer::New(isolate,number));

    v8::Local<v8::Function> fct= v8::Local<v8::Function>::New(isolate,signalTripDataResetted);
    fct->Call(isolate->GetCurrentContext()->Global(), argc, argv);
}

void FuelStopAdvisorWrapper::SetTripDataResettedListener(const v8::FunctionCallbackInfo<v8::Value> &args)
{
    v8::Isolate* isolate = args.GetIsolate();

    if (!args[0]->IsFunction()) {
        isolate->ThrowException(
        v8::Exception::TypeError(v8::String::NewFromUtf8(isolate,"Requires a function as parameter"))
        );
    }

    v8::Local<v8::Function> fct = v8::Local<v8::Function>::Cast(args[0]);
    v8::Persistent<v8::Function> persfct(isolate,fct);
    signalTripDataResetted.Reset(isolate,persfct);;

    v8::Local<v8::Object> ret = v8::Object::New(isolate);
    ret->Set( 0, v8::Boolean::New(isolate, v8::True) );

    args.GetReturnValue().Set(ret);
}

void FuelStopAdvisorWrapper::Init(v8::Local<v8::Object> target) {
    v8::Isolate* isolate = target->GetIsolate();

    // Prepare constructor template
    v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(isolate, New);
    tpl->SetClassName(v8::String::NewFromUtf8(isolate, "FuelStopAdvisorWrapper"));
    tpl->InstanceTemplate()->SetInternalFieldCount(1);

    // Add all prototype methods, getters and setters here.
    NODE_SET_PROTOTYPE_METHOD(tpl, "getVersion", GetVersion);
    NODE_SET_PROTOTYPE_METHOD(tpl, "getInstantData", GetInstantData);
    NODE_SET_PROTOTYPE_METHOD(tpl, "setTripDataUpdatedListener", SetTripDataUpdatedListener);
    NODE_SET_PROTOTYPE_METHOD(tpl, "setFuelStopAdvisorWarningListener", SetFuelStopAdvisorWarningListener);
    NODE_SET_PROTOTYPE_METHOD(tpl, "setTripDataResettedListener", SetTripDataResettedListener);
    NODE_SET_PROTOTYPE_METHOD(tpl, "getSpeed", GetSpeed);
    NODE_SET_PROTOTYPE_METHOD(tpl, "getLevel", GetLevel);
    NODE_SET_PROTOTYPE_METHOD(tpl, "getInstantConsumption", GetInstantConsumption);
    NODE_SET_PROTOTYPE_METHOD(tpl, "getOdometer", GetOdometer);

    // This has to be last, otherwise the properties won't show up on the
    // object in JavaScript.
    constructor.Reset(isolate, tpl->GetFunction());
    target->Set(v8::String::NewFromUtf8(isolate, "FuelStopAdvisorWrapper"),
                 tpl->GetFunction());
}

FuelStopAdvisorWrapper::FuelStopAdvisorWrapper() {
}

FuelStopAdvisorWrapper::~FuelStopAdvisorWrapper() {
}

void FuelStopAdvisorWrapper::New(const v8::FunctionCallbackInfo<v8::Value> &args) {
    v8::Isolate* isolate = args.GetIsolate();

    if (args.IsConstructCall()) {
      // Invoked as constructor: `new MyObject(...)`
//      double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue(); //no parameters
        FuelStopAdvisorWrapper* obj = new FuelStopAdvisorWrapper();
        obj->Wrap(args.This());
        args.GetReturnValue().Set(args.This());
        DemonstratorProxy* proxy = new DemonstratorProxy(obj);
        obj->mp_demonstratorProxy = proxy;
    } else { // not tested yet
      // Invoked as plain function `MyObject(...)`, turn into construct call.
      const int argc = 1;
      v8::Local<v8::Value> argv[argc] = { args[0] };
      v8::Local<v8::Context> context = isolate->GetCurrentContext();
      v8::Local<v8::Function> cons = v8::Local<v8::Function>::New(isolate, constructor);
      v8::Local<v8::Object> result = cons->NewInstance(context, argc, argv).ToLocalChecked();
      args.GetReturnValue().Set(result);
    }
}

void FuelStopAdvisorWrapper::NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();

  const unsigned argc = 1;
  v8::Local<v8::Value> argv[argc] = { args[0] };
  v8::Local<v8::Function> cons = v8::Local<v8::Function>::New(isolate, constructor);
  v8::Local<v8::Context> context = isolate->GetCurrentContext();
  v8::Local<v8::Object> instance =
      cons->NewInstance(context, argc, argv).ToLocalChecked();

  args.GetReturnValue().Set(instance);
}

void FuelStopAdvisorWrapper::GetVersion(const v8::FunctionCallbackInfo<v8::Value> &args) {
    v8::Isolate* isolate = args.GetIsolate();

    // Retrieves the pointer to the wrapped object instance.
    FuelStopAdvisorWrapper* obj = ObjectWrap::Unwrap<FuelStopAdvisorWrapper>(args.This());

    ::DBus::Struct< uint16_t, uint16_t, uint16_t, std::string > DBus_version = obj->mp_demonstratorProxy->mp_fuelStopAdvisorProxy->GetVersion();

    v8::Local<v8::Object> ret = v8::Object::New(isolate);
    ret->Set( 0, v8::Int32::New(isolate,DBus_version._1) );
    ret->Set( 1, v8::Int32::New(isolate,DBus_version._2) );
    ret->Set( 2, v8::Int32::New(isolate,DBus_version._3) );
    ret->Set( 3, v8::String::NewFromUtf8(isolate,DBus_version._4.c_str()) );

    args.GetReturnValue().Set(ret);
}

void FuelStopAdvisorWrapper::GetInstantData(const v8::FunctionCallbackInfo<v8::Value> &args) {
    v8::Isolate* isolate = args.GetIsolate();

    // Retrieves the pointer to the wrapped object instance.
    FuelStopAdvisorWrapper* obj = ObjectWrap::Unwrap<FuelStopAdvisorWrapper>(args.This());

    std::map< uint16_t, ::DBus::Variant > instant_data = obj->mp_demonstratorProxy->mp_fuelStopAdvisorProxy->GetInstantData();


    v8::Local<v8::Array> ret = v8::Array::New(isolate);

    for (std::map< uint16_t, ::DBus::Variant >::iterator iter = instant_data.begin(); iter != instant_data.end(); iter++) {
        v8::Local<v8::Object> data = v8::Object::New(isolate);
        ::DBus::Variant value = iter->second;
        printf("GetInstantData%d\n",iter->first);
        printf("GetInstantData%s\n",value.signature().c_str());
        data->Set(v8::String::NewFromUtf8(isolate,"key"), v8::Uint32::New(isolate,iter->first));
        switch (iter->first) {
        case GENIVI_FUELSTOPADVISOR_FUEL_LEVEL:
            data->Set(v8::String::NewFromUtf8(isolate,"value"), v8::Int32::New(isolate,15));
            break;
        case GENIVI_FUELSTOPADVISOR_INSTANT_FUEL_CONSUMPTION_PER_DISTANCE:
            data->Set(v8::String::NewFromUtf8(isolate,"value"), v8::Int32::New(isolate,55));
            break;
        case GENIVI_FUELSTOPADVISOR_TANK_DISTANCE:
            data->Set(v8::String::NewFromUtf8(isolate,"value"), v8::Int32::New(isolate,300));
            break;
        case GENIVI_FUELSTOPADVISOR_ENHANCED_TANK_DISTANCE:
            data->Set(v8::String::NewFromUtf8(isolate,"value"), v8::Int32::New(isolate,400));
            break;
        default:
            break;
        }
        ret->Set(ret->Length(), data);
    }

    args.GetReturnValue().Set(ret);
}

void FuelStopAdvisorWrapper::GetSpeed(const v8::FunctionCallbackInfo<v8::Value> &args) {
    v8::Isolate* isolate = args.GetIsolate();

    // Retrieves the pointer to the wrapped object instance.
    FuelStopAdvisorWrapper* obj = ObjectWrap::Unwrap<FuelStopAdvisorWrapper>(args.This());

    v8::Local<v8::Object> ret = v8::Object::New(isolate);

    args.GetReturnValue().Set(ret);
}

void FuelStopAdvisorWrapper::GetLevel(const v8::FunctionCallbackInfo<v8::Value> &args) {
    v8::Isolate* isolate = args.GetIsolate();

    // Retrieves the pointer to the wrapped object instance.
    FuelStopAdvisorWrapper* obj = ObjectWrap::Unwrap<FuelStopAdvisorWrapper>(args.This());

    DBus::Variant variant = obj->mp_demonstratorProxy->mp_managerProxy->GetLevel();
    DBus::MessageIter it = variant.reader();
    uint16_t level;
    it >> level;

    v8::Local<v8::Object> ret = v8::Object::New(isolate);
    ret->Set( 0, v8::Uint32::New(isolate, level) );

    args.GetReturnValue().Set(ret);
}

void FuelStopAdvisorWrapper::GetInstantConsumption(const v8::FunctionCallbackInfo<v8::Value> &args) {
    v8::Isolate* isolate = args.GetIsolate();

    // Retrieves the pointer to the wrapped object instance.
    FuelStopAdvisorWrapper* obj = ObjectWrap::Unwrap<FuelStopAdvisorWrapper>(args.This());

    DBus::Variant variant = obj->mp_demonstratorProxy->mp_managerProxy->GetInstantConsumption();
    DBus::MessageIter it = variant.reader();
    uint32_t consumption;
    it >> consumption;

    v8::Local<v8::Object> ret = v8::Object::New(isolate);
    ret->Set( 0, v8::Uint32::New(isolate,consumption) );

    args.GetReturnValue().Set(ret);
}

void FuelStopAdvisorWrapper::GetOdometer(const v8::FunctionCallbackInfo<v8::Value> &args) {
    v8::Isolate* isolate = args.GetIsolate();

    // Retrieves the pointer to the wrapped object instance.
    FuelStopAdvisorWrapper* obj = ObjectWrap::Unwrap<FuelStopAdvisorWrapper>(args.This());

    v8::Local<v8::Object> ret = v8::Object::New(isolate);

    args.GetReturnValue().Set(ret);
}



void RegisterModule(v8::Handle<v8::Object> target) {
    FuelStopAdvisorWrapper::Init(target);
}

NODE_MODULE(FuelStopAdvisorWrapper, RegisterModule)