2024-07-18 16:34:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-08-12 10:40:26 +00:00
|
|
|
"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"
|
2024-08-12 10:40:26 +00:00
|
|
|
"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!!!")
|
2024-08-12 10:40:26 +00:00
|
|
|
|
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
defer cancel()
|
2024-08-12 16:24:31 +00:00
|
|
|
// Just for logs
|
|
|
|
go func(ctx context.Context) {
|
|
|
|
<-ctx.Done()
|
|
|
|
log.Println("GOT INTERUPT SIGNAL")
|
|
|
|
}(ctx)
|
2024-08-12 10:40:26 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-08-12 10:40:26 +00:00
|
|
|
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
|
2024-08-12 16:24:31 +00:00
|
|
|
initLoop:
|
|
|
|
for {
|
|
|
|
select {
|
2024-08-15 13:47:20 +00:00
|
|
|
case <-ctx.Done(): // For interupt
|
2024-08-12 16:24:31 +00:00
|
|
|
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...")
|
2024-08-19 14:07:46 +00:00
|
|
|
// if err := m.PowerOnCtx(ctx); err != nil {
|
2024-08-12 16:24:31 +00:00
|
|
|
// logger.Println("Turn on error:", err.Error())
|
|
|
|
// }
|
2024-08-19 14:07:46 +00:00
|
|
|
time.Sleep(time.Second)
|
2024-08-12 16:24:31 +00:00
|
|
|
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() {
|
2024-08-12 16:24:31 +00:00
|
|
|
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
|
2024-08-12 16:24:31 +00:00
|
|
|
defer func() {
|
|
|
|
logger.Println("||||||||||||||||| CLOSE |||||||||||||||")
|
|
|
|
m.Close()
|
|
|
|
}()
|
2024-08-09 08:21:02 +00:00
|
|
|
|
2024-08-19 14:07:46 +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")
|
|
|
|
|
2024-08-12 10:40:26 +00:00
|
|
|
// Cmd("AT+CREG?")
|
|
|
|
// Cmd("AT+CNMI?")
|
2024-09-30 17:43:37 +00:00
|
|
|
Cmd("AT+CNETSCAN")
|
2024-08-12 10:40:26 +00:00
|
|
|
// 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 {
|
2024-08-12 16:24:31 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
logger.Println("Break main loop")
|
|
|
|
return nil
|
|
|
|
default:
|
2024-08-19 14:07:46 +00:00
|
|
|
// Cmd("AT+CPSI?")
|
|
|
|
// Cmd("AT+CIPGSMLOC=1")
|
2024-08-12 16:24:31 +00:00
|
|
|
// Cmd("AT+CSQ")
|
2024-08-15 13:47:20 +00:00
|
|
|
// Cmd("AT+CTZU?")
|
|
|
|
// Cmd("AT+CPIN?")
|
2024-08-12 16:24:31 +00:00
|
|
|
// Cmd("AT+CCLK?")
|
|
|
|
// logger.Println(m.Gps().GetStatus())
|
|
|
|
// m.Update()
|
|
|
|
// st, _ := m.Gps().GetStatus()
|
2024-08-19 14:07:46 +00:00
|
|
|
// 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())
|
2024-08-19 14:07:46 +00:00
|
|
|
// logger.Println(m.Ic().Ping())
|
|
|
|
time.Sleep(time.Second)
|
2024-08-12 16:24:31 +00:00
|
|
|
}
|
2024-08-12 10:40:26 +00:00
|
|
|
// 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")
|
|
|
|
// }
|
2024-08-12 10:40:26 +00:00
|
|
|
// 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
|
|
|
}
|