Skip to content

Commit

Permalink
combined traccar http functions
Browse files Browse the repository at this point in the history
  • Loading branch information
talkkonnect committed Jan 11, 2022
1 parent 5f8ed0d commit 593387b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 81 deletions.
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,11 @@ func (b *Talkkonnect) ClientStart() {
}

if Config.Global.Hardware.Traccar.Enabled && Config.Global.Hardware.Traccar.Track && Config.Global.Hardware.Traccar.Protocol.Name == "osmand" {
go httpSendTraccarOsmand()
go httpSendTraccar("osmand")
}

if Config.Global.Hardware.Traccar.Enabled && Config.Global.Hardware.Traccar.Track && Config.Global.Hardware.Traccar.Protocol.Name == "opengts" {
go httpSendTraccarOpenGTS()
go httpSendTraccar("opengts")
}

if Config.Global.Hardware.Traccar.Enabled && Config.Global.Hardware.Traccar.Track && Config.Global.Hardware.Traccar.Protocol.Name == "t55" {
Expand Down
151 changes: 73 additions & 78 deletions gps.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"net"
"net/http"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -86,6 +87,13 @@ var (
goodGPSRead bool
)

var (
TCPErrorCount int
HTTPErrorCount int
TCPErrorThreshold int = 5
HTTPErrorThreshold int = 5
)

func getGpsPosition(verbosity int) (bool, error) {
RMCSentenceValid = false
GGASentenceValid = false
Expand Down Expand Up @@ -234,24 +242,30 @@ func getGpsPosition(verbosity int) (bool, error) {
return false, errors.New("gnss not enabled")
}

func httpSendTraccarOsmand() {
func httpSendTraccar(tprotocol string) {

GPSDataChannelReceivers++
TCPErrorCount := 0
HTTPErrorCount := 0
TCPErrorThreshold := 5
HTTPErrorThreshold := 5

for {
GNSSDataTraccar := <-GNSSDataPublic
TraccarDateTime := GNSSDataTraccar.DateTime.Format("2006-01-02") + "%20" + GNSSDataTraccar.DateTime.Format("15:04:05")
TraccarServerFullURLOsman := (fmt.Sprint(Config.Global.Hardware.Traccar.Protocol.Osmand.ServerURL) + ":" + fmt.Sprint(Config.Global.Hardware.Traccar.Protocol.Osmand.Port) + "/?" + "id=" + Config.Global.Hardware.Traccar.ClientId + "&" +
"timestamp=" + TraccarDateTime + "&" + "lat=" + fmt.Sprintf("%f", GNSSDataTraccar.Lattitude) +
"&" + "lon=" + fmt.Sprintf("%f", GNSSDataTraccar.Longitude) + "&" + "speed=" + fmt.Sprintf("%f", GNSSDataTraccar.Speed) + "&" + "course=" +
fmt.Sprintf("%f", GNSSDataTraccar.Course) + "&" + "variation=" + fmt.Sprintf("%f", GNSSDataTraccar.Variation) + "&" + "hdop=" + fmt.Sprintf("%f", GNSSData.HDOP) + "&" + "altitude=" + fmt.Sprintf("%f", GNSSData.Altitude))

var TraccarServerFullURL string

if tprotocol == "osmand" {
TraccarDateTime := GNSSDataTraccar.DateTime.Format("2006-01-02") + "%20" + GNSSDataTraccar.DateTime.Format("15:04:05")
TraccarServerFullURL = (fmt.Sprint(Config.Global.Hardware.Traccar.Protocol.Osmand.ServerURL) + ":" + fmt.Sprint(Config.Global.Hardware.Traccar.Protocol.Osmand.Port) + "/?" + "id=" + Config.Global.Hardware.Traccar.ClientId + "&" +
"timestamp=" + TraccarDateTime + "&" + "lat=" + fmt.Sprintf("%f", GNSSDataTraccar.Lattitude) +
"&" + "lon=" + fmt.Sprintf("%f", GNSSDataTraccar.Longitude) + "&" + "speed=" + fmt.Sprintf("%f", GNSSDataTraccar.Speed) + "&" + "course=" +
fmt.Sprintf("%f", GNSSDataTraccar.Course) + "&" + "variation=" + fmt.Sprintf("%f", GNSSDataTraccar.Variation) + "&" + "hdop=" + fmt.Sprintf("%f", GNSSData.HDOP) + "&" + "altitude=" + fmt.Sprintf("%f", GNSSData.Altitude))

}

if tprotocol == "opengts" {
TraccarServerFullURL = (fmt.Sprint(Config.Global.Hardware.Traccar.Protocol.Opengts.ServerURL) + ":" + fmt.Sprint(Config.Global.Hardware.Traccar.Protocol.Opengts.Port) + "/?id=" + Config.Global.Hardware.Traccar.ClientId + "&gprmc=" + GNSSDataTraccar.RMCRaw)
}

client := http.Client{Timeout: 5 * time.Second}
response, err := client.Get(TraccarServerFullURLOsman)
response, err := client.Get(TraccarServerFullURL)
if err != nil {
tcpErrorsTrap := "(refused|reset)" // add tcp errors here as they are found
re := regexp.MustCompile(tcpErrorsTrap)
Expand All @@ -260,38 +274,77 @@ func httpSendTraccarOsmand() {
if matched {
TCPErrorCount++
log.Println("error: TCP/IP Error Communicating with Traccar Server")
//alert user with sound or screen of tcp error
eventSound := findEventSound("traccarTCPError")
if eventSound.Enabled {
if v, err := strconv.Atoi(eventSound.Volume); err == nil {
localMediaPlayer(eventSound.FileName, v, eventSound.Blocking, 0, 1)
}
}
continue
}
}
//handle http status codes and errors here
if response.StatusCode >= 200 && response.StatusCode <= 299 {
HTTPErrorCount = 0
log.Printf("debug: OSMand Protocol Traccar Server HTTP Response Code %v With Status %v\n", response.StatusCode, http.StatusText(response.StatusCode))
//alert user with sound or screen of http error
log.Printf("debug: %v Protocol Traccar Server HTTP Response Code %v With Status %v\n", tprotocol, response.StatusCode, http.StatusText(response.StatusCode))
eventSound := findEventSound("traccarHTTP2XXResponse")
if eventSound.Enabled {
if v, err := strconv.Atoi(eventSound.Volume); err == nil {
localMediaPlayer(eventSound.FileName, v, eventSound.Blocking, 0, 1)
}
}
}
if response.StatusCode >= 300 && response.StatusCode <= 399 {
HTTPErrorCount++
log.Printf("debug: OSMand Protocol Traccar Server HTTP Response Code %v With Status %v\n", response.StatusCode, http.StatusText(response.StatusCode))
//alert user with sound or screen of http error
log.Printf("debug: %v Protocol Traccar Server HTTP Response Code %v With Status %v\n", tprotocol, response.StatusCode, http.StatusText(response.StatusCode))
eventSound := findEventSound("traccarHTTP3XXResponse")
if eventSound.Enabled {
if v, err := strconv.Atoi(eventSound.Volume); err == nil {
localMediaPlayer(eventSound.FileName, v, eventSound.Blocking, 0, 1)
}
}
}
if response.StatusCode >= 400 && response.StatusCode <= 499 {
HTTPErrorCount++
log.Printf("debug: OSMand Protocol Traccar Server HTTP Response Code %v With Status %v\n", response.StatusCode, http.StatusText(response.StatusCode))
//alert user with sound or screen of http error
log.Printf("debug: %v Protocol Traccar Server HTTP Response Code %v With Status %v\n", tprotocol, response.StatusCode, http.StatusText(response.StatusCode))
eventSound := findEventSound("traccarHTTP4XXResponse")
if eventSound.Enabled {
if v, err := strconv.Atoi(eventSound.Volume); err == nil {
localMediaPlayer(eventSound.FileName, v, eventSound.Blocking, 0, 1)
}
}
}
if response.StatusCode >= 500 && response.StatusCode <= 599 {
HTTPErrorCount++
log.Printf("debug: OSMand Protocol Traccar Server HTTP Response Code %v With Status %v\n", response.StatusCode, http.StatusText(response.StatusCode))
//alert user with sound or screen of http error
log.Printf("debug: %v Protocol Traccar Server HTTP Response Code %v With Status %v\n", tprotocol, response.StatusCode, http.StatusText(response.StatusCode))
eventSound := findEventSound("traccarHTTP5XXResponse")
if eventSound.Enabled {
if v, err := strconv.Atoi(eventSound.Volume); err == nil {
localMediaPlayer(eventSound.FileName, v, eventSound.Blocking, 0, 1)
}
}
}
response.Body.Close()

if TCPErrorCount >= TCPErrorThreshold {
Config.Global.Hardware.Traccar.Enabled = false
eventSound := findEventSound("traccarTooManyErrors")
if eventSound.Enabled {
if v, err := strconv.Atoi(eventSound.Volume); err == nil {
localMediaPlayer(eventSound.FileName, v, eventSound.Blocking, 0, 1)
}
}
return
}
if HTTPErrorCount >= HTTPErrorThreshold {
Config.Global.Hardware.Traccar.Enabled = false
eventSound := findEventSound("traccarTooManyErrors")
if eventSound.Enabled {
if v, err := strconv.Atoi(eventSound.Volume); err == nil {
localMediaPlayer(eventSound.FileName, v, eventSound.Blocking, 0, 1)
}
}
return
}
}
}
Expand Down Expand Up @@ -376,64 +429,6 @@ func tcpSendT55Traccar() {
}
}

func httpSendTraccarOpenGTS() {

GPSDataChannelReceivers++
TCPErrorCount := 0
HTTPErrorCount := 0
TCPErrorThreshold := 5
HTTPErrorThreshold := 5

for {
GNSSDataTraccar := <-GNSSDataPublic
TraccarServerFullURLOpenGTS := (fmt.Sprint(Config.Global.Hardware.Traccar.Protocol.Opengts.ServerURL) + ":" + fmt.Sprint(Config.Global.Hardware.Traccar.Protocol.Opengts.Port) + "/?id=" + Config.Global.Hardware.Traccar.ClientId + "&gprmc=" + GNSSDataTraccar.RMCRaw)

client := http.Client{Timeout: 5 * time.Second}
response, err := client.Get(TraccarServerFullURLOpenGTS)
if err != nil {
tcpErrorsTrap := "(refused|reset)" // add tcp errors here as they are found
re := regexp.MustCompile(tcpErrorsTrap)
matched := re.MatchString(err.Error())
log.Println("error: Failed Communication with Traccar Server ", err)
if matched {
TCPErrorCount++
log.Println("error: TCP/IP Error Communicating with Traccar Server")
//alert user with sound or screen of tcp error
continue
}
}
//handle http status codes and errors here
if response.StatusCode >= 200 && response.StatusCode <= 299 {
HTTPErrorCount = 0
log.Printf("debug: OpenGTS Protocol Traccar Server HTTP Response Code %v With Status %v\n", response.StatusCode, http.StatusText(response.StatusCode))
//alert user with sound or screen of http error
}
if response.StatusCode >= 300 && response.StatusCode <= 399 {
HTTPErrorCount++
log.Printf("debug: OpenGTS Protocol Traccar Server HTTP Response Code %v With Status %v\n", response.StatusCode, http.StatusText(response.StatusCode))
//alert user with sound or screen of http error
}
if response.StatusCode >= 400 && response.StatusCode <= 499 {
HTTPErrorCount++
log.Printf("debug: OpenGTS Protocol Traccar Server HTTP Response Code %v With Status %v\n", response.StatusCode, http.StatusText(response.StatusCode))
//alert user with sound or screen of http error
}
if response.StatusCode >= 500 && response.StatusCode <= 599 {
HTTPErrorCount++
log.Printf("debug: OpenGTS Protocol Traccar Server HTTP Response Code %v With Status %v\n", response.StatusCode, http.StatusText(response.StatusCode))
//alert user with sound or screen of http error
}
response.Body.Close()

if TCPErrorCount >= TCPErrorThreshold {
Config.Global.Hardware.Traccar.Enabled = false
}
if HTTPErrorCount >= HTTPErrorThreshold {
Config.Global.Hardware.Traccar.Enabled = false
}
}
}

func consoleScreenLogging() {
GPSDataChannelReceivers++
for {
Expand Down
3 changes: 2 additions & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ const (
)

/* Release Notes
1. Added Error Handling Structure to GPS.go
1. Combined http traccar protocols into one function
2. Added sound events for user to know traccar status
*/

0 comments on commit 593387b

Please sign in to comment.