Skip to content

Commit f1ca54f

Browse files
committed
feat: add static method to retrieve Hamlib version information
1 parent 6bf8203 commit f1ca54f

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

index.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,17 @@ declare class HamLib {
265265
*/
266266
static getSupportedRigs(): SupportedRigInfo[];
267267

268+
/**
269+
* Get Hamlib library version information
270+
* @returns Hamlib version string including version number, build date, and architecture
271+
* @static
272+
* @example
273+
* const version = HamLib.getHamlibVersion();
274+
* console.log(`Hamlib version: ${version}`);
275+
* // Output: "Hamlib 4.5.5 2024-01-15 12:00:00 64-bit"
276+
*/
277+
static getHamlibVersion(): string;
278+
268279
/**
269280
* Open connection to device
270281
* Must be called before other operations

lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ class HamLib {
3030
return nativeModule.HamLib.getSupportedRigs();
3131
}
3232

33+
/**
34+
* Get Hamlib library version information
35+
* @returns {string} Hamlib version string (e.g., "Hamlib 4.5.5 2024-01-15 12:00:00 64-bit")
36+
* @static
37+
*/
38+
static getHamlibVersion() {
39+
return nativeModule.HamLib.getHamlibVersion();
40+
}
41+
3342
/**
3443
* Open connection to the radio device
3544
* Must be called before other operations

src/hamlib.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3720,9 +3720,10 @@ Napi::Function NodeHamLib::GetClass(Napi::Env env) {
37203720
NodeHamLib::InstanceMethod("close", & NodeHamLib::Close),
37213721
NodeHamLib::InstanceMethod("destroy", & NodeHamLib::Destroy),
37223722
NodeHamLib::InstanceMethod("getConnectionInfo", & NodeHamLib::GetConnectionInfo),
3723-
3724-
// Static method to get supported rig models
3723+
3724+
// Static methods
37253725
NodeHamLib::StaticMethod("getSupportedRigs", & NodeHamLib::GetSupportedRigs),
3726+
NodeHamLib::StaticMethod("getHamlibVersion", & NodeHamLib::GetHamlibVersion),
37263727
});
37273728
constructor = Napi::Persistent(ret);
37283729
constructor.SuppressDestruct();
@@ -3832,6 +3833,16 @@ Napi::Value NodeHamLib::GetSupportedRigs(const Napi::CallbackInfo& info) {
38323833
return rigArray;
38333834
}
38343835

3836+
// Get Hamlib version information
3837+
Napi::Value NodeHamLib::GetHamlibVersion(const Napi::CallbackInfo& info) {
3838+
Napi::Env env = info.Env();
3839+
3840+
// Reference to external hamlib_version2 variable
3841+
extern const char* hamlib_version2;
3842+
3843+
return Napi::String::New(env, hamlib_version2);
3844+
}
3845+
38353846
// Serial Port Configuration Methods
38363847

38373848
// Set serial configuration parameter (data_bits, stop_bits, parity, handshake, etc.)

src/hamlib.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ class NodeHamLib : public Napi::ObjectWrap<NodeHamLib> {
164164

165165
// Static method to get supported rig models
166166
static Napi::Value GetSupportedRigs(const Napi::CallbackInfo&);
167-
167+
168+
// Static method to get Hamlib version
169+
static Napi::Value GetHamlibVersion(const Napi::CallbackInfo&);
170+
168171
static Napi::Function GetClass(Napi::Env);
169172

170173
static int freq_change_cb(RIG*, vfo_t, freq_t, void*);

test/test_loader.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ const { HamLib } = require('../index.js');
77

88
console.log('🧪 测试node-hamlib模块加载和基础功能...\n');
99

10+
// 输出Hamlib版本信息
11+
try {
12+
const hamlibVersion = HamLib.getHamlibVersion();
13+
console.log('📌 Hamlib版本信息:');
14+
console.log(` ${hamlibVersion}\n`);
15+
} catch (e) {
16+
console.log('⚠️ 无法获取Hamlib版本信息:', e.message, '\n');
17+
}
18+
1019
let testsPassed = 0;
1120
let testsFailed = 0;
1221

@@ -34,8 +43,9 @@ try {
3443

3544
// 2. 静态方法测试
3645
console.log('\n📊 静态方法测试:');
46+
test('getHamlibVersion静态方法存在', () => typeof HamLib.getHamlibVersion === 'function');
3747
test('getSupportedRigs静态方法存在', () => typeof HamLib.getSupportedRigs === 'function');
38-
48+
3949
try {
4050
const supportedRigs = HamLib.getSupportedRigs();
4151
test('getSupportedRigs返回数组', () => Array.isArray(supportedRigs));
@@ -46,6 +56,27 @@ try {
4656
return first.rigModel && first.modelName && first.mfgName;
4757
});
4858
console.log(` 📈 找到 ${supportedRigs.length} 个支持的电台型号`);
59+
60+
// 打印所有支持的设备型号
61+
console.log('\n📻 所有支持的设备型号:');
62+
console.log(' ────────────────────────────────────────────────────────────────');
63+
console.log(' 型号ID | 制造商 | 型号名称 | 状态');
64+
console.log(' ────────────────────────────────────────────────────────────────');
65+
supportedRigs.forEach((rig, index) => {
66+
const model = String(rig.rigModel).padEnd(7);
67+
const mfg = (rig.mfgName || '').substring(0, 20).padEnd(20);
68+
const name = (rig.modelName || '').substring(0, 28).padEnd(28);
69+
const status = rig.status || '';
70+
console.log(` ${model} | ${mfg} | ${name} | ${status}`);
71+
72+
// 每50行输出一个分隔符,便于阅读
73+
if ((index + 1) % 50 === 0 && index + 1 < supportedRigs.length) {
74+
console.log(' ────────────────────────────────────────────────────────────────');
75+
}
76+
});
77+
console.log(' ────────────────────────────────────────────────────────────────');
78+
console.log(` 共计: ${supportedRigs.length} 个型号\n`);
79+
4980
} catch (e) {
5081
console.log(`❌ getSupportedRigs调用失败: ${e.message}`);
5182
testsFailed++;
@@ -92,9 +123,9 @@ try {
92123
const totalMethods = instanceMethods.length + staticMethods.length;
93124

94125
test(`实例方法数量正确 (80个)`, () => instanceMethods.length === 80);
95-
test(`静态方法数量正确 (1个)`, () => staticMethods.length === 1);
96-
test(`总方法数量正确 (81个)`, () => totalMethods === 81);
97-
126+
test(`静态方法数量正确 (2个)`, () => staticMethods.length === 2);
127+
test(`总方法数量正确 (82个)`, () => totalMethods === 82);
128+
98129
console.log(` 📊 实例方法: ${instanceMethods.length}个`);
99130
console.log(` 📊 静态方法: ${staticMethods.length}个`);
100131
console.log(` 📊 总计: ${totalMethods}个方法`);

0 commit comments

Comments
 (0)