implement the full Interface/Func pattern

master
Florian Thienel 3 years ago
parent 978f47a1c5
commit a20458a904

@ -20,8 +20,16 @@ func (f EncoderFunc) Encode() ([]byte, int) {
return f() return f()
} }
type Requester interface {
Request(context.Context, string) ([]string, error)
}
type RequesterFunc func(context.Context, string) ([]string, error) type RequesterFunc func(context.Context, string) ([]string, error)
func (f RequesterFunc) Request(ctx context.Context, request string) ([]string, error) {
return f(ctx, request)
}
const ( const (
// CRLF line ending for AT commands // CRLF line ending for AT commands
CRLF = "\x0d\x0a" CRLF = "\x0d\x0a"
@ -35,18 +43,18 @@ const (
) )
// SendMessage according to [PEI] 6.13.2 // SendMessage according to [PEI] 6.13.2
func SendMessage(destination tetra.Identity, sds Encoder) string { func SendMessage(destination tetra.Identity, message Encoder) string {
pdu := make([]byte, 0, 256) pdu := make([]byte, 0, 256)
pduBits := 0 pduBits := 0
pdu, pduBits = sds.Encode(pdu, pduBits) pdu, pduBits = message.Encode(pdu, pduBits)
return fmt.Sprintf("AT+CMGS=%s,%d"+CRLF+"%s"+CtrlZ, destination, pduBits, tetra.BinaryToHex(pdu)) return fmt.Sprintf("AT+CMGS=%s,%d"+CRLF+"%s"+CtrlZ, destination, pduBits, tetra.BinaryToHex(pdu))
} }
var sendMessageDescription = regexp.MustCompile(`^\+CMGS: .+\(\d*-(\d*)\)$`) var sendMessageDescription = regexp.MustCompile(`^\+CMGS: .+\(\d*-(\d*)\)$`)
// RequestMaxMessagePDUBits uses the given RequesterFunc to find out how many bits a message PDU may have (see [PEI] 6.13.2). // RequestMaxMessagePDUBits uses the given RequesterFunc to find out how many bits a message PDU may have (see [PEI] 6.13.2).
func RequestMaxMessagePDUBits(ctx context.Context, requester RequesterFunc) (int, error) { func RequestMaxMessagePDUBits(ctx context.Context, requester Requester) (int, error) {
responses, err := requester(ctx, "AT+CMGS=?") responses, err := requester.Request(ctx, "AT+CMGS=?")
if err != nil { if err != nil {
return 0, err return 0, err
} }

Loading…
Cancel
Save