120 lines
3.1 KiB
Fennel
120 lines
3.1 KiB
Fennel
;; each sentence is introduced by $ and terminated by *
|
|
;; first two chars are "talker id"
|
|
;; 3-5 are the message type
|
|
;; fields are comma-delimited
|
|
|
|
;; we assume that we can parse the message based only on type
|
|
;; (i.e. that the same type should always be parsed the same way no
|
|
;; matter who the talker)
|
|
|
|
;; outline: split message into lines, extract values between
|
|
;; $ and * as talker, type, fields
|
|
;; then call the parser for the type
|
|
|
|
(import-macros { : define-tests : expect : expect= } :assert)
|
|
|
|
(fn split-line [line]
|
|
(let [(talker msgtype rest) (string.match line "$(..)(...)(.+)*")
|
|
fields (icollect [k _ (string.gmatch rest ",([^,]*)")] k)]
|
|
{ : talker :message-type msgtype :fields fields }))
|
|
|
|
|
|
|
|
(expect=
|
|
(split-line "$GLGSV,2,1,07,78,47,295,34,77,35,208,19,88,36,267,30,87,67,025,27,1*74\n")
|
|
{
|
|
:talker "GL"
|
|
:message-type "GSV"
|
|
:fields ["2" "1" "07" "78" "47" "295" "34" "77" "35" "208" "19" "88"
|
|
"36" "267" "30" "87" "67" "025" "27" "1"
|
|
]
|
|
}
|
|
)
|
|
|
|
(expect=
|
|
(split-line "$GNGNS,111134.00,5131.348976,N,00005.551003,W,AAAANN,19,0.7,20.3,47.0,,,V*0C\n")
|
|
{
|
|
:talker "GN"
|
|
:message-type "GNS"
|
|
:fields ["111134.00" ; utc
|
|
"5131.348976" "N"
|
|
"00005.551003" "W"
|
|
"AAAANN"
|
|
"19" ; number of satellites
|
|
"0.7" ; hdop
|
|
"20.3" ; height
|
|
"47.0" ; geoidal separation
|
|
"" "" ; differential data age, reference station
|
|
"V" ; not valid for navigation (?)
|
|
]
|
|
}
|
|
)
|
|
|
|
(local latlon-signs { :N 1 :S -1 :W -1 :E 1 } )
|
|
|
|
(local
|
|
msg-types
|
|
{
|
|
:GNS
|
|
(fn [fields]
|
|
(let [[utc lat latsign lon lonsign mode total-space-vehicles
|
|
hdop altitude geoidal-separation _ _ nav-status] fields]
|
|
{ :lat (* (/ (tonumber lat) 100) (. latlon-signs latsign))
|
|
:lon (* (/ (tonumber lon) 100) (. latlon-signs lonsign))
|
|
: mode
|
|
:total-space-vehicles (tonumber total-space-vehicles)
|
|
:hdop (tonumber hdop)
|
|
:altitude (tonumber altitude)
|
|
:geoidal-separation (tonumber geoidal-separation)
|
|
: nav-status
|
|
}
|
|
))
|
|
:VTG
|
|
(fn [[bearing-true _ bearing-mag _ knots _ kmh _ mode]]
|
|
{
|
|
:bearing-true (tonumber bearing-true)
|
|
:bearing-magnetic (tonumber bearing-mag)
|
|
:speed-knots (tonumber knots)
|
|
:speed-kmh (tonumber kmh)
|
|
: mode
|
|
}
|
|
)
|
|
})
|
|
|
|
|
|
(fn parse-line [line]
|
|
(let [{ : message-type : talker : fields &as split} (split-line line)
|
|
parser (. msg-types message-type)]
|
|
(if parser
|
|
(doto (parser fields)
|
|
(tset :message-type message-type)
|
|
(tset :talker talker))
|
|
split)))
|
|
|
|
(expect=
|
|
(parse-line
|
|
"$GNGNS,111134.00,5131.348976,N,00005.551003,W,AAAANN,19,0.7,20.3,47.0,,,V*0C\n")
|
|
{:altitude 20.3
|
|
:geoidal-separation 47
|
|
:hdop 0.7
|
|
:lat 51.31348976
|
|
:lon -0.05551003
|
|
:message-type "GNS"
|
|
:mode "AAAANN"
|
|
:nav-status "V"
|
|
:talker "GN"
|
|
:total-space-vehicles 19})
|
|
|
|
(expect=
|
|
(parse-line
|
|
"$GNVTG,263.3,T,266.5,M,1.6,N,2.9,K,A*32\n")
|
|
{
|
|
:talker :GN
|
|
:message-type :VTG
|
|
:bearing-true 263.3
|
|
:bearing-magnetic 266.5
|
|
:speed-knots 1.6
|
|
:speed-kmh 2.9
|
|
:mode :A
|
|
})
|