mirror of
https://github.com/renorris/openfsd
synced 2026-03-31 03:25:34 +08:00
add metar request/response to protocol
This commit is contained in:
43
protocol/metar_request.go
Normal file
43
protocol/metar_request.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MetarRequestPDU struct {
|
||||
From string `validate:"required,alphanum,max=7"`
|
||||
To string `validate:"required,alphanum,max=7"`
|
||||
Station string `validate:"required,alphanum,max=4"`
|
||||
}
|
||||
|
||||
func (p *MetarRequestPDU) Serialize() string {
|
||||
return fmt.Sprintf("$AX%s:%s:METAR:%s%s", p.From, p.To, p.Station, PacketDelimeter)
|
||||
}
|
||||
|
||||
func ParseMetarRequestPDU(rawPacket string) (*MetarRequestPDU, error) {
|
||||
rawPacket = strings.TrimSuffix(rawPacket, PacketDelimeter)
|
||||
rawPacket = strings.TrimPrefix(rawPacket, "$AX")
|
||||
fields := strings.Split(rawPacket, Delimeter)
|
||||
|
||||
if len(fields) != 4 {
|
||||
return nil, NewGenericFSDError(SyntaxError)
|
||||
}
|
||||
|
||||
if fields[2] != "METAR" {
|
||||
return nil, NewGenericFSDError(SyntaxError)
|
||||
}
|
||||
|
||||
pdu := MetarRequestPDU{
|
||||
From: fields[0],
|
||||
To: fields[1],
|
||||
Station: fields[3],
|
||||
}
|
||||
|
||||
err := V.Struct(pdu)
|
||||
if err != nil {
|
||||
return nil, NewGenericFSDError(SyntaxError)
|
||||
}
|
||||
|
||||
return &pdu, nil
|
||||
}
|
||||
Reference in New Issue
Block a user