missing error handling added

This commit is contained in:
Torsten Harenberg
2025-02-28 14:59:31 +01:00
parent 49058adbfd
commit 4ed3ce9b10
7 changed files with 265 additions and 147 deletions

61
gpsd.go
View File

@@ -90,19 +90,24 @@ func ParseWatchMessage(input string) (*WatchMessage, error) {
var msg WatchMessage
if err := json.Unmarshal([]byte(jsonPart), &msg); err != nil {
return nil, fmt.Errorf("Fehler beim Parsen des JSON: %w", err)
return nil, fmt.Errorf("fehler beim Parsen des JSON: %w", err)
}
return &msg, nil
}
func startGPSdTCPServer(gpsdaddress string) error {
func startGPSdTCPServer(gpsdaddress string) {
listener, err := net.Listen("tcp", gpsdaddress)
if err != nil {
writeDebug(fmt.Sprintf("Error starting TCP server: %v\n", err), 0)
return err
return
}
defer listener.Close()
defer func() {
err := listener.Close()
if err != nil {
writeDebug(err.Error(), 0)
}
}()
writeDebug(fmt.Sprintf("TCP server started on port %s", gpsdaddress), 1)
for {
@@ -130,10 +135,13 @@ func isNetConnClosedErr(err error) bool {
}
func addClient(client net.Conn) {
if s.NMEAPassthrough {
if s.Userconfig.NMEAPassthrough {
go func() {
defer func() {
client.Close()
err := client.Close()
if err != nil {
writeDebug(err.Error(), 0)
}
removeClient(client)
writeDebug(fmt.Sprintf("GPSd Client disconnected: %v\n", client.RemoteAddr()), 0)
}()
@@ -151,7 +159,10 @@ func addClient(client net.Conn) {
go func() {
defer func() {
client.Close()
err := client.Close()
if err != nil {
writeDebug(err.Error(), 0)
}
removeClient(client)
writeDebug(fmt.Sprintf("GPSd Client disconnected: %v\n", client.RemoteAddr()), 0)
}()
@@ -160,7 +171,10 @@ func addClient(client net.Conn) {
for {
time.Sleep(100 * time.Millisecond)
// looks like gpsd does not expect \n terminated lines so read what is there from the socket
client.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
err := client.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
if err != nil {
writeDebug(err.Error(), 0)
}
buff := make([]byte, 1024)
n, err := rd.Read(buff)
if isNetConnClosedErr(err) {
@@ -173,8 +187,8 @@ func addClient(client net.Conn) {
}
line := string(buff[:n])
writeDebug(fmt.Sprintf("gpsd: Received from client: %v\n", string(line)), 1)
msg, err := ParseWatchMessage(string(line))
writeDebug(fmt.Sprintf("gpsd: Received from client: %v\n", line), 1)
msg, err := ParseWatchMessage(line)
if err == nil {
// fill devices list
@@ -236,7 +250,7 @@ func publishTPV() {
func readAndBroadcast() {
device := s.DeviceType
if s.NMEAPassthrough {
if s.Userconfig.NMEAPassthrough {
for {
nmeaSentence, err := s.GPSStream.DequeueOrWait()
if err != nil {
@@ -335,10 +349,10 @@ func updateTPVFromRMC(s nmea.RMC, device string) {
currentTPV.Class = "TPV"
currentTPV.Device = device
currentTPV.Time = parseTime(s.Time.String())
currentTPV.Lat = float64(s.Latitude)
currentTPV.Lon = float64(s.Longitude)
currentTPV.Speed = float64(s.Speed * knotsToMs)
currentTPV.Track = float64(s.Course)
currentTPV.Lat = s.Latitude
currentTPV.Lon = s.Longitude
currentTPV.Speed = s.Speed * knotsToMs
currentTPV.Track = s.Course
// Now publish currentTPV to gpsd
publishTPV()
}
@@ -347,9 +361,9 @@ func updateTPVFromGGA(s nmea.GGA, device string) {
currentTPV.Class = "TPV"
currentTPV.Device = device
currentTPV.Time = parseTime(s.Time.String())
currentTPV.Lat = float64(s.Latitude)
currentTPV.Lon = float64(s.Longitude)
currentTPV.Alt = float64(s.Altitude)
currentTPV.Lat = s.Latitude
currentTPV.Lon = s.Longitude
currentTPV.Alt = s.Altitude
// Publish currentTPV to gpsd
publishTPV()
}
@@ -358,9 +372,9 @@ func updateTPVFromGSA(s nmea.GSA) {
// Update only the fields provided by GSA
fixtype, _ := strconv.Atoi(s.FixType)
currentTPV.Mode = fixtype
currentTPV.PDOP = float64(s.PDOP)
currentTPV.HDOP = float64(s.HDOP)
currentTPV.VDOP = float64(s.VDOP)
currentTPV.PDOP = s.PDOP
currentTPV.HDOP = s.HDOP
currentTPV.VDOP = s.VDOP
// Publish currentTPV to gpsd
publishTPV()
}
@@ -391,7 +405,10 @@ func broadcastToClients(message string) {
_, err := client.Write([]byte(message + "\n"))
if err != nil {
writeDebug(fmt.Sprintf("gpsd: error writing to client %v: %v\n", client.RemoteAddr(), err), 0)
client.Close()
err = client.Close()
if err != nil {
writeDebug(err.Error(), 0)
}
delete(clients, client)
}
writeDebug(fmt.Sprintf("gpsd: message: %v", message), 1)