@@ -33,24 +33,25 @@ namespace OZW {
3333
3434 uv_async_init (uv_default_loop (), &async, async_cb_handler);
3535
36- Local<Function> callbackHandle = Nan::Get ( info.This (),
37- Nan::New<String>(" emit" ).ToLocalChecked ()
38- ).ToLocalChecked ()
39- .As <Function>();
40-
41- emit_cb = new Nan::Callback (callbackHandle);
42-
4336 OZW* self = ObjectWrap::Unwrap<OZW>(info.This ());
44-
37+ ::std::string version (" " );
38+ #if OPENZWAVE_EXCEPTIONS
39+ try {
40+ #endif
4541 OpenZWave::Options::Create (self->config_path , self->userpath , self->option_overrides );
4642 OpenZWave::Options::Get ()->Lock ();
47-
4843 OpenZWave::Manager::Create ();
49- OpenZWave::Manager* mgr = OpenZWave::Manager::Get ();
50- mgr->AddWatcher (ozw_watcher_callback, NULL );
51- mgr->AddDriver (path);
52- ::std::string version (OpenZWave::Manager::getVersionAsString ());
53-
44+ OZWManager (AddWatcher, ozw_watcher_callback, NULL );
45+ OZWManager (AddDriver, path);
46+ version = OpenZWave::Manager::getVersionAsString ();
47+ #if OPENZWAVE_EXCEPTIONS
48+ } catch ( OpenZWave::OZWException& e ) {
49+ char buffer [100 ];
50+ sprintf (buffer, " Exception connecting in %s(%d): %s" ,
51+ e.GetFile ().c_str (), e.GetLine (), e.GetMsg ().c_str ());
52+ Nan::ThrowError ( buffer );
53+ }
54+ #endif
5455 Local < v8::Value > cbinfo[16 ];
5556 cbinfo[0 ] = Nan::New<String>(" connected" ).ToLocalChecked ();
5657 cbinfo[1 ] = Nan::New<String>(version).ToLocalChecked ();
@@ -110,12 +111,27 @@ namespace OZW {
110111 CheckMinArgs (1 , " path" );
111112 ::std::string path (*Nan::Utf8String ( info[0 ] ));
112113
113- OpenZWave::Manager::Get ()->RemoveDriver (path);
114- OpenZWave::Manager::Get ()->RemoveWatcher (ozw_watcher_callback, NULL );
114+ OZWManager ( RemoveDriver, path );
115+ OZWManager ( RemoveWatcher, ozw_watcher_callback, NULL );
116+ #if OPENZWAVE_EXCEPTIONS
117+ try {
118+ #endif
115119 OpenZWave::Manager::Destroy ();
116120 OpenZWave::Options::Destroy ();
117-
118- delete emit_cb;
121+ #if OPENZWAVE_EXCEPTIONS
122+ } catch ( OpenZWave::OZWException& e ) {
123+ char buffer [100 ];
124+ sprintf (buffer, " Exception disconnecting in %s(%d): %s" ,
125+ e.GetFile ().c_str (), e.GetLine (), e.GetMsg ().c_str ());
126+ Nan::ThrowError ( buffer );
127+ }
128+ #endif
129+ // FIXME: seems some recent innocuous change in NaN causes the context (ctx_obj) to be freed.
130+ // Therefore, deleting this V8 resource will cause V8 to crash. NOT deleting it could memleak
131+ // when you're reloading the driver.
132+ //
133+ // delete emit_cb;
134+ //
119135 }
120136
121137 /*
@@ -127,15 +143,15 @@ namespace OZW {
127143 // ===================================================================
128144 {
129145 Nan::HandleScope scope;
130- OpenZWave::Manager::Get ()-> ResetController ( homeid);
146+ OZWManager ( ResetController, homeid);
131147 }
132148
133149 // ===================================================================
134150 NAN_METHOD (OZW::SoftReset)
135151 // ===================================================================
136152 {
137153 Nan::HandleScope scope;
138- OpenZWave::Manager::Get ()-> SoftReset ( homeid);
154+ OZWManager ( SoftReset, homeid);
139155 }
140156
141157
@@ -144,7 +160,8 @@ namespace OZW {
144160 // ===================================================================
145161 {
146162 Nan::HandleScope scope;
147- uint8 ctrlid = OpenZWave::Manager::Get ()->GetControllerNodeId (homeid);
163+ uint8 ctrlid = -1 ;
164+ OZWManagerAssign (ctrlid, GetControllerNodeId, homeid);
148165 info.GetReturnValue ().Set (
149166 Nan::New<Integer>(ctrlid)
150167 );
@@ -155,7 +172,8 @@ namespace OZW {
155172 // ===================================================================
156173 {
157174 Nan::HandleScope scope;
158- uint8 sucid = OpenZWave::Manager::Get ()->GetSUCNodeId (homeid);
175+ uint8 sucid = -1 ;
176+ OZWManagerAssign (sucid, GetSUCNodeId, homeid);
159177 info.GetReturnValue ().Set (
160178 Nan::New<Integer>(sucid)
161179 );
@@ -171,7 +189,8 @@ namespace OZW {
171189 // ===================================================================
172190 {
173191 Nan::HandleScope scope;
174- bool isprimary = OpenZWave::Manager::Get ()->IsPrimaryController (homeid);
192+ bool isprimary = false ;
193+ OZWManagerAssign (isprimary, IsPrimaryController, homeid);
175194 info.GetReturnValue ().Set (Nan::New<Boolean>(isprimary));
176195 }
177196
@@ -185,7 +204,8 @@ namespace OZW {
185204 // ===================================================================
186205 {
187206 Nan::HandleScope scope;
188- bool issuc = OpenZWave::Manager::Get ()->IsStaticUpdateController (homeid);
207+ bool issuc = false ;
208+ OZWManagerAssign (issuc, IsStaticUpdateController, homeid);
189209 info.GetReturnValue ().Set (Nan::New<Boolean>(issuc));
190210 }
191211
@@ -198,7 +218,8 @@ namespace OZW {
198218 // ===================================================================
199219 {
200220 Nan::HandleScope scope;
201- bool isbridge = OpenZWave::Manager::Get ()->IsBridgeController (homeid);
221+ bool isbridge = false ;
222+ OZWManagerAssign (isbridge, IsBridgeController, homeid);
202223 info.GetReturnValue ().Set (Nan::New<Boolean>(isbridge));
203224 }
204225
@@ -209,7 +230,8 @@ namespace OZW {
209230 // ===================================================================
210231 {
211232 Nan::HandleScope scope;
212- ::std::string libver = OpenZWave::Manager::Get ()->GetLibraryVersion (homeid);
233+ ::std::string libver (" " );
234+ OZWManagerAssign (libver, GetLibraryVersion, homeid);
213235 info.GetReturnValue ().Set (
214236 Nan::New<String>(
215237 libver.c_str ()
@@ -237,7 +259,8 @@ namespace OZW {
237259 // ===================================================================
238260 {
239261 Nan::HandleScope scope;
240- ::std::string libtype = OpenZWave::Manager::Get ()->GetLibraryTypeName (homeid);
262+ ::std::string libtype (" " );
263+ OZWManagerAssign (libtype, GetLibraryTypeName, homeid);
241264 info.GetReturnValue ().Set (
242265 Nan::New<String>(
243266 libtype.c_str ()
@@ -250,7 +273,8 @@ namespace OZW {
250273 // ===================================================================
251274 {
252275 Nan::HandleScope scope;
253- uint32 cnt = OpenZWave::Manager::Get ()->GetSendQueueCount (homeid);
276+ uint32 cnt = 0 ;
277+ OZWManagerAssign (cnt, GetSendQueueCount, homeid);
254278 info.GetReturnValue ().Set (Nan::New<Integer>(cnt));
255279 }
256280
0 commit comments