diff --git a/api/modem/at/at.go b/api/modem/at/at.go index 0c9dc0f..3ea0297 100644 --- a/api/modem/at/at.go +++ b/api/modem/at/at.go @@ -13,7 +13,7 @@ import ( // Some constants const ( ReadTimeout = time.Second - InputBufSize = 2048 + InputBufSize = 512 ) type atPort struct { diff --git a/api/modem/gps/gps.go b/api/modem/gps/gps.go index 2ff1115..8bd0236 100644 --- a/api/modem/gps/gps.go +++ b/api/modem/gps/gps.go @@ -53,7 +53,7 @@ func (g *gps) Update() error { 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 { - return fmt.Errorf("decode: %w", err) + g.logger.Printf("error decode: %s\n", err.Error()) } return nil } diff --git a/api/modem/gps/nmea.go b/api/modem/gps/nmea.go index 061fc20..082e6c2 100644 --- a/api/modem/gps/nmea.go +++ b/api/modem/gps/nmea.go @@ -30,7 +30,7 @@ const ( nmeaFlagsMask = (1 << 18) - 1 - collectTimeout = 5 * time.Second + collectTimeout = 2 * time.Second ) func secondCountDownTimer(title string, logger *log.Logger, t time.Duration) { diff --git a/api/modem/gps/status.go b/api/modem/gps/status.go index 367b408..8333f54 100644 --- a/api/modem/gps/status.go +++ b/api/modem/gps/status.go @@ -7,15 +7,17 @@ import ( ) type Status struct { - GotResponses bool `json:"gotResponses"` - FoundSatelitesCount int `json:"foundSatelitesCount"` - ActiveSatelitesCounte int `json:"activeSatelitesCount"` + GotResponses bool `json:"gotResponses"` + IsValidData bool `json:"isValidData"` + FoundSatelitesCount int `json:"foundSatelitesCount"` + ActiveSatelitesCount int `json:"activeSatelitesCount"` } var StatusNil = Status{ - GotResponses: false, - FoundSatelitesCount: 0, - ActiveSatelitesCounte: 0, + GotResponses: false, + IsValidData: false, + FoundSatelitesCount: 0, + ActiveSatelitesCount: 0, } func (g *gps) GetStatus() (Status, error) { @@ -44,17 +46,82 @@ checkLoop: return StatusNil, fmt.Errorf("nmea invalid sentence: %s", s) } switch values[0][3:] { // Switch by content - case "gsv": + case "GSV": + g.logger.Println("check GSV") + // Check len if len(values) < 17 { g.logger.Println("GSV too small values") 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 { g.logger.Println("GSV too small values") 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 + } } } diff --git a/api/modem/modem.go b/api/modem/modem.go index d7d3302..7234773 100644 --- a/api/modem/modem.go +++ b/api/modem/modem.go @@ -107,8 +107,8 @@ func (m *modem) Init() error { } // Init submodules - // submodulesLogger := m.logger.Writer() // FOR more logs - submodulesLogger := io.Discard // FOR less logs + submodulesLogger := m.logger.Writer() // FOR more logs + // submodulesLogger := io.Discard // FOR less logs m.logger.Println("=============================== Init submodules") m.ic = internet.New(log.New(submodulesLogger, "modem-internet : ", log.LstdFlags), m.port)