Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Using go formatter for examples.
Mentioned .Close()
  • Loading branch information
josip authored Aug 17, 2023
1 parent 8122266 commit 2cf3c4e
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,51 @@

## Installation

<pre><code># go get github.com/stratoberry/go-gpsd</code></pre>
```
# go get github.com/stratoberry/go-gpsd
```

go-gpsd has no external dependencies.

## Usage

go-gpsd is a streaming client for GPSD's JSON service and as such can be used only in async manner unlike clients for other languages which support both async and sync modes.

<pre><code>import ("github.com/stratoberry/go-gpsd")
```go
import ("github.com/stratoberry/go-gpsd")

func main() {
gps := gpsd.Dial("localhost:2947")
}
</code></pre>
```

After `Dial`ing the server, you should install stream filters. Stream filters allow you to capture only certain types of GPSD reports.

<pre><code>gps.AddFilter("TPV", tpvFilter)</code></pre>
```go
gps.AddFilter("TPV", tpvFilter)
```

Filter functions have a type of `gps.Filter` and should receive one argument of type `interface{}`.

<pre><code>tpvFilter := func(r interface{}) {
```go
tpvFilter := func(r interface{}) {
report := r.(*gpsd.TPVReport)
fmt.Println("Location updated", report.Lat, report.Lon)
}</code></pre>
}
```

Due to the nature of GPSD reports your filter will manually have to cast the type of the argument it received to a proper `*gpsd.Report` struct pointer.

After installing all needed filters, call the `Watch` method to start observing reports. Please note that at this time installed filters can't be removed.

<pre><code>done := gps.Watch()
&lt;-done</code></pre>
```go
done := gps.Watch()
<-done
// ...some time later...
gps.Close()
```

`Watch()` will span a new goroutine in which all data processing will happen, `done` channel won't send anything.
`Watch()` spans a new goroutine in which all data processing will happen, `done` doesn't send anything.

### Currently supported GPSD report types

Expand Down

0 comments on commit 2cf3c4e

Please sign in to comment.