Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: introducing optional canbus Mfg code, fixing canbus provider UniqueNumber logic #1831

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions packages/server-admin-ui/src/views/ServerConfig/BasicProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,13 +794,29 @@ const NMEA2000 = (props) => {
)}
{(props.value.options.type === 'canbus' ||
props.value.options.type === 'canbus-canboatjs') && (
<TextInput
title="Interface"
name="options.interface"
helpText="Example: can0"
value={props.value.options.interface}
onChange={props.onChange}
/>
<div>
<TextInput
title="Interface"
name="options.interface"
helpText="Example: can0"
value={props.value.options.interface}
onChange={props.onChange}
/>
<TextInput
title="UniqueNumber"
name="options.uniqueNumber"
helpText="Example: any number from 1 to 2097151, will be equal to SerialNumber of a SignalK NMEA2000 device. Leave empty for random (default). Set a fixed value if you have problem with source identification on some B&G MFD's after SignalK restart."
value={props.value.options.uniqueNumber}
onChange={props.onChange}
/>
<TextInput
title="ManufacturerCode"
name="options.mfgCode"
helpText="Example: 999 - Unknown (default), 0 - Internal, or any other mabufacturer code to emulate. Leave empty for default 999. Set to 0 if you have problem with source identification on some B&G MFD's after SignalK restart."
value={props.value.options.mfgCode}
onChange={props.onChange}
/>
</div>
)}
{(props.value.options.type === 'ngt-1-canboatjs' ||
props.value.options.type === 'ikonvert-canboatjs' ||
Expand Down
29 changes: 24 additions & 5 deletions src/interfaces/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,18 @@ module.exports = function (app) {
]
}

if (
provider.options.type === 'canbus-canboatjs' &&
!provider.options.uniqueNumber
) {
provider.options.uniqueNumber = Math.floor(Math.random() * 2097151)
if (provider.options.type === 'canbus-canboatjs') {
if (isNumber(provider.options.uniqueNumber)) {
provider.options.uniqueNumber = parseInt(provider.options.uniqueNumber)
} else {
provider.options.uniqueNumber = Math.floor(Math.random() * 2097151)
}

if (isNumber(provider.options.mfgCode)) {
provider.options.mfgCode = parseInt(provider.options.mfgCode)
} else {
if (provider.options.mfgCode !== '') delete provider.options.mfgCode //if value is not empty or not a number then removing property
}
}

if (applyProviderSettings(updatedProvider, provider, res)) {
Expand All @@ -168,6 +175,18 @@ module.exports = function (app) {
}
}

function isNumber(s) {
mrstas marked this conversation as resolved.
Show resolved Hide resolved
return typeof s == 'number'
? true
: typeof s == 'string'
? s.trim() === ''
? false
: !isNaN(s)
: (typeof s).match(/object|function/)
? false
: !isNaN(s)
}

function applyProviderSettings(target, source, res) {
if (source.type === 'Unknown') {
res.status(401).send('Can not update an Unknown type')
Expand Down