track

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 9 Imported by: 2 modules, 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.

Documentation

Overview

Package track holds what every tracker protocol of this module shares: the decoded report (Fix, Alert, Record, Message), the radio networks a device heard (Network, Cell, WiFiAP), the Codec interface each protocol implements, the optional interfaces a server looks for (Correlator, DatagramCodec, DatagramCommander, PasswordCommander, SMSRequester), named commands that mean the same on every protocol (Named, ReadNamed, NamedTemplates), alert types with their severities, and the CRCs the binary protocols use.

A server reads frames from a connection with Codec.Decode, sends Message.Reply back, and frames commands with Codec.Command. Sessions, device lists, queues and storage are the server's business; this package and the protocol packages do no I/O of their own beyond the reader they are given, keep no state outside a codec, and never log.

Index

Examples

Constants

View Source
const (
	ActionEngineStop   = "engineStop"
	ActionEngineResume = "engineResume"
	ActionLocate       = "locate"
	ActionReboot       = "reboot"
	ActionInterval     = "interval"
)

The named actions.

View Source
const (
	Critical = "critical"
	Major    = "major"
	Minor    = "minor"
	Info     = "info"
)

Severities of alerts.

Variables

Actions lists the named actions in catalogue order.

View Source
var ErrSkip = errors.New("frame skipped")

ErrSkip is returned by Decode for a frame that carries nothing (noise the codec resynchronised over); the session reads on.

View Source
var ErrUnsupported = errors.New("not supported by this protocol")

ErrUnsupported marks a named command the protocol has no command for.

Functions

func AnswerFailed

func AnswerFailed(text string, words []string) bool

AnswerFailed reports whether a command answer holds one of the failure words (lower case).

func CRC16IBM

func CRC16IBM(b []byte) uint16

CRC16IBM is CRC-16/ARC: reflected 0x8005, init 0, no final XOR.

func CRCITU

func CRCITU(b []byte) uint16

CRCITU is CRC-16/X-25: reflected 0x1021, init and final XOR 0xFFFF.

func CommandText

func CommandText(content any) (string, error)

CommandText reads the common command forms: a string, or {"text": "…"}.

func Expand

func Expand(tmpl string, n Named, imei, password string) string

Expand fills a template.

func F

func F(v float64) *float64

F and I return pointers, for optional Fix fields.

func FailureWords

func FailureWords(words []string, def ...string) ([]string, error)

FailureWords checks a profile's failureWords: the words (any case) whose presence in a device's command answer marks the command failed. Empty keeps def, the protocol's default list; words must not be blank.

func I

func I(v int) *int

I returns a pointer to v.

func NamedText

func NamedText(kind string, n Named, t NamedTemplates, def map[string]string, iv IntervalRange, imei, password string) (string, error)

NamedText resolves a named command to its text for protocols whose commands are text: the profile's template for the action, else def's. The interval is checked against iv first.

Example

A named command, as an application sends it, becomes a protocol's text.

package main

import (
	"fmt"

	"github.com/cB-Sigma/trackers/track"
)

func main() {
	n, _, err := track.ReadNamed(map[string]any{"action": "interval", "seconds": 60.0})
	if err != nil {
		panic(err)
	}
	defaults := map[string]string{track.ActionInterval: "TIMER,{seconds}#"}
	text, err := track.NamedText("gt06", n, nil, defaults, track.IntervalRange{Min: 10, Max: 18000}, "359710049099999", "")
	fmt.Println(text, err)
}
Output:
TIMER,60# <nil>

func SeverityOf

func SeverityOf(typ string) string

SeverityOf is the severity of an alert type: critical, major, minor or info (unknown types are minor).

func StrictJSON

func StrictJSON(raw []byte, v any) error

StrictJSON decodes a profile, refusing unknown keys.

func Unsupported

func Unsupported(kind, action, why string) error

Unsupported is the error of a named command a protocol lacks; why says what to do instead.

Types

type Alert

type Alert struct {
	Type     string // our name: "emergency", "main-power-removed", "harsh-braking", …
	Severity string
	Code     string // the protocol's own code, when there is one
}

Alert is an event the device reported.

func NewAlert

func NewAlert(typ, code string) Alert

NewAlert is an alert of a known type with its severity.

type Cell

type Cell struct {
	MCC, MNC, LAC int
	CID           int64
	RSSI          *int
}

Cell is one mobile cell. RSSI is in dBm (nil when not reported).

type Codec

type Codec interface {
	// Decode reads the next frame. io.EOF ends the session; ErrSkip reads
	// on; any other error closes the connection.
	Decode(r *bufio.Reader) (*Message, error)
	// Command encodes a command for the device and reports whether the
	// device answers it. content is the command as decoded from JSON: a
	// string, {"text": …}, a named command ({"action": …}) or a
	// protocol's own form.
	Command(imei string, content any, seq uint32) (frame []byte, answers bool, err error)
}

Codec is one connection's protocol state.

type Correlator

type Correlator interface {
	Correlation(content any, seq uint32) (corr string, strict bool)
}

Correlator is implemented by codecs whose command answers name the command they answer: Correlation is the Message.Correlation of the answer to this command ("" when its answer names none). With strict an answer that names no command is not this command's; without it (a device may not echo the name) it is taken.

type DatagramCodec

type DatagramCodec interface {
	DecodeDatagram(b []byte) ([]*Message, error)
}

DatagramCodec is implemented by codecs whose UDP framing differs from their TCP framing (Teltonika).

type DatagramCommander

type DatagramCommander interface {
	DatagramCommands() bool
}

DatagramCommander is implemented by codecs with their own UDP framing (DatagramCodec) that can also frame commands for UDP. A DatagramCodec without it takes commands over TCP only, since its TCP command framing would not be understood in a datagram.

