package sms import ( "fmt" "io" "log" "strings" "github.com/CGSG-2021-AE4/modem-test/api/modem/at" ) type dialer struct { logger *log.Logger port at.Port } type Dialer interface { Init() error Send(number, msg string) error ReadNew() ([]string, error) io.Closer } func New(logger *log.Logger, port at.Port) Dialer { return &dialer{ logger: logger, port: port, } } func (d *dialer) Init() error { // Ensure serial port if !d.port.IsConnected() { return fmt.Errorf("serial port is not connected") } // Ensure text format if resp, err := d.port.Send("AT+CMGF=1"); err != nil || !resp.Check() { if err != nil { return fmt.Errorf("set to text format request: %w", err) } return fmt.Errorf("set SIM format: error response: %s", resp) } return nil } func (d *dialer) Send(number, msg string) error { d.port.Send(fmt.Sprintf("AT+CMGS=\"%s\"", number)) // Because it will throw error resp, err := d.port.RawSend(fmt.Sprintf("%s\x1A", msg)) // Add additional \r\n because there is not supposed to be if err != nil { return fmt.Errorf("message request: %w", err) } if at.Resp(resp).Check() { return nil } d.logger.Println("SEND RESPONSE:", resp) errCode, err := GetError([]byte(resp)) if err != nil { return fmt.Errorf("send sms failed and failed to get error: %w", err) } return fmt.Errorf("failed to send with SMS error: %d - %s", errCode, DecodeError(errCode)) } // Reads all new messages func (d *dialer) ReadNew() ([]string, error) { resp, err := d.port.Send("AT+CMGL=\"ALL\"") if err != nil { return nil, fmt.Errorf("AT+CMGL request: %w", err) } msgs := strings.Split(strings.Replace(string(resp), "\r", "", -1), "\n") outMsgs := make([]string, 0) for _, s := range msgs { if len(s) >= len("+CMGL:") && s[:len("+CMGL:")] == "+CMGL:" { params := strings.Split(s[len("+CMGL:"):], ",") d.logger.Println("GET MSG:", params) } else { outMsgs = append(outMsgs, s) } } return outMsgs, nil // TODO } func (d *dialer) Close() error { return nil }