diff --git a/api/modem/at/at.go b/api/modem/at/at.go index 6a2f98d..d435f64 100644 --- a/api/modem/at/at.go +++ b/api/modem/at/at.go @@ -83,13 +83,13 @@ func (p *atPort) IsConnected() bool { // Low level write/read function func (p *atPort) makeReq(msg string) (string, error) { // Write - p.port.ResetInputBuffer() if _, err := p.port.Write([]byte(msg)); err != nil { return "", fmt.Errorf("serial port write: %w", err) } + time.Sleep(time.Millisecond) // Read readLen, err := p.port.Read(p.inputBuf) - p.logger.Println(msg, "RAWREAD:", string(p.inputBuf[:readLen])) + // p.logger.Println(msg, "\x1b[38;2;150;150;150mRAWREAD:", string(p.inputBuf[:readLen]), "\x1b[38;2;255;255;255m") if err != nil { return "", fmt.Errorf("port read: %w", err) } diff --git a/api/modem/gpio.go b/api/modem/gpio.go index c08da44..b0a236d 100644 --- a/api/modem/gpio.go +++ b/api/modem/gpio.go @@ -8,7 +8,7 @@ import ( ) type gpioPin struct { - logger *log.Logger + Logger *log.Logger Pin gpio.Pin } @@ -18,22 +18,22 @@ func (p gpioPin) Init() error { func (p gpioPin) sendOnOffSignal() { p.Pin.Output() - p.logger.Println("Power on 0/3 + 100ms") + p.Logger.Println("Power on 0/3 + 100ms") p.Pin.Low() p.Pin.Toggle() time.Sleep(100 * time.Millisecond) - p.logger.Println("Power on 1/3 + 3s") + p.Logger.Println("Power on 1/3 + 3s") p.Pin.High() p.Pin.Toggle() time.Sleep(3 * time.Second) - p.logger.Println("Power on 2/3 + 30s") + p.Logger.Println("Power on 2/3 + 30s") p.Pin.Low() p.Pin.Toggle() time.Sleep(30 * time.Second) - p.logger.Println("Power on 3/3") + p.Logger.Println("Power on 3/3") } func (p gpioPin) PowerOn() { diff --git a/api/modem/gps.go b/api/modem/gps.go index 78cdf78..d532acd 100644 --- a/api/modem/gps.go +++ b/api/modem/gps.go @@ -50,8 +50,10 @@ func (gps *GpsInfo) decode(str string) error { if err != nil { return fmt.Errorf("parse longitude: %w", err) } + newGpsInfo.Latitude /= 100 + newGpsInfo.Longitude /= 100 newGpsInfo.LatitudeIndicator = strs[1] - newGpsInfo.LatitudeIndicator = strs[3] + newGpsInfo.LongitudeIndicator = strs[3] newGpsInfo.Date = strs[4] newGpsInfo.Time = strs[5] newGpsInfo.Altitude, err = strconv.ParseFloat(strs[6], 64) diff --git a/api/modem/modem.go b/api/modem/modem.go index d38dd81..31a81d1 100644 --- a/api/modem/modem.go +++ b/api/modem/modem.go @@ -43,6 +43,8 @@ type Modem interface { // CalculateSpeed(newLatitude, newlongitude float64) Update() error GetInfo() (string, error) + GetShortInfo() string + SaveGPS(path string) error TestGPS() error } @@ -50,18 +52,18 @@ func New(logger *log.Logger) Modem { return &modem{ logger: logger, baudrate: 115200, - onOffPin: gpioPin{Pin: 6}, + onOffPin: gpioPin{Logger: logger, Pin: 6}, lastUpdateTime: time.Now(), } } func (m *modem) Init() error { // Turn module on - // log.Println("=============================== Turn on module") - // if err := m.onOffPin.Init(); err != nil { - // return fmt.Errorf("gpio pin init: %w", err) - // } - // onOffPin.PowerOn() + log.Println("=============================== Turn on module") + if err := m.onOffPin.Init(); err != nil { + return fmt.Errorf("gpio pin init: %w", err) + } + // m.onOffPin.PowerOn() // Search m.logger.Println("=============================== Search") @@ -214,6 +216,21 @@ func (m *modem) SaveGPS(path string) error { } return nil } +func (m *modem) testConsole() { + for { + var inStr string + fmt.Scanln(&inStr) + if inStr == "exit" { + return + } + resp, err := m.port.Send(inStr) + if err != nil { + m.logger.Println("ERROR:", err.Error()) + } + log.Println(resp) + log.Println("------------------") + } +} func (m *modem) checkPort(portName string) (outErr error) { defer func() { @@ -235,16 +252,25 @@ func (m *modem) checkPort(portName string) (outErr error) { } defer m.port.Disconnect() // Do not bother about errors... + // 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) + } + m.port.Send("ATE0") // This shit sometimes enables echo mode... why... when... but it can + // Ping m.logger.Println("Ping...") - if err := m.ping(); err != nil { return fmt.Errorf("ping error: %w", err) } + m.logger.Println("\x1b[38;2;0;255;0mOK\x1b[38;2;255;255;255m") // Check model m.logger.Println("Check model...") - resp, err := m.port.Send("AT+CGMM") if err != nil { return fmt.Errorf("get model: %w", err) @@ -261,7 +287,7 @@ func (m *modem) checkPort(portName string) (outErr error) { if model[:len(rightModel)] != rightModel { return fmt.Errorf("invalid modem model: %s", model) } - m.logger.Println("Model right") + m.logger.Println("\x1b[38;2;0;255;0mOK\x1b[38;2;255;255;255m") return nil } @@ -282,7 +308,7 @@ SearchLoop: m.logger.Printf("Checking port %s ...\n", p) if err := m.checkPort("/dev/" + p); err != nil { - m.logger.Printf("Check failed: %s\n", err.Error()) + m.logger.Printf("\x1b[38;2;255;0;0mCheck failed: %s\x1b[38;2;255;255;255m\n", err.Error()) continue SearchLoop } @@ -306,10 +332,14 @@ func (m *modem) ping() error { func (m *modem) switchToGpsMode() error { m.logger.Println("Enabling GPS mode...") - // Reset intput + // 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?") diff --git a/docker-compose.yml b/docker-compose.yml index 22af931..63af15a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,4 +14,4 @@ services: devices: - /dev/i2c-1 - /dev/ttyS0 - restart: unless-stopped \ No newline at end of file + restart: unless-stopped diff --git a/main.go b/main.go index a90f3c6..219b07e 100644 --- a/main.go +++ b/main.go @@ -17,9 +17,18 @@ func main() { func mainE() error { m := modem.New(log.New(os.Stdout, "modem:", log.LstdFlags)) + log.Println("||||||||||||||||| INIT |||||||||||||||") if err := m.Init(); err != nil { return err } + log.Println("||||||||||||||||| GET INFO |||||||||||||||||") + if info, err := m.GetInfo(); err != nil { + return err + } else { + log.Println(info) + } + log.Println("||||||||||||||||| GET SHORT INFO ||||||||||||||||") + log.Println(m.GetShortInfo()) return nil // ports, err := serial.GetPortsList() // if err != nil {