gpsd

package module
v0.0.0-...-f5dd3ab Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2021 License: MIT Imports: 10 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-gpsd

GPSD client for Golang without CGO and additional dependencies.

It provides interface similar to the official C, C++ and Python libraries.

Tested with GPSD 3.16, proto version 3.11.

Usage

The following example illustrates basic concepts and how to use this library:

package main

import (
	"errors"
	"flag"
	"fmt"
	"os"

	"github.com/amenzhinsky/go-gpsd"
)

var addrFlag string

func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [FLAG...]\n", os.Args[0])
		flag.PrintDefaults()
	}
	flag.StringVar(&addrFlag, "addr", gpsd.DefaultAddress, "address to connect to")
	flag.Parse()
	if flag.NArg() != 0 {
		flag.Usage()
		os.Exit(2)
	}
	if err := mon(); err != nil {
		fmt.Fprintf(os.Stderr, "error: %s\n", err)
		os.Exit(1)
	}
}

func mon() error {
	g, err := gpsd.Dial(addrFlag)
	if err != nil {
		return err
	}
	defer g.Close()

	if err := g.Stream(gpsd.WATCH_ENABLE|gpsd.WATCH_JSON, ""); err != nil {
		return err
	}
	defer g.Stream(gpsd.WATCH_DISABLE, "")

	for v := range g.C() {
		switch t := v.(type) {
		case *gpsd.VERSION:
			fmt.Printf("GPSD Version: %s, Proto: %.0f.%.0f\n", t.Release, t.ProtoMajor, t.ProtoMinor)
		case *gpsd.DEVICES:
			if len(t.Devices) == 0 {
				return errors.New("no devices available")
			}
			fmt.Println("Available devices:")
			for _, d := range t.Devices {
				fmt.Printf("\t%s\n", d.Path)
			}
		case *gpsd.TPV:
			if t.Lat != 0 && t.Lon != 0 {
				fmt.Printf("%.9f %.9f\n", t.Lat, t.Lon)
			} else {
				fmt.Println("n/a")
			}
		}
	}
	return g.Err()
}

Testing

GPSD address TEST_ADDR should be set to enable integrated testing, e.g. TEST_ADDR=localhost:2947 go test.

Debugging

A positive value of DEBUG or DEBUG_GPSD enables debug output, unless you want to use a custom logger that can be set with WithLogger(logger) option.

Contributing

Any contributions are welcome, just fork the repo and open a pull request or simply file an issue.

Documentation

Index

Constants

View Source
const (
	// WATCH_ENABLE enable streaming.
	WATCH_ENABLE = 0x0001

	// WATCH_DISABLE disable watching.
	WATCH_DISABLE = 0x0002

	// WATCH_JSON JSON output.
	WATCH_JSON = 0x0010

	// WATCH_NMEA output in NMEA.
	WATCH_NMEA = 0x0020

	// WATCH_RARE output of packets in hex.
	WATCH_RARE = 0x0040

	// WATCH_RAW output of raw packets.
	WATCH_RAW = 0x0080

	// WATCH_SCALED scale output to floats.
	WATCH_SCALED = 0x0100

	// WATCH_TIMING timing information.
	WATCH_TIMING = 0x0200

	// WATCH_DEVICE watch specific device.
	WATCH_DEVICE = 0x0800

	// WATCH_SPLIT24 split AIS Type 24s.
	WATCH_SPLIT24 = 0x1000

	// WATCH_PPS enable PPS JSON.
	WATCH_PPS = 0x2000
)
View Source
const DefaultAddress = ":2947"

DefaultAddress is the default address gpsd listens to.

Variables

View Source
var ErrClosed = errors.New("closed")

ErrClosed signalizes that client is closed.

Functions

This section is empty.

Types

type ATT

