-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConnector.jl
303 lines (266 loc) · 11.2 KB
/
Connector.jl
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#=
Autor: Adriano Bassignana Bargamo 2021
Licence: GPL 2
Program for reading the aircraft position via Telnet protocol over TCP-IP.
=#
using Sockets
using EzXML
using Dates
#using Geodesy
mutable struct TelnetConnection
ipAddress::IPv4
ipPort::Int
sock::Union{TCPSocket,Nothing}
telnetData::Vector{Any}
function TelnetConnection(address::String)
(ipAddress,ipPort) = getFGFSPositionIpAndPort(address)
new(IPv4(ipAddress),ipPort,nothing,Any[])
end
end
struct FGFSPosition
latitudeDeg::Float64
longitudeDeg::Float64
altitudeFt::Float64
directionDeg::Float64
distanceNm::Float64
speedMph::Float64
time::Float64
function FGFSPosition(lat::Float64,lon::Float64,alt::Float64)
new(lat,lon,alt,0.0,0.0,0.0,time())
end
function FGFSPosition(lat::Float64,lon::Float64,alt::Float64,precPosition::FGFSPosition)
t = time()
try
dir = Main.Geodesics.azimuth(precPosition.longitudeDeg,precPosition.latitudeDeg,lon,lat)
dist = Main.Geodesics.surface_distance(precPosition.longitudeDeg,precPosition.latitudeDeg,lon,lat,Main.Geodesics.localEarthRadius(lat)) / 1852.0
deltaTime = t - precPosition.time
speedMph = (dist-precPosition.distanceNm) * 3600 / deltaTime
new(lat,lon,alt,dir,dist,speedMph,t)
catch err
println("FGFSPosition - Error: $err")
new(lat,lon,alt,precPosition.dir,precPosition.dist,precPosition.speedMph,t)
end
end
end
mutable struct FGFSPositionRoute
marks::Vector{FGFSPosition}
size::Int64
actual::Union{FGFSPosition,Nothing}
precPosition::Union{FGFSPosition,Nothing}
actualDistance::Float64
actualSpeed::Float64
actualDirectionDeg::Float64
radiusStep::Float64
radiusStepFactor::Float64
stepTime::Float64
telnetLastTime::Float64
telnet::Union{TelnetConnection,Nothing}
function FGFSPositionRoute(centralPointRadiusDistance,radiusStepFactor = 0.5)
new(Any[],0,nothing,nothing,0.0,0.0,0.0,centralPointRadiusDistance,radiusStepFactor,2.0,0.0,nothing)
end
end
telnetConnectionSockIsOpen(telnet::Union{TelnetConnection,Nothing}) = telnet == nothing || telnet.sock == nothing ? false : Sockets.isopen(telnet.sock)
telnetConnectionSockIsOpen(positionRoute::Union{FGFSPositionRoute,Nothing}) = positionRoute == nothing || positionRoute.telnet == nothing || positionRoute.telnet.sock == nothing ? false : Sockets.isopen(positionRoute.telnet.sock)
function getFGFSPositionIpAndPort(ipAddressAndPort::String)
s = split(ipAddressAndPort,":")
ip = "127.0.0.1"
p = 5000
if Base.size(s)[1] > 1
try
p = parse(Int,s[2])
catch
end
end
if length(s[1]) > 0 ip = string(s[1]) end
return ip,p
end
function setFGFSConnect(telnet::TelnetConnection,debugLevel::Int)
@async begin
try
if !telnetConnectionSockIsOpen(telnet)
telnet.sock = connect(telnet.ipAddress,telnet.ipPort)
sleep(0.5)
debugLevel > 1 && println("setFGFSConnect - Frist connection $(telnet.ipAddress):$(telnet.ipPort)")
end
try
while telnetConnectionSockIsOpen(telnet)
line = Sockets.readline(telnet.sock)
if length(line) > 0
push!(telnet.telnetData,line)
end
end
catch err
telnet.sock = nothing
debugLevel > 1 && println("setFGFSConnect - connection ended with error $err")
end
catch err
telnet.sock = nothing
debugLevel > 1 && println("setFGFSConnect - socket not create with error $err")
end
end
return telnet
end
function getFGFSValues(s::String,typeOfdata::Char)
try
if typeOfdata == 's'
a = split(s,"=")
return split(a[2],"'")[2]
elseif typeOfdata == 'f'
a = split(s,"=")
return parse(Float64,split(a[2],"'")[2])
end
catch
end
return nothing
end
function getFGFSPathScenery(ipAddressAndPort,debugLevel::Int)
try
retray = 1
telnet = setFGFSConnect(TelnetConnection(ipAddressAndPort),debugLevel)
sleep(0.5)
while telnetConnectionSockIsOpen(telnet) && retray <= 10
sleep(0.1)
if retray == 1 write(telnet.sock,string("get /sim/fg-scenery","\r\n")) end
if size(telnet.telnetData)[1] > 0
return getFGFSValues(telnet.telnetData[1],'s')
end
retray += 1
end
debugLevel > 1 && println("\ngetFGFSPathScenery - Sockets is close")
return nothing
catch err
debugLevel > 1 && println("\ngetFGFSPathScenery - Error connection: $err")
return nothing
end
end
function getFGFSPositionLat(ipAddressAndPort,debugLevel::Int)
try
retray = 1
telnet = setFGFSConnect(TelnetConnection(ipAddressAndPort),debugLevel)
sleep(0.5)
while telnetConnectionSockIsOpen(telnet) && retray <= 10
sleep(0.1)
if retray == 1 write(telnet.sock,string("get /position/latitude-deg","\r\n")) end
if size(telnet.telnetData)[1] > 0
return getFGFSValues(telnet.telnetData[1],'f')
end
retray += 1
end
debugLevel > 1 && println("\ngetFGFSPathScenery - Sockets is close")
return nothing
catch err
debugLevel > 1 && println("\ngetFGFSPathScenery - Error connection: $err")
return nothing
end
end
function getFGFSPositionLon(ipAddressAndPort,debugLevel::Int)
try
retray = 1
telnet = setFGFSConnect(TelnetConnection(ipAddressAndPort),debugLevel)
sleep(0.5)
while telnetConnectionSockIsOpen(telnet) && retray <= 10
sleep(0.1)
if retray == 1 write(telnet.sock,string("get /position/longitude-deg","\r\n")) end
if size(telnet.telnetData)[1] > 0
return getFGFSValues(telnet.telnetData[1],'f')
end
retray += 1
end
debugLevel > 1 && println("\ngetFGFSPathScenery - Sockets is close")
return nothing
catch err
debugLevel > 1 && println("\ngetFGFSPathScenery - Error connection: $err")
return nothing
end
end
function getFGFSPosition(telnet::TelnetConnection, precPosition::Union{FGFSPosition,Nothing},debugLevel::Int)
telnetDataXML = ""
telnet.telnetData = Any[]
try
retray = 1
while telnetConnectionSockIsOpen(telnet) && retray <= 3
write(telnet.sock,string("dump /position","\r\n"))
sleep(0.5)
if size(telnet.telnetData)[1] >= 8
for td in telnet.telnetData[2:end] telnetDataXML *= td end
try
primates = EzXML.root(EzXML.parsexml(telnetDataXML))
lat = Base.parse(Float64,EzXML.nodecontent.(findall("//latitude-deg",primates))[1])
lon = Base.parse(Float64,EzXML.nodecontent.(findall("//longitude-deg",primates))[1])
alt = Base.parse(Float64,EzXML.nodecontent.(findall("//altitude-ft",primates))[1])
alt = alt - Base.parse(Float64,EzXML.nodecontent.(findall("//ground-elev-ft",primates))[1])
if precPosition == nothing
position = FGFSPosition(lat,lon,alt)
else
position = FGFSPosition(lat,lon,alt,precPosition)
end
return position
catch err
debugLevel > 1 && println("\ngetFGFSPosition - Error in XML: $err")
return nothing
end
end
retray += 1
end
debugLevel > 1 && println("\ngetFGFSPosition - Sockets is close")
return nothing
catch err
debugLevel > 1 && println("\ngetFGFSPosition - Error connection: $err")
return nothing
end
end
function getFGFSPositionSetTask(ipAddressAndPort::String,centralPointRadiusDistance::Float64,radiusStepFactor::Float64,debugLevel::Int)
positionRoute = FGFSPositionRoute(centralPointRadiusDistance,radiusStepFactor)
maxRetray = 10
@async while true
positionRoute.telnet = setFGFSConnect(TelnetConnection(ipAddressAndPort),debugLevel)
sleep(1.0)
if telnetConnectionSockIsOpen(positionRoute)
while telnetConnectionSockIsOpen(positionRoute)
retray = 1
while telnetConnectionSockIsOpen(positionRoute) && retray <= maxRetray
position = getFGFSPosition(positionRoute.telnet,positionRoute.precPosition,debugLevel)
if position == nothing
if !telnetConnectionSockIsOpen(positionRoute)
break
else
debugLevel > 0 && println("\ngetFGFSPositionSetTask - Error: contact lost to FGFS program | n. retray: $retray")
retray += 1
sleep(1.0)
end
else
sleep(positionRoute.stepTime)
retray = 1
if positionRoute.size == 0
push!(positionRoute.marks,position)
positionRoute.size += 1
end
oldDistance = positionRoute.actualDistance
oldTime = 0.0
if positionRoute.actual != nothing oldTime = positionRoute.actual.time end
positionRoute.actual = position
positionRoute.actualDistance = Main.Geodesics.surface_distance(positionRoute.marks[end].longitudeDeg,positionRoute.marks[end].latitudeDeg,position.longitudeDeg,position.latitudeDeg,Main.Geodesics.localEarthRadius(position.latitudeDeg)) / 1852.0
if oldTime > 0.0 && (positionRoute.actual.time - positionRoute.marks[end].time) > 0.0
positionRoute.actualSpeed = positionRoute.actualDistance * 3600 / (positionRoute.actual.time - positionRoute.marks[end].time)
else
positionRoute.actualSpeed = 0.0
end
positionRoute.actualDirectionDeg = Main.Geodesics.azimuth(positionRoute.marks[end].longitudeDeg,positionRoute.marks[end].latitudeDeg,position.longitudeDeg,position.latitudeDeg)
if positionRoute.actualDistance >= (positionRoute.radiusStep * positionRoute.radiusStepFactor)
# Speed, radius and direction correction
push!(positionRoute.marks,position)
positionRoute.size += 1
end
positionRoute.precPosition = position
positionRoute.telnetLastTime = time()
break
end
end
end
if telnetConnectionSockIsOpen(positionRoute) && retray > maxRetray
Sockets.close(positionRoute.telnet.sock)
end
end
end
return positionRoute
end