This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelemetry.py
138 lines (115 loc) · 4.17 KB
/
telemetry.py
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
# wthud - Head-up Display for War Thunder
# Copyright (C) 2020 wysiwyng
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Telemetry requests from War Thunder localhost server. Adapted from
# https://github.com/PowerBroker2/WarThunder
import requests
import socket
IP_ADDRESS = '127.0.0.1' #socket.gethostbyname(socket.gethostname())
URL_INDICATORS = 'http://{}:8111/indicators'.format(IP_ADDRESS)
URL_STATE = 'http://{}:8111/state'.format(IP_ADDRESS)
URL_COMMENTS = 'http://{}:8111/gamechat?lastId={}'
URL_EVENTS = 'http://{}:8111/hudmsg?lastEvt=-1&lastDmg={}'
URL_MAP_IMG = 'http://{}:8111/map.img'.format(IP_ADDRESS)
URL_MAP_OBJ = 'http://{}:8111/map_obj.json'.format(IP_ADDRESS)
URL_MAP_INFO = 'http://{}:8111/map_info.json'.format(IP_ADDRESS)
FT_TO_M = 0.3048
IN_FLIGHT = 0
IN_MENU = -1
NO_MISSION = -2
WT_NOT_RUNNING = -3
OTHER_ERROR = -4
METRICS_PLANES = ['p-', 'f-', 'f2', 'f3', 'f4', 'f6', 'f7', 'f8', 'f9', 'os',
'sb', 'tb', 'a-', 'pb', 'am', 'ad', 'fj', 'b-', 'xp', 'bt',
'xa', 'xf', 'sp', 'hu', 'ty', 'fi', 'gl', 'ni', 'fu', 'fu',
'se', 'bl', 'be', 'su', 'te', 'st', 'mo', 'we', 'ha']
TIMEOUT_VAL = 0.05
_s = requests.Session()
def get_json(url):
return _s.get(url, timeout=TIMEOUT_VAL).json()
def find_altitude(indicators):
name = indicators['type']
# account for freedom units in US and UK planes
if name[:2] in METRICS_PLANES:
if 'altitude_10k' in indicators.keys():
return indicators['altitude_10k'] * FT_TO_M
elif 'altitude_hour' in indicators.keys():
return indicators['altitude_hour'] * FT_TO_M
elif 'altitude_min' in indicators.keys():
return indicators['altitude_min'] * FT_TO_M
else:
return 0
else:
if 'altitude_10k' in indicators.keys():
return indicators['altitude_10k']
elif 'altitude_hour' in indicators.keys():
return indicators['altitude_hour']
elif 'altitude_min' in indicators.keys():
return indicators['altitude_min']
else:
return 0
def get_indicators():
try:
obj = get_json(URL_INDICATORS)
if obj['valid']:
obj['aviahorizon_pitch'] = -obj['aviahorizon_pitch'] if 'aviahorizon_pitch' in obj else None
obj['aviahorizon_roll'] = -obj['aviahorizon_roll'] if 'aviahorizon_roll' in obj else None
obj['alt_m'] = find_altitude(obj)
return obj
except:
return None
def get_state():
try:
obj = get_json(URL_STATE)
if obj['valid']:
return obj
except:
return None
def get_map_obj():
try:
obj = get_json(URL_MAP_OBJ)
if obj['valid']:
return obj
except:
return None
def get_map_info():
try:
obj = get_json(URL_MAP_INFO)
if obj['valid']:
return obj
except:
return None
def get_flight_data():
try:
ind = get_indicators()
state = get_state()
if ind['valid'] and state['valid']:
obj = {}
obj.update(ind)
obj.update(state)
return obj
except:
return None
def calc_additional_data(obj):
# get speed for further calculations
speed = obj.get('speed')
if not speed:
speed = obj.get('speed')
if __name__ == '__main__':
while True:
ind = get_indicators()
state = get_state()
pass