Some interface improvements.

This commit is contained in:
Andrey Egorov 2024-07-25 16:58:09 +03:00
parent 211ff6c63a
commit 781d9ccda4
4 changed files with 80 additions and 86 deletions

View File

@ -2,6 +2,7 @@ package at
import ( import (
"fmt" "fmt"
"io"
"log" "log"
"time" "time"
@ -32,6 +33,8 @@ type Port interface {
IsConnected() bool IsConnected() bool
Send(cmd string) (Resp, error) Send(cmd string) (Resp, error)
io.Closer
} }
func New(logger *log.Logger, portName string, baudrate int) Port { func New(logger *log.Logger, portName string, baudrate int) Port {
@ -110,3 +113,7 @@ func (p *atPort) Send(cmd string) (Resp, error) {
return Resp(resp), nil return Resp(resp), nil
} }
func (p *atPort) Close() error {
return p.port.Close()
}

View File

@ -43,3 +43,7 @@ func (p gpioPin) PowerOn() {
func (p *gpioPin) PowerOff() { func (p *gpioPin) PowerOff() {
p.sendOnOffSignal() p.sendOnOffSignal()
} }
func (p *gpioPin) Close() error {
return gpio.Close()
}

View File

@ -8,7 +8,7 @@ import (
"time" "time"
) )
type GpsInfo struct { type GpsData struct {
Latitude float64 `json:"Latitude"` Latitude float64 `json:"Latitude"`
Longitude float64 `json:"Longitude"` Longitude float64 `json:"Longitude"`
LatitudeIndicator string `json:"Latitude_indicator"` // North/South LatitudeIndicator string `json:"Latitude_indicator"` // North/South
@ -20,13 +20,13 @@ type GpsInfo struct {
Time string `json:"-"` Time string `json:"-"`
} }
var GpsInfoNil = GpsInfo{} var GpsInfoNil = GpsData{}
func deg2rad(deg float64) float64 { func deg2rad(deg float64) float64 {
return deg * (math.Pi / 180) return deg * (math.Pi / 180)
} }
func (gps *GpsInfo) calculateSpeed(newLatitude, newLongitude float64, lastUpdateTime time.Time) { func (gps *GpsData) calculateSpeed(newLatitude, newLongitude float64, lastUpdateTime time.Time) {
earthRad := 6371.0 // TODO ? earthRad := 6371.0 // TODO ?
dLat := deg2rad(math.Abs(newLatitude - gps.Latitude)) dLat := deg2rad(math.Abs(newLatitude - gps.Latitude))
dLon := deg2rad(math.Abs(newLongitude - gps.Longitude)) dLon := deg2rad(math.Abs(newLongitude - gps.Longitude))
@ -37,9 +37,9 @@ func (gps *GpsInfo) calculateSpeed(newLatitude, newLongitude float64, lastUpdate
gps.Speed = earthRad * c / (math.Abs(float64(time.Since(lastUpdateTime)))) gps.Speed = earthRad * c / (math.Abs(float64(time.Since(lastUpdateTime))))
} }
func (gps *GpsInfo) decode(str string) error { func (gps *GpsData) decode(str string) error {
var err error var err error
newGpsInfo := GpsInfo{} newGpsInfo := GpsData{}
strs := strings.Split(strings.Split(strings.Replace(str, " ", "", -1), "\n")[0], ",") strs := strings.Split(strings.Split(strings.Replace(str, " ", "", -1), "\n")[0], ",")
newGpsInfo.Latitude, err = strconv.ParseFloat(strs[0], 64) newGpsInfo.Latitude, err = strconv.ParseFloat(strs[0], 64)

View File

@ -1,18 +1,22 @@
package modem package modem
import ( import (
"encoding/json"
"fmt" "fmt"
"io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"slices"
"strings" "strings"
"time" "time"
"github.com/CGSG-2021-AE4/modem-test/api/modem/at" "github.com/CGSG-2021-AE4/modem-test/api/modem/at"
) )
type ModemData struct {
Port string `json:"Port"`
GpsData
}
type modem struct { type modem struct {
// Internal values // Internal values
logger *log.Logger logger *log.Logger
@ -27,23 +31,16 @@ type modem struct {
onOffPin gpioPin onOffPin gpioPin
// Other values // Other values
gpsInfo GpsInfo gpsInfo GpsData
lastUpdateTime time.Time lastUpdateTime time.Time
} }
type Modem interface { type Modem interface {
Init() error Init() error
// SearchPort(isSoft bool) error Validate() bool
Connect() error
Disconnect() error
IsAvailable() bool
IsConnected() bool
// Ping() error
// SwitchToGpsMode() error
// CalculateSpeed(newLatitude, newlongitude float64)
Update() error Update() error
GetInfo() (string, error) GetInfo() ModemData
TestGPS() error io.Closer
} }
func New(logger *log.Logger) Modem { func New(logger *log.Logger) Modem {
@ -81,45 +78,24 @@ func (m *modem) Init() error {
// Connect // Connect
m.logger.Println("=============================== Connect") m.logger.Println("=============================== Connect")
if err := m.Connect(); err != nil { if err := m.connect(); err != nil {
return fmt.Errorf("connect: %w", err) return fmt.Errorf("connect: %w", err)
} }
// Tests // Tests
m.logger.Println("=============================== Test") m.logger.Println("=============================== Test")
if err := m.TestGPS(); err != nil { if err := m.testGPS(); err != nil {
return fmt.Errorf("testGPS: %w", err) return fmt.Errorf("testGPS: %w", err)
} }
return nil return nil
} }
func (m *modem) Connect() error { func (m *modem) Validate() bool {
if !m.IsAvailable() { return m.isConnected()
return fmt.Errorf("port is not defined")
}
return m.port.Connect()
}
func (m *modem) Disconnect() error {
if !m.IsAvailable() {
return fmt.Errorf("port is not defined")
}
return m.port.Disconnect()
}
func (m *modem) IsAvailable() bool {
return m.port != nil
}
func (m *modem) IsConnected() bool {
if m.IsAvailable() {
return m.port.IsConnected()
}
return false
} }
func (m *modem) Update() error { func (m *modem) Update() error {
if !m.IsConnected() { if !m.isConnected() {
m.logger.Println("No connection to module") m.logger.Println("No connection to module")
return nil return nil
} }
@ -158,7 +134,46 @@ func (m *modem) Update() error {
return nil return nil
} }
func (m *modem) TestGPS() error { func (m *modem) GetInfo() ModemData {
return ModemData{
Port: m.port.GetName(),
GpsData: m.gpsInfo,
}
}
func (m *modem) Close() error {
// Not right way I think
if err := m.port.Close(); err != nil {
return err
}
if err := m.onOffPin.Close(); err != nil {
return err
}
return nil
}
func (m *modem) connect() error {
if !m.Validate() {
return fmt.Errorf("port is not defined")
}
return m.port.Connect()
}
func (m *modem) disconnect() error {
if !m.Validate() {
return fmt.Errorf("port is not defined")
}
return m.port.Disconnect()
}
func (m *modem) isConnected() bool {
if m.port != nil {
return m.port.IsConnected()
}
return false
}
func (m *modem) testGPS() error {
m.logger.Println("Testing GPS") m.logger.Println("Testing GPS")
if err := m.switchToGpsMode(); err != nil { if err := m.switchToGpsMode(); err != nil {
@ -169,39 +184,16 @@ func (m *modem) TestGPS() error {
return fmt.Errorf("update: %w", err) return fmt.Errorf("update: %w", err)
} }
m.logger.Println("Current coords:", m.GetShortInfo()) m.logger.Println("Current coords:", m.getShortInfo())
return nil return nil
} }
type ModemInfo struct {
Port string `json:"Port"`
GpsInfo
}
type Info struct {
Modem ModemInfo `json:"Modem"`
}
func (m *modem) GetInfo() (string, error) {
info := Info{
Modem: ModemInfo{
Port: m.port.GetName(),
GpsInfo: m.gpsInfo,
},
}
buf, err := json.Marshal(info)
if err != nil {
fmt.Errorf("marshal info: %w", err)
}
return string(buf), nil // why you clamp
}
// Difference: I do not set \n at the end of string // Difference: I do not set \n at the end of string
func (m *modem) GetShortInfo() 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) return fmt.Sprintf("%f,%s,%f,%s", m.gpsInfo.Latitude, m.gpsInfo.LatitudeIndicator, m.gpsInfo.Longitude, m.gpsInfo.LongitudeIndicator)
} }
func (m *modem) SaveGPS(path string) error { func (m *modem) saveGPS(path string) error {
f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil { if err != nil {
return fmt.Errorf("open file: %w", err) return fmt.Errorf("open file: %w", err)
@ -209,7 +201,7 @@ func (m *modem) SaveGPS(path string) error {
defer f.Close() defer f.Close()
if _, err = f.WriteString(m.GetShortInfo()); err != nil { if _, err = f.WriteString(m.getShortInfo()); err != nil {
return fmt.Errorf("write file: %W", err) return fmt.Errorf("write file: %W", err)
} }
return nil return nil
@ -269,7 +261,7 @@ func (m *modem) searchPort(isSoft bool) error {
// Get ports // Get ports
ports := []string{"ttyUSB1", "ttyUSB2", "ttyUSB3"} ports := []string{"ttyUSB1", "ttyUSB2", "ttyUSB3"}
if !isSoft { if !isSoft {
ps, err := GetTtyDevices() ps, err := getTtyDevices()
if err != nil { if err != nil {
fmt.Errorf("get serial devices: %w", err) fmt.Errorf("get serial devices: %w", err)
} }
@ -338,24 +330,15 @@ func (m *modem) switchToGpsMode() error {
return nil return nil
} }
func GetTtyDevices() ([]string, error) { func getTtyDevices() ([]string, error) {
devices := []string{}
// Get ports // Get ports
/**/ /**/
out, err := exec.Command("/bin/ls", "/dev").Output() out, err := exec.Command("ls", "--", "/dev/tty[!0-9]*").Output()
if err != nil { if err != nil {
return nil, fmt.Errorf("execute ls command: %w", err) return nil, fmt.Errorf("execute ls command: %w", err)
} }
allPorts := strings.Split(string(out), "\n") ports := strings.Split(string(out), "\n")
for _, p := range allPorts { return ports, nil
if len(p) > 3 && p[:3] == "tty" {
devices = append(devices, p)
}
}
slices.Reverse(devices) // ASK why
return devices, nil
} }
/* /*