From c6feee037bedf52d55830a9d157125119a4dc6cb Mon Sep 17 00:00:00 2001 From: Antti Pihlaja Date: Tue, 2 Jul 2019 18:18:11 +0300 Subject: [PATCH] Fix usage of deprecated v8 apis --- src/cpp/IRSDKWrapper.h | 92 +++++++-------- src/cpp/IrSdkBindingHelpers.cpp | 20 ++-- src/cpp/IrSdkBindingHelpers.h | 14 +-- src/cpp/IrSdkNodeBindings.cpp | 196 +++++++++++++++++--------------- 4 files changed, 169 insertions(+), 153 deletions(-) diff --git a/src/cpp/IRSDKWrapper.h b/src/cpp/IRSDKWrapper.h index 935d0bd..b6a4294 100644 --- a/src/cpp/IRSDKWrapper.h +++ b/src/cpp/IRSDKWrapper.h @@ -9,64 +9,64 @@ #include #include -namespace NodeIrSdk { +namespace NodeIrSdk +{ - class IRSDKWrapper - { - public: - IRSDKWrapper(); - ~IRSDKWrapper(); - - bool startup(); - void shutdown(); - - bool isInitialized() const; - bool isConnected() const; +class IRSDKWrapper +{ +public: + IRSDKWrapper(); + ~IRSDKWrapper(); - bool updateTelemetry(); // returns true if telemetry update available - bool updateSessionInfo(); // returns true if session info update available + bool startup(); + void shutdown(); - const std::string getSessionInfo() const; // returns yaml string + bool isInitialized() const; + bool isConnected() const; - struct TelemetryVar - { - irsdk_varHeader* header; - irsdk_VarType type; + bool updateTelemetry(); // returns true if telemetry update available + bool updateSessionInfo(); // returns true if session info update available - union { // choose correct based on irsdk_VarType - char* value; - float* floatValue; - int* intValue; - bool* boolValue; - double* doubleValue; - }; + const std::string getSessionInfo() const; // returns yaml string - TelemetryVar(irsdk_varHeader* varHeader); - ~TelemetryVar(); + struct TelemetryVar + { + irsdk_varHeader *header; + irsdk_VarType type; + + union { // choose correct based on irsdk_VarType + char *value; + float *floatValue; + int *intValue; + bool *boolValue; + double *doubleValue; }; - const std::vector getVarHeaders() const; + TelemetryVar(irsdk_varHeader *varHeader); + ~TelemetryVar(); + }; - bool getVarVal(TelemetryVar& var) const; + const std::vector getVarHeaders() const; - const double getLastTelemetryUpdateTS() const; // returns JS compatible TS + bool getVarVal(TelemetryVar &var) const; - private: - HANDLE hMemMapFile; - const char *pSharedMem; - const irsdk_header *pHeader; - int lastTickCount; - int lastSessionInfoUpdate; - time_t lastValidTime; - char* data; - int dataLen; - std::string sessionInfoStr; + const double getLastTelemetryUpdateTS() const; // returns JS compatible TS - std::vector varHeadersArr; +private: + HANDLE hMemMapFile; + const char *pSharedMem; + const irsdk_header *pHeader; + int lastTickCount; + int lastSessionInfoUpdate; + time_t lastValidTime; + char *data; + int dataLen; + std::string sessionInfoStr; - void updateVarHeaders(); // updates map and vector - const char* getSessionInfoStr() const; + std::vector varHeadersArr; - }; + void updateVarHeaders(); // updates map and vector + const char *getSessionInfoStr() const; +}; -} \ No newline at end of file +} // namespace NodeIrSdk diff --git a/src/cpp/IrSdkBindingHelpers.cpp b/src/cpp/IrSdkBindingHelpers.cpp index bb6d82d..a059632 100644 --- a/src/cpp/IrSdkBindingHelpers.cpp +++ b/src/cpp/IrSdkBindingHelpers.cpp @@ -5,7 +5,7 @@ using namespace v8; using namespace std; -Handle NodeIrSdk::convertTelemetryValueToObject(IRSDKWrapper::TelemetryVar& var, const int& index) +Local NodeIrSdk::convertTelemetryValueToObject(IRSDKWrapper::TelemetryVar& var, const int& index) { switch (var.type) { case irsdk_char: @@ -34,10 +34,10 @@ Handle NodeIrSdk::convertTelemetryValueToObject(IRSDKWrapper::TelemetryVa } } -Handle NodeIrSdk::convertTelemetryVarToObject(IRSDKWrapper::TelemetryVar& var) +Local NodeIrSdk::convertTelemetryVarToObject(IRSDKWrapper::TelemetryVar& var) { if (var.header->count > 1) { - Handle arr = Nan::New(var.header->count); + Local arr = Nan::New(var.header->count); for (int i = 0; i < var.header->count; ++i) { arr->Set(i, convertTelemetryValueToObject(var, i)); @@ -50,13 +50,13 @@ Handle NodeIrSdk::convertTelemetryVarToObject(IRSDKWrapper::TelemetryVar& } } -void NodeIrSdk::convertVarHeaderToObject(IRSDKWrapper::TelemetryVar& var, Handle& obj) +void NodeIrSdk::convertVarHeaderToObject(IRSDKWrapper::TelemetryVar& var, Local& obj) { Nan::Set(obj, Nan::New("name").ToLocalChecked(), Nan::New(var.header->name).ToLocalChecked()); Nan::Set(obj, Nan::New("desc").ToLocalChecked(), Nan::New(var.header->desc).ToLocalChecked()); Nan::Set(obj, Nan::New("unit").ToLocalChecked(), Nan::New(var.header->unit).ToLocalChecked()); Nan::Set(obj, Nan::New("count").ToLocalChecked(), Nan::New(var.header->count)); - + switch (var.header->type) { case irsdk_char: Nan::Set(obj, Nan::New("type").ToLocalChecked(), Nan::New("char").ToLocalChecked()); @@ -82,7 +82,7 @@ void NodeIrSdk::convertVarHeaderToObject(IRSDKWrapper::TelemetryVar& var, Handle } } -Handle NodeIrSdk::getMaskedValues(const int& val, char* unit) +Local NodeIrSdk::getMaskedValues(const int& val, char* unit) { if (strcmp(unit,"irsdk_Flags") == 0) { return getValueArr(val, FLAG_MASKS); @@ -103,9 +103,9 @@ Handle NodeIrSdk::getMaskedValues(const int& val, char* unit) return Nan::New(static_cast(val)); } -Handle NodeIrSdk::getValueArr(const int& val, const std::vector MASKS) +Local NodeIrSdk::getValueArr(const int& val, const std::vector MASKS) { - Handle arr = Nan::New(); + Local arr = Nan::New(); int counter = 0; for (const auto& mask : MASKS) { @@ -116,7 +116,7 @@ Handle NodeIrSdk::getValueArr(const int& val, const std::vector NodeIrSdk::getStringValue(const int& val, const std::vector& map) +Local NodeIrSdk::getStringValue(const int& val, const std::vector& map) { for (const auto& mask : map) { if (mask.val == val) { @@ -126,7 +126,7 @@ Handle NodeIrSdk::getStringValue(const int& val, const std::vector convertTelemetryValueToObject(IRSDKWrapper::TelemetryVar& var, const int& index); - Handle convertTelemetryVarToObject(IRSDKWrapper::TelemetryVar& var); - void convertVarHeaderToObject(IRSDKWrapper::TelemetryVar& var, Handle& obj); + Local convertTelemetryValueToObject(IRSDKWrapper::TelemetryVar& var, const int& index); + Local convertTelemetryVarToObject(IRSDKWrapper::TelemetryVar& var); + void convertVarHeaderToObject(IRSDKWrapper::TelemetryVar& var, Local& obj); struct MaskName { @@ -22,10 +22,10 @@ namespace NodeIrSdk MaskName(int val, const char* name); }; - Handle getStringValue(const int& val, const std::vector& map); + Local getStringValue(const int& val, const std::vector& map); - Handle getMaskedValues(const int& val, char* unit); - Handle getValueArr(const int& val, const std::vector MASKS); + Local getMaskedValues(const int& val, char* unit); + Local getValueArr(const int& val, const std::vector MASKS); const std::vector FLAG_MASKS = { MaskName((int)irsdk_checkered, "Checkered"), @@ -58,7 +58,7 @@ namespace NodeIrSdk MaskName((int)irsdk_startSet, "StartSet"), MaskName((int)irsdk_startGo, "StartGo") }; - + const std::vector PIT_SV_MASKS = { MaskName((int)irsdk_LFTireChange, "LFTireChange"), MaskName((int)irsdk_RFTireChange, "RFTireChange"), diff --git a/src/cpp/IrSdkNodeBindings.cpp b/src/cpp/IrSdkNodeBindings.cpp index 8fc71de..f9a8265 100644 --- a/src/cpp/IrSdkNodeBindings.cpp +++ b/src/cpp/IrSdkNodeBindings.cpp @@ -10,118 +10,134 @@ using namespace v8; -namespace NodeIrSdk { +namespace NodeIrSdk +{ - void start(const Nan::FunctionCallbackInfo& args) - { - args.GetReturnValue().Set(Nan::New(irsdk.startup())); - } +void start(const Nan::FunctionCallbackInfo &args) +{ + args.GetReturnValue().Set(Nan::New(irsdk.startup())); +} - void shutdown(const Nan::FunctionCallbackInfo& args) - { - irsdk.shutdown(); - args.GetReturnValue().Set(Nan::Undefined()); - } +void shutdown(const Nan::FunctionCallbackInfo &args) +{ + irsdk.shutdown(); + args.GetReturnValue().Set(Nan::Undefined()); +} - void isInitialized(const Nan::FunctionCallbackInfo& args) - { - args.GetReturnValue().Set(Nan::New(irsdk.isInitialized())); - } +void isInitialized(const Nan::FunctionCallbackInfo &args) +{ + args.GetReturnValue().Set(Nan::New(irsdk.isInitialized())); +} - void isConnected(const Nan::FunctionCallbackInfo& args) - { - args.GetReturnValue().Set(Nan::New(irsdk.isConnected())); - } +void isConnected(const Nan::FunctionCallbackInfo &args) +{ + args.GetReturnValue().Set(Nan::New(irsdk.isConnected())); +} - void updateSessionInfo(const Nan::FunctionCallbackInfo& args) - { - args.GetReturnValue().Set(Nan::New(irsdk.updateSessionInfo())); - } +void updateSessionInfo(const Nan::FunctionCallbackInfo &args) +{ + args.GetReturnValue().Set(Nan::New(irsdk.updateSessionInfo())); +} - void getSessionInfo(const Nan::FunctionCallbackInfo& args) - { - args.GetReturnValue().Set( +void getSessionInfo(const Nan::FunctionCallbackInfo &args) +{ + args.GetReturnValue().Set( Nan::Encode(irsdk.getSessionInfo().c_str(), irsdk.getSessionInfo().length(), Nan::BINARY)); - } +} + +void updateTelemetry(const Nan::FunctionCallbackInfo &args) +{ + args.GetReturnValue().Set(Nan::New(irsdk.updateTelemetry())); +} + +void getTelemetry(const Nan::FunctionCallbackInfo &args) +{ + Local rootObj = Nan::New(); + Local valuesObj = Nan::New(); + Nan::Set(rootObj, Nan::New("timestamp").ToLocalChecked(), Nan::New(irsdk.getLastTelemetryUpdateTS()).ToLocalChecked()); + + std::vector headers = irsdk.getVarHeaders(); - void updateTelemetry(const Nan::FunctionCallbackInfo& args) + for (const auto item : headers) { - args.GetReturnValue().Set(Nan::New(irsdk.updateTelemetry())); + IRSDKWrapper::TelemetryVar var(item); + irsdk.getVarVal(var); + Local varValue = convertTelemetryVarToObject(var); + Nan::Set(valuesObj, Nan::New(var.header->name).ToLocalChecked(), varValue); } + Nan::Set(rootObj, Nan::New("values").ToLocalChecked(), valuesObj); + args.GetReturnValue().Set(rootObj); +} + +void getTelemetryDescription(const Nan::FunctionCallbackInfo &args) +{ + Local obj = Nan::New(); + std::vector headers = irsdk.getVarHeaders(); - void getTelemetry(const Nan::FunctionCallbackInfo& args) + for (const auto item : headers) { - Local rootObj = Nan::New(); - Local valuesObj = Nan::New(); - Nan::Set(rootObj, Nan::New("timestamp").ToLocalChecked(), Nan::New(irsdk.getLastTelemetryUpdateTS()).ToLocalChecked()); + IRSDKWrapper::TelemetryVar var(item); + irsdk.getVarVal(var); + Local varObj = Nan::New(); + convertVarHeaderToObject(var, varObj); + Nan::Set(obj, Nan::New(var.header->name).ToLocalChecked(), varObj); + } + args.GetReturnValue().Set(obj); +} - std::vector headers = irsdk.getVarHeaders(); +NAN_METHOD(sendCmd) +{ - for (const auto item : headers) - { - IRSDKWrapper::TelemetryVar var(item); - irsdk.getVarVal(var); - Handle varValue = convertTelemetryVarToObject(var); - Nan::Set(valuesObj, Nan::New(var.header->name).ToLocalChecked(), varValue); - } - Nan::Set(rootObj, Nan::New("values").ToLocalChecked(), valuesObj); - args.GetReturnValue().Set(rootObj); - } + if (!irsdk.isInitialized() || !irsdk.isConnected()) + return; - void getTelemetryDescription(const Nan::FunctionCallbackInfo& args) + if (info.Length() > 4 || info.Length() < 1) { - Local obj = Nan::New(); - std::vector headers = irsdk.getVarHeaders(); - - for (const auto item : headers) - { - IRSDKWrapper::TelemetryVar var(item); - irsdk.getVarVal(var); - Handle varObj = Nan::New(); - convertVarHeaderToObject(var, varObj); - Nan::Set(obj, Nan::New(var.header->name).ToLocalChecked(), varObj); - } - args.GetReturnValue().Set(obj); + std::cerr << "sendCommand: invalid arguments (1 to 4 accepted)" << std::endl; + return; } - - NAN_METHOD(sendCmd) { - if (!irsdk.isInitialized() || !irsdk.isConnected()) return; - - if (info.Length() > 4 || info.Length() < 1 ) { - std::cerr << "sendCommand: invalid arguments (1 to 4 accepted)" << std::endl; + for (int i = 0; i < info.Length(); ++i) + { + if (!info[i]->IsInt32()) + { + std::cerr << "sendCommand: invalid argument type, int32 needed" << std::endl; return; } - - - for (int i = 0; i < info.Length(); ++i) { - if (!info[i]->IsInt32()) { - std::cerr << "sendCommand: invalid argument type, int32 needed" << std::endl; - return; - } - } - - switch (info.Length()) { - case 1: - broadcastCmd(info[0]->Int32Value(), 0, 0); - break; - case 2: - broadcastCmd(info[0]->Int32Value(), info[1]->Int32Value(), 0); - break; - case 3: - broadcastCmd( - info[0]->Int32Value(), info[1]->Int32Value(), info[2]->Int32Value()); - break; - case 4: - broadcastCmd( - info[0]->Int32Value(), info[1]->Int32Value(), - info[2]->Int32Value(), info[3]->Int32Value()); - break; - } } - static void cleanUp(void* arg) + switch (info.Length()) { - irsdk.shutdown(); + case 1: + broadcastCmd( + info[0]->Int32Value(Nan::GetCurrentContext()).FromJust(), + 0, + 0); + break; + case 2: + broadcastCmd( + info[0]->Int32Value(Nan::GetCurrentContext()).FromJust(), + info[1]->Int32Value(Nan::GetCurrentContext()).FromJust(), + 0); + break; + case 3: + broadcastCmd( + info[0]->Int32Value(Nan::GetCurrentContext()).FromJust(), + info[1]->Int32Value(Nan::GetCurrentContext()).FromJust(), + info[2]->Int32Value(Nan::GetCurrentContext()).FromJust()); + break; + case 4: + broadcastCmd( + info[0]->Int32Value(Nan::GetCurrentContext()).FromJust(), + info[1]->Int32Value(Nan::GetCurrentContext()).FromJust(), + info[2]->Int32Value(Nan::GetCurrentContext()).FromJust(), + info[3]->Int32Value(Nan::GetCurrentContext()).FromJust()); + break; } } + +static void cleanUp(void *arg) +{ + irsdk.shutdown(); +} +} // namespace NodeIrSdk