hamlib 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/hamlib.cpp CHANGED
@@ -1,6 +1,7 @@
1
1
  #include "hamlib.h"
2
2
  #include <string>
3
3
  #include <vector>
4
+ #include <memory>
4
5
 
5
6
  // Cross-platform compatibility for hamlib token types
6
7
  // Linux versions use token_t, some others use hamlib_token_t
@@ -23,6 +24,311 @@ using namespace Napi;
23
24
  Napi::FunctionReference NodeHamLib::constructor;
24
25
  Napi::ThreadSafeFunction tsfn;
25
26
 
27
+ // Base AsyncWorker implementation
28
+ HamLibAsyncWorker::HamLibAsyncWorker(Napi::Function& callback, NodeHamLib* hamlib_instance)
29
+ : AsyncWorker(callback), hamlib_instance_(hamlib_instance), result_code_(0), error_message_("") {}
30
+
31
+ // Specific AsyncWorker classes for each operation
32
+ class OpenAsyncWorker : public HamLibAsyncWorker {
33
+ public:
34
+ OpenAsyncWorker(Napi::Function& callback, NodeHamLib* hamlib_instance)
35
+ : HamLibAsyncWorker(callback, hamlib_instance) {}
36
+
37
+ void Execute() override {
38
+ result_code_ = rig_open(hamlib_instance_->my_rig);
39
+ if (result_code_ != RIG_OK) {
40
+ error_message_ = rigerror(result_code_);
41
+ } else {
42
+ rig_set_freq_callback(hamlib_instance_->my_rig, NodeHamLib::freq_change_cb, hamlib_instance_);
43
+ auto ppt_cb = +[](RIG *rig, vfo_t vfo, ptt_t ptt, rig_ptr_t arg) {
44
+ printf("PPT pushed!");
45
+ return 0;
46
+ };
47
+ int cb_result = rig_set_ptt_callback(hamlib_instance_->my_rig, ppt_cb, NULL);
48
+ rig_set_trn(hamlib_instance_->my_rig, RIG_TRN_POLL);
49
+ hamlib_instance_->rig_is_open = true;
50
+ }
51
+ }
52
+
53
+ void OnOK() override {
54
+ Napi::Env env = Env();
55
+ Callback().Call({env.Undefined(), Napi::Number::New(env, result_code_)});
56
+ }
57
+
58
+ void OnError(const Napi::Error& e) override {
59
+ Napi::Env env = Env();
60
+ Callback().Call({Napi::Error::New(env, error_message_).Value(), env.Undefined()});
61
+ }
62
+ };
63
+
64
+ class SetFrequencyAsyncWorker : public HamLibAsyncWorker {
65
+ public:
66
+ SetFrequencyAsyncWorker(Napi::Function& callback, NodeHamLib* hamlib_instance, freq_t freq, vfo_t vfo)
67
+ : HamLibAsyncWorker(callback, hamlib_instance), freq_(freq), vfo_(vfo) {}
68
+
69
+ void Execute() override {
70
+ result_code_ = rig_set_freq(hamlib_instance_->my_rig, vfo_, freq_);
71
+ if (result_code_ != RIG_OK) {
72
+ error_message_ = rigerror(result_code_);
73
+ }
74
+ }
75
+
76
+ void OnOK() override {
77
+ Napi::Env env = Env();
78
+ Callback().Call({env.Undefined(), Napi::Number::New(env, result_code_)});
79
+ }
80
+
81
+ void OnError(const Napi::Error& e) override {
82
+ Napi::Env env = Env();
83
+ Callback().Call({Napi::Error::New(env, error_message_).Value(), env.Undefined()});
84
+ }
85
+
86
+ private:
87
+ freq_t freq_;
88
+ vfo_t vfo_;
89
+ };
90
+
91
+ class GetFrequencyAsyncWorker : public HamLibAsyncWorker {
92
+ public:
93
+ GetFrequencyAsyncWorker(Napi::Function& callback, NodeHamLib* hamlib_instance, vfo_t vfo)
94
+ : HamLibAsyncWorker(callback, hamlib_instance), vfo_(vfo), freq_(0) {}
95
+
96
+ void Execute() override {
97
+ result_code_ = rig_get_freq(hamlib_instance_->my_rig, vfo_, &freq_);
98
+ if (result_code_ != RIG_OK) {
99
+ error_message_ = rigerror(result_code_);
100
+ }
101
+ }
102
+
103
+ void OnOK() override {
104
+ Napi::Env env = Env();
105
+ Callback().Call({env.Undefined(), Napi::Number::New(env, freq_)});
106
+ }
107
+
108
+ void OnError(const Napi::Error& e) override {
109
+ Napi::Env env = Env();
110
+ Callback().Call({Napi::Error::New(env, error_message_).Value(), env.Undefined()});
111
+ }
112
+
113
+ private:
114
+ vfo_t vfo_;
115
+ freq_t freq_;
116
+ };
117
+
118
+ class SetModeAsyncWorker : public HamLibAsyncWorker {
119
+ public:
120
+ SetModeAsyncWorker(Napi::Function& callback, NodeHamLib* hamlib_instance, rmode_t mode, pbwidth_t width)
121
+ : HamLibAsyncWorker(callback, hamlib_instance), mode_(mode), width_(width) {}
122
+
123
+ void Execute() override {
124
+ result_code_ = rig_set_mode(hamlib_instance_->my_rig, RIG_VFO_CURR, mode_, width_);
125
+ if (result_code_ != RIG_OK) {
126
+ error_message_ = rigerror(result_code_);
127
+ }
128
+ }
129
+
130
+ void OnOK() override {
131
+ Napi::Env env = Env();
132
+ Callback().Call({env.Undefined(), Napi::Number::New(env, result_code_)});
133
+ }
134
+
135
+ void OnError(const Napi::Error& e) override {
136
+ Napi::Env env = Env();
137
+ Callback().Call({Napi::Error::New(env, error_message_).Value(), env.Undefined()});
138
+ }
139
+
140
+ private:
141
+ rmode_t mode_;
142
+ pbwidth_t width_;
143
+ };
144
+
145
+ class GetModeAsyncWorker : public HamLibAsyncWorker {
146
+ public:
147
+ GetModeAsyncWorker(Napi::Function& callback, NodeHamLib* hamlib_instance)
148
+ : HamLibAsyncWorker(callback, hamlib_instance), mode_(0), width_(0) {}
149
+
150
+ void Execute() override {
151
+ result_code_ = rig_get_mode(hamlib_instance_->my_rig, RIG_VFO_CURR, &mode_, &width_);
152
+ if (result_code_ != RIG_OK) {
153
+ error_message_ = rigerror(result_code_);
154
+ }
155
+ }
156
+
157
+ void OnOK() override {
158
+ Napi::Env env = Env();
159
+ Napi::Object obj = Napi::Object::New(env);
160
+ obj.Set(Napi::String::New(env, "mode"), (char)mode_);
161
+ obj.Set(Napi::String::New(env, "width"), width_);
162
+ Callback().Call({env.Undefined(), obj});
163
+ }
164
+
165
+ void OnError(const Napi::Error& e) override {
166
+ Napi::Env env = Env();
167
+ Callback().Call({Napi::Error::New(env, error_message_).Value(), env.Undefined()});
168
+ }
169
+
170
+ private:
171
+ rmode_t mode_;
172
+ pbwidth_t width_;
173
+ };
174
+
175
+ class SetPttAsyncWorker : public HamLibAsyncWorker {
176
+ public:
177
+ SetPttAsyncWorker(Napi::Function& callback, NodeHamLib* hamlib_instance, ptt_t ptt)
178
+ : HamLibAsyncWorker(callback, hamlib_instance), ptt_(ptt) {}
179
+
180
+ void Execute() override {
181
+ result_code_ = rig_set_ptt(hamlib_instance_->my_rig, RIG_VFO_CURR, ptt_);
182
+ if (result_code_ != RIG_OK) {
183
+ error_message_ = rigerror(result_code_);
184
+ }
185
+ }
186
+
187
+ void OnOK() override {
188
+ Napi::Env env = Env();
189
+ Callback().Call({env.Undefined(), Napi::Number::New(env, result_code_)});
190
+ }
191
+
192
+ void OnError(const Napi::Error& e) override {
193
+ Napi::Env env = Env();
194
+ Callback().Call({Napi::Error::New(env, error_message_).Value(), env.Undefined()});
195
+ }
196
+
197
+ private:
198
+ ptt_t ptt_;
199
+ };
200
+
201
+ class GetStrengthAsyncWorker : public HamLibAsyncWorker {
202
+ public:
203
+ GetStrengthAsyncWorker(Napi::Function& callback, NodeHamLib* hamlib_instance)
204
+ : HamLibAsyncWorker(callback, hamlib_instance), strength_(0) {}
205
+
206
+ void Execute() override {
207
+ result_code_ = rig_get_strength(hamlib_instance_->my_rig, RIG_VFO_CURR, &strength_);
208
+ if (result_code_ != RIG_OK) {
209
+ error_message_ = rigerror(result_code_);
210
+ }
211
+ }
212
+
213
+ void OnOK() override {
214
+ Napi::Env env = Env();
215
+ Callback().Call({env.Undefined(), Napi::Number::New(env, strength_)});
216
+ }
217
+
218
+ void OnError(const Napi::Error& e) override {
219
+ Napi::Env env = Env();
220
+ Callback().Call({Napi::Error::New(env, error_message_).Value(), env.Undefined()});
221
+ }
222
+
223
+ private:
224
+ int strength_;
225
+ };
226
+
227
+ class SetVfoAsyncWorker : public HamLibAsyncWorker {
228
+ public:
229
+ SetVfoAsyncWorker(Napi::Function& callback, NodeHamLib* hamlib_instance, vfo_t vfo)
230
+ : HamLibAsyncWorker(callback, hamlib_instance), vfo_(vfo) {}
231
+
232
+ void Execute() override {
233
+ result_code_ = rig_set_vfo(hamlib_instance_->my_rig, vfo_);
234
+ if (result_code_ != RIG_OK) {
235
+ error_message_ = rigerror(result_code_);
236
+ }
237
+ }
238
+
239
+ void OnOK() override {
240
+ Napi::Env env = Env();
241
+ Callback().Call({env.Undefined(), Napi::Number::New(env, result_code_)});
242
+ }
243
+
244
+ void OnError(const Napi::Error& e) override {
245
+ Napi::Env env = Env();
246
+ Callback().Call({Napi::Error::New(env, error_message_).Value(), env.Undefined()});
247
+ }
248
+
249
+ private:
250
+ vfo_t vfo_;
251
+ };
252
+
253
+ class GetVfoAsyncWorker : public HamLibAsyncWorker {
254
+ public:
255
+ GetVfoAsyncWorker(Napi::Function& callback, NodeHamLib* hamlib_instance)
256
+ : HamLibAsyncWorker(callback, hamlib_instance), vfo_(0) {}
257
+
258
+ void Execute() override {
259
+ result_code_ = rig_get_vfo(hamlib_instance_->my_rig, &vfo_);
260
+ if (result_code_ != RIG_OK) {
261
+ error_message_ = rigerror(result_code_);
262
+ }
263
+ }
264
+
265
+ void OnOK() override {
266
+ Napi::Env env = Env();
267
+ Callback().Call({env.Undefined(), Napi::Number::New(env, vfo_)});
268
+ }
269
+
270
+ void OnError(const Napi::Error& e) override {
271
+ Napi::Env env = Env();
272
+ Callback().Call({Napi::Error::New(env, error_message_).Value(), env.Undefined()});
273
+ }
274
+
275
+ private:
276
+ vfo_t vfo_;
277
+ };
278
+
279
+ class CloseAsyncWorker : public HamLibAsyncWorker {
280
+ public:
281
+ CloseAsyncWorker(Napi::Function& callback, NodeHamLib* hamlib_instance)
282
+ : HamLibAsyncWorker(callback, hamlib_instance) {}
283
+
284
+ void Execute() override {
285
+ result_code_ = rig_close(hamlib_instance_->my_rig);
286
+ if (result_code_ != RIG_OK) {
287
+ error_message_ = rigerror(result_code_);
288
+ } else {
289
+ hamlib_instance_->rig_is_open = false;
290
+ }
291
+ }
292
+
293
+ void OnOK() override {
294
+ Napi::Env env = Env();
295
+ Callback().Call({env.Undefined(), Napi::Number::New(env, result_code_)});
296
+ }
297
+
298
+ void OnError(const Napi::Error& e) override {
299
+ Napi::Env env = Env();
300
+ Callback().Call({Napi::Error::New(env, error_message_).Value(), env.Undefined()});
301
+ }
302
+ };
303
+
304
+ class DestroyAsyncWorker : public HamLibAsyncWorker {
305
+ public:
306
+ DestroyAsyncWorker(Napi::Function& callback, NodeHamLib* hamlib_instance)
307
+ : HamLibAsyncWorker(callback, hamlib_instance) {}
308
+
309
+ void Execute() override {
310
+ if (hamlib_instance_->rig_is_open) {
311
+ rig_close(hamlib_instance_->my_rig);
312
+ }
313
+ result_code_ = rig_cleanup(hamlib_instance_->my_rig);
314
+ if (result_code_ != RIG_OK) {
315
+ error_message_ = rigerror(result_code_);
316
+ } else {
317
+ hamlib_instance_->rig_is_open = false;
318
+ }
319
+ }
320
+
321
+ void OnOK() override {
322
+ Napi::Env env = Env();
323
+ Callback().Call({env.Undefined(), Napi::Number::New(env, result_code_)});
324
+ }
325
+
326
+ void OnError(const Napi::Error& e) override {
327
+ Napi::Env env = Env();
328
+ Callback().Call({Napi::Error::New(env, error_message_).Value(), env.Undefined()});
329
+ }
330
+ };
331
+
26
332
  NodeHamLib::NodeHamLib(const Napi::CallbackInfo & info): ObjectWrap(info) {
27
333
  Napi::Env env = info.Env();
28
334
  this->m_currentInfo = (Napi::CallbackInfo *)&info;
@@ -126,73 +432,76 @@ int NodeHamLib::freq_change_cb(RIG *rig, vfo_t vfo, freq_t freq, void* arg) {
126
432
  Napi::Value NodeHamLib::Open(const Napi::CallbackInfo & info) {
127
433
  Napi::Env env = info.Env();
128
434
 
129
- int retcode = rig_open(my_rig);
130
- if (retcode != RIG_OK) {
131
- printf("rig_open: error = %s\n", rigerror(retcode));
132
- // Napi::TypeError::New(env, "Unable to open rig")
133
- // .ThrowAsJavaScriptException();
435
+ if (info.Length() < 1 || !info[0].IsFunction()) {
436
+ Napi::TypeError::New(env, "Callback function is required").ThrowAsJavaScriptException();
437
+ return env.Undefined();
134
438
  }
135
-
136
-
137
-
138
- rig_set_freq_callback(my_rig, NodeHamLib::freq_change_cb, this);
139
-
140
- auto ppt_cb =+[](RIG *rig, vfo_t vfo, ptt_t ptt, rig_ptr_t arg) {
141
- printf("PPT pushed!");
142
- return 0;
143
- };
144
- retcode = rig_set_ptt_callback (my_rig, ppt_cb, NULL);
145
- rig_set_trn(my_rig, RIG_TRN_POLL);
146
- if (retcode != RIG_OK ) {
147
- printf("rig_set_trn: error = %s \n", rigerror(retcode));
148
- }
149
-
150
- printf ("callback: %s", rigerror(retcode));
151
-
152
- rig_is_open = true;
153
- return Napi::Number::New(env, retcode);
439
+
440
+ Napi::Function callback = info[0].As<Napi::Function>();
441
+ OpenAsyncWorker* worker = new OpenAsyncWorker(callback, this);
442
+ worker->Queue();
443
+
444
+ return env.Undefined();
154
445
  }
155
446
 
156
447
  Napi::Value NodeHamLib::SetVFO(const Napi::CallbackInfo & info) {
157
448
  Napi::Env env = info.Env();
158
- int retcode;
159
449
 
160
450
  if (!rig_is_open) {
161
451
  Napi::TypeError::New(env, "Rig is not open!")
162
452
  .ThrowAsJavaScriptException();
163
453
  return env.Null();
164
454
  }
165
- if (info.Length() < 1) {
166
- Napi::TypeError::New(env, "Must Specify VFO-A or VFO-B")
455
+
456
+ if (info.Length() < 2) {
457
+ Napi::TypeError::New(env, "Must specify VFO and callback")
167
458
  .ThrowAsJavaScriptException();
168
459
  return env.Null();
169
460
  }
461
+
170
462
  if (!info[0].IsString()) {
171
- Napi::TypeError::New(env, "Must Specify VFO-A or VFO-B as a string")
463
+ Napi::TypeError::New(env, "Must specify VFO-A or VFO-B as a string")
464
+ .ThrowAsJavaScriptException();
465
+ return env.Null();
466
+ }
467
+
468
+ if (!info[1].IsFunction()) {
469
+ Napi::TypeError::New(env, "Second parameter must be callback function")
172
470
  .ThrowAsJavaScriptException();
173
471
  return env.Null();
174
472
  }
473
+
175
474
  auto name = info[0].As < Napi::String > ().Utf8Value().c_str();
475
+ vfo_t vfo;
476
+
176
477
  if (strcmp(name, "VFO-A") == 0) {
177
- retcode = rig_set_vfo(my_rig, RIG_VFO_A);
478
+ vfo = RIG_VFO_A;
178
479
  } else if (strcmp(name, "VFO-B") == 0) {
179
- retcode = rig_set_vfo(my_rig, RIG_VFO_B);
480
+ vfo = RIG_VFO_B;
180
481
  } else {
181
- retcode = 1;
482
+ Napi::TypeError::New(env, "Invalid VFO name")
483
+ .ThrowAsJavaScriptException();
484
+ return env.Null();
182
485
  }
183
- return Napi::Number::New(env, retcode);
486
+
487
+ Napi::Function callback = info[1].As<Napi::Function>();
488
+ SetVfoAsyncWorker* worker = new SetVfoAsyncWorker(callback, this, vfo);
489
+ worker->Queue();
490
+
491
+ return env.Undefined();
184
492
  }
185
493
 
186
494
  Napi::Value NodeHamLib::SetFrequency(const Napi::CallbackInfo & info) {
187
495
  Napi::Env env = info.Env();
188
- int retcode;
496
+
189
497
  if (!rig_is_open) {
190
498
  Napi::TypeError::New(env, "Rig is not open!")
191
499
  .ThrowAsJavaScriptException();
192
500
  return env.Null();
193
501
  }
194
- if (info.Length() < 1) {
195
- Napi::TypeError::New(env, "Must specify frequency")
502
+
503
+ if (info.Length() < 2) {
504
+ Napi::TypeError::New(env, "Must specify frequency and callback")
196
505
  .ThrowAsJavaScriptException();
197
506
  return env.Null();
198
507
  }
@@ -202,134 +511,161 @@ Napi::Value NodeHamLib::SetFrequency(const Napi::CallbackInfo & info) {
202
511
  .ThrowAsJavaScriptException();
203
512
  return env.Null();
204
513
  }
514
+
205
515
  auto freq = info[0].As < Napi::Number > ().Int32Value();
206
516
 
207
517
  // Support optional VFO parameter
208
518
  vfo_t vfo = RIG_VFO_CURR;
209
- if (info.Length() >= 2 && info[1].IsString()) {
210
- auto vfostr = info[1].As < Napi::String > ().Utf8Value().c_str();
211
- if (strcmp(vfostr, "VFO-A") == 0) {
212
- vfo = RIG_VFO_A;
213
- } else if (strcmp(vfostr, "VFO-B") == 0) {
214
- vfo = RIG_VFO_B;
519
+ Napi::Function callback;
520
+
521
+ if (info.Length() == 2) {
522
+ if (!info[1].IsFunction()) {
523
+ Napi::TypeError::New(env, "Second parameter must be callback function")
524
+ .ThrowAsJavaScriptException();
525
+ return env.Null();
526
+ }
527
+ callback = info[1].As<Napi::Function>();
528
+ } else if (info.Length() == 3) {
529
+ if (info[1].IsString()) {
530
+ auto vfostr = info[1].As < Napi::String > ().Utf8Value().c_str();
531
+ if (strcmp(vfostr, "VFO-A") == 0) {
532
+ vfo = RIG_VFO_A;
533
+ } else if (strcmp(vfostr, "VFO-B") == 0) {
534
+ vfo = RIG_VFO_B;
535
+ }
536
+ }
537
+ if (!info[2].IsFunction()) {
538
+ Napi::TypeError::New(env, "Third parameter must be callback function")
539
+ .ThrowAsJavaScriptException();
540
+ return env.Null();
215
541
  }
542
+ callback = info[2].As<Napi::Function>();
216
543
  }
217
544
 
218
- retcode = rig_set_freq(my_rig, vfo, freq);
219
- return Napi::Number::New(env, retcode);
545
+ SetFrequencyAsyncWorker* worker = new SetFrequencyAsyncWorker(callback, this, freq, vfo);
546
+ worker->Queue();
547
+
548
+ return env.Undefined();
220
549
  }
221
550
 
222
551
  Napi::Value NodeHamLib::SetMode(const Napi::CallbackInfo & info) {
223
552
  Napi::Env env = info.Env();
224
- int retcode;
225
- pbwidth_t bandwidth;
553
+
226
554
  if (!rig_is_open) {
227
555
  Napi::TypeError::New(env, "Rig is not open!")
228
556
  .ThrowAsJavaScriptException();
229
557
  return env.Null();
230
558
  }
231
- if (info.Length() < 1) {
232
- Napi::TypeError::New(env, "Must Specify Mode")
559
+
560
+ if (info.Length() < 2) {
561
+ Napi::TypeError::New(env, "Must specify mode and callback")
233
562
  .ThrowAsJavaScriptException();
234
563
  return env.Null();
235
564
  }
236
565
 
237
566
  if (!info[0].IsString()) {
238
- Napi::TypeError::New(env, "Must Specify Mode as string")
567
+ Napi::TypeError::New(env, "Must specify mode as string")
239
568
  .ThrowAsJavaScriptException();
240
569
  return env.Null();
241
570
  }
242
571
 
243
572
  auto modestr = info[0].As < Napi::String > ().Utf8Value().c_str();
244
573
  auto mode = rig_parse_mode(modestr);
574
+ pbwidth_t bandwidth = RIG_PASSBAND_NORMAL;
575
+ Napi::Function callback;
245
576
 
246
- if (info.Length() > 1) {
247
- if (!info[1].IsString()) {
248
- Napi::TypeError::New(env, "Must Specify Mode as string")
577
+ if (info.Length() == 2) {
578
+ if (!info[1].IsFunction()) {
579
+ Napi::TypeError::New(env, "Second parameter must be callback function")
249
580
  .ThrowAsJavaScriptException();
250
581
  return env.Null();
251
582
  }
252
- auto bandstr = info[1].As < Napi::String > ().Utf8Value().c_str();
253
- if (strcmp(bandstr, "narrow") == 0) {
254
- bandwidth = rig_passband_narrow(my_rig, mode);
255
- } else if (strcmp(bandstr, "wide") == 0) {
256
- bandwidth = rig_passband_wide(my_rig, mode);
257
- } else {
258
- bandwidth = RIG_PASSBAND_NORMAL;
583
+ callback = info[1].As<Napi::Function>();
584
+ } else if (info.Length() == 3) {
585
+ if (info[1].IsString()) {
586
+ auto bandstr = info[1].As < Napi::String > ().Utf8Value().c_str();
587
+ if (strcmp(bandstr, "narrow") == 0) {
588
+ bandwidth = rig_passband_narrow(my_rig, mode);
589
+ } else if (strcmp(bandstr, "wide") == 0) {
590
+ bandwidth = rig_passband_wide(my_rig, mode);
591
+ }
259
592
  }
260
- } else {
261
- bandwidth = RIG_PASSBAND_NORMAL;
593
+ if (!info[2].IsFunction()) {
594
+ Napi::TypeError::New(env, "Third parameter must be callback function")
595
+ .ThrowAsJavaScriptException();
596
+ return env.Null();
597
+ }
598
+ callback = info[2].As<Napi::Function>();
262
599
  }
263
600
 
264
- retcode = rig_set_mode(my_rig, RIG_VFO_CURR, mode, bandwidth);
265
- if (retcode != RIG_OK) {
266
-
267
- Napi::TypeError::New(env, rigerror(retcode))
268
- .ThrowAsJavaScriptException();
269
- return env.Null();
270
- }
271
- return Napi::Number::New(env, retcode);
601
+ SetModeAsyncWorker* worker = new SetModeAsyncWorker(callback, this, mode, bandwidth);
602
+ worker->Queue();
603
+
604
+ return env.Undefined();
272
605
  }
273
606
 
274
607
  Napi::Value NodeHamLib::SetPtt(const Napi::CallbackInfo & info) {
275
608
  Napi::Env env = info.Env();
276
- int retcode;
277
- bool ptt_state;
609
+
278
610
  if (!rig_is_open) {
279
611
  Napi::TypeError::New(env, "Rig is not open!")
280
612
  .ThrowAsJavaScriptException();
281
613
  return env.Null();
282
614
  }
283
- if (info.Length() < 1) {
284
- Napi::TypeError::New(env, "Specify true or false for ppt state")
615
+
616
+ if (info.Length() < 2) {
617
+ Napi::TypeError::New(env, "Must specify PTT state and callback")
285
618
  .ThrowAsJavaScriptException();
286
619
  return env.Null();
287
620
  }
288
621
 
289
622
  if (!info[0].IsBoolean()) {
290
- Napi::TypeError::New(env, "PTT state is not boolean")
623
+ Napi::TypeError::New(env, "PTT state must be boolean")
291
624
  .ThrowAsJavaScriptException();
292
625
  return env.Null();
293
626
  }
294
- ptt_state = info[0].As < Napi::Boolean > ();
295
- if (ptt_state) {
296
- retcode = rig_set_ptt(my_rig, RIG_VFO_CURR, RIG_PTT_ON);
297
- } else {
298
- retcode = rig_set_ptt(my_rig, RIG_VFO_CURR, RIG_PTT_OFF);
299
- }
300
- if (retcode != RIG_OK) {
301
-
302
- Napi::TypeError::New(env, rigerror(retcode))
627
+
628
+ if (!info[1].IsFunction()) {
629
+ Napi::TypeError::New(env, "Second parameter must be callback function")
303
630
  .ThrowAsJavaScriptException();
304
631
  return env.Null();
305
632
  }
306
- return Napi::Number::New(env, retcode);
633
+
634
+ bool ptt_state = info[0].As < Napi::Boolean > ();
635
+ Napi::Function callback = info[1].As<Napi::Function>();
636
+
637
+ ptt_t ptt = ptt_state ? RIG_PTT_ON : RIG_PTT_OFF;
638
+ SetPttAsyncWorker* worker = new SetPttAsyncWorker(callback, this, ptt);
639
+ worker->Queue();
640
+
641
+ return env.Undefined();
307
642
  }
308
643
 
309
644
  Napi::Value NodeHamLib::GetVFO(const Napi::CallbackInfo & info) {
310
645
  Napi::Env env = info.Env();
311
- int retcode;
312
- vfo_t vfo;
646
+
313
647
  if (!rig_is_open) {
314
648
  Napi::TypeError::New(env, "Rig is not open!")
315
649
  .ThrowAsJavaScriptException();
316
650
  return env.Null();
317
651
  }
318
- retcode = rig_get_vfo(my_rig, & vfo);
319
- if (retcode == RIG_OK) {
320
- return Napi::Number::New(env, vfo);
321
- } else {
322
- //dont throw an exception here, not every radio reports vfo
323
- Napi::Error::New(env, rigerror(retcode));
652
+
653
+ if (info.Length() < 1 || !info[0].IsFunction()) {
654
+ Napi::TypeError::New(env, "Callback function is required")
655
+ .ThrowAsJavaScriptException();
324
656
  return env.Null();
325
657
  }
326
-
658
+
659
+ Napi::Function callback = info[0].As<Napi::Function>();
660
+ GetVfoAsyncWorker* worker = new GetVfoAsyncWorker(callback, this);
661
+ worker->Queue();
662
+
663
+ return env.Undefined();
327
664
  }
328
665
 
329
666
  Napi::Value NodeHamLib::GetFrequency(const Napi::CallbackInfo & info) {
330
667
  Napi::Env env = info.Env();
331
- int retcode;
332
- freq_t freq;
668
+
333
669
  if (!rig_is_open) {
334
670
  Napi::TypeError::New(env, "Rig is not open!")
335
671
  .ThrowAsJavaScriptException();
@@ -338,93 +674,122 @@ Napi::Value NodeHamLib::GetFrequency(const Napi::CallbackInfo & info) {
338
674
 
339
675
  // Support optional VFO parameter
340
676
  vfo_t vfo = RIG_VFO_CURR;
341
- if (info.Length() >= 1 && info[0].IsString()) {
342
- auto vfostr = info[0].As < Napi::String > ().Utf8Value().c_str();
343
- if (strcmp(vfostr, "VFO-A") == 0) {
344
- vfo = RIG_VFO_A;
345
- } else if (strcmp(vfostr, "VFO-B") == 0) {
346
- vfo = RIG_VFO_B;
347
- }
348
- }
677
+ Napi::Function callback;
349
678
 
350
- retcode = rig_get_freq(my_rig, vfo, & freq);
351
- if (retcode == RIG_OK) {
352
- return Napi::Number::New(env, freq);
679
+ if (info.Length() == 1) {
680
+ if (!info[0].IsFunction()) {
681
+ Napi::TypeError::New(env, "Parameter must be callback function")
682
+ .ThrowAsJavaScriptException();
683
+ return env.Null();
684
+ }
685
+ callback = info[0].As<Napi::Function>();
686
+ } else if (info.Length() == 2) {
687
+ if (info[0].IsString()) {
688
+ auto vfostr = info[0].As < Napi::String > ().Utf8Value().c_str();
689
+ if (strcmp(vfostr, "VFO-A") == 0) {
690
+ vfo = RIG_VFO_A;
691
+ } else if (strcmp(vfostr, "VFO-B") == 0) {
692
+ vfo = RIG_VFO_B;
693
+ }
694
+ }
695
+ if (!info[1].IsFunction()) {
696
+ Napi::TypeError::New(env, "Second parameter must be callback function")
697
+ .ThrowAsJavaScriptException();
698
+ return env.Null();
699
+ }
700
+ callback = info[1].As<Napi::Function>();
353
701
  } else {
354
- Napi::Error::New(env, rigerror(retcode));
702
+ Napi::TypeError::New(env, "Must provide callback function")
703
+ .ThrowAsJavaScriptException();
355
704
  return env.Null();
356
705
  }
706
+
707
+ GetFrequencyAsyncWorker* worker = new GetFrequencyAsyncWorker(callback, this, vfo);
708
+ worker->Queue();
709
+
710
+ return env.Undefined();
357
711
  }
358
712
 
359
713
  Napi::Value NodeHamLib::GetMode(const Napi::CallbackInfo & info) {
360
714
  Napi::Env env = info.Env();
361
- int retcode;
362
- rmode_t rmode;
363
- pbwidth_t width;
715
+
364
716
  if (!rig_is_open) {
365
717
  Napi::TypeError::New(env, "Rig is not open!")
366
718
  .ThrowAsJavaScriptException();
367
719
  return env.Null();
368
720
  }
369
- retcode = rig_get_mode(my_rig, RIG_VFO_CURR, & rmode, & width);
370
- if (retcode == RIG_OK) {
371
- Napi::Object obj = Napi::Object::New(env);
372
- obj.Set(Napi::String::New(env, "mode"), (char)rmode);
373
- obj.Set(Napi::String::New(env, "width"), width);
374
- return obj;
375
- } else {
376
- Napi::Error::New(env, rigerror(retcode));
721
+
722
+ if (info.Length() < 1 || !info[0].IsFunction()) {
723
+ Napi::TypeError::New(env, "Callback function is required")
724
+ .ThrowAsJavaScriptException();
377
725
  return env.Null();
378
726
  }
727
+
728
+ Napi::Function callback = info[0].As<Napi::Function>();
729
+ GetModeAsyncWorker* worker = new GetModeAsyncWorker(callback, this);
730
+ worker->Queue();
731
+
732
+ return env.Undefined();
379
733
  }
380
734
 
381
735
  Napi::Value NodeHamLib::GetStrength(const Napi::CallbackInfo & info) {
382
736
  Napi::Env env = info.Env();
383
- int retcode;
384
- int strength;
737
+
385
738
  if (!rig_is_open) {
386
739
  Napi::TypeError::New(env, "Rig is not open!")
387
740
  .ThrowAsJavaScriptException();
388
741
  return env.Null();
389
742
  }
390
- retcode = rig_get_strength(my_rig, RIG_VFO_CURR, & strength);
391
- if (retcode == RIG_OK) {
392
- return Napi::Number::New(env, strength);
393
- } else {
394
- Napi::Error::New(env, rigerror(retcode));
743
+
744
+ if (info.Length() < 1 || !info[0].IsFunction()) {
745
+ Napi::TypeError::New(env, "Callback function is required")
746
+ .ThrowAsJavaScriptException();
395
747
  return env.Null();
396
748
  }
749
+
750
+ Napi::Function callback = info[0].As<Napi::Function>();
751
+ GetStrengthAsyncWorker* worker = new GetStrengthAsyncWorker(callback, this);
752
+ worker->Queue();
753
+
754
+ return env.Undefined();
397
755
  }
398
756
 
399
757
  Napi::Value NodeHamLib::Close(const Napi::CallbackInfo & info) {
400
758
  Napi::Env env = info.Env();
759
+
401
760
  if (!rig_is_open) {
402
761
  Napi::TypeError::New(env, "Rig is not open!")
403
762
  .ThrowAsJavaScriptException();
404
763
  return env.Null();
405
764
  }
406
- int retcode = rig_close(my_rig);
407
- if (retcode != RIG_OK) {
408
- Napi::TypeError::New(env, "Unable to open rig")
765
+
766
+ if (info.Length() < 1 || !info[0].IsFunction()) {
767
+ Napi::TypeError::New(env, "Callback function is required")
409
768
  .ThrowAsJavaScriptException();
769
+ return env.Null();
410
770
  }
411
- rig_is_open = false;
412
- return Napi::Number::New(env, retcode);
771
+
772
+ Napi::Function callback = info[0].As<Napi::Function>();
773
+ CloseAsyncWorker* worker = new CloseAsyncWorker(callback, this);
774
+ worker->Queue();
775
+
776
+ return env.Undefined();
413
777
  }
414
778
 
415
779
  Napi::Value NodeHamLib::Destroy(const Napi::CallbackInfo & info) {
416
780
  Napi::Env env = info.Env();
417
- if (rig_is_open) {
418
- rig_close(my_rig);
419
- }
420
- int retcode = rig_cleanup(my_rig);
421
- if (retcode != RIG_OK) {
422
-
423
- Napi::TypeError::New(env, rigerror(retcode))
781
+
782
+ if (info.Length() < 1 || !info[0].IsFunction()) {
783
+ Napi::TypeError::New(env, "Callback function is required")
424
784
  .ThrowAsJavaScriptException();
785
+ return env.Null();
425
786
  }
426
- rig_is_open = false;
427
- return Napi::Number::New(env, retcode);
787
+
788
+ Napi::Function callback = info[0].As<Napi::Function>();
789
+ DestroyAsyncWorker* worker = new DestroyAsyncWorker(callback, this);
790
+ worker->Queue();
791
+
792
+ return env.Undefined();
428
793
  }
429
794
 
430
795
  Napi::Value NodeHamLib::GetConnectionInfo(const Napi::CallbackInfo & info) {