sim-modem/main.go

201 lines
5.0 KiB
Go
Raw Permalink Normal View History

2024-07-18 16:34:26 +00:00
package main
import (
"context"
2024-08-15 13:47:20 +00:00
"fmt"
2024-07-18 16:34:26 +00:00
"log"
2024-07-23 16:02:28 +00:00
"os"
"os/signal"
"syscall"
2024-08-10 12:21:27 +00:00
"time"
2024-07-22 15:53:34 +00:00
2024-08-08 10:26:33 +00:00
"gitea.unprism.ru/KRBL/sim-modem/api/modem"
2024-07-18 16:34:26 +00:00
)
func main() {
log.Println("CGSG forever!!!")
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
// Just for logs
go func(ctx context.Context) {
<-ctx.Done()
log.Println("GOT INTERUPT SIGNAL")
}(ctx)
if err := mainE(ctx); err != nil {
2024-07-18 16:34:26 +00:00
log.Println("MAIN finished with error:", err.Error())
}
log.Println("END")
}
2024-08-15 13:47:20 +00:00
var m modem.Modem
var logger *log.Logger
func InetInit() error {
// Connect to internet
if err := m.Ic().Connect(); err != nil {
return fmt.Errorf("connect to internet: %w", err)
}
// Setup route table
// Linux now always manage to add ppp0 interface to route table in time so it is better to add ф loop here
setupRouteTableLoop:
for {
if err := m.Ic().SetDefaultRouteTable(); err != nil {
logger.Println("set route table:", err.Error())
time.Sleep(2 * time.Second)
} else {
break setupRouteTableLoop
}
}
return nil
}
func Cmd(cmd string) {
2024-09-30 17:43:37 +00:00
resp, err := m.At().SendWithTimeout(cmd, 10*time.Second) //50*time.Millisecond)
2024-08-15 13:47:20 +00:00
logger.Println(cmd, "===>", resp, err)
}
func mainE(ctx context.Context) error {
2024-08-15 13:47:20 +00:00
logger = log.New(os.Stdout, "main : ", log.LstdFlags)
m = modem.New(log.New(logger.Writer(), "modem : ", log.LstdFlags))
2024-08-09 08:21:02 +00:00
2024-08-02 16:43:15 +00:00
logger.Println("||||||||||||||||| INIT |||||||||||||||")
2024-08-15 13:47:20 +00:00
// If power is down modem won't find suitable devices add will try to send powerOn signal and then try again
initLoop:
for {
select {
2024-08-15 13:47:20 +00:00
case <-ctx.Done(): // For interupt
logger.Println("Break init loop")
return nil
default:
if err := m.Init(); err != nil {
logger.Println("Init ended with error:", err.Error())
// logger.Println("Turn on...")
// if err := m.PowerOnCtx(ctx); err != nil {
// logger.Println("Turn on error:", err.Error())
// }
time.Sleep(time.Second)
continue initLoop
}
break initLoop
2024-08-07 14:34:56 +00:00
}
2024-07-23 14:59:26 +00:00
}
2024-08-15 13:47:20 +00:00
// Final check for sure
2024-08-06 17:37:20 +00:00
if !m.IsConnected() {
logger.Println("Modem is not connected")
2024-07-26 19:58:34 +00:00
return nil
}
2024-08-15 13:47:20 +00:00
// Close() deinits everything recursively
defer func() {
logger.Println("||||||||||||||||| CLOSE |||||||||||||||")
m.Close()
}()
2024-08-09 08:21:02 +00:00
// Internet connect
2024-09-30 17:43:37 +00:00
// if err := InetInit(); err != nil {
// return err
// }
2024-07-29 13:51:54 +00:00
2024-08-09 15:17:20 +00:00
logger.Println("||||||||||||||||| SMS |||||||||||||||||")
// Select ME PMS
// logger.Println("SEND SMS")
// logger.Println(m.Sms().Send("+79218937173", "CGSG forever!!!"))
// m.At().RawSend("\r\n\x1A\r\n")
// Cmd("AT+CREG?")
// Cmd("AT+CNMI?")
2024-09-30 17:43:37 +00:00
Cmd("AT+CNETSCAN")
// Cmd("AT+CSCA?")
// Cmd("AT+CPOL?")
// Cmd("AT+COPS?")
// // Cmd("AT+COPS=?")
// Cmd("AT+CPSI?")
// resp, err = m.At().Send("AT+CNMI=2,2")
2024-08-09 15:17:20 +00:00
for {
select {
case <-ctx.Done():
logger.Println("Break main loop")
return nil
default:
// Cmd("AT+CPSI?")
// Cmd("AT+CIPGSMLOC=1")
// Cmd("AT+CSQ")
2024-08-15 13:47:20 +00:00
// Cmd("AT+CTZU?")
// Cmd("AT+CPIN?")
// Cmd("AT+CCLK?")
// logger.Println(m.Gps().GetStatus())
// m.Update()
// st, _ := m.Gps().GetStatus()
// logger.Printf("FOUND SATELITES: %d\n", st.FoundSatelitesCount)
// data := m.GetData()
// logger.Printf("GPS DATA: %f%s %f%s\n", data.Latitude, data.LatitudeIndicator, data.Longitude, data.LongitudeIndicator)
2024-08-15 13:47:20 +00:00
// logger.Println(m.GetTime())
// logger.Println(m.Ic().Ping())
time.Sleep(time.Second)
}
// Cmd("AT+CSQ")
// Cmd("AT+COPS?")
2024-08-10 12:21:27 +00:00
// if err := m.CheckSignal(); err != nil {
// logger.Println(err)
// } else {
// logger.Println("AAAAAAAAAAA THERE IS SIGNAL")
// }
// readLen, err := m.At().SerialPort().Read(buf)
// if err != nil {
// return err
// }
// if readLen > 0 {
// logger.Println(string(buf[:readLen]))
// }
2024-08-02 16:43:15 +00:00
}
2024-08-09 15:17:20 +00:00
// resp, err = m.At().Send("AT+CPMS?")
// logger.Println("Prefered mem storage:", resp, err)
// resp, err = m.At().Send("AT+CREG?")
// logger.Println("Network registration:", resp, err)
// resp, err = m.At().Send("AT+CPMS?")
// logger.Println("Prefered mem storage:", resp, err)
// resp, err = m.At().Send("AT+CPMS=?")
// logger.Println("Possible mem storage:", resp, err)
// resp, err = m.At().Send("AT+CNMI?")
// logger.Println("New message indications:", resp, err)
// resp, err = m.At().Send("AT+CMGL=\"REC UNREAD\"")
// logger.Println("New messages:", resp, err)
// resp, err = m.At().Send("AT+CNMI=2,1")
// logger.Println("AT+CNMI=2,1:", resp, err)
// resp, err = m.At().Send("AT+CNMI?")
// logger.Println("New message indications:", resp, err)
// logger.Println("Reading port...")
// for {
// readLen, err := m.At().SerialPort().Read(buf)
// if err != nil {
// return err
// }
// if readLen > 0 {
// logger.Println(string(buf[:readLen]))
// }
// }
// for {
// resp, err = m.At().Send("AT+CSQ")
// logger.Println("AT+CSQ:", resp, err)
// time.Sleep(500 * time.Millisecond)
// }
// logger.Println("||||||||||||||||| Checking gps status |||||||||||||||||")
// st, err := m.Gps().GetStatus()
// if err != nil {
// return err
// }
// logger.Printf("GPS Status:%+v\n", st)
2024-07-29 17:03:22 +00:00
2024-08-07 14:34:56 +00:00
// logger.Println("Turn off", m.PowerOff())
2024-07-23 14:59:26 +00:00
return nil
2024-07-18 16:34:26 +00:00
}