@ canboat/ ts- pgns (英语)
一个综合的TypeScript库,为海洋电子通讯提供强型NMEA 2000参数组号(PGN)定义和公用事业.
目录
概览
该库提供TypeScript定义和公用事业,用于与NMEA 2000参数组编号(PGNs)合作,这是海洋电子网络中使用的标准化数据电文. 它建于Canboat项目PGN定义之上,提供数百个海洋数据电文格式的型号安全访问.
什么是PGNs吗? 参数组号是NMEA 2000协议中的标准化消息格式,定义不同类型海洋数据(GPS位置,引擎数据,气象信息等)如何在网络中构建和传输.
特征
- QQ 类型安全: 有 TypeScript 接口的完整打入的 PGN 类
- 全面覆盖: 支持数百个标准 NMEA 2000 PGN
- 功用函数: PGN 操作和转换的辅助功能
- QQ 编号支持: NMEA 2000 计数和比特字段的内置支持
- 实地验证: 类型安全、有适当验证的外地访问
- QQ 记录良好: 综合联合来文文件文件和实例
- * 转换公用事业: 在不同的 PGN 表达式中转换
- 性能优化: 高效查找和创建 PGN 对象
安装
npm install @canboat/ts-pgns
快速启动
import { PGN_129029, PGN_127488 } from '@canboat/ts-pgns'
import { pgnToActisenseSerialFormat } from '@canboat/canboatjs'
// Create a GPS Position PGN (129029)
const gpsPosition = new PGN_129029({
latitude: 47.6062, // Seattle latitude
longitude: -122.3321, // Seattle longitude
altitude: 56.0,
gnssType: 3, // GPS
method: 1, // GNSS fix
integrity: 0, // No integrity checking
numberOfSvs: 8, // 8 satellites
hdop: 1.2, // Horizontal dilution of precision
geoidalSeparation: -34.0
})
// Create an Engine Parameters PGN (127488)
const engineData = new PGN_127488({
engineInstance: 0,
oilPressure: 275000, // 275 kPa
oilTemperature: 363.15, // 90°C in Kelvin
temperature: 353.15, // 80°C coolant temp
alternatorPotential: 14.2,
fuelRate: 15.5, // L/h
totalEngineHours: 1250.5,
coolantPressure: 95000,
fuelPressure: 280000
})
console.log(
`GPS Position: ${gpsPosition.fields.latitude}, ${gpsPosition.fields.longitude}`
)
console.log(`Engine Oil Pressure: ${engineData.fields.oilPressure} Pa`)
//Generate actisense serial formated position
const n2k = pgnToActisenseSerialFormat(gpsPosition)
console.log(`actisense posistion ${n2k}`)
API 文档
用法实例
使用PGN定义
import {
getPGNWithNumber,
getPGNWithId,
findMatchingDefinition
} from '@canboat/ts-pgns'
// Get PGN definition by number
const windDefs = getPGNWithNumber(130306) // Wind Data
if (windDefs) {
console.log(`Found ${windDefs.length} wind data variants`)
}
// Get specific PGN definition by ID
const gpsPosition = getPGNWithId('gnssPositionData')
if (gpsPosition) {
console.log(`PGN Description: ${gpsPosition.Description}`)
console.log(`Field count: ${gpsPosition.Fields.length}`)
}
使用数字
import {
getEnumerationValue,
getEnumerationName,
getBitEnumerationValue
} from '@canboat/ts-pgns'
// Get numeric value for enumeration
const gnssType = getEnumerationValue('GNSS_TYPE', 'GPS')
console.log(`GPS type value: ${gnssType}`) // 3
// Get name from numeric value
const methodName = getEnumerationName('GNSS_METHOD', 1)
console.log(`Method: ${methodName}`) // "GNSS fix"
// Work with bit enumerations
const alertBit = getBitEnumerationValue('ENGINE_ALERT', 'Warning')
PGN 场操作
import { mapCamelCaseKeys, mapNameKeysToCamelCase } from '@canboat/ts-pgns'
// Convert PGN field IDs to camelCase names
const pgn = new PGN_129029({/* ... */})
const camelCasePgn = mapCamelCaseKeys(pgn)
// Now you can access fields by name instead of ID
console.log(camelCasePgn.fields.latitude) // instead of fields.latitude
console.log(camelCasePgn.fields.longitude) // instead of fields.longitude
创建组函数信件
import {
createNmeaGroupFunction,
GroupFunction,
SeatalkPilotMode16,
PGN_65379_SeatalkPilotMode
} from '@canboat/ts-pgns'
const pgn = new PGN_65379_SeatalkPilotMode({
pilotMode: SeatalkPilotMode16.TrackMode,
subMode: 0xffff
})
// Create a command group function to change this data
const requestMsg = createNmeaGroupFunction(
GroupFunction.Command,
pgn,
42 // destination address
)
PGN 类
该图书馆包括数百个NMEA 2000 PGN的型安全课. 一些常用的包括:
导航定位( P)
PGN_129029- 全球导航卫星系统位置数据PGN_129025- 位置,快速更新PGN_129026- COG & SOG, 快速更新PGN_127250- 船行驶PGN_130306- 风能数据
引擎推进( P)
PGN_127488- 引擎参数,快速更新PGN_127489- 动力引擎参数PGN_127505- 流体水平PGN_130312- 温度
电气系统
PGN_127508- 电池状况PGN_127506- DC详细状况PGN_127513- 电池配置状态
环境
PGN_130310- 环境参数PGN_130311- 环境参数PGN_130316- 温度,扩展范围
每个PGN类提供:
- 类型安全实地访问
- 合适的类型脚本接口
- 内建验证
- 获取基本的PGN定义
公用事业
核心公用事业
createPGN(id, fields, dst?)- 通过 ID 创建 PGN 实例findMatchingDefinition(pgn)- 找到PGN的匹配定义getPGNWithNumber(num)- 按数字获得PGN定义getPGNWithId(id)- 通过身份识别获得PGN定义
外地转换
mapCamelCaseKeys(pgn)- 将字段ID转换为骆驼Case名称mapNameKeysToCamelCase(pgn)- 将字段名称转换为骆驼Case IDisMatch(pgn, matchFields)- 检查 PGN 是否匹配特定的字段值
编号帮助
getEnumeration(name)编号 :getEnumerations()- 查阅查点getEnumerationValue(enumName, value)- 从名称中获取数字值getEnumerationName(enumName, value)- 从数值中获取名称getBitEnumeration(name)- 访问比特点数getBitEnumerationValue(enumName, value)- 从名称中获取比特值
信号K 服务器集成
convertCamelCase(pluginApp, pgn)- 根据服务器能力转换密钥convertNamesToCamel(pluginApp, pgn)- 根据服务器能力转换名称
类型安全
此库提供了全面的 TypeScript 支持 :
// Strongly typed fields
const gps = new PGN_129029({
latitude: 47.6062, // number
longitude: -122.3321, // number
gnssType: 3, // number (enumeration)
method: 1 // number (enumeration)
})
// TypeScript will catch field type errors
const badGps = new PGN_129029({
latitude: 'invalid', // ❌ TypeScript error: string not assignable to number
gnssType: 'GPS' // ❌ TypeScript error: string not assignable to number
})
// Proper enumeration usage
import { GnssType } from '@canboat/ts-pgns'
const correctGps = new PGN_129029({
latitude: 47.6062,
longitude: -122.3321,
gnssType: GnssType.Gps, // ✅ Type-safe enumeration
method: 1
})
来源建筑
# Clone the repository
git clone https://github.com/canboat/ts-pgns.git
cd ts-pgns
# Install dependencies
npm install
# Build the project
npm run build
# Generate the typescript files if you have changed canboat.json
npm run generate
npm run build
# Run tests
npm test
# Format code
npm run format
捐款
我们欢迎捐款! 请看我们的 贡献准则 详细情况.
发展 工作流量
- 叉子存储器
- 创建特性分支 :
git checkout -b feature/amazing-feature - 做你的改变
- 运行测试 :
npm test - 运行更漂亮和提示 :
npm run format - 提交更改 :
git commit -m 'Add amazing feature' - 推到树枝上:
git push origin feature/amazing-feature - 打开拉请求
版本政策
重要说明: 该图书馆目前正在积极发展。 微量释放可能包括破解更改. 请钉入特定的次要版本(例如, 1.10.x在你的依赖.
一旦API稳定下来,将只在主要释放中引入突破性变化.
{
"dependencies": {
"@canboat/ts-pgns": "~1.10.0" // Pin to minor version
}
}
许可证
根据Apache许可证第2.0版获得许可. 见 自由主义 详细情况.
相关项目
由Canboat项目捐助者制作