Skip to content

Commit

Permalink
update: resources-provider config options for charts (#1809)
Browse files Browse the repository at this point in the history
* Add chart to config
  • Loading branch information
panaaj authored Oct 28, 2024
1 parent 8d16277 commit 1081de8
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 34 deletions.
6 changes: 5 additions & 1 deletion packages/resources-provider-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# CHANGELOG: RESOURCES-PROVIDER

___Note: Signal K server on which this plugin is installed must implement the `ResourceProvider API`.___
___Note: Can only be used with Signal K server version 2.0.0 or later.___

---

## v1.2.0

- **Update**: Update plugin configuration to include `charts`.

## v1.1.2

- **Update**: Change plugin category keyword to `signalk-category-utility`.
Expand Down
2 changes: 2 additions & 0 deletions packages/resources-provider-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ _Example:_
...
/regions
...
/charts
...
/my_custom_type
...
```
Expand Down
2 changes: 1 addition & 1 deletion packages/resources-provider-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@signalk/resources-provider",
"version": "1.1.2",
"version": "1.2.0",
"description": "Resources provider plugin for Signal K server.",
"main": "plugin/index.js",
"keywords": [
Expand Down
111 changes: 79 additions & 32 deletions packages/resources-provider-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import {
Plugin,
ServerAPI,
ResourceProviderRegistry
ResourceProviderRegistry,
SIGNALKRESOURCETYPES
} from '@signalk/server-api'

import { FileStore, getUuid } from './lib/filestorage'
import { StoreRequestParams } from './types'

interface ResourceProviderApp extends ServerAPI, ResourceProviderRegistry {}

interface ProviderSettings {
standard: {
routes: boolean
waypoints: boolean
notes: boolean
regions: boolean
charts: boolean
}
custom: Array<{ name: string }>
}

const CONFIG_SCHEMA = {
properties: {
standard: {
Expand All @@ -32,6 +44,10 @@ const CONFIG_SCHEMA = {
regions: {
type: 'boolean',
title: 'REGIONS'
},
charts: {
type: 'boolean',
title: 'CHART SOURCES'
}
}
},
Expand Down Expand Up @@ -75,6 +91,11 @@ const CONFIG_UISCHEMA = {
'ui:widget': 'checkbox',
'ui:title': ' ',
'ui:help': '/signalk/v2/api/resources/regions'
},
charts: {
'ui:widget': 'checkbox',
'ui:title': ' ',
'ui:help': '/signalk/v2/api/resources/charts'
}
}
}
Expand All @@ -85,39 +106,21 @@ module.exports = (server: ResourceProviderApp): Plugin => {
name: 'Resources Provider (built-in)',
schema: () => CONFIG_SCHEMA,
uiSchema: () => CONFIG_UISCHEMA,
start: (options) => {
doStartup(options)
start: (settings) => {
doStartup(settings as ProviderSettings)
},
stop: () => {
doShutdown()
}
}

const db: FileStore = new FileStore(plugin.id, server.debug)
let config: ProviderSettings

let config = {
standard: {
routes: true,
waypoints: true,
notes: true,
regions: true
},
custom: [],
path: './resources'
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const doStartup = (options: any) => {
const doStartup = (settings: ProviderSettings) => {
try {
server.debug(`${plugin.name} starting.......`)
if (options && options.standard) {
config = options
} else {
// save defaults if no options loaded
server.savePluginOptions(config, () => {
server.debug(`Default configuration applied...`)
})
}
config = cleanConfig(settings)
server.debug(`Applied config: ${JSON.stringify(config)}`)

// compile list of enabled resource types
Expand All @@ -129,8 +132,7 @@ module.exports = (server: ResourceProviderApp): Plugin => {
})

if (config.custom && Array.isArray(config.custom)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const customTypes = config.custom.map((i: any) => {
const customTypes = config.custom.map((i: { name: string }) => {
return i.name
})
apiProviderFor = apiProviderFor.concat(customTypes)
Expand All @@ -156,12 +158,13 @@ module.exports = (server: ResourceProviderApp): Plugin => {
// register as provider for enabled resource types
const result = registerProviders(apiProviderFor)

const msg =
result.length !== 0
? `${result.toString()} not registered!`
: `Providing: ${apiProviderFor.toString()}`

server.setPluginStatus(msg)
if (result.length !== 0) {
server.setPluginError(
`Error registering providers: ${result.toString()}`
)
} else {
server.setPluginStatus(`Providing: ${apiProviderFor.toString()}`)
}
})
.catch((e: Error) => {
server.debug(e.message)
Expand All @@ -182,6 +185,50 @@ module.exports = (server: ResourceProviderApp): Plugin => {
server.setPluginStatus(msg)
}

/** process changes in config schema */
const cleanConfig = (options: ProviderSettings): ProviderSettings => {
server.debug(`Check / Clean loaded settings...`)

const defaultConfig: ProviderSettings = {
standard: {
routes: true,
waypoints: true,
notes: true,
regions: true,
charts: true
},
custom: []
}

// set / save defaults if no saved settings
if (!options?.standard) {
server.savePluginOptions(defaultConfig, () => {
server.debug(`Default configuration applied...`)
})
return defaultConfig
}

// check / clean settings
if (!Array.isArray(options?.custom)) {
options.custom = []
}
SIGNALKRESOURCETYPES.forEach((r) => {
if (!(r in options.standard)) {
options.standard[r] = true
}
})

options.custom = options.custom.filter(
(i) => !(i.name in defaultConfig.standard)
)

server.savePluginOptions(options, () => {
server.debug(`Configuration cleaned and saved...`)
})

return options
}

const getVesselPosition = () => {
const p = server.getSelfPath('navigation.position')
return p && p.value ? [p.value.longitude, p.value.latitude] : null
Expand Down

0 comments on commit 1081de8

Please sign in to comment.