type ATT struct {
	Class   string    `json:"class"`
	Device  string    `json:"device"`
	Time    time.Time `json:"time,omitempty"`
	Heading float64   `json:"heading,omitempty"`
	MagSt   string    `json:"mag_st,omitempty"`
	Pitch   float64   `json:"pitch,omitempty"`
	PitchSt string    `json:"pitch_st,omitempty"`
	Yaw     float64   `json:"yaw,omitempty"`
	YawSt   string    `json:"yaw_st,omitempty"`
	Roll    float64   `json:"roll,omitempty"`
	RollSt  string    `json:"roll_st,omitempty"`
	Dip     float64   `json:"dip,omitempty"`
	MagLen  float64   `json:"mag_len,omitempty"`
	MagX    float64   `json:"mag_x,omitempty"`
	MagY    float64   `json:"mag_y,omitempty"`
	MagZ    float64   `json:"mag_z,omitempty"`
	AccLen  float64   `json:"acc_len,omitempty"`
	AccX    float64   `json:"acc_x,omitempty"`
	AccY    float64   `json:"acc_y,omitempty"`
	AccZ    float64   `json:"acc_z,omitempty"`
	GyroX   float64   `json:"gyro_x,omitempty"`
	GyroY   float64   `json:"gyro_y,omitempty"`
	Depth   float64   `json:"depth,omitempty"`
	Temp    float64   `json:"temp,omitempty"`
}

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a gpsd client implementation.

https://gpsd.gitlab.io/gpsd/gpsd_json.html https://gpsd.gitlab.io/gpsd/client-howto.html

func Dial

func Dial(addr string, opts ...Option) (*Client, error)

Dial dials the named address and returns a gpsd client for the connection.

func New

func New(opts ...Option) (*Client, error)

New creates a new gpsd client.

func (*Client) C

func (c *Client) C() <-chan Report

C returns a channel where report structures can be read from.

func (*Client) Close

func (c *Client) Close() error

Close closes underlying gpsd connection.

func (*Client) Done

func (c *Client) Done() <-chan struct{}

Done reports where the client is stopped.

func (*Client) Err

func (c *Client) Err() error

Err returns client errors, makes sense to call it only when C is closed.

func (*Client) Send

func (c *Client) Send(b []byte) error

Send sends the given raw data to gpsd, use it only if you know what you're doing.

Do not use marshalled report structs here, because they're supposed to be used to receive data from the daemon only.

func (*Client) Stream

func (c *Client) Stream(flags WatchFlag, devpath string) error

Stream changes watch policy the second argument can only be a device path and it only implies WATCH_DEVICE flag.

type DEVICE

type DEVICE struct {
	Class string `json:"class"`

	// can be string whet it's a member of DEVICES and float64 when used separately
	Activated interface{} `json:"activated,omitempty"`
	Path      string      `json:"path,omitempty"`
	Flags     float64     `json:"flags,omitempty"`
	Driver    string      `json:"driver,omitempty"`
	Subtype   string      `json:"subtype,omitempty"`
	Bps       float64     `json:"bps,omitempty"`
	Parity    string      `json:"parity,omitempty"`
	Stopbits  float64     `json:"stopbits"`
	Native    float64     `json:"native,omitempty"`
	Cycle     float64     `json:"cycle,omitempty"`
	Mincycle  float64     `json:"mincycle,omitempty"`
}

type DEVICES

type DEVICES struct {
	Class   string   `json:"class"`
	Devices []DEVICE `json:"devices"`
	Remote  string   `json:"remote,omitempty"`
}

type ERROR

type ERROR struct {
	Class   string `json:"class"`
	Message string `json:"message"`
}

type GST

type GST struct {
	Class  string    `json:"class"`
	Device string    `json:"device,omitempty"`
	Time   time.Time `json:"time,omitempty"`
	Rms    float64   `json:"rms,omitempty"`
	Major  float64   `json:"major,omitempty"`
	Minor  float64   `json:"minor,omitempty"`
	Orient float64   `json:"orient,omitempty"`
	Lat    float64   `json:"lat,omitempty"`
	Lon    float64   `json:"lon,omitempty"`
	Alt    float64   `json:"alt,omitempty"`
}

type Logger

type Logger interface {
	Errorf(format string, v ...interface{})
	Debugf(format string, v ...interface{})
}

Logger gives ability to use different logger implementations.

type OSC

type OSC struct {
	Class       string  `json:"class"`
	Device      string  `json:"device"`
	Running     bool    `json:"running"`
	Reference   bool    `json:"reference"`
	Disciplined bool    `json:"disciplined"`
	Delta       float64 `json:"delta"`
}

type Option

type Option func(c *Client)

Option is a client configuration option.

func WithChannel

func WithChannel(ch chan Report) Option