type Duration

type Duration time.Duration

Duration is a profile duration: a string such as "90s" or "15m", or a number of seconds.

func (Duration) D

func (d Duration) D() time.Duration

D is the duration as a time.Duration.

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON writes the duration as a string ("1m30s").

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) error

UnmarshalJSON reads "90s" or 90.

type Fix

type Fix struct {
	Time     time.Time
	Lat, Lon float64
	Valid    bool     // the receiver had a fix
	Alt      *float64 // metres
	Speed    *float64 // km/h
	Heading  *float64 // degrees from north
	Sats     *int
	HDOP     *float64
}

Fix is one position report.

type IntervalRange

type IntervalRange struct{ Min, Max, Step int }

IntervalRange bounds the interval command of a protocol, in seconds; Step, when set, is the unit the protocol counts in (from 0).

func (IntervalRange) Check

func (iv IntervalRange) Check(s int) error

Check bounds an interval.

type Message

type Message struct {
	// IMEI identifies the device; codecs set it on every frame that
	// carries it (a login at least).
	IMEI   string
	Login  bool
	Secret string // a password the login carried, if the protocol has one
	// Raw is the packet as received, for forwarding (codecs that keep it).
	Raw []byte
	// Records are the reports in the frame.
	Records []Record
	// Reply is sent to the device once the frame is accepted; Reject is
	// sent instead when the IMEI is refused (then the connection closes).
	Reply, Reject []byte
	// Response is the device's answer to a command; Failed marks an
	// answer that says the command failed. Details, when set, go into the
	// command's result.
	Response *string
	Failed   bool
	Details  map[string]any
	// Correlation names the command an answer is to, when the protocol's
	// answers carry that (the server flag of a GT06 answer, the operation
	// and keys of an AIS-140 OTA response); "" when it does not. See
	// Correlator.
	Correlation string
	// Kind names the packet kind (login, normal, health, emergency,
	// response), for forward filters and logs.
	Kind string
	// ICCID is the SIM a login named.
	ICCID string
	// ReplyKind and ReplyCode identify the device's answer to an SMS
	// request (SMSRequester).
	ReplyKind, ReplyCode string
	// Warnings are what the codec accepted from a device that deviates
	// from its protocol (a variant IMEI form, an unknown field).
	Warnings []Warning
}

Message is what a codec decoded from one frame.

type Named

type Named struct {
	Action string
	// Seconds is the interval of ActionInterval.
	Seconds int
	// Force sends an engine stop while the vehicle is reported moving.
	Force bool
}

Named is a named command read from command content.

func ReadNamed

func ReadNamed(content any) (Named, bool, error)

ReadNamed reads a named command: ok is false when content has no "action" member (another command form, such as {"text": …}).

type NamedTemplates

type NamedTemplates map[string]string

NamedTemplates is a profile's namedCommands: action → the device command text, with {seconds}, {password} and {imei}. A template replaces the codec's default for that action; "" turns the action off (the device has no such command).

func (NamedTemplates) Check

func (t NamedTemplates) Check(password bool) error

Check refuses unknown actions and placeholders; {password} only where the protocol's commands carry the device's commandPassword.

func (NamedTemplates) Template

func (t NamedTemplates) Template(action, def string) (string, bool)

Template returns the profile's text for action, falling back to def; ok is false when neither has one.

type Network

type Network struct {
	Cells []Cell
	WiFi  []WiFiAP
	// TA is the GSM timing advance, when reported.
	TA *int
}

Network is what a tracker reported about the radio networks around it.

type NewCodec

type NewCodec func(profile json.RawMessage) (func() Codec, error)

NewCodec is what each protocol package exports: it validates a profile (the protocol's JSON settings; empty for the defaults) and returns a factory of codecs, one per connection.

type PasswordCommander

type PasswordCommander interface {
	CommandWithPassword(imei, password string, content any, seq uint32) (frame []byte, answers bool, err error)
}

PasswordCommander is implemented by codecs whose commands carry the device's command password (AIS-140, Queclink, Ruptela). Command is then CommandWithPassword with no password: the profile's, if it has one.

type Record

type Record struct {
	Fix     *Fix
	Status  map[string]any
	Alerts  []Alert
	History bool // stored on the device and sent late
	Time    time.Time
	// Network is the cells and WiFi access points the device reported.
	Network *Network
	// Trailer is the trailer tag the device read ("" none), when the
	// protocol reports one (AIS-140 RFID trackers).
	Trailer *string
}

Record is one report: a position, status values and alerts, any of which may be absent.

type SMSRequester

type SMSRequester interface {
	SMSRequest(content any) (kind string, ok bool)
}

SMSRequester is a codec whose commands may be requests sent to the device by SMS rather than over its connection: it returns the request kind ("HCHK") for such a command.

type Skip

type Skip struct {
	Warning
	// Reply, when set, is sent to the device before reading on (an HTTP
	// codec's 401 to a request without its token).
	Reply []byte
}

Skip is an ErrSkip with a reason the operator should see: the frame is passed over (the session reads on) and the reason is a warning against the device.

func (*Skip) Error

func (s *Skip) Error() string

Error says the frame was skipped, and why.

func (*Skip) Is

func (s *Skip) Is(target error) bool

Is makes a Skip an ErrSkip.

type Warning

type Warning struct {
	Code, Setting, Msg, Detail string
}

Warning is a deviation a codec accepted. Code is a short camel-case name ("imeiFormat"); Setting the dotted profile path that changes the behaviour, if any.

type WiFiAP

type WiFiAP struct {
	MAC  string
	RSSI *int
}

WiFiAP is one WiFi access point: MAC as aa:bb:cc:dd:ee:ff, RSSI in dBm.

Jump to

Keyboard shortcuts

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