Improve gps status.

This commit is contained in:
yotia 2024-08-07 11:35:06 +03:00
parent 0837d376f7
commit 59bd2c5db3
5 changed files with 81 additions and 14 deletions

View File

@ -13,7 +13,7 @@ import (
// Some constants // Some constants
const ( const (
ReadTimeout = time.Second ReadTimeout = time.Second
InputBufSize = 2048 InputBufSize = 512
) )
type atPort struct { type atPort struct {

View File

@ -53,7 +53,7 @@ func (g *gps) Update() error {
return fmt.Errorf("get GPS info: error response: %s", resp) 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 { if err := g.data.decode(strings.Split(strings.Replace(resp.RmFront("+CGPSINFO:").String(), "\r", "", -1), "\n")[0]); err != nil {
return fmt.Errorf("decode: %w", err) g.logger.Printf("error decode: %s\n", err.Error())
} }
return nil return nil
} }

View File

@ -30,7 +30,7 @@ const (
nmeaFlagsMask = (1 << 18) - 1 nmeaFlagsMask = (1 << 18) - 1
collectTimeout = 5 * time.Second collectTimeout = 2 * time.Second
) )
func secondCountDownTimer(title string, logger *log.Logger, t time.Duration) { func secondCountDownTimer(title string, logger *log.Logger, t time.Duration) {

View File

@ -8,14 +8,16 @@ import (
type Status struct { type Status struct {
GotResponses bool `json:"gotResponses"` GotResponses bool `json:"gotResponses"`
IsValidData bool `json:"isValidData"`
FoundSatelitesCount int `json:"foundSatelitesCount"` FoundSatelitesCount int `json:"foundSatelitesCount"`
ActiveSatelitesCounte int `json:"activeSatelitesCount"` ActiveSatelitesCount int `json:"activeSatelitesCount"`
} }
var StatusNil = Status{ var StatusNil = Status{
GotResponses: false, GotResponses: false,
IsValidData: false,
FoundSatelitesCount: 0, FoundSatelitesCount: 0,
ActiveSatelitesCounte: 0, ActiveSatelitesCount: 0,
} }
func (g *gps) GetStatus() (Status, error) { func (g *gps) GetStatus() (Status, error) {
@ -44,17 +46,82 @@ checkLoop:
return StatusNil, fmt.Errorf("nmea invalid sentence: %s", s) return StatusNil, fmt.Errorf("nmea invalid sentence: %s", s)
} }
switch values[0][3:] { // Switch by content switch values[0][3:] { // Switch by content
case "gsv": case "GSV":
g.logger.Println("check GSV")
// Check len
if len(values) < 17 { if len(values) < 17 {
g.logger.Println("GSV too small values") g.logger.Println("GSV too small values")
continue checkLoop continue checkLoop
} }
c, err := strconv.Atoi(values[4])
// 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 { if err != nil {
g.logger.Println("GSV too small values") g.logger.Println("GSV too small values")
continue checkLoop continue checkLoop
} }
st.FoundSatelitesCount += c _ = 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":
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":
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
// Msg index
if values[2] == "A" {
st.IsValidData = true
}
} }
} }

View File

@ -107,8 +107,8 @@ func (m *modem) Init() error {
} }
// Init submodules // Init submodules
// submodulesLogger := m.logger.Writer() // FOR more logs submodulesLogger := m.logger.Writer() // FOR more logs
submodulesLogger := io.Discard // FOR less logs // submodulesLogger := io.Discard // FOR less logs
m.logger.Println("=============================== Init submodules") m.logger.Println("=============================== Init submodules")
m.ic = internet.New(log.New(submodulesLogger, "modem-internet : ", log.LstdFlags), m.port) m.ic = internet.New(log.New(submodulesLogger, "modem-internet : ", log.LstdFlags), m.port)