Improved logs, other fixes.
This commit is contained in:
		| @@ -28,15 +28,20 @@ type GpsInfo struct { | ||||
| 	Time               string  `json:"-"` | ||||
| } | ||||
|  | ||||
| var GpsInfoNil = GpsInfo{} | ||||
|  | ||||
| type modem struct { | ||||
| 	// Serial stuff | ||||
| 	// Internal values | ||||
| 	logger *log.Logger | ||||
|  | ||||
| 	// Serial values | ||||
| 	baudrate int | ||||
|  | ||||
| 	deviceName  string | ||||
| 	port        at.Port | ||||
| 	isAvailable bool | ||||
|  | ||||
| 	// Gpio stuff | ||||
| 	// Gpio values | ||||
| 	onOffPin gpioPin | ||||
|  | ||||
| 	// Other values | ||||
| @@ -58,6 +63,7 @@ type Modem interface { | ||||
|  | ||||
| func New() Modem { | ||||
| 	return &modem{ | ||||
| 		logger:         log.New(os.Stdout, "modem:", log.LstdFlags), | ||||
| 		baudrate:       115200, | ||||
| 		onOffPin:       gpioPin{Pin: 6}, | ||||
| 		lastUpdateTime: time.Now(), | ||||
| @@ -66,18 +72,19 @@ func New() Modem { | ||||
|  | ||||
| func (m *modem) Init() error { | ||||
| 	// Turn module on | ||||
| 	if err := m.onOffPin.Init(); err != nil { | ||||
| 		return fmt.Errorf("gpio pin init: %w", err) | ||||
| 	} | ||||
| 	// log.Println("=============================== Turn on module") | ||||
| 	// if err := m.onOffPin.Init(); err != nil { | ||||
| 	// 	return fmt.Errorf("gpio pin init: %w", err) | ||||
| 	// } | ||||
| 	// onOffPin.PowerOn() | ||||
|  | ||||
| 	log.Println("===============================") | ||||
| 	// Search | ||||
| 	m.logger.Println("=============================== Search") | ||||
| 	// Soft search | ||||
| 	if err := m.SearchPort(true); err != nil { | ||||
| 		return fmt.Errorf("soft port search: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	// Common search | ||||
| 	// Wide search | ||||
| 	if m.port == nil { | ||||
| 		if err := m.SearchPort(false); err != nil { | ||||
| 			return fmt.Errorf("not soft port search: %w", err) | ||||
| @@ -87,15 +94,14 @@ func (m *modem) Init() error { | ||||
| 		return errors.New("no port is detected") | ||||
| 	} | ||||
|  | ||||
| 	log.Println("===============================") | ||||
|  | ||||
| 	// Connect | ||||
| 	m.logger.Println("=============================== Connect") | ||||
| 	if err := m.Connect(); err != nil { | ||||
| 		return fmt.Errorf("connect: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	log.Println("===============================") | ||||
| 	// Tests | ||||
| 	m.logger.Println("=============================== Test") | ||||
| 	if err := m.TestGPS(); err != nil { | ||||
| 		return fmt.Errorf("testGPS: %w", err) | ||||
| 	} | ||||
| @@ -117,14 +123,14 @@ func (m *modem) checkPort(portName string) error { | ||||
| 	defer m.port.Disconnect() // Do not bother about errors... | ||||
|  | ||||
| 	// Ping | ||||
| 	log.Println("Ping...") | ||||
| 	m.logger.Println("Ping...") | ||||
|  | ||||
| 	if err := m.Ping(); err != nil { | ||||
| 		return fmt.Errorf("ping error: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	// Check model | ||||
| 	log.Println("Check model...") | ||||
| 	m.logger.Println("Check model...") | ||||
|  | ||||
| 	resp, err := m.port.Send("AT+CGMM") | ||||
| 	if err != nil { | ||||
| @@ -138,11 +144,11 @@ func (m *modem) checkPort(portName string) error { | ||||
| 		return fmt.Errorf("get model: %w", err) | ||||
| 	} | ||||
| 	rightModel := "SIMCOM_SIM7600E-H" | ||||
| 	// log.Printf("[% x]\n [% x]", []byte("SIMCOM_SIM7600E-H"), []byte(model)) | ||||
| 	// 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) | ||||
| 	} | ||||
| 	log.Println("Model right") | ||||
| 	m.logger.Println("Model right") | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| @@ -160,14 +166,14 @@ func (m *modem) SearchPort(isSoft bool) error { | ||||
| 	// Check ports | ||||
| SearchLoop: | ||||
| 	for _, p := range ports { | ||||
| 		log.Printf("Checking port %s ...\n", p) | ||||
| 		m.logger.Printf("Checking port %s ...\n", p) | ||||
|  | ||||
| 		if err := m.checkPort("/dev/" + p); err != nil { | ||||
| 			log.Printf("Check failed: %s\n", err.Error()) | ||||
| 			m.logger.Printf("Check failed: %s\n", err.Error()) | ||||
| 			continue SearchLoop | ||||
| 		} | ||||
|  | ||||
| 		log.Print("Found modem on port: ", p) | ||||
| 		m.logger.Print("Found modem on port: ", p) | ||||
| 		m.port = at.New("/dev/"+p, m.baudrate) | ||||
| 		m.isAvailable = true | ||||
| 		return nil | ||||
| @@ -194,7 +200,7 @@ func (m *modem) Ping() error { | ||||
| } | ||||
|  | ||||
| func (m *modem) SwitchToGpsMode() error { | ||||
| 	log.Println("Enabling GPS mode...") | ||||
| 	m.logger.Println("Enabling GPS mode...") | ||||
| 	// Reset intput | ||||
| 	if err := m.port.GetSerialPort().ResetInputBuffer(); err != nil { | ||||
| 		return fmt.Errorf("reset input buffer: %w", err) | ||||
| @@ -210,10 +216,10 @@ func (m *modem) SwitchToGpsMode() error { | ||||
| 	} | ||||
| 	ans := strings.Replace(strings.Split(strings.Split(resp.RmFront("+CGPS:").String(), "\n")[0], ",")[0], " ", "", -1) | ||||
| 	if ans == "1" { | ||||
| 		log.Println("GPS already enabled") | ||||
| 		m.logger.Println("GPS already enabled") | ||||
| 		return nil | ||||
| 	} | ||||
| 	log.Println(ans) | ||||
| 	m.logger.Println(ans) | ||||
|  | ||||
| 	// Modem is not in GPS mode | ||||
| 	resp, err = m.port.Send("AT+CGPS=1") | ||||
| @@ -223,7 +229,7 @@ func (m *modem) SwitchToGpsMode() error { | ||||
| 	if !resp.Check() { | ||||
| 		return fmt.Errorf("switch tp GPS failed") | ||||
| 	} | ||||
| 	log.Println("GPS mode enabled") | ||||
| 	m.logger.Println("GPS mode enabled") | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| @@ -232,7 +238,7 @@ func deg2rad(deg float64) float64 { | ||||
| } | ||||
|  | ||||
| func (m *modem) CalculateSpeed(newLatitude, newLongitude float64) { | ||||
| 	log.Println("Calculate speed") | ||||
| 	m.logger.Println("Calculate speed") | ||||
| 	earthRad := 6371.0 // TODO ? | ||||
| 	dLat := deg2rad(math.Abs(newLatitude - m.gpsInfo.Latitude)) | ||||
| 	dLon := deg2rad(math.Abs(newLongitude - m.gpsInfo.Longitude)) | ||||
| @@ -243,10 +249,41 @@ func (m *modem) CalculateSpeed(newLatitude, newLongitude float64) { | ||||
| 	m.gpsInfo.Speed = earthRad * c / (math.Abs(float64(time.Since(m.lastUpdateTime)))) | ||||
| } | ||||
|  | ||||
| func decodeGpsInfo(strs [9]string) (GpsInfo, error) { | ||||
| 	var err error | ||||
| 	newGpsInfo := GpsInfo{} | ||||
|  | ||||
| 	newGpsInfo.Latitude, err = strconv.ParseFloat(strs[0], 64) | ||||
| 	if err != nil { | ||||
| 		return GpsInfoNil, fmt.Errorf("parse latitude: %w", err) | ||||
| 	} | ||||
| 	newGpsInfo.Longitude, err = strconv.ParseFloat(strs[2], 64) | ||||
| 	if err != nil { | ||||
| 		return GpsInfoNil, fmt.Errorf("parse longitude: %w", err) | ||||
| 	} | ||||
| 	newGpsInfo.LatitudeIndicator = strs[1] | ||||
| 	newGpsInfo.LatitudeIndicator = strs[3] | ||||
| 	newGpsInfo.Date = strs[4] | ||||
| 	newGpsInfo.Time = strs[5] | ||||
| 	newGpsInfo.Altitude, err = strconv.ParseFloat(strs[6], 64) | ||||
| 	if err != nil { | ||||
| 		return GpsInfoNil, fmt.Errorf("parse altitude: %w", err) | ||||
| 	} | ||||
| 	newGpsInfo.Speed, err = strconv.ParseFloat(strs[7], 64) | ||||
| 	if err != nil { | ||||
| 		return GpsInfoNil, fmt.Errorf("parse speed: %w", err) | ||||
| 	} | ||||
| 	newGpsInfo.Course, err = strconv.ParseFloat(strs[8], 64) | ||||
| 	if err != nil { | ||||
| 		return GpsInfoNil, fmt.Errorf("parse course: %w", err) | ||||
| 	} | ||||
| 	return newGpsInfo, nil | ||||
| } | ||||
|  | ||||
| func (m *modem) Update() error { | ||||
| 	log.Println("Update") | ||||
| 	m.logger.Println("Update") | ||||
| 	if !m.isAvailable { | ||||
| 		log.Println("No connection to module") | ||||
| 		m.logger.Println("No connection to module") | ||||
| 		return nil | ||||
| 	} | ||||
| 	// ans, err := m.port.Request(at.CmdQuestion, "CGPSINFO") | ||||
| @@ -259,13 +296,13 @@ func (m *modem) Update() error { | ||||
| 	// 	if err != nil { | ||||
| 	// 		return fmt.Errorf("switch to GPS info mode: %w", err) | ||||
| 	// 	} | ||||
| 	// 	log.Println("switched to GPS mode") | ||||
| 	// 	m.logger.Println("switched to GPS mode") | ||||
| 	// } else { | ||||
| 	// 	log.Println("mode in right GPS mode") | ||||
| 	// 	m.logger.Println("mode in right GPS mode") | ||||
| 	// } | ||||
|  | ||||
| 	// Update | ||||
| 	log.Println("Receiving GPS data...") | ||||
| 	m.logger.Println("Receiving GPS data...") | ||||
| 	resp, err := m.port.Send("AT+CGPSINFO") | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("receive GPS data: %w", err) | ||||
| @@ -273,34 +310,16 @@ func (m *modem) Update() error { | ||||
| 	if !resp.Check() { | ||||
| 		return fmt.Errorf("error response") | ||||
| 	} | ||||
| 	log.Println("Decoding data...") | ||||
| 	m.logger.Println("Decoding data...") | ||||
| 	coordinates := strings.Split(strings.Split(resp.RmFront("+CGPSINFO:").String(), "\n")[0], ",") | ||||
|  | ||||
| 	m.gpsInfo.Latitude, err = strconv.ParseFloat(coordinates[0], 64) | ||||
| 	newGpsInfo, err := decodeGpsInfo([9]string(coordinates)) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("parse latitude: %w", err) | ||||
| 		m.logger.Println("Gps info decode error:", err.Error()) | ||||
| 		return nil | ||||
| 	} | ||||
| 	m.gpsInfo.Longitude, err = strconv.ParseFloat(coordinates[2], 64) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("parse longitude: %w", err) | ||||
| 	} | ||||
| 	m.gpsInfo.LatitudeIndicator = coordinates[1] | ||||
| 	m.gpsInfo.LatitudeIndicator = coordinates[3] | ||||
| 	m.gpsInfo.Date = coordinates[4] | ||||
| 	m.gpsInfo.Time = coordinates[5] | ||||
| 	m.gpsInfo.Altitude, err = strconv.ParseFloat(coordinates[6], 64) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("parse altitude: %w", err) | ||||
| 	} | ||||
| 	m.gpsInfo.Speed, err = strconv.ParseFloat(coordinates[7], 64) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("parse speed: %w", err) | ||||
| 	} | ||||
| 	m.gpsInfo.Course, err = strconv.ParseFloat(coordinates[8], 64) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("parse course: %w", err) | ||||
| 	} | ||||
| 	log.Println("Decoded successfully") | ||||
| 	m.gpsInfo = newGpsInfo | ||||
| 	m.logger.Println("Decoded successfully") | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| @@ -347,7 +366,7 @@ func (m *modem) SaveGPS(path string) error { | ||||
| } | ||||
|  | ||||
| func (m *modem) TestGPS() error { | ||||
| 	log.Println("Testing GPS") | ||||
| 	m.logger.Println("Testing GPS") | ||||
|  | ||||
| 	if err := m.SwitchToGpsMode(); err != nil { | ||||
| 		return fmt.Errorf("switch to GPS: %w", err) | ||||
| @@ -357,7 +376,7 @@ func (m *modem) TestGPS() error { | ||||
| 		return fmt.Errorf("update: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	log.Println("Current coords:", m.GetShortInfo()) | ||||
| 	m.logger.Println("Current coords:", m.GetShortInfo()) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| @@ -366,7 +385,6 @@ func GetTtyDevices() ([]string, error) { | ||||
|  | ||||
| 	// Get ports | ||||
| 	/**/ | ||||
| 	log.Print("Search for ports...") | ||||
| 	out, err := exec.Command("/bin/ls", "/dev").Output() | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("execute ls command: %w", err) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user