WithChannel changes reports channel for adjusting buffer size, default is 10.

func WithConn

func WithConn(conn net.Conn) Option

WithConn sets client connection.

func WithLogger

func WithLogger(logger Logger) Option

WithLogger sets logger.

type POLL

type POLL struct {
	Class  string    `json:"class"`
	Time   time.Time `json:"time"`
	Active float64   `json:"active"`
	Tpv    []TPV     `json:"tpv"`
	Sky    []SKY     `json:"sky"`
	Gst    []GST     `json:"gst"`
}

type PPS

type PPS struct {
	Class     string  `json:"class"`
	Device    string  `json:"device"`
	RealSec   float64 `json:"real_sec"`
	RealNSec  float64 `json:"real_nsec"`
	ClockSec  float64 `json:"clock_sec"`
	ClockNSec float64 `json:"clock_nsec"`
	Precision float64 `json:"precision"`
}

type RAW

type RAW []byte

type Report

type Report interface {
	// contains filtered or unexported methods
}

Report is a grouping interface for all types of reports.

type SKY

type SKY struct {
	Class      string      `json:"class"`
	Device     string      `json:"device,omitempty"`
	Time       time.Time   `json:"time,omitempty"`
	Xdop       float64     `json:"xdop,omitempty"`
	Ydop       float64     `json:"ydop,omitempty"`
	Vdop       float64     `json:"vdop,omitempty"`
	Tdop       float64     `json:"tdop,omitempty"`
	Hdop       float64     `json:"hdop,omitempty"`
	Pdop       float64     `json:"pdop,omitempty"`
	Gdop       float64     `json:"gdop,omitempty"`
	Satellites []Satellite `json:"satellites"`
}

type Satellite

type Satellite struct {
	PRN  float64 `json:"PRN"`
	Az   float64 `json:"az"`
	El   float64 `json:"el"`
	Ss   float64 `json:"ss"`
	Used bool    `json:"used"`
}

type TOFF

type TOFF struct {
	Class     string  `json:"class"`
	Device    string  `json:"device"`
	RealSec   float64 `json:"real_sec"`
	RealNSec  float64 `json:"real_nsec"`
	ClockSec  float64 `json:"clock_sec"`
	ClockNSec float64 `json:"clock_nsec"`
}

type TPV

type TPV struct {
	Class  string    `json:"class"`
	Device string    `json:"device,omitempty"`
	Status float64   `json:"status,omitempty"`
	Mode   float64   `json:"mode"`
	Time   time.Time `json:"time,omitempty"`
	Ept    float64   `json:"ept,omitempty"`
	Lat    float64   `json:"lat,omitempty"`
	Lon    float64   `json:"lon,omitempty"`
	Alt    float64   `json:"alt,omitempty"`
	Eph    float64   `json:"eph,omitempty"`
	Epx    float64   `json:"epx,omitempty"`
	Epy    float64   `json:"epy,omitempty"`
	Epv    float64   `json:"epv,omitempty"`
	Track  float64   `json:"track,omitempty"`
	Speed  float64   `json:"speed,omitempty"`
	Climb  float64   `json:"climb,omitempty"`
	Epd    float64   `json:"epd,omitempty"`
	Eps    float64   `json:"eps,omitempty"`
	Epc    float64   `json:"epc,omitempty"`
}

type VERSION

type VERSION struct {
	Class      string  `json:"class"`
	Release    string  `json:"release"`
	Rev        string  `json:"rev"`
	ProtoMajor float64 `json:"proto_major"`
	ProtoMinor float64 `json:"proto_minor"`
	Remove     string  `json:"remote,omitempty"`
}

type WATCH

type WATCH struct {
	Class   string  `json:"class"`
	Enable  bool    `json:"enable,omitempty"`
	Json    bool    `json:"json,omitempty"`
	Nmea    bool    `json:"nmea,omitempty"`
	Raw     float64 `json:"raw,omitempty"`
	Scaled  bool    `json:"scaled,omitempty"`
	Split24 bool    `json:"split24,omitempty"`
	Pps     bool    `json:"pps,omitempty"`
	Device  string  `json:"device,omitempty"`
	Remote  string  `json:"remote,omitempty"`
}

type WatchFlag

type WatchFlag uint32

WatchFlag configures watch parameters.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL