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: Add NMEA stream handling to Course API. #1750

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
82 changes: 76 additions & 6 deletions docs/src/develop/rest-api/course_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

The _Course API_ provides common course operations under the path `/signalk/v2/api/vessels/self/navigation/course` ensuring that all related Signal K data model values are maintained and consistent. This provides a set of data that can be confidently used for _course calculations_ and _autopilot operation_.

Additionally, the Course API persists course information on the server to ensure data is not lost in the event of a server restart.

Client applications use `HTTP` requests (`PUT`, `GET`,`DELETE`) to perform operations and retrieve course data.

Additionally, the Course API persists course information on the server to ensure data is not lost in the event of a server restart.
The Course API also listens for destination information in the NMEA stream and will set / clear the destination accordingly _(e.g. NMEA0183 RMB sentence, NMEA2000 PGN 129284)_. See [Configuration](#Configuration) for more details.

_Note: You can view the _Course API_ OpenAPI definition in the Admin UI (Documentation => OpenApi)._

Expand Down Expand Up @@ -125,6 +127,7 @@ HTTP GET 'http://hostname:3000/signalk/v2/api/vessels/self/navigation/course'

The contents of the response will reflect the operation used to set the current course. The `nextPoint` & `previousPoint` sections will always contain values but `activeRoute` will only contain values when a route is being followed.


#### 1. Operation: Navigate to a location _(lat, lon)_

_Example response:_
Expand Down Expand Up @@ -235,19 +238,86 @@ _Example response:_

## Cancelling navigation

To cancel the current course navigation and clear the course data
To cancel the current course navigation and clear the course data.

```typescript
HTTP DELETE 'http://hostname:3000/signalk/v2/api/vessels/self/navigation/course/'
HTTP DELETE 'http://hostname:3000/signalk/v2/api/vessels/self/navigation/course'
```

_Note: This operation will NOT change the destination information coming from the NMEA input stream! If the NMEA source device is still emitting destination data this will reappear as the current destination._

To ignore destination data from NMEA sources see [Configuration](#configuration) below.



## Configuration

The default configuration of the Course API will accept destination information from both API requests and NMEA stream data sources.

For NMEA sources, Course API monitors the the following Signal K paths populated by both the `nmea0183-to-signalk` and `n2k-to-signalk` plugins:
- _navigation.courseRhumbline.nextPoint.position_
- _navigation.courseGreatCircle.nextPoint.position_

HTTP requests are prioritised over NMEA data sources, so making an API request will overwrite the destination information received from and NMEA source.

Note: when the destination cleared using an API request, if the NMEA stream is emitting an active destination position, this will then be used by the Course API to populate course data.


#### Ignoring NMEA Destination Information

The Course API can be configured to ignore destination data in the NMEA stream by enabling `apiOnly` mode.

In `apiOnly` mode destination information can only be set / cleared using HTTP API requests.

- **`apiOnly` Mode = Off _(default)_**

- Destination data is accepted from both _HTTP API_ and _NMEA_ sources
- Setting a destination using the HTTP API will override the destination data received via NMEA
- When clearing the destination using the HTTP API, if destination data is received via NMEA this will then be used as the active destination.
- To clear destination sourced via NMEA, clear the destination on the source device.

- **`apiOnly` Mode = On**

- Course operations are only accepted via the HTTP API
- NMEA stream data is ignored
- Switching to `apiOnly` mode when an NMEA sourced destination is active will clear the destination.


#### Retrieving Course API Configuration

To retrieve the Course API configuration settings, submit a HTTP `GET` request to `/signalk/v2/api/vessels/self/navigation/course/_config`.

```typescript
HTTP GET 'http://hostname:3000/signalk/v2/api/vessels/self/navigation/course/_config'
```

_Example response:_
```JSON
{
"apiOnly": false
}
```

#### Enable / Disable `apiOnly` mode

To enable `apiOnly` mode, submit a HTTP `POST` request to `/signalk/v2/api/vessels/self/navigation/course/_config/apiOnly`.

_Enable apiOnly mode:_
```typescript
HTTP POST 'http://hostname:3000/signalk/v2/api/vessels/self/navigation/course/_config/apiOnly'
```

To disable `apiOnly` mode, submit a HTTP `DELETE` request to `/signalk/v2/api/vessels/self/navigation/course/_config/apiOnly`.

_Disable apiOnly mode:_
```typescript
HTTP DELETE 'http://hostname:3000/signalk/v2/api/vessels/self/navigation/course/_config/apiOnly'
```

---

## Course Calculations

Whilst not performing course calculations, the _Course API_ defines the paths to accommodate the values calculated during course navigation.

Click [here](./course_calculations.md) for details.



43 changes: 43 additions & 0 deletions packages/server-admin-ui/src/views/ServerConfig/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ServerSettings extends Component {
}
this.fetchSettings = fetchSettings.bind(this)
this.handleChange = this.handleChange.bind(this)
this.handleCourseApiChange = this.handleCourseApiChange.bind(this)
this.handleOptionChange = this.handleOptionChange.bind(this)
this.handleInterfaceChange = this.handleInterfaceChange.bind(this)
this.handleSaveSettings = this.handleSaveSettings.bind(this)
Expand All @@ -53,6 +54,15 @@ class ServerSettings extends Component {
this.setState({ [event.target.name]: value })
}

handleCourseApiChange(event) {
const value =
event.target.type === 'checkbox'
? event.target.checked
: event.target.value
this.state.courseApi[event.target.name] = value
this.setState({ courseApi: this.state.courseApi })
}

handleOptionChange(event) {
const value =
event.target.type === 'checkbox'
Expand Down Expand Up @@ -303,6 +313,39 @@ class ServerSettings extends Component {
</FormText>
</Col>
</FormGroup>
<FormGroup row>
<Col md="2">
<Label>
API Only Mode
<br />
<i>(Course API)</i>
</Label>
</Col>
<Col>
<FormGroup check>
<Label className="switch switch-text switch-primary">
<Input
type="checkbox"
name="apiOnly"
id="apiOnly"
className="switch-input"
onChange={this.handleCourseApiChange}
checked={this.state.courseApi.apiOnly}
/>
<span
className="switch-label"
data-on="On"
data-off="Off"
/>
<span className="switch-handle" />
</Label>
<FormText color="muted">
Accept course operations only via HTTP requests.
Destination data from NMEA sources is not used.
</FormText>
</FormGroup>
</Col>
</FormGroup>
</Form>
</CardBody>
<CardFooter>
Expand Down
8 changes: 8 additions & 0 deletions packages/server-api/src/deltas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ export type Update = {
$source?: SourceRef
} & ({ values: PathValue[] } | { meta: Meta[] }) // require either values or meta or both

export function hasValues(u: Update): u is Update & { values: PathValue[] } {
return 'values' in u && Array.isArray(u.values)
}

export function hasMeta(u: Update): u is Update & { meta: Meta[] } {
return 'meta' in u && Array.isArray(u.meta)
}

// Update delta
export interface PathValue {
path: Path
Expand Down
Loading