Compare commits
36 Commits
781d9ccda4
...
master
Author | SHA1 | Date | |
---|---|---|---|
4f89f15146 | |||
509006fae1 | |||
3f5412fac0 | |||
5065cfdfca | |||
5059814d39 | |||
d21ad0ce00 | |||
75d05f197c | |||
b3fb13b7b3 | |||
dde1411b18 | |||
6c110b9a8b | |||
cb07b6ac62 | |||
be99ed7fbf | |||
d625866d92 | |||
7ee03906f8 | |||
0b76884112 | |||
59bd2c5db3 | |||
0837d376f7 | |||
6a96656434 | |||
026c1aa3bb | |||
90a06e6afa | |||
e2e02ebbfe | |||
9bc94bfe7c | |||
061ba2a859 | |||
6498d20378 | |||
b831d44294 | |||
fd9e999b5a | |||
d05e2205d6 | |||
1b741c7dab | |||
e0110c558e | |||
225a0d2264 | |||
2eb21228e3 | |||
ebab41c510 | |||
92d42d413a | |||
b9056d2299 | |||
12555f2f46 | |||
eb899f0b45 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,4 @@
|
||||
out/
|
||||
Makefile
|
||||
go.sum
|
||||
.git/
|
||||
*.swp
|
||||
|
@ -4,7 +4,12 @@ WORKDIR /app
|
||||
COPY ./ ./
|
||||
RUN go mod download
|
||||
|
||||
RUN apt-get update
|
||||
RUN apt-get install -y iputils-ping
|
||||
RUN apt-get install -y ppp
|
||||
RUN apt-get install -y net-tools
|
||||
|
||||
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -o /modem-test
|
||||
|
||||
CMD ["/modem-test"]
|
||||
CMD ["/modem-test"]
|
||||
|
7
Makefile
Normal file
7
Makefile
Normal file
@ -0,0 +1,7 @@
|
||||
export GOOS=linux
|
||||
export GOARCH=arm
|
||||
export GOARM=6
|
||||
export CGO_ENABLED=0
|
||||
|
||||
build:
|
||||
@go build -o out/out main.go
|
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.bug.st/serial"
|
||||
@ -12,12 +13,14 @@ import (
|
||||
// Some constants
|
||||
const (
|
||||
ReadTimeout = time.Second
|
||||
InputBufSize = 128
|
||||
InputBufSize = 512
|
||||
)
|
||||
|
||||
type atPort struct {
|
||||
logger *log.Logger
|
||||
|
||||
mutex sync.Mutex // Mutex for all operation with serial port
|
||||
|
||||
baudrate int
|
||||
portName string
|
||||
port serial.Port
|
||||
@ -26,13 +29,18 @@ type atPort struct {
|
||||
|
||||
type Port interface {
|
||||
GetName() string
|
||||
GetSerialPort() serial.Port // For extra need
|
||||
GetBaudrate() int
|
||||
SerialPort() serial.Port // For extra need
|
||||
Mutex() sync.Locker // retruns pointer to mutex for advanced use like readign NMEA reports
|
||||
|
||||
Connect() error
|
||||
Disconnect() error
|
||||
IsConnected() bool
|
||||
|
||||
RawSend(msg string) error
|
||||
RawRead(timeout time.Duration) (string, error)
|
||||
Send(cmd string) (Resp, error)
|
||||
SendWithTimeout(cmd string, timeout time.Duration) (Resp, error)
|
||||
|
||||
io.Closer
|
||||
}
|
||||
@ -50,11 +58,22 @@ func (p *atPort) GetName() string {
|
||||
return p.portName
|
||||
}
|
||||
|
||||
func (p *atPort) GetSerialPort() serial.Port {
|
||||
func (p *atPort) GetBaudrate() int {
|
||||
return p.baudrate
|
||||
}
|
||||
|
||||
func (p *atPort) SerialPort() serial.Port {
|
||||
return p.port
|
||||
}
|
||||
|
||||
func (p *atPort) Mutex() sync.Locker {
|
||||
return &p.mutex
|
||||
}
|
||||
|
||||
func (p *atPort) Connect() error {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
p.logger.Println("Connecting to", p.portName, "...")
|
||||
s, err := serial.Open(p.portName, &serial.Mode{BaudRate: p.baudrate})
|
||||
if err != nil {
|
||||
@ -70,8 +89,10 @@ func (p *atPort) Connect() error {
|
||||
}
|
||||
|
||||
func (p *atPort) Disconnect() error {
|
||||
p.mutex.Lock()
|
||||
defer func() {
|
||||
p.port = nil
|
||||
p.mutex.Unlock()
|
||||
}()
|
||||
if err := p.port.Close(); err != nil {
|
||||
return fmt.Errorf("close port: %w", err)
|
||||
@ -80,34 +101,79 @@ func (p *atPort) Disconnect() error {
|
||||
}
|
||||
|
||||
func (p *atPort) IsConnected() bool {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
return p.port != nil
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
// Read
|
||||
readLen, err := p.port.Read(p.inputBuf)
|
||||
p.logger.Println(msg, "RAWREAD:", string(p.inputBuf[:readLen]))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("port read: %w", err)
|
||||
}
|
||||
func (p *atPort) RawRead(timeout time.Duration) (string, error) {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
return string(p.inputBuf[:readLen]), nil
|
||||
deadline := time.Now().Add(timeout)
|
||||
outBuf := make([]byte, 0)
|
||||
|
||||
readLoop:
|
||||
for {
|
||||
readLen, err := p.port.Read(p.inputBuf)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("port read: %w", err)
|
||||
}
|
||||
if readLen == 0 && time.Now().After(deadline) {
|
||||
break readLoop
|
||||
}
|
||||
outBuf = append(outBuf, p.inputBuf[:readLen]...)
|
||||
// if readLen < len(p.inputBuf) {
|
||||
// break readLoop
|
||||
// }
|
||||
}
|
||||
// p.logger.Println(msg, "\x1b[38;2;150;150;150mRAWREAD:", string(outBuf), "\x1b[38;2;255;255;255m")
|
||||
return string(outBuf), nil
|
||||
}
|
||||
|
||||
// Low level write/read function
|
||||
func (p *atPort) RawSend(msg string) error {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
// Write
|
||||
if _, err := p.port.Write([]byte(msg)); err != nil {
|
||||
return fmt.Errorf("serial port write: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *atPort) Send(cmd string) (Resp, error) {
|
||||
cmd += "\r\n"
|
||||
rawResp, err := p.makeReq(cmd)
|
||||
err := p.RawSend(cmd + "\r\n")
|
||||
if err != nil {
|
||||
return RespNil, fmt.Errorf("make request: %w", err)
|
||||
return RespNil, fmt.Errorf("%s request: %w", cmd, err)
|
||||
}
|
||||
rawResp, err := p.RawRead(ReadTimeout)
|
||||
if err != nil {
|
||||
return RespNil, fmt.Errorf("%s request: %w", cmd, err)
|
||||
}
|
||||
|
||||
if len(rawResp) <= 4 {
|
||||
return RespNil, fmt.Errorf("read too small msg: %d byte", len(rawResp))
|
||||
return RespNil, fmt.Errorf("%s request: read too small msg: %d byte - %s", cmd, len(rawResp), string(rawResp))
|
||||
}
|
||||
resp := rawResp[2 : len(rawResp)-2] // Cut \r\n
|
||||
|
||||
return Resp(resp), nil
|
||||
}
|
||||
|
||||
func (p *atPort) SendWithTimeout(cmd string, timeout time.Duration) (Resp, error) {
|
||||
err := p.RawSend(cmd + "\r\n")
|
||||
if err != nil {
|
||||
return RespNil, fmt.Errorf("%s request: %w", cmd, err)
|
||||
}
|
||||
rawResp, err := p.RawRead(timeout)
|
||||
if err != nil {
|
||||
return RespNil, fmt.Errorf("%s request: %w", cmd, err)
|
||||
}
|
||||
|
||||
if len(rawResp) <= 4 {
|
||||
return RespNil, fmt.Errorf("%s request: read too small msg: %d byte - %s", cmd, len(rawResp), string(rawResp))
|
||||
}
|
||||
resp := rawResp[2 : len(rawResp)-2] // Cut \r\n
|
||||
|
||||
@ -115,5 +181,8 @@ func (p *atPort) Send(cmd string) (Resp, error) {
|
||||
}
|
||||
|
||||
func (p *atPort) Close() error {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
return p.port.Close()
|
||||
}
|
||||
|
@ -12,6 +12,14 @@ func (resp Resp) RmFront(str string) Resp {
|
||||
return Resp(string(resp)[len(str):])
|
||||
}
|
||||
|
||||
func (resp Resp) CheckFront(str string) bool {
|
||||
return len(resp) >= len(str) && resp[:len(str)].String() == str
|
||||
}
|
||||
|
||||
func (resp Resp) String() string {
|
||||
return string(resp)
|
||||
}
|
||||
|
||||
func (resp Resp) Bytes() []byte {
|
||||
return []byte(resp)
|
||||
}
|
||||
|
@ -1,49 +0,0 @@
|
||||
package modem
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
gpio "github.com/stianeikeland/go-rpio/v4"
|
||||
)
|
||||
|
||||
type gpioPin struct {
|
||||
logger *log.Logger
|
||||
Pin gpio.Pin
|
||||
}
|
||||
|
||||
func (p gpioPin) Init() error {
|
||||
return gpio.Open()
|
||||
}
|
||||
|
||||
func (p gpioPin) sendOnOffSignal() {
|
||||
p.Pin.Output()
|
||||
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.Pin.High()
|
||||
p.Pin.Toggle()
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
func (p gpioPin) PowerOn() {
|
||||
p.sendOnOffSignal()
|
||||
}
|
||||
|
||||
func (p *gpioPin) PowerOff() {
|
||||
p.sendOnOffSignal()
|
||||
}
|
||||
|
||||
func (p *gpioPin) Close() error {
|
||||
return gpio.Close()
|
||||
}
|
84
api/modem/gpio/gpio.go
Normal file
84
api/modem/gpio/gpio.go
Normal file
@ -0,0 +1,84 @@
|
||||
package gpio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
gpio "github.com/stianeikeland/go-rpio/v4"
|
||||
)
|
||||
|
||||
const (
|
||||
waitCtxTimeout = 100 * time.Microsecond
|
||||
)
|
||||
|
||||
type gpioPin struct {
|
||||
logger *log.Logger
|
||||
pin gpio.Pin
|
||||
}
|
||||
|
||||
type Pin interface {
|
||||
Init() error
|
||||
PowerOn()
|
||||
PowerOnCtx(ctx context.Context)
|
||||
io.Closer
|
||||
}
|
||||
|
||||
func New(logger *log.Logger, pin uint8) Pin {
|
||||
return gpioPin{
|
||||
logger: logger,
|
||||
pin: gpio.Pin(pin),
|
||||
}
|
||||
}
|
||||
|
||||
func (p gpioPin) Init() error {
|
||||
return gpio.Open()
|
||||
}
|
||||
|
||||
func waitCtx(ctx context.Context, timeout time.Duration) {
|
||||
deadline := time.Now().Add(timeout)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
if time.Now().After(deadline) {
|
||||
return
|
||||
}
|
||||
}
|
||||
time.Sleep(waitCtxTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
func (p gpioPin) sendOnOffSignal(ctx context.Context) {
|
||||
p.pin.Output()
|
||||
p.logger.Println("Power on 0/3 + 100ms")
|
||||
p.pin.Low()
|
||||
p.pin.Toggle()
|
||||
waitCtx(ctx, 100*time.Millisecond)
|
||||
|
||||
p.logger.Println("Power on 1/3 + 3s")
|
||||
p.pin.High()
|
||||
p.pin.Toggle()
|
||||
waitCtx(ctx, 3*time.Second)
|
||||
|
||||
p.logger.Println("Power on 2/3 + 30s")
|
||||
p.pin.Low()
|
||||
p.pin.Toggle()
|
||||
waitCtx(ctx, 30*time.Second)
|
||||
|
||||
p.logger.Println("Power on 3/3")
|
||||
}
|
||||
|
||||
func (p gpioPin) PowerOn() {
|
||||
p.sendOnOffSignal(context.Background())
|
||||
}
|
||||
|
||||
func (p gpioPin) PowerOnCtx(ctx context.Context) {
|
||||
p.sendOnOffSignal(ctx)
|
||||
}
|
||||
|
||||
func (p gpioPin) Close() error {
|
||||
return gpio.Close()
|
||||
}
|
@ -1,18 +1,20 @@
|
||||
package modem
|
||||
package gps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type GpsData struct {
|
||||
Latitude float64 `json:"Latitude"`
|
||||
Longitude float64 `json:"Longitude"`
|
||||
LatitudeIndicator string `json:"Latitude_indicator"` // North/South
|
||||
LongitudeIndicator string `json:"Longitude_indicator"` // West/East
|
||||
type Data struct {
|
||||
Latitude float64 `json:"Latitude"` // ddmm.mmmmmm
|
||||
Longitude float64 `json:"Longitude"` // dddmm.mmmmmm
|
||||
LatitudeIndicator string `json:"Latitude_indicator"` // N/S - North/South
|
||||
LongitudeIndicator string `json:"Longitude_indicator"` // W/E - West/East
|
||||
Speed float64 `json:"Speed"`
|
||||
Course float64 `json:"-"`
|
||||
Altitude float64 `json:"-"`
|
||||
@ -20,13 +22,13 @@ type GpsData struct {
|
||||
Time string `json:"-"`
|
||||
}
|
||||
|
||||
var GpsInfoNil = GpsData{}
|
||||
var GpsInfoNil = Data{}
|
||||
|
||||
func deg2rad(deg float64) float64 {
|
||||
return deg * (math.Pi / 180)
|
||||
}
|
||||
|
||||
func (gps *GpsData) calculateSpeed(newLatitude, newLongitude float64, lastUpdateTime time.Time) {
|
||||
func (gps *Data) CalculateSpeed(newLatitude, newLongitude float64, lastUpdateTime time.Time) {
|
||||
earthRad := 6371.0 // TODO ?
|
||||
dLat := deg2rad(math.Abs(newLatitude - gps.Latitude))
|
||||
dLon := deg2rad(math.Abs(newLongitude - gps.Longitude))
|
||||
@ -37,36 +39,43 @@ func (gps *GpsData) calculateSpeed(newLatitude, newLongitude float64, lastUpdate
|
||||
gps.Speed = earthRad * c / (math.Abs(float64(time.Since(lastUpdateTime))))
|
||||
}
|
||||
|
||||
func (gps *GpsData) decode(str string) error {
|
||||
var err error
|
||||
newGpsInfo := GpsData{}
|
||||
strs := strings.Split(strings.Split(strings.Replace(str, " ", "", -1), "\n")[0], ",")
|
||||
// To remove warning
|
||||
|
||||
// Parse string from AT command that contains gps data
|
||||
func (gps *Data) decode(str string) error {
|
||||
var err error
|
||||
newGpsInfo := Data{}
|
||||
strs := strings.Split(strings.Split(strings.Replace(str, " ", "", -1), "\n")[0], ",")
|
||||
logger := log.New(io.Discard, "modem-gps", log.LstdFlags)
|
||||
|
||||
if len(strs) < 7 {
|
||||
return fmt.Errorf("ERROR: too small msg: %s", strs)
|
||||
}
|
||||
newGpsInfo.Latitude, err = strconv.ParseFloat(strs[0], 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse latitude: %w", err)
|
||||
logger.Println("ERROR parse latitude:", err.Error())
|
||||
}
|
||||
newGpsInfo.Longitude, err = strconv.ParseFloat(strs[2], 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse longitude: %w", err)
|
||||
logger.Println("ERROR parse longitude:", err.Error())
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse altitude: %w", err)
|
||||
logger.Println("ERROR parse altitude:", err.Error())
|
||||
}
|
||||
newGpsInfo.Speed, err = strconv.ParseFloat(strs[7], 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse speed: %w", err)
|
||||
logger.Println("ERROR parse speed:", err.Error())
|
||||
}
|
||||
// Course sometimes may be null
|
||||
if len(strs[8]) > 0 {
|
||||
newGpsInfo.Course, err = strconv.ParseFloat(strs[8], 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse course: %w", err)
|
||||
logger.Println("ERROR parse course:", err.Error())
|
||||
}
|
||||
}
|
||||
*gps = newGpsInfo
|
107
api/modem/gps/gps.go
Normal file
107
api/modem/gps/gps.go
Normal file
@ -0,0 +1,107 @@
|
||||
package gps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/at"
|
||||
)
|
||||
|
||||
type gps struct {
|
||||
logger *log.Logger
|
||||
port at.Port
|
||||
data Data
|
||||
}
|
||||
|
||||
type Gps interface {
|
||||
Init() error
|
||||
Update() error
|
||||
|
||||
GetData() Data
|
||||
GetStatus() (Status, error)
|
||||
|
||||
io.Closer
|
||||
}
|
||||
|
||||
func New(logger *log.Logger, port at.Port) Gps {
|
||||
return &gps{
|
||||
logger: logger,
|
||||
port: port,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *gps) Init() error {
|
||||
if !g.port.IsConnected() {
|
||||
return fmt.Errorf("at port is not connected")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gps) Update() error {
|
||||
if err := g.switchGpsMode(true); err != nil {
|
||||
return fmt.Errorf("try to GPS mode: %w", err)
|
||||
}
|
||||
defer g.switchGpsMode(false)
|
||||
|
||||
resp, err := g.port.Send("AT+CGPSINFO")
|
||||
if err != nil {
|
||||
return fmt.Errorf("receive GPS data: %w", err)
|
||||
}
|
||||
if !resp.Check() || !resp.CheckFront("+CGPSINFO:") {
|
||||
return fmt.Errorf("get GPS info: error response: %s", resp)
|
||||
}
|
||||
|
||||
if err := g.data.decode(strings.Split(strings.Replace(resp.RmFront("+CGPSINFO:").String(), "\r", "", -1), "\n")[0]); err != nil {
|
||||
g.logger.Printf("error decode: %s\n", err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gps) GetData() Data {
|
||||
return g.data
|
||||
}
|
||||
|
||||
func (g *gps) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gps) switchGpsMode(on bool) error {
|
||||
onStr := "0"
|
||||
if on {
|
||||
onStr = "1"
|
||||
}
|
||||
|
||||
// Reset input
|
||||
if err := g.port.SerialPort().ResetInputBuffer(); err != nil {
|
||||
return fmt.Errorf("reset input buffer: %w", err)
|
||||
}
|
||||
// Reset output
|
||||
if err := g.port.SerialPort().ResetOutputBuffer(); err != nil {
|
||||
return fmt.Errorf("reset output buffer: %w", err)
|
||||
}
|
||||
|
||||
// Check gps mode status
|
||||
resp, err := g.port.Send("AT+CGPS?")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !resp.Check() || !resp.CheckFront("+CGPS:") {
|
||||
return fmt.Errorf("get GPS mode: error response: %s", resp)
|
||||
}
|
||||
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 = g.port.Send("AT+CGPS=" + onStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !resp.Check() {
|
||||
return fmt.Errorf("set GPS mode: error response: %s", resp)
|
||||
}
|
||||
return nil
|
||||
}
|
151
api/modem/gps/nmea.go
Normal file
151
api/modem/gps/nmea.go
Normal file
@ -0,0 +1,151 @@
|
||||
package gps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type nmeaFlags int
|
||||
|
||||
const (
|
||||
gga nmeaFlags = 1 << iota // global positioning systemfix data
|
||||
rmc // recommended minimumspecific GPS/TRANSIT data
|
||||
gpgsv // GPS satellites in view
|
||||
gpgsa // GPS DOP and active satellites
|
||||
vtg // track made good and ground speed
|
||||
xfi // Global Positioning SystemExtended FixData.)Bit 6 – GLGSV (GLONASS satellites in view GLONASSfixesonly
|
||||
glgsa // 1. GPS/2. Glonass/3. GALILE DOPandActiveSatellites.
|
||||
gns // fix data for GNSS receivers; output for GPS, GLONASS, GALILEO
|
||||
_ // Reserved
|
||||
gagsv // GALILEO satellites in view
|
||||
_ // Reserved
|
||||
_ // Reserved
|
||||
_ // Reserved
|
||||
_ // Reserved
|
||||
_ // Reserved
|
||||
bdpqgsa // BEIDOU/QZSS DOP and activesatellites
|
||||
bdpqgsv // BEIDOUQZSS satellites in view
|
||||
|
||||
nmeaFlagsMask = (1 << 18) - 1
|
||||
|
||||
collectTimeout = 1 * time.Second
|
||||
)
|
||||
|
||||
func secondCountDownTimer(title string, logger *log.Logger, t time.Duration) {
|
||||
_ = title
|
||||
_ = logger
|
||||
counter := 0
|
||||
for {
|
||||
if counter > int(t.Seconds()) {
|
||||
break
|
||||
}
|
||||
// logger.Printf("%s: %d/%f\n", title, counter, t.Seconds())
|
||||
time.Sleep(time.Second)
|
||||
counter += 1
|
||||
}
|
||||
}
|
||||
|
||||
// Go... otherwise will throw warning
|
||||
var nmeaFlagsAll = gga | rmc | gpgsv | gpgsa | vtg | xfi | glgsa | gns | gagsv | bdpqgsa | bdpqgsv
|
||||
|
||||
func (g *gps) rawCollect(flags nmeaFlags) (string, error) {
|
||||
// Need to omplement low level write/read here because collect must be atomic operation
|
||||
// If other command is executed(write/read) it will read a part of nmea report and cause an error
|
||||
|
||||
// Setup gps
|
||||
|
||||
// Set output rate to 10Hz
|
||||
if resp, err := g.port.Send("AT+CGPSNMEARATE=1"); err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
if !resp.Check() {
|
||||
return "", fmt.Errorf("set output rate: error response: %s", resp)
|
||||
}
|
||||
}
|
||||
|
||||
g.switchGpsMode(true)
|
||||
g.port.Mutex().Lock()
|
||||
s := g.port.SerialPort()
|
||||
defer func() {
|
||||
g.port.Mutex().Unlock()
|
||||
g.switchGpsMode(false)
|
||||
}()
|
||||
|
||||
// Send AT+CGPSINFOCFG=255, flags
|
||||
flags &= nmeaFlagsMask
|
||||
if _, err := s.Write([]byte("AT+CGPSINFOCFG=1,31\r\n")); err != nil {
|
||||
return "", fmt.Errorf("serial port write 1: %w", err)
|
||||
}
|
||||
// Do I need to read answer
|
||||
|
||||
// Wait
|
||||
secondCountDownTimer("Collecting NMEA data", g.logger, collectTimeout)
|
||||
|
||||
// Send AT+CGPSINFOCFG=0, flags
|
||||
if _, err := s.Write([]byte("AT+CGPSINFOCFG=0,31\r\n")); err != nil {
|
||||
return "", fmt.Errorf("serial port write 2: %w", err)
|
||||
}
|
||||
if _, err := s.Write([]byte("AT+CGPSFTM=0\r\n")); err != nil { // For sure because sometimes it cannot stop...
|
||||
return "", fmt.Errorf("serial port write 2: %w", err)
|
||||
}
|
||||
|
||||
time.Sleep(100 * time.Millisecond) // To enshure
|
||||
|
||||
// Read
|
||||
outBuf := make([]byte, 0)
|
||||
buf := make([]byte, 128)
|
||||
|
||||
readLoop:
|
||||
for {
|
||||
n, err := s.Read(buf)
|
||||
if err != nil {
|
||||
return string(outBuf), fmt.Errorf("serial port read: %w", err)
|
||||
}
|
||||
if n == 0 {
|
||||
break readLoop
|
||||
}
|
||||
outBuf = append(outBuf, buf[:n]...)
|
||||
}
|
||||
|
||||
return string(outBuf), nil
|
||||
}
|
||||
|
||||
func (g *gps) collectNmeaReports(flags nmeaFlags) ([]string, error) {
|
||||
// Raw collect
|
||||
resp, err := g.rawCollect(flags)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("raw collect: %w", err)
|
||||
}
|
||||
|
||||
// DEBUG
|
||||
g.logger.Println("NMEA raw collect:", resp)
|
||||
|
||||
// Right responce struct:
|
||||
// \r\n
|
||||
// OK
|
||||
// \r\n
|
||||
// (NMEA sentence)...
|
||||
// \r\n
|
||||
// OK
|
||||
// \r\n
|
||||
strs := strings.Split(strings.Replace(resp, "\r", "", -1), "\n")
|
||||
|
||||
// Check
|
||||
// Now wait for:
|
||||
// OK
|
||||
// (NMEA sentence)...
|
||||
// OK
|
||||
// if len(strs) < 2 {
|
||||
// return nil, fmt.Errorf("responce too few rows: %d", len(strs))
|
||||
// }
|
||||
// if !(strs[0] == "OK" && strs[len(strs)-1] == "OK") {
|
||||
// return nil, fmt.Errorf("not OK responce: [% s]", strs)
|
||||
// }
|
||||
|
||||
// This... response is not stable
|
||||
// Every time it gives one or two OK and in ramdom order
|
||||
// So I will not check gor it
|
||||
return strs, nil
|
||||
}
|
151
api/modem/gps/status.go
Normal file
151
api/modem/gps/status.go
Normal file
@ -0,0 +1,151 @@
|
||||
package gps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Status struct {
|
||||
GotResponses bool `json:"gotResponses"`
|
||||
IsValidData bool `json:"isValidData"`
|
||||
FoundSatelitesCount int `json:"foundSatelitesCount"`
|
||||
ActiveSatelitesCount int `json:"activeSatelitesCount"`
|
||||
Rms float32 `json:"rms"` // Root mean square
|
||||
}
|
||||
|
||||
var StatusNil = Status{
|
||||
GotResponses: false,
|
||||
IsValidData: false,
|
||||
FoundSatelitesCount: 0,
|
||||
ActiveSatelitesCount: 0,
|
||||
Rms: 0,
|
||||
}
|
||||
|
||||
func (g *gps) GetStatus() (Status, error) {
|
||||
// Provides more information about signal and possible problems using NMEA reports
|
||||
|
||||
// Collect reports
|
||||
reports, err := g.collectNmeaReports(nmeaFlagsAll) // Now minimum
|
||||
if err != nil {
|
||||
return StatusNil, fmt.Errorf("collect nmea reports: %w", err)
|
||||
}
|
||||
|
||||
// Annalise
|
||||
st := Status{}
|
||||
|
||||
checkLoop:
|
||||
for _, s := range reports {
|
||||
// Check for NMEA format
|
||||
if len(s) < 1 || s[0] != '$' {
|
||||
continue checkLoop
|
||||
}
|
||||
st.GotResponses = true
|
||||
|
||||
// g.logger.Println("NMEA check:", s)
|
||||
values := strings.Split(s, ",")
|
||||
if len(values[0]) != 6 {
|
||||
return StatusNil, fmt.Errorf("nmea invalid sentence: %s", s)
|
||||
}
|
||||
switch values[0][3:] { // Switch by content
|
||||
case "GSV": // Any satelites
|
||||
// g.logger.Println("check GSV")
|
||||
// Check len
|
||||
if len(values) < 17 {
|
||||
g.logger.Println("GSV too small values")
|
||||
continue checkLoop
|
||||
}
|
||||
|
||||
// Decode
|
||||
// 0 - msg type
|
||||
// 1 - number of msgs
|
||||
// 2 - index of this msg
|
||||
// 3 - number of visible satelites
|
||||
// 4: - other data
|
||||
|
||||
// Msg index
|
||||
index, err := strconv.Atoi(values[3])
|
||||
if err != nil {
|
||||
g.logger.Println("GSV too small values")
|
||||
continue checkLoop
|
||||
}
|
||||
_ = index
|
||||
// if index != 0 {
|
||||
// g.logger.Println("discard not first GSV msg")
|
||||
// continue checkLoop
|
||||
// }
|
||||
|
||||
// Count
|
||||
satCount, err := strconv.Atoi(values[4])
|
||||
if err != nil {
|
||||
g.logger.Println("GSV too small values")
|
||||
continue checkLoop
|
||||
}
|
||||
st.FoundSatelitesCount = satCount
|
||||
case "GSA": // Active satelites
|
||||
// g.logger.Println("check GSA")
|
||||
|
||||
// Check len
|
||||
if len(values) < 17 {
|
||||
g.logger.Println("GSV too small values")
|
||||
continue checkLoop
|
||||
}
|
||||
|
||||
// Decode
|
||||
// 0 - msg type
|
||||
// 1 - mode of selecting format
|
||||
// 2 - mode of selected format
|
||||
// 3:15 - IDs of active satelites
|
||||
// 15: - other data
|
||||
|
||||
// Counting active satelites
|
||||
count := 0
|
||||
for _, v := range values[3:15] {
|
||||
if _, err := strconv.Atoi(v); err == nil {
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
st.ActiveSatelitesCount = max(st.ActiveSatelitesCount, count)
|
||||
case "RMC": // Minimum GPS data
|
||||
// g.logger.Println("check RMC")
|
||||
// Check len
|
||||
if len(values) < 12 {
|
||||
g.logger.Println("RMC too small values")
|
||||
continue checkLoop
|
||||
}
|
||||
|
||||
// Decode
|
||||
// 0 - msg type
|
||||
// 1 - time
|
||||
// 2 - is data valid or not
|
||||
// 3: - other data
|
||||
|
||||
// Is valid value
|
||||
if values[2] == "A" {
|
||||
st.IsValidData = true
|
||||
}
|
||||
case "GST":
|
||||
// g.logger.Println("check GST")
|
||||
// Check len
|
||||
if len(values) < 8 {
|
||||
g.logger.Println("GST too small values")
|
||||
continue checkLoop
|
||||
}
|
||||
|
||||
// Decode
|
||||
// 0 - msg type
|
||||
// 1 - time
|
||||
// 2 - Root Mean Square
|
||||
// 3: - other data
|
||||
|
||||
rms, err := strconv.ParseFloat(values[2], 32)
|
||||
if err != nil {
|
||||
g.logger.Println("RMS decode:", err.Error())
|
||||
continue checkLoop
|
||||
}
|
||||
st.Rms = float32(rms)
|
||||
}
|
||||
}
|
||||
|
||||
return st, nil
|
||||
}
|
310
api/modem/internet/ic.go
Normal file
310
api/modem/internet/ic.go
Normal file
@ -0,0 +1,310 @@
|
||||
package internet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/at"
|
||||
)
|
||||
|
||||
const (
|
||||
pingPacketsCount = 3
|
||||
pingTimeout = 5
|
||||
inetConnectedTimeout = 4 * time.Second
|
||||
pingAddr = "8.8.8.8"
|
||||
ifName = "ppp0" // Interface name
|
||||
inetMetric = 2000
|
||||
)
|
||||
|
||||
type conn struct {
|
||||
logger *log.Logger
|
||||
port at.Port
|
||||
|
||||
pppPort string
|
||||
|
||||
isConnectExecuted bool
|
||||
isInited bool
|
||||
isRouteSet bool
|
||||
|
||||
connectTime time.Time
|
||||
gw string // Gateway
|
||||
}
|
||||
|
||||
type Conn interface {
|
||||
Init(pppPort string) error
|
||||
|
||||
Connect() error
|
||||
Disconnect() error
|
||||
|
||||
SetDefaultRouteTable() error
|
||||
UnsetDefaultRouteTable() error
|
||||
|
||||
IsConnected() bool // Check interface existance
|
||||
Ping() error
|
||||
|
||||
io.Closer
|
||||
}
|
||||
|
||||
func New(logger *log.Logger, port at.Port) Conn {
|
||||
return &conn{
|
||||
logger: logger,
|
||||
port: port,
|
||||
|
||||
isConnectExecuted: false,
|
||||
isInited: false,
|
||||
isRouteSet: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *conn) Init(pppPort string) error {
|
||||
c.pppPort = pppPort
|
||||
// Setup only setup
|
||||
if err := c.setup(); err != nil {
|
||||
return fmt.Errorf("setup: %w", err)
|
||||
}
|
||||
c.isInited = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) Connect() error {
|
||||
if !c.isInited {
|
||||
return fmt.Errorf("internet submodule is not inited")
|
||||
}
|
||||
// Check is already connected
|
||||
if c.isConnectExecuted {
|
||||
return fmt.Errorf("already connected")
|
||||
}
|
||||
// Check signal
|
||||
|
||||
resp, err := exec.Command("pon", pppConfigName).Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("execute pon cmd: %w", err)
|
||||
}
|
||||
if len(resp) > 0 {
|
||||
c.logger.Println("pon response:", string(resp))
|
||||
}
|
||||
|
||||
c.isConnectExecuted = true
|
||||
c.connectTime = time.Now()
|
||||
c.gw, err = c.GetHostIp()
|
||||
if err != nil {
|
||||
return fmt.Errorf("get host ip: %w", err)
|
||||
}
|
||||
c.logger.Println("\x1b[38;2;0;255;0mInternet connected.\x1b[38;2;255;255;255m")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) Disconnect() error {
|
||||
// return nil // Temporary do not turn off inet
|
||||
if !c.isConnectExecuted {
|
||||
return nil
|
||||
// return fmt.Errorf("internet is not connected")
|
||||
}
|
||||
c.isConnectExecuted = false
|
||||
resp, err := exec.Command("poff", pppConfigName).Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("execute poff cmd: %w", err)
|
||||
}
|
||||
if len(resp) > 0 {
|
||||
c.logger.Println("poff response:", string(resp))
|
||||
}
|
||||
c.isConnectExecuted = false
|
||||
c.logger.Println("\x1b[38;2;0;255;0mInternet disconnected.\x1b[38;2;255;255;255m")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) SetDefaultRouteTable() error {
|
||||
// route add -net default gw 10.64.64.64 metric 2000 dev ppp0
|
||||
resp, err := exec.Command("route", "add", "-net", "default", "gw", c.gw, "metric", strconv.Itoa(inetMetric), "dev", ifName).Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("execute add cmd: %w", err)
|
||||
}
|
||||
// Check response
|
||||
if len(resp) != 0 {
|
||||
c.logger.Println("Not nil response:", string(resp))
|
||||
}
|
||||
c.isRouteSet = true
|
||||
c.logger.Println("\x1b[38;2;0;255;0mInternet route table set.\x1b[38;2;255;255;255m")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) UnsetDefaultRouteTable() error {
|
||||
if !c.isRouteSet {
|
||||
return fmt.Errorf("route table is not set")
|
||||
}
|
||||
resp, err := exec.Command("route", "del", "-net", "default", "gw", c.gw, "metric", strconv.Itoa(inetMetric), "dev", ifName).Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("execute del cmd: %w", err)
|
||||
}
|
||||
// Check response
|
||||
if len(resp) != 0 {
|
||||
c.logger.Println("Not nil response:", string(resp))
|
||||
}
|
||||
c.logger.Println("\x1b[38;2;0;255;0mInternet route table unset.\x1b[38;2;255;255;255m")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) ping(flags []string, timeout int) error {
|
||||
c.logger.Println("Ping", flags[len(flags)-1])
|
||||
|
||||
// Just counter
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go func(c *conn, ctx context.Context) {
|
||||
for i := 0; i < timeout; i++ {
|
||||
c.logger.Printf("Ping %d/%d", i, timeout)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(time.Second):
|
||||
}
|
||||
}
|
||||
}(c, ctx)
|
||||
|
||||
// Executing cmd
|
||||
cmd := exec.Command("ping", flags...)
|
||||
resp, err := cmd.Output()
|
||||
cancel()
|
||||
if err != nil {
|
||||
c.logger.Println("Ping default interface cmd error:", err)
|
||||
}
|
||||
|
||||
// Parse
|
||||
lines := strings.Split(string(resp), "\n")
|
||||
|
||||
// Look for string "--- *.*.*.* ping statistics ---" by first simbol '-'
|
||||
stLineI := 0
|
||||
searchStLineLoop:
|
||||
for i, l := range lines {
|
||||
if len(l) > 0 && l[0] == '-' {
|
||||
stLineI = i + 1
|
||||
break searchStLineLoop
|
||||
}
|
||||
}
|
||||
if stLineI == 0 || stLineI >= len(lines) {
|
||||
return fmt.Errorf("failed to find statistics line: %d", stLineI)
|
||||
}
|
||||
stStr := lines[stLineI]
|
||||
|
||||
// Get third value "packet lost"
|
||||
values := strings.Split(stStr, ",")
|
||||
if len(values) < 3 {
|
||||
return fmt.Errorf("invalid statistic values(len): [%s]", values)
|
||||
}
|
||||
|
||||
// Get number
|
||||
words := strings.Split(values[2], " ")
|
||||
if len(words) < 2 {
|
||||
return fmt.Errorf("invalid \"packets lost\" value(words count): [%s]", words)
|
||||
}
|
||||
// First is ''
|
||||
// Second is '...%'
|
||||
packetsLost, err := strconv.Atoi(words[1][:len(words[1])-1]) // Without '%' char
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse \"packets lost\" value: %w", err)
|
||||
}
|
||||
if packetsLost == 100 {
|
||||
return fmt.Errorf("lost all packages")
|
||||
}
|
||||
if packetsLost > 0 {
|
||||
c.logger.Printf("lost some packets: %d%%\n", packetsLost)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) GetHostIp() (string, error) {
|
||||
if !c.isConnectExecuted {
|
||||
return "", fmt.Errorf("internet not connected")
|
||||
}
|
||||
// Wait some time for system to setup route table
|
||||
time.Sleep(time.Until(c.connectTime.Add(inetConnectedTimeout)))
|
||||
|
||||
// Execute cmd
|
||||
resp, err := exec.Command("route").Output()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("exec route cmd: %w", err)
|
||||
}
|
||||
// Check and split to lines
|
||||
lines := strings.Split(string(resp), "\n")
|
||||
if len(lines) <= 3 || lines[0] != "Kernel IP routing table" {
|
||||
return "", fmt.Errorf("invalid route response: [% s]", lines)
|
||||
}
|
||||
// Search line about ppp interface
|
||||
searchLoop:
|
||||
for _, l := range lines[1:] {
|
||||
words := strings.Fields(l)
|
||||
if len(words) != 8 {
|
||||
/// c.logger.Printf("invalid route line(words number): [%s]\n", words)
|
||||
continue searchLoop
|
||||
}
|
||||
if words[7] == ifName {
|
||||
if words[3] != "UH" {
|
||||
// c.logger.Println("invalid flags")
|
||||
continue searchLoop
|
||||
}
|
||||
return words[0], nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("found no suitable ppp interface")
|
||||
}
|
||||
|
||||
func (c *conn) PingDefault() error {
|
||||
return c.ping([]string{"-c", strconv.Itoa(pingPacketsCount), "-w", strconv.Itoa(pingTimeout), pingAddr}, pingTimeout)
|
||||
}
|
||||
|
||||
func (c *conn) PingPPP() error {
|
||||
return c.ping([]string{"-I", ifName, "-c", string(pingPacketsCount), "-w", string(pingTimeout), pingAddr}, pingTimeout)
|
||||
}
|
||||
|
||||
func (c *conn) Ping() error {
|
||||
return c.PingDefault()
|
||||
}
|
||||
|
||||
func (c *conn) IsConnected() bool {
|
||||
if !c.isConnectExecuted {
|
||||
return false
|
||||
}
|
||||
// Make "ifconfig" request
|
||||
resp, err := exec.Command("ifconfig").Output()
|
||||
if err != nil {
|
||||
c.logger.Println("ifconfig cmd error:", err.Error())
|
||||
return false
|
||||
}
|
||||
lines := strings.Split(string(resp), "\n")
|
||||
for _, l := range lines {
|
||||
if len(l) == 0 {
|
||||
continue
|
||||
}
|
||||
if l[0] == ' ' {
|
||||
continue
|
||||
}
|
||||
interfaceName := strings.Split(l, ":")[0]
|
||||
if interfaceName == ifName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false // Did not found
|
||||
}
|
||||
|
||||
func (c *conn) Close() error {
|
||||
c.isInited = false
|
||||
// Unset route table
|
||||
if c.isRouteSet {
|
||||
if err := c.UnsetDefaultRouteTable(); err != nil {
|
||||
c.logger.Println("unset route table error:", err.Error())
|
||||
}
|
||||
}
|
||||
// Disconnect
|
||||
if c.isConnectExecuted {
|
||||
if err := c.Disconnect(); err != nil {
|
||||
c.logger.Println("diconnect error:", err.Error())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
131
api/modem/internet/setup.go
Normal file
131
api/modem/internet/setup.go
Normal file
@ -0,0 +1,131 @@
|
||||
package internet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/utils"
|
||||
)
|
||||
|
||||
var apns = map[string]string{
|
||||
"Tinkoff": "m.tinkoff",
|
||||
"Megafon": "internet",
|
||||
"BeeLine": "internet.beeline.ru",
|
||||
}
|
||||
|
||||
const pppConfigName = "annalistnet"
|
||||
const pppConfig = `
|
||||
# Annalist project custom internet connection
|
||||
|
||||
# APN:
|
||||
connect "/usr/sbin/chat -v -f /etc/chatscripts/gprs -T %s"
|
||||
|
||||
# Port:
|
||||
%s
|
||||
|
||||
# Baudrate:
|
||||
%d
|
||||
|
||||
noipdefault
|
||||
usepeerdns
|
||||
defaultroute
|
||||
persist
|
||||
noauth
|
||||
nocrtscts
|
||||
local
|
||||
`
|
||||
|
||||
func (c *conn) setup() error {
|
||||
if err := c.checkReqs(); err != nil {
|
||||
return fmt.Errorf("check requirenments: %w", err)
|
||||
}
|
||||
// Configure ppp
|
||||
// what is better ASK If /etc/ppp/peers/annalistnet not exists
|
||||
if err := c.configurePPP(); err != nil {
|
||||
return fmt.Errorf("configure ppp: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check requirenments
|
||||
func (c *conn) checkReqs() error {
|
||||
// Check AT port for sure
|
||||
if c.port == nil || !c.port.IsConnected() {
|
||||
return fmt.Errorf("AT port is not connect or nil")
|
||||
}
|
||||
|
||||
// Ensure all necessary packages installed
|
||||
if err := c.ensurePackage("ppp"); err != nil {
|
||||
return fmt.Errorf("ensure ppp package: %w", err)
|
||||
}
|
||||
// if err := c.ensurePackage("net-tools"); err != nil {
|
||||
// return fmt.Errorf("ensure net-tools package: %w", err)
|
||||
// }
|
||||
|
||||
// Check SIM is valid
|
||||
if err := utils.CheckPIN(c.port, c.logger); err != nil {
|
||||
return fmt.Errorf("PIN check: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) ensurePackage(pname string) error {
|
||||
if c.checkPackageExist(pname) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("package %s not installed", pname)
|
||||
// c.logger.Println("Installing", pname, "package...")
|
||||
// resp, err := exec.Command("apt-get", "install", pname).Output()
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("execute install cmd: %w", err)
|
||||
// }
|
||||
// c.logger.Println(resp)
|
||||
// c.logger.Println("\x1b[38;2;255;0;0mComplete\x1b[38;2;255;255;255m")
|
||||
// return nil
|
||||
}
|
||||
|
||||
func (c *conn) checkPackageExist(pname string) bool {
|
||||
resp, err := exec.Command("apt-mark", "showmanual", pname).Output()
|
||||
if err != nil {
|
||||
c.logger.Println("check package error: ", err.Error())
|
||||
return false
|
||||
}
|
||||
return string(resp[:len(pname)]) == pname
|
||||
}
|
||||
|
||||
func (c *conn) configurePPP() error {
|
||||
// Get provider name and its APN
|
||||
resp, err := c.port.Send("AT+CSPN?")
|
||||
if err != nil {
|
||||
return fmt.Errorf("AT+CSPN? request: %w", err)
|
||||
}
|
||||
if !resp.Check() {
|
||||
return fmt.Errorf("get provider: error response: %s", resp)
|
||||
}
|
||||
strs := strings.Split(string(resp), "\"")
|
||||
if len(strs) < 3 {
|
||||
return fmt.Errorf("parse AT+CSPN response: %s", string(resp))
|
||||
}
|
||||
provider := strs[1]
|
||||
apn := apns[provider]
|
||||
if apn == "" {
|
||||
return fmt.Errorf("no apn for provider: %s", provider)
|
||||
}
|
||||
|
||||
// Make config
|
||||
c.logger.Printf("Config ppp values: %s, %s, %d", apn, c.pppPort, c.port.GetBaudrate())
|
||||
config := fmt.Sprintf(pppConfig, apn, c.pppPort, c.port.GetBaudrate())
|
||||
|
||||
// Write to file
|
||||
f, err := os.OpenFile("/etc/ppp/peers/"+pppConfigName, os.O_CREATE|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open ppp config file %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
if _, err := f.Write([]byte(config)); err != nil {
|
||||
return fmt.Errorf("write to ppp config file: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,172 +1,315 @@
|
||||
package modem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/CGSG-2021-AE4/modem-test/api/modem/at"
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/at"
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/gpio"
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/gps"
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/internet"
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/sms"
|
||||
)
|
||||
|
||||
// yy/MM/dd,hh:mm:ss+zzzz
|
||||
const timeLayout = "06/01/02,15:04:05-0700"
|
||||
|
||||
var ttyPorts = []string{"ttyUSB1", "ttyUSB2", "ttyUSB3", "ttyS0", "ttyAMA2"}
|
||||
var availableModels = []string{"SIMCOM_SIM7600E-H", "SIMCOM_SIM808"}
|
||||
|
||||
type ModemData struct {
|
||||
Port string `json:"Port"`
|
||||
GpsData
|
||||
gps.Data
|
||||
}
|
||||
|
||||
type modem struct {
|
||||
// Internal values
|
||||
logger *log.Logger
|
||||
mutex sync.Mutex
|
||||
|
||||
// Serial values
|
||||
baudrate int
|
||||
|
||||
baudrate int
|
||||
deviceName string
|
||||
port at.Port
|
||||
model string
|
||||
|
||||
// Gpio values
|
||||
onOffPin gpioPin
|
||||
onOffPin gpio.Pin // For turning on and off
|
||||
|
||||
// Other values
|
||||
gpsInfo GpsData
|
||||
lastUpdateTime time.Time
|
||||
|
||||
// GPS
|
||||
gps gps.Gps
|
||||
|
||||
// Internet connection
|
||||
ic internet.Conn
|
||||
|
||||
// Sms and calls
|
||||
sms sms.Sms
|
||||
}
|
||||
|
||||
type Modem interface {
|
||||
Init() error
|
||||
Validate() bool
|
||||
IsConnected() bool
|
||||
Update() error
|
||||
GetInfo() ModemData
|
||||
GetData() ModemData
|
||||
GetTime() (time.Time, error)
|
||||
|
||||
PowerOn() error
|
||||
PowerOnCtx(ctx context.Context) error // Because it takes ~30 seconds
|
||||
PowerOff() error
|
||||
|
||||
// Access to SMS, GPS, AT interfaces mostly for debug
|
||||
At() at.Port // Send
|
||||
Gps() gps.Gps // Update, GetData, GetStatus
|
||||
Sms() sms.Sms // Send, ReadNew
|
||||
Ic() internet.Conn // Connect, Disconnect
|
||||
|
||||
io.Closer
|
||||
}
|
||||
|
||||
func New(logger *log.Logger) Modem {
|
||||
return &modem{
|
||||
logger: logger,
|
||||
baudrate: 115200,
|
||||
onOffPin: gpioPin{Pin: 6},
|
||||
logger: logger,
|
||||
baudrate: 115200,
|
||||
onOffPin: gpio.New(log.New(logger.Writer(), "gpio", log.LstdFlags), 6),
|
||||
|
||||
lastUpdateTime: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *modem) Init() error {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
|
||||
// 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()
|
||||
m.logger.Println("=============================== Init gpio")
|
||||
if err := m.onOffPin.Init(); err != nil {
|
||||
return fmt.Errorf("gpio pin init: %w", err)
|
||||
}
|
||||
|
||||
// Search
|
||||
m.logger.Println("=============================== Search")
|
||||
// Soft search
|
||||
if err := m.searchPort(true); err != nil {
|
||||
ports, err := m.searchPort(true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("soft port search: %w", err)
|
||||
}
|
||||
// Wide search
|
||||
if m.port == nil {
|
||||
if err := m.searchPort(false); err != nil {
|
||||
return fmt.Errorf("not soft port search: %w", err)
|
||||
if len(ports) == 0 {
|
||||
// Wide search
|
||||
ports, err := m.searchPort(true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("wide port search: %w", err)
|
||||
}
|
||||
if len(ports) == 0 {
|
||||
return fmt.Errorf("no AT ports found")
|
||||
}
|
||||
}
|
||||
if m.port == nil {
|
||||
return fmt.Errorf("no port is detected")
|
||||
if len(ports) == 1 {
|
||||
// return fmt.Errorf("only one AT port found")
|
||||
m.logger.Println("!!!!! only one AT port found")
|
||||
}
|
||||
|
||||
// !!!!
|
||||
// Internet connection and serial connection on one port is impossible, so:
|
||||
// port[0] is for serial
|
||||
// port[1] is for internet(ppp)
|
||||
m.logger.Println(ports)
|
||||
|
||||
// Connect
|
||||
m.logger.Println("=============================== Connect")
|
||||
m.port = at.New(m.logger, ports[0], m.baudrate)
|
||||
if err := m.connect(); err != nil {
|
||||
return fmt.Errorf("connect: %w", err)
|
||||
}
|
||||
|
||||
// Tests
|
||||
m.logger.Println("=============================== Test")
|
||||
if err := m.testGPS(); err != nil {
|
||||
return fmt.Errorf("testGPS: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// Init submodules
|
||||
submodulesLogger := m.logger.Writer() // FOR more logs
|
||||
// submodulesLogger := io.Discard // FOR less logs
|
||||
|
||||
func (m *modem) Validate() bool {
|
||||
return m.isConnected()
|
||||
m.logger.Println("=============================== Init submodules")
|
||||
m.ic = internet.New(log.New(submodulesLogger, "modem-internet : ", log.LstdFlags), m.port)
|
||||
if err := m.ic.Init(ports[len(ports)-1]); err != nil {
|
||||
m.logger.Printf("\x1b[38;2;255;0;0mInternet: %s\x1b[38;2;255;255;255m\n", err.Error())
|
||||
} else {
|
||||
m.logger.Println("\x1b[38;2;0;255;0mInternet OK\x1b[38;2;255;255;255m")
|
||||
}
|
||||
|
||||
// m.sms = sms.New(log.New(submodulesLogger, "modem-sms : ", log.LstdFlags), m.port)
|
||||
// if err := m.sms.Init(); err != nil {
|
||||
// m.logger.Printf("\x1b[38;2;255;0;0mSMS: %s\x1b[38;2;255;255;255m\n", err.Error())
|
||||
// } else {
|
||||
// m.logger.Println("\x1b[38;2;0;255;0mSMS OK\x1b[38;2;255;255;255m")
|
||||
// }
|
||||
|
||||
m.gps = gps.New(log.New(submodulesLogger, "modem-gps : ", log.LstdFlags), m.port)
|
||||
if err := m.gps.Init(); err != nil {
|
||||
m.logger.Printf("\x1b[38;2;255;0;0mgps init %w\x1b[38;2;255;255;255m\n", err)
|
||||
} else {
|
||||
m.logger.Println("\x1b[38;2;0;255;0mGPS OK\x1b[38;2;255;255;255m")
|
||||
}
|
||||
|
||||
// Tests
|
||||
// GPS works fine but almost always there is no signal
|
||||
// m.logger.Println("=============================== Test")
|
||||
// if err := m.testGPS(); err != nil {
|
||||
// return fmt.Errorf("testGPS: %w", err)
|
||||
// }
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *modem) Update() error {
|
||||
if !m.isConnected() {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
|
||||
if !m.IsConnected() {
|
||||
m.logger.Println("No connection to module")
|
||||
return nil
|
||||
}
|
||||
m.logger.Println("Update")
|
||||
// ans, err := m.port.Request(at.CmdQuestion, "CGPSINFO")
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("check GPS info mode: %w", err)
|
||||
// }
|
||||
// ok := ans == "1"
|
||||
// if !ok {
|
||||
// _, err := m.port.Request(at.CmdCheck, "CGPSINFO")
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("switch to GPS info mode: %w", err)
|
||||
// }
|
||||
// m.logger.Println("switched to GPS mode")
|
||||
// } else {
|
||||
// m.logger.Println("mode in right GPS mode")
|
||||
// }
|
||||
m.logger.Println("Update", m.gps)
|
||||
if err := m.gps.Update(); err != nil {
|
||||
m.logger.Println("gps update:", err.Error())
|
||||
}
|
||||
// Read new messages
|
||||
|
||||
// Update
|
||||
m.logger.Println("Receiving GPS data...")
|
||||
resp, err := m.port.Send("AT+CGPSINFO")
|
||||
if err != nil {
|
||||
return fmt.Errorf("receive GPS data: %w", err)
|
||||
}
|
||||
if !resp.Check() {
|
||||
return fmt.Errorf("error response")
|
||||
}
|
||||
m.logger.Println("Decoding data...")
|
||||
|
||||
if err := m.gpsInfo.decode(strings.Split(strings.Replace(resp.RmFront("+CGPSINFO:").String(), "\r", "", -1), "\n")[0]); err != nil {
|
||||
m.logger.Println("Gps info decode error:", err.Error())
|
||||
return nil
|
||||
}
|
||||
m.logger.Println("Decoded successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *modem) GetInfo() ModemData {
|
||||
func (m *modem) GetData() ModemData {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
|
||||
return ModemData{
|
||||
Port: m.port.GetName(),
|
||||
GpsData: m.gpsInfo,
|
||||
Port: m.port.GetName(),
|
||||
Data: m.gps.GetData(),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *modem) Close() error {
|
||||
// Not right way I think
|
||||
if err := m.port.Close(); err != nil {
|
||||
return err
|
||||
func (m *modem) GetTime() (time.Time, error) {
|
||||
// Make request
|
||||
resp, err := m.port.Send("AT+CCLK?")
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
if !resp.Check() || !resp.CheckFront("+CCLK: ") {
|
||||
return time.Time{}, fmt.Errorf("CCLK? error response: %s", resp.String())
|
||||
}
|
||||
// Extract time string
|
||||
values := strings.Split(strings.Split(resp.String(), "\n")[0], "\"")
|
||||
if len(values) < 2 {
|
||||
return time.Time{}, fmt.Errorf("invalid values (len): [%s]", values)
|
||||
}
|
||||
timeStr := values[1]
|
||||
m.logger.Println("Raw time:", timeStr)
|
||||
if len(timeStr) != len("yy/MM/dd,hh:mm:ss+zz") {
|
||||
return time.Time{}, fmt.Errorf("invalid time string: %s", timeStr)
|
||||
}
|
||||
// Convert time zone
|
||||
timeZoneStr := timeStr[len(timeStr)-2:]
|
||||
timeZone, err := strconv.Atoi(timeZoneStr)
|
||||
if err != nil {
|
||||
return time.Time{}, fmt.Errorf("parse time zone: %w", err)
|
||||
}
|
||||
timeStr = fmt.Sprintf("%s%02d%02d", timeStr[:len(timeStr)-2], timeZone/4, timeZone%4)
|
||||
// Parse to golang time
|
||||
return time.Parse(timeLayout, timeStr)
|
||||
}
|
||||
|
||||
func (m *modem) PowerOn() error {
|
||||
m.onOffPin.PowerOn() // DEBUG do not want to wait 30 seconds
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *modem) PowerOnCtx(ctx context.Context) error {
|
||||
m.onOffPin.PowerOnCtx(ctx) // DEBUG do not want to wait 30 seconds
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *modem) PowerOff() error {
|
||||
_, err := m.At().Send("AT+CPOF")
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *modem) restart() error {
|
||||
m.PowerOff()
|
||||
time.Sleep(10 * time.Second)
|
||||
m.PowerOn()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *modem) Sms() sms.Sms {
|
||||
return m.sms
|
||||
}
|
||||
|
||||
func (m *modem) Gps() gps.Gps {
|
||||
return m.gps
|
||||
}
|
||||
|
||||
func (m *modem) At() at.Port {
|
||||
return m.port
|
||||
}
|
||||
|
||||
func (m *modem) Ic() internet.Conn {
|
||||
return m.ic
|
||||
}
|
||||
|
||||
func (m *modem) Close() error { // I can't return error so I log it
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
|
||||
// Close submodules
|
||||
if m.sms != nil {
|
||||
if err := m.sms.Close(); err != nil {
|
||||
m.logger.Printf("\x1b[38;2;255;0;0mclose sms error: %s\x1b[38;2;255;255;255m\n", err.Error())
|
||||
}
|
||||
}
|
||||
if m.ic != nil {
|
||||
if err := m.ic.Close(); err != nil {
|
||||
m.logger.Printf("\x1b[38;2;255;0;0mclose internet error: %s\x1b[38;2;255;255;255m\n", err.Error())
|
||||
}
|
||||
}
|
||||
if m.gps != nil {
|
||||
if err := m.gps.Close(); err != nil {
|
||||
m.logger.Printf("\x1b[38;2;255;0;0mclose gps error: %s\x1b[38;2;255;255;255m\n", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Close gpio and serial
|
||||
if err := m.onOffPin.Close(); err != nil {
|
||||
return err
|
||||
m.logger.Printf("\x1b[38;2;255;0;0mclose gpio pin error: %s\x1b[38;2;255;255;255m\n", err.Error())
|
||||
}
|
||||
if err := m.port.Close(); err != nil {
|
||||
m.logger.Printf("\x1b[38;2;255;0;0mclose serial port error: %s\x1b[38;2;255;255;255m\n", err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
///////////// Private functions
|
||||
|
||||
func (m *modem) connect() error {
|
||||
if !m.Validate() {
|
||||
if m.port == nil {
|
||||
return fmt.Errorf("port is not defined")
|
||||
}
|
||||
return m.port.Connect()
|
||||
}
|
||||
|
||||
func (m *modem) disconnect() error {
|
||||
if !m.Validate() {
|
||||
if m.port == nil {
|
||||
return fmt.Errorf("port is not defined")
|
||||
}
|
||||
return m.port.Disconnect()
|
||||
}
|
||||
|
||||
func (m *modem) isConnected() bool {
|
||||
func (m *modem) IsConnected() bool {
|
||||
if m.port != nil {
|
||||
return m.port.IsConnected()
|
||||
}
|
||||
@ -176,10 +319,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)
|
||||
}
|
||||
@ -190,7 +329,8 @@ func (m *modem) testGPS() error {
|
||||
|
||||
// Difference: I do not set \n at the end of string
|
||||
func (m *modem) getShortInfo() string {
|
||||
return fmt.Sprintf("%f,%s,%f,%s", m.gpsInfo.Latitude, m.gpsInfo.LatitudeIndicator, m.gpsInfo.Longitude, m.gpsInfo.LongitudeIndicator)
|
||||
d := m.gps.GetData()
|
||||
return fmt.Sprintf("%f,%s,%f,%s", d.Latitude, d.LatitudeIndicator, d.Longitude, d.LongitudeIndicator)
|
||||
}
|
||||
|
||||
func (m *modem) saveGPS(path string) error {
|
||||
@ -198,7 +338,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 {
|
||||
@ -207,6 +346,67 @@ func (m *modem) saveGPS(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Short way to send command
|
||||
func (m *modem) printCmd(cmd string) {
|
||||
if resp, err := m.port.Send(cmd); err != nil {
|
||||
m.logger.Println("FAILED TO SEND REQ", cmd, "===>", err.Error())
|
||||
} else {
|
||||
_ = resp
|
||||
// m.logger.Println("CMD", cmd, "===>", resp)
|
||||
}
|
||||
}
|
||||
|
||||
// Some required commands before checking port
|
||||
func (m *modem) setupPort() error {
|
||||
// Reset input
|
||||
if err := m.port.SerialPort().ResetInputBuffer(); err != nil {
|
||||
return fmt.Errorf("reset input buffer: %w", err)
|
||||
}
|
||||
// Reset output
|
||||
if err := m.port.SerialPort().ResetOutputBuffer(); err != nil {
|
||||
return fmt.Errorf("reset output buffer: %w", err)
|
||||
}
|
||||
|
||||
// m.restart()
|
||||
// These commands ensure that correct modes are set
|
||||
// m.port.RawSend("\r\n\x1A\r\n") // Sometimes enables echo mode
|
||||
//if m.At().GetName() == "/dev/ttyUSB0" {
|
||||
// buf := make([]byte, 256)
|
||||
// for i := 0; i < 10; i++ {
|
||||
// len, err := m.port.SerialPort().Read(buf)
|
||||
// if err != nil {
|
||||
// m.logger.Println("ERROR:", err.Error())
|
||||
// }
|
||||
// if len != 0 {
|
||||
// m.logger.Println(string(buf[:len]))
|
||||
// }
|
||||
// time.Sleep(time.Second)
|
||||
// m.logger.Println(".")
|
||||
// }
|
||||
//}
|
||||
m.printCmd("AT") // Sometimes enables echo mode
|
||||
m.printCmd("AT") // Sometimes enables echo mode
|
||||
m.printCmd("ATE0") // Sometimes enables echo mode
|
||||
m.printCmd("AT+CGPSFTM=0") // Sometimes does not turn off nmea
|
||||
m.printCmd("AT+CMEE=2") // Turn on errors describtion
|
||||
m.printCmd("AT+CTZU=1") // Turn on time update
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *modem) checkCurPortDead() error {
|
||||
if err := m.port.RawSend("AT\r\n"); err != nil {
|
||||
return fmt.Errorf("raw send: %w", err)
|
||||
}
|
||||
if resp, err := m.port.RawRead(time.Second); err != nil || len(resp) == 0 {
|
||||
if err != nil {
|
||||
return fmt.Errorf("raw read: %w", err)
|
||||
}
|
||||
return fmt.Errorf("read 0")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *modem) checkPort(portName string) (outErr error) {
|
||||
defer func() {
|
||||
if outErr != nil { // Clear port if there is error
|
||||
@ -227,113 +427,99 @@ func (m *modem) checkPort(portName string) (outErr error) {
|
||||
}
|
||||
defer m.port.Disconnect() // Do not bother about errors...
|
||||
|
||||
// m.restart()
|
||||
|
||||
// To filter dead ports
|
||||
|
||||
if err := m.checkCurPortDead(); err != nil {
|
||||
return fmt.Errorf("echo: %w", err)
|
||||
}
|
||||
|
||||
if err := m.setupPort(); err != nil {
|
||||
return fmt.Errorf("setup port: %w", err)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
if !resp.Check() {
|
||||
return fmt.Errorf("error response: %s", resp)
|
||||
return fmt.Errorf("get model: error response: %s", resp)
|
||||
}
|
||||
model := strings.Split(resp.String(), "\n")[0]
|
||||
if err != nil {
|
||||
return fmt.Errorf("get model: %w", err)
|
||||
}
|
||||
rightModel := "SIMCOM_SIM7600E-H"
|
||||
// m.logger.Printf("[% x]\n [% x]", []byte("SIMCOM_SIM7600E-H"), []byte(model))
|
||||
if model[:len(rightModel)] != rightModel {
|
||||
return fmt.Errorf("invalid modem model: %s", model)
|
||||
}
|
||||
m.logger.Println("Model right")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *modem) searchPort(isSoft bool) error {
|
||||
// Get ports
|
||||
ports := []string{"ttyUSB1", "ttyUSB2", "ttyUSB3"}
|
||||
if !isSoft {
|
||||
ps, err := getTtyDevices()
|
||||
if err != nil {
|
||||
fmt.Errorf("get serial devices: %w", err)
|
||||
// Check model
|
||||
foundModel := ""
|
||||
for _, rightModel := range availableModels {
|
||||
if len(model) >= len(rightModel) && model[:len(rightModel)] == rightModel {
|
||||
foundModel = rightModel
|
||||
break
|
||||
}
|
||||
ports = ps
|
||||
}
|
||||
|
||||
// Check ports
|
||||
SearchLoop:
|
||||
for _, p := range ports {
|
||||
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())
|
||||
continue SearchLoop
|
||||
}
|
||||
|
||||
m.logger.Print("Found modem on port: ", p)
|
||||
m.port = at.New(m.logger, "/dev/"+p, m.baudrate)
|
||||
return nil
|
||||
if foundModel == "" {
|
||||
return fmt.Errorf("invalid model: %s", model)
|
||||
}
|
||||
m.model = foundModel
|
||||
m.logger.Println("\x1b[38;2;0;255;0mOK\x1b[38;2;255;255;255m")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *modem) ping() error {
|
||||
resp, err := m.port.Send("AT")
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("AT request: %w", err)
|
||||
}
|
||||
if !resp.Check() {
|
||||
return fmt.Errorf("connection lost")
|
||||
return fmt.Errorf("AT request: error response: %s", resp)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *modem) switchToGpsMode() error {
|
||||
m.logger.Println("Enabling GPS mode...")
|
||||
// Reset intput
|
||||
if err := m.port.GetSerialPort().ResetInputBuffer(); err != nil {
|
||||
return fmt.Errorf("reset input buffer: %w", err)
|
||||
}
|
||||
|
||||
// Check gps mode status
|
||||
resp, err := m.port.Send("AT+CGPS?")
|
||||
func (m *modem) searchPort(isSoft bool) ([]string, error) {
|
||||
// Get ports
|
||||
ports, err := getTtyPorts(isSoft)
|
||||
if err != nil {
|
||||
return fmt.Errorf("make at ask: %w", err)
|
||||
fmt.Errorf("get devices: %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
|
||||
// Check ports
|
||||
return m.getAtPorts(ports)
|
||||
}
|
||||
|
||||
func getTtyDevices() ([]string, error) {
|
||||
func (m *modem) getAtPorts(ports []string) ([]string, error) {
|
||||
outPorts := make([]string, 0)
|
||||
for _, p := range ports {
|
||||
m.logger.Printf("Checking port %s ...\n", p)
|
||||
|
||||
if err := m.checkPort("/dev/" + p); err != nil {
|
||||
m.logger.Printf("\x1b[38;2;255;0;0mCheck failed: %s\x1b[38;2;255;255;255m\n", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
m.logger.Print("Found AT port: ", p)
|
||||
outPorts = append(outPorts, "/dev/"+p)
|
||||
}
|
||||
return outPorts, nil
|
||||
}
|
||||
|
||||
func getTtyPorts(isSoft bool) ([]string, error) {
|
||||
if isSoft {
|
||||
return ttyPorts, nil
|
||||
}
|
||||
// Get ports
|
||||
/**/
|
||||
out, err := exec.Command("ls", "--", "/dev/tty[!0-9]*").Output()
|
||||
out, err := exec.Command("ls", "/dev/tty[!0-9]*").Output()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("execute ls command: %w", err)
|
||||
}
|
||||
|
88
api/modem/sms/setup.go
Normal file
88
api/modem/sms/setup.go
Normal file
@ -0,0 +1,88 @@
|
||||
package sms
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (d *dialer) setupMsgSt() error {
|
||||
// Check for free space for messages
|
||||
// !!! I use one! memory for all three bindings
|
||||
// 1: read and delete
|
||||
// 2: sending
|
||||
// 3: write received
|
||||
|
||||
// First try SM
|
||||
if _, err := d.port.Send(`AT+CPMS="SM","SM","SM"`); err != nil {
|
||||
return fmt.Errorf(`AT+CPMS="SM","SM","SM" request: %w`, err)
|
||||
}
|
||||
st, err := d.getCurMsgStSize()
|
||||
if err != nil {
|
||||
return fmt.Errorf("SM: %w", err)
|
||||
}
|
||||
if st[0].Used < st[0].Total {
|
||||
d.logger.Printf("Use SM message storage: %d/%d\n", st[0].Used, st[0].Total)
|
||||
return nil // There is space
|
||||
}
|
||||
d.logger.Printf("SM message storage is full: %d/%d\n", st[0].Used, st[0].Total)
|
||||
|
||||
// Second try ME
|
||||
if _, err := d.port.Send(`AT+CPMS="ME","ME","ME"`); err != nil {
|
||||
return fmt.Errorf(`AT+CPMS="ME","ME","ME" request: %w`, err)
|
||||
}
|
||||
st, err = d.getCurMsgStSize()
|
||||
if err != nil {
|
||||
return fmt.Errorf("ME: %w", err)
|
||||
}
|
||||
if st[0].Used < st[0].Total {
|
||||
d.logger.Printf("Use ME message storage: %d/%d\n", st[0].Used, st[0].Total)
|
||||
return nil // There is space
|
||||
}
|
||||
d.logger.Printf("ME message storage is full: %d/%d\n", st[0].Used, st[0].Total)
|
||||
// Otherwise error
|
||||
return fmt.Errorf("all storages are full")
|
||||
}
|
||||
|
||||
// Message storage
|
||||
type msgSt struct {
|
||||
Name string
|
||||
Used int
|
||||
Total int
|
||||
}
|
||||
|
||||
// Get size of used and total mem of current memory storage
|
||||
func (d *dialer) getCurMsgStSize() ([]msgSt, error) {
|
||||
// Request
|
||||
resp, err := d.port.Send("AT+CPMS?")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("AT+CPMS? request: %w", err)
|
||||
}
|
||||
// Check start and end
|
||||
if !resp.Check() && !resp.CheckFront("+CPMS:") {
|
||||
return nil, fmt.Errorf("AT+CPMS")
|
||||
}
|
||||
// Remove front and cut to values
|
||||
resp = resp.RmFront("+CPMS:")
|
||||
values := strings.Split(strings.ReplaceAll(strings.Split(resp.String(), "\n")[0], "\r", ""), ",")
|
||||
if len(values) != 9 {
|
||||
return nil, fmt.Errorf("CPMS response: invalid values count: [%s]", values)
|
||||
}
|
||||
|
||||
// Parse values
|
||||
outMsgs := [3]msgSt{}
|
||||
for i := 0; i < 3; i++ {
|
||||
name := values[i]
|
||||
used, err := strconv.Atoi(values[i*3+1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse value #%d: %w", i+1, err)
|
||||
}
|
||||
total, err := strconv.Atoi(values[i*3+2])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse value #%d, %w", i+2, err)
|
||||
}
|
||||
|
||||
outMsgs[i] = msgSt{name, used, total}
|
||||
}
|
||||
return outMsgs[:], nil
|
||||
}
|
111
api/modem/sms/sms.go
Normal file
111
api/modem/sms/sms.go
Normal file
@ -0,0 +1,111 @@
|
||||
package sms
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/at"
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/utils"
|
||||
)
|
||||
|
||||
type dialer struct {
|
||||
logger *log.Logger
|
||||
port at.Port
|
||||
}
|
||||
|
||||
type Sms interface {
|
||||
Init() error
|
||||
Send(number, msg string) error // Send sms
|
||||
ReadNew() ([]string, error) // Read new smses
|
||||
|
||||
io.Closer
|
||||
}
|
||||
|
||||
func New(logger *log.Logger, port at.Port) Sms {
|
||||
return &dialer{
|
||||
logger: logger,
|
||||
port: port,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dialer) Init() error {
|
||||
// Ensure serial port is connected
|
||||
if !d.port.IsConnected() {
|
||||
return fmt.Errorf("serial port is not connected")
|
||||
}
|
||||
// Check ping
|
||||
if err := utils.CheckPIN(d.port, d.logger); err != nil {
|
||||
return fmt.Errorf("check PIN: %w", err)
|
||||
}
|
||||
// Setup prefered message storage
|
||||
// if err := d.setupMsgSt(); err != nil { // Does not use store now
|
||||
// d.logger.Printf("ERROR setup msg storage: %s\n", err.Error())
|
||||
// }
|
||||
|
||||
// Set notifications into console
|
||||
if resp, err := d.port.Send("AT+CNMI=2,2"); err != nil || !resp.Check() {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("CNMI= error response: %s", resp.String())
|
||||
}
|
||||
|
||||
// Check number
|
||||
if resp, err := d.port.Send("AT+CNUM"); err != nil || !resp.Check() {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("CNUM error response: %s", resp.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *dialer) Send(number, msg string) error {
|
||||
sresp, err := d.port.Send(fmt.Sprintf(`AT+CMGSEX="%s"`, number)) // Because it will throw error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.logger.Println(sresp)
|
||||
|
||||
// Message body
|
||||
if err := d.port.RawSend(fmt.Sprintf("%s\x1A", msg)); err != nil {
|
||||
return fmt.Errorf("message request: %w", err)
|
||||
}
|
||||
resp, err := d.port.RawRead(at.ReadTimeout)
|
||||
if err != nil {
|
||||
return fmt.Errorf("message request read: %w", err)
|
||||
}
|
||||
d.logger.Println("Send response:", resp)
|
||||
if !at.Resp(resp).Check() {
|
||||
return fmt.Errorf("error response: %s", resp)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Reads all new messages
|
||||
func (d *dialer) ReadNew() ([]string, error) {
|
||||
resp, err := d.port.Send("AT+CMGL=\"ALL\"")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
d.logger.Println("raw sms:", resp)
|
||||
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
|
||||
}
|
37
api/modem/utils/service.go
Normal file
37
api/modem/utils/service.go
Normal file
@ -0,0 +1,37 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/at"
|
||||
)
|
||||
|
||||
func CheckService(port at.Port, logger *log.Logger) (bool, error) {
|
||||
srv, err := getService(port)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("get service: %w", err)
|
||||
}
|
||||
if srv == "NO SERVICE" {
|
||||
return false, nil
|
||||
}
|
||||
logger.Println("Current service:", srv)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Returns service
|
||||
func getService(port at.Port) (string, error) {
|
||||
resp, err := port.Send("AT+CPSI?")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !resp.Check() || !resp.CheckFront("+CPSI: ") {
|
||||
return "", fmt.Errorf("error response: %s", resp)
|
||||
}
|
||||
values := strings.Split(strings.ReplaceAll(strings.Split(resp.RmFront("+CPSI: ").String(), "\n")[0], "\r", ""), ",")
|
||||
if len(values) < 2 {
|
||||
return "", fmt.Errorf("invalid values(len): [% s]", values)
|
||||
}
|
||||
return values[0], nil
|
||||
}
|
109
api/modem/utils/signal.go
Normal file
109
api/modem/utils/signal.go
Normal file
@ -0,0 +1,109 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/at"
|
||||
)
|
||||
|
||||
func CheckSignal(port at.Port, logger *log.Logger) (bool, error) {
|
||||
rssi, ber, err := getSignalQuality(port)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("get signal quality: %w", err)
|
||||
}
|
||||
logger.Printf("check signal: rssi=%d ber=%d\n", rssi, ber)
|
||||
if err := checkRssi(rssi); err != nil {
|
||||
return false, nil
|
||||
}
|
||||
if err := checkBer(ber); err != nil {
|
||||
logger.Printf("bad ber(not critical): %s", err.Error()) // Happened not to be critical
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func checkRssi(rssi int) error {
|
||||
// rssi - Received signal strength indicator
|
||||
// 0 -113 dBm or less
|
||||
// 1 -111 dBm
|
||||
// 2...30 -109... - 53 dBm
|
||||
// 31 -51 dBm or greater
|
||||
// 99 not known or not detectable
|
||||
// 100 -116 dBm or less
|
||||
// 101 -115 dBm
|
||||
// 102…191 -114... - 26dBm
|
||||
// 191 -25 dBm or greater
|
||||
// 199 not known or not detectable
|
||||
// 100…199 expand to TDSCDMA, indicate RSCPreceived
|
||||
|
||||
if rssi <= 2 { // Too poor
|
||||
return fmt.Errorf("too poor <= -109dBm")
|
||||
}
|
||||
if rssi > 2 && rssi <= 31 {
|
||||
return nil // Can live
|
||||
}
|
||||
if rssi == 99 {
|
||||
return fmt.Errorf("not known or not detectable")
|
||||
}
|
||||
if rssi >= 100 && rssi <= 102 {
|
||||
return fmt.Errorf("too poor <= -114dBm")
|
||||
}
|
||||
if rssi > 102 && rssi <= 191 {
|
||||
return nil // Can live
|
||||
}
|
||||
if rssi == 199 {
|
||||
return fmt.Errorf("not known or not detectable")
|
||||
}
|
||||
return fmt.Errorf("invalid code %d", rssi)
|
||||
}
|
||||
|
||||
func checkBer(ber int) error {
|
||||
// ber - Bit error rate
|
||||
// 0 <0.01%
|
||||
// 1 0.01% --- 0.1%
|
||||
// 2 0.1% --- 0.5%
|
||||
// 3 0.5% --- 1.0%
|
||||
// 4 1.0% --- 2.0%
|
||||
// 5 2.0% --- 4.0%
|
||||
// 6 4.0% --- 8.0%
|
||||
// 7 >=8.0%
|
||||
// 99 not known or not detectable
|
||||
if ber >= 0 && ber <= 3 {
|
||||
// ber -> [0%;1%)
|
||||
// Ok, can live
|
||||
return nil
|
||||
}
|
||||
if ber >= 4 && ber <= 7 {
|
||||
return fmt.Errorf("too high: %d code", ber)
|
||||
}
|
||||
if ber == 99 {
|
||||
return fmt.Errorf("not known or not detectable")
|
||||
}
|
||||
return fmt.Errorf("invalid code %d", ber)
|
||||
}
|
||||
|
||||
// Returns rssi and ber(look above)
|
||||
func getSignalQuality(port at.Port) (int, int, error) {
|
||||
resp, err := port.Send("AT+CSQ")
|
||||
if err != nil {
|
||||
return 99, 99, err
|
||||
}
|
||||
if !resp.Check() || !resp.CheckFront("+CSQ: ") {
|
||||
return 99, 99, fmt.Errorf("error response: %s", resp)
|
||||
}
|
||||
values := strings.Split(strings.ReplaceAll(strings.Split(resp.RmFront("+CSQ: ").String(), "\n")[0], "\r", ""), ",")
|
||||
if len(values) != 2 {
|
||||
return 99, 99, fmt.Errorf("invalid values(len): [% s]", values)
|
||||
}
|
||||
rssi, err := strconv.Atoi(values[0])
|
||||
if err != nil {
|
||||
return 99, 99, fmt.Errorf("parse rssi: %w", err)
|
||||
}
|
||||
ber, err := strconv.Atoi(values[1])
|
||||
if err != nil {
|
||||
return 99, 99, fmt.Errorf("parse ber: %w", err)
|
||||
}
|
||||
return rssi, ber, nil
|
||||
}
|
26
api/modem/utils/sim.go
Normal file
26
api/modem/utils/sim.go
Normal file
@ -0,0 +1,26 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/at"
|
||||
)
|
||||
|
||||
func CheckPIN(port at.Port, logger *log.Logger) error {
|
||||
// Get code
|
||||
resp, err := port.Send("AT+CPIN?")
|
||||
if err != nil {
|
||||
return fmt.Errorf("AT+CPIN? request: %w", err)
|
||||
}
|
||||
if !resp.Check() || !resp.CheckFront("+CPIN:") {
|
||||
return fmt.Errorf("AT+CPIN? error response: %s", resp)
|
||||
}
|
||||
code := strings.ReplaceAll(strings.ReplaceAll(strings.Split(resp.RmFront("+CPIN:").String(), "\n")[0], "\r", ""), " ", "")
|
||||
if code != "READY" {
|
||||
return fmt.Errorf("not READY code: %s", code)
|
||||
}
|
||||
logger.Println("PIN is ready")
|
||||
return nil
|
||||
}
|
@ -14,4 +14,4 @@ services:
|
||||
devices:
|
||||
- /dev/i2c-1
|
||||
- /dev/ttyS0
|
||||
restart: unless-stopped
|
||||
restart: unless-stopped
|
||||
|
2
go.mod
2
go.mod
@ -1,4 +1,4 @@
|
||||
module github.com/CGSG-2021-AE4/modem-test
|
||||
module gitea.unprism.ru/KRBL/sim-modem
|
||||
|
||||
go 1.22.5
|
||||
|
||||
|
188
main.go
188
main.go
@ -1,30 +1,200 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/CGSG-2021-AE4/modem-test/api/modem"
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.Println("CGSG forever!!!")
|
||||
if err := mainE(); err != nil {
|
||||
|
||||
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 {
|
||||
log.Println("MAIN finished with error:", err.Error())
|
||||
}
|
||||
log.Println("END")
|
||||
}
|
||||
|
||||
func mainE() error {
|
||||
m := modem.New(log.New(os.Stdout, "modem:", log.LstdFlags))
|
||||
if err := m.Init(); err != nil {
|
||||
return err
|
||||
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
|
||||
// ports, err := serial.GetPortsList()
|
||||
}
|
||||
|
||||
func Cmd(cmd string) {
|
||||
resp, err := m.At().SendWithTimeout(cmd, 10*time.Second) //50*time.Millisecond)
|
||||
logger.Println(cmd, "===>", resp, err)
|
||||
}
|
||||
|
||||
func mainE(ctx context.Context) error {
|
||||
logger = log.New(os.Stdout, "main : ", log.LstdFlags)
|
||||
m = modem.New(log.New(logger.Writer(), "modem : ", log.LstdFlags))
|
||||
|
||||
logger.Println("||||||||||||||||| INIT |||||||||||||||")
|
||||
|
||||
// If power is down modem won't find suitable devices add will try to send powerOn signal and then try again
|
||||
initLoop:
|
||||
for {
|
||||
select {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// Final check for sure
|
||||
if !m.IsConnected() {
|
||||
logger.Println("Modem is not connected")
|
||||
return nil
|
||||
}
|
||||
// Close() deinits everything recursively
|
||||
defer func() {
|
||||
logger.Println("||||||||||||||||| CLOSE |||||||||||||||")
|
||||
m.Close()
|
||||
}()
|
||||
|
||||
// Internet connect
|
||||
// if err := InetInit(); err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
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?")
|
||||
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")
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
logger.Println("Break main loop")
|
||||
return nil
|
||||
default:
|
||||
// Cmd("AT+CPSI?")
|
||||
// Cmd("AT+CIPGSMLOC=1")
|
||||
// Cmd("AT+CSQ")
|
||||
// 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)
|
||||
// logger.Println(m.GetTime())
|
||||
// logger.Println(m.Ic().Ping())
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
// Cmd("AT+CSQ")
|
||||
// Cmd("AT+COPS?")
|
||||
|
||||
// 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]))
|
||||
// }
|
||||
}
|
||||
|
||||
// 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
|
||||
// }
|
||||
// log.Println(ports)
|
||||
// return nil
|
||||
// logger.Printf("GPS Status:%+v\n", st)
|
||||
|
||||
// logger.Println("Turn off", m.PowerOff())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user