Added read.

This commit is contained in:
Andrey Egorov 2024-07-29 20:03:22 +03:00
parent d05e2205d6
commit fd9e999b5a
4 changed files with 89 additions and 48 deletions

View File

@ -6,6 +6,8 @@ import (
"strconv"
"strings"
"time"
"github.com/CGSG-2021-AE4/modem-test/api/modem/at"
)
type GpsData struct {
@ -75,3 +77,61 @@ func (gps *GpsData) decode(str string) error {
*gps = newGpsInfo
return nil
}
func (gps *GpsData) Update(port at.Port) error {
if err := switchGpsMode(port, true); err != nil {
return fmt.Errorf("try to GPS mode: %w", err)
}
defer switchGpsMode(port, false)
resp, err := port.Send("AT+CGPSINFO")
if err != nil {
return fmt.Errorf("receive GPS data: %w", err)
}
if !resp.Check() {
return fmt.Errorf("error response")
}
if err := gps.decode(strings.Split(strings.Replace(resp.RmFront("+CGPSINFO:").String(), "\r", "", -1), "\n")[0]); err != nil {
return fmt.Errorf("decode: %w", err)
}
return nil
}
func switchGpsMode(port at.Port, on bool) error {
onStr := "0"
if on {
onStr = "1"
}
// Reset input
if err := port.GetSerialPort().ResetInputBuffer(); err != nil {
return fmt.Errorf("reset input buffer: %w", err)
}
// Reset output
if err := port.GetSerialPort().ResetOutputBuffer(); err != nil {
return fmt.Errorf("reset output buffer: %w", err)
}
// Check gps mode status
resp, err := port.Send("AT+CGPS?")
if err != nil {
return fmt.Errorf("make at ask: %w", err)
}
if !resp.Check() {
return fmt.Errorf("error response")
}
ans := strings.Replace(strings.Split(strings.Split(resp.RmFront("+CGPS:").String(), "\n")[0], ",")[0], " ", "", -1)
if ans == onStr {
return nil
}
// Modem is not in GPS mode
resp, err = port.Send("AT+CGPS=" + onStr)
if err != nil {
return fmt.Errorf("try to switch to gps: %w", err)
}
if !resp.Check() {
return fmt.Errorf("switch tp GPS failed")
}
return nil
}

View File

@ -52,8 +52,9 @@ type Modem interface {
Update() error
GetInfo() ModemData
// Temp access to sms interface
// Temp access to SMS and AT interface
Sms() sms.Dialer
At() at.Port
io.Closer
}
@ -173,6 +174,10 @@ func (m *modem) Sms() sms.Dialer {
return m.sms
}
func (m *modem) At() at.Port {
return m.port
}
func (m *modem) Close() error {
if err := m.sms.Close(); err != nil {
return fmt.Errorf("sms: %w", err)
@ -216,10 +221,6 @@ func (m *modem) isConnected() bool {
func (m *modem) testGPS() error {
m.logger.Println("Testing GPS")
if err := m.switchToGpsMode(); err != nil {
return fmt.Errorf("switch to GPS: %w", err)
}
if err := m.Update(); err != nil {
return fmt.Errorf("update: %w", err)
}
@ -238,7 +239,6 @@ func (m *modem) saveGPS(path string) error {
if err != nil {
return fmt.Errorf("open file: %w", err)
}
defer f.Close()
if _, err = f.WriteString(m.getShortInfo()); err != nil {
@ -246,6 +246,7 @@ func (m *modem) saveGPS(path string) error {
}
return nil
}
func (m *modem) testConsole() {
for {
var inStr string
@ -361,44 +362,6 @@ func (m *modem) ping() error {
return nil
}
func (m *modem) switchToGpsMode() error {
m.logger.Println("Enabling GPS mode...")
// Reset input
if err := m.port.GetSerialPort().ResetInputBuffer(); err != nil {
return fmt.Errorf("reset input buffer: %w", err)
}
// Reset output
if err := m.port.GetSerialPort().ResetOutputBuffer(); err != nil {
return fmt.Errorf("reset output buffer: %w", err)
}
// Check gps mode status
resp, err := m.port.Send("AT+CGPS?")
if err != nil {
return fmt.Errorf("make at ask: %w", err)
}
if !resp.Check() {
return fmt.Errorf("error response")
}
ans := strings.Replace(strings.Split(strings.Split(resp.RmFront("+CGPS:").String(), "\n")[0], ",")[0], " ", "", -1)
if ans == "1" {
m.logger.Println("GPS already enabled")
return nil
}
m.logger.Println(ans)
// Modem is not in GPS mode
resp, err = m.port.Send("AT+CGPS=1")
if err != nil {
return fmt.Errorf("try to switch to gps: %w", err)
}
if !resp.Check() {
return fmt.Errorf("switch tp GPS failed")
}
m.logger.Println("GPS mode enabled")
return nil
}
func getTtyDevices() ([]string, error) {
// Get ports
/**/

View File

@ -44,8 +44,8 @@ func (d *dialer) Init() error {
}
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
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)
}
@ -66,7 +66,18 @@ func (d *dialer) ReadNew() ([]string, error) {
return nil, fmt.Errorf("AT+CMGL request: %w", err)
}
msgs := strings.Split(strings.Replace(string(resp), "\r", "", -1), "\n")
return msgs, nil // TODO
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 {

View File

@ -29,8 +29,15 @@ func mainE() error {
log.Println(m.GetInfo())
log.Println("||||||||||||||||| SEND SMS |||||||||||||||||")
if err := m.Sms().Send("+79218937173", "CGSG forever"); err != nil {
log.Println(m.At().Send("AT+CNUM"))
// if err := m.Sms().Send("+79218937173", "CGSG forever"); err != nil {
// return err
// }
if ms, err := m.Sms().ReadNew(); err != nil {
return err
} else {
log.Println("NEW:", ms)
}
return nil
}