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 ¶
- Constants
- Variables
- func AnswerFailed(text string, words []string) bool
- func CRC16IBM(b []byte) uint16
- func CRCITU(b []byte) uint16
- func CommandText(content any) (string, error)
- func Expand(tmpl string, n Named, imei, password string) string
- func F(v float64) *float64
- func FailureWords(words []string, def ...string) ([]string, error)
- func I(v int) *int
- func NamedText(kind string, n Named, t NamedTemplates, def map[string]string, ...) (string, error)
- func SeverityOf(typ string) string
- func StrictJSON(raw []byte, v any) error
- func Unsupported(kind, action, why string) error
- type Alert
- type Cell
- type Codec
- type Correlator
- type DatagramCodec
- type DatagramCommander
- type Duration
- type Fix
- type IntervalRange
- type Message
- type Named
- type NamedTemplates
- type Network
- type NewCodec
- type PasswordCommander
- type Record
- type SMSRequester
- type Skip
- type Warning
- type WiFiAP
Examples ¶
Constants ¶
const ( ActionEngineStop = "engineStop" ActionEngineResume = "engineResume" ActionLocate = "locate" ActionReboot = "reboot" ActionInterval = "interval" )
The named actions.
const ( Critical = "critical" Major = "major" Minor = "minor" Info = "info" )
Severities of alerts.
Variables ¶
var Actions = []string{ActionEngineStop, ActionEngineResume, ActionLocate, ActionReboot, ActionInterval}
Actions lists the named actions in catalogue order.
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.
var ErrUnsupported = errors.New("not supported by this protocol")
ErrUnsupported marks a named command the protocol has no command for.
Functions ¶
func AnswerFailed ¶
AnswerFailed reports whether a command answer holds one of the failure words (lower case).
func CommandText ¶
CommandText reads the common command forms: a string, or {"text": "…"}.
func FailureWords ¶
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 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 ¶
SeverityOf is the severity of an alert type: critical, major, minor or info (unknown types are minor).
func StrictJSON ¶
StrictJSON decodes a profile, refusing unknown keys.
func Unsupported ¶
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.
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 ¶
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 ¶
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 ¶
Duration is a profile duration: a string such as "90s" or "15m", or a number of seconds.
func (Duration) MarshalJSON ¶
MarshalJSON writes the duration as a string ("1m30s").
func (*Duration) UnmarshalJSON ¶
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).
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.
type NamedTemplates ¶
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.
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 ¶
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.