forked from talkkonnect/talkkonnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgps.go
192 lines (165 loc) · 5.16 KB
/
gps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* talkkonnect headless mumble client/gateway with lcd screen and channel control
* Copyright (C) 2018-2019, Suvir Kumar <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* talkkonnect is the based on talkiepi and barnard by Daniel Chote and Tim Cooper
*
* The Initial Developer of the Original Code is
* Suvir Kumar <[email protected]>
* Portions created by the Initial Developer are Copyright (C) Suvir Kumar. All Rights Reserved.
*
* Contributor(s):
*
* Suvir Kumar <[email protected]>
*
* My Blog is at www.talkkonnect.com
* The source code is hosted at github.com/talkkonnect
*
* gps.go -> talkkonnect function to interface to USB GPS Neo6M
*/
package talkkonnect
import (
"bufio"
"encoding/hex"
"errors"
"fmt"
"github.com/jacobsa/go-serial/serial"
"github.com/talkkonnect/go-nmea"
"io/ioutil"
"log"
"net/http"
)
var (
sendToTracCar bool = true
tracCarURL string = "http://demo.traccar.org"
tracCarPort string = "5060"
tracCarID string = "12345"
tracCarFullURL string = ""
)
var goodGPSRead bool = false
func getGpsPosition(verbose bool) (bool, error) {
if GpsEnabled {
if Port == "" {
return false, errors.New("You Must Specify Port")
}
if Even && Odd {
return false, errors.New("can't specify both even and odd parity")
}
parity := serial.PARITY_NONE
if Even {
parity = serial.PARITY_EVEN
} else if Odd {
parity = serial.PARITY_ODD
}
options := serial.OpenOptions{
PortName: Port,
BaudRate: Baud,
DataBits: DataBits,
StopBits: StopBits,
MinimumReadSize: MinRead,
InterCharacterTimeout: CharTimeOut,
ParityMode: parity,
Rs485Enable: Rs485,
Rs485RtsHighDuringSend: Rs485HighDuringSend,
Rs485RtsHighAfterSend: Rs485HighAfterSend,
}
f, err := serial.Open(options)
if err != nil {
GpsEnabled = false
return false, errors.New("Cannot Open Serial Port")
} else {
defer f.Close()
}
if TxData != "" {
txData, err := hex.DecodeString(TxData)
if err != nil {
GpsEnabled = false
return false, errors.New("Cannot Decode Hex Data")
}
log.Println("Sending: ", hex.EncodeToString(txData))
count, err := f.Write(txData)
if err != nil {
return false, errors.New("Error writing to serial port")
} else {
log.Println("Wrote %v bytes\n", count)
}
}
if Rx {
serialPort, err := serial.Open(options)
if err != nil {
log.Println("warn: Unable to Open Serial Port Error ", err)
}
defer serialPort.Close()
reader := bufio.NewReader(serialPort)
scanner := bufio.NewScanner(reader)
goodGPSRead = false
for scanner.Scan() {
s, err := nmea.Parse(scanner.Text())
if err == nil {
if s.DataType() == nmea.TypeRMC {
m := s.(nmea.RMC)
if m.Latitude != 0 && m.Longitude != 0 {
goodGPSRead = true
tracCarFullURL = fmt.Sprintf(tracCarURL + ":" + tracCarPort + "?" + "id=" + tracCarID + "&" + "lat={" + nmea.FormatGPS(m.Latitude) + "}" + "&" + "lon={" + nmea.FormatGPS(m.Longitude) + "}" + "&" + "timestamp={" + GPSTime + "}" + "&" + "hdop={" + "}" + "&a")
GPSTime = fmt.Sprintf("%v", m.Time)
GPSDate = fmt.Sprintf("%v", m.Date)
GPSLatitude = m.Latitude
GPSLongitude = m.Longitude
if verbose {
log.Println("info: Time: ", m.Time)
log.Println("info: Validity: ", m.Validity)
log.Println("info: Latitude GPS: ", nmea.FormatGPS(m.Latitude))
log.Println("info: Latitude DMS: ", nmea.FormatDMS(m.Latitude))
log.Println("info: Longitude GPS: ", nmea.FormatGPS(m.Longitude))
log.Println("info: Longitude DMS: ", nmea.FormatDMS(m.Longitude))
log.Println("info: Speed: ", m.Speed)
log.Println("info: Course: ", m.Course)
log.Println("info: Date: ", m.Date)
log.Println("info: Variation: ", m.Variation)
log.Println(tracCarFullURL)
}
if sendToTracCar {
httpSendTracCar()
}
break
} else {
log.Println("warn: Got Latitude 0 and Longtitude 0 from GPS")
}
} else {
log.Println("warn: GPS Sentence Format Was not nmea.RMC")
}
} else {
log.Println("warn: Scanner Function Error ", err)
}
}
} else {
return false, errors.New("Rx Not Set")
}
return goodGPSRead, nil
}
return false, errors.New("GPS Not Enabled")
}
func httpSendTracCar() {
response, err := http.Get(tracCarFullURL)
if err != nil {
log.Println("error: Cannot Read traccar Webpage! Error ", err)
return
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Println("error: Error Sending Data to traccar Webpage!")
} else {
log.Println("info: traccar web response ", string(contents))
}
}
}