-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
357 lines (300 loc) · 13.3 KB
/
main.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import sys
import time
import socket
from datetime import datetime, timezone, timedelta
import ntplib
import threading
import pyuipc
from gui.gui import GUI
from shotwn import BasicTimeDelta
from shotwn import FlipFlop
from shotwn.settings import Settings
from shotwn.SingleInstance import SingleInstance
DEFAULT_SETTINGS = {
"minimize_to_tray": True,
"startup": {
"auto_sync": False,
"tray": False
}
}
SIMULATORS = [
"ANY",
"MFS 98",
"MFS 2000",
"CFS 2",
"CFS 1",
"Reserved",
"FS 2002",
"FS 2004",
"Flight Simulator X",
"ESP",
"Prepar3D",
"Flight Simulator X64",
"Prepar3D v4"
]
SIMULATOR_XPLANE_ID = 100
SIMULATOR_XPLANE = "X Plane"
SIMULATOR_XPLANE_YEAR_DELTA = 7
class OffsetSet:
def __init__(self, offsets):
self.offsets = offsets
self.prepared_data = None
self.latest_read = None
self.prepare()
def prepare(self):
offsets_rough_dict = []
for key, offset in self.offsets.items():
offsets_rough_dict.append((offset[0], offset[1]))
self.prepared_data = pyuipc.prepare_data(offsets_rough_dict, True)
def read(self):
offset_results = {}
offset_values = pyuipc.read(self.prepared_data)
for (key, offset), value in zip(self.offsets.items(), offset_values):
offset_results[key] = value
self.latest_read = offset_results
return offset_results
def write(self, offset, value):
offset_raw = self.offsets[offset]
pyuipc.write([(offset_raw[0], offset_raw[1], value)])
class FSSync:
def __init__(self):
self.offset_sets = []
self.pyuipc_open = False
self.opened_sim = None
def reset(self):
self.offset_sets = []
self.close_pyuipc()
def connect_pyuipc(self):
if self.pyuipc_open:
return self.pyuipc_open
try:
pyuipc.open(0)
except pyuipc.FSUIPCException as exc: # noqa: F841
print(exc)
return None
sim_offset = self.create_offset_set({
'SIM_VERSION': [0x3308, 'b'],
'IS_XPLANE': [0x6FFF, 'b']
})
result = sim_offset.read()
if result['IS_XPLANE']:
self.pyuipc_open = SIMULATOR_XPLANE_ID
self.opened_sim = SIMULATOR_XPLANE
else:
self.pyuipc_open = result['SIM_VERSION']
try:
self.opened_sim = SIMULATORS[self.pyuipc_open]
except IndexError:
self.opened_sim = f'Unknown Sim: {self.pyuipc_open}'
return True
def close_pyuipc(self):
self.pyuipc_open = False
pyuipc.close()
def create_offset_set(self, offsets):
new_offset_set = OffsetSet(offsets)
self.offset_sets.append(new_offset_set)
return new_offset_set
def read_all_offset_sets(self):
for offset_set in self.offset_sets:
offset_set.read()
class FSTimeSync:
def __init__(self):
self.si = SingleInstance(32092)
self.settings = Settings('settings.json', DEFAULT_SETTINGS)
self.fs_sync = FSSync()
self.gui = GUI(self)
self.mw_emit = self.gui.main_window.act.emit # TODO: Switch from mw_emit to mw_act
self.mw_act = self.gui.main_window_act
self.time_offsets = None
self.sync_run = False
self.sync_thread = None
self.time_run = False
self.time_thread = None
self.live_sync_enabled = False
self.now_source = "S"
self.ntp_client = ntplib.NTPClient()
self.ntp_delta = None
self.offset = BasicTimeDelta()
def start(self):
try:
self.sync_thread = threading.Thread(None, self.sync_thread_runner, "Sync Thread", daemon=True)
self.sync_thread.start()
self.time_thread = threading.Thread(None, self.time_thread_loop, "Time Thread", daemon=True)
self.time_thread.start()
self.gui.start() # locking
finally:
self.stop()
def stop(self):
self.sync_run = False
self.time_run = False
self.fs_sync.close_pyuipc()
sys.exit(0)
def get_now(self):
if self.now_source == "NTP":
try:
if not self.ntp_delta:
response = self.ntp_client.request('pool.ntp.org')
print(time.ctime(response.tx_time))
self.ntp_delta = datetime.now(timezone.utc) - datetime.fromtimestamp(response.tx_time, timezone.utc)
print(self.ntp_delta)
self.gui.remove_message(1, 0)
return datetime.utcnow() - self.ntp_delta
except (ntplib.NTPException, socket.gaierror) as exc:
self.gui.add_message(1, 0, "Can't reach NTP server.")
print(exc)
return datetime.utcnow()
return datetime.utcnow()
def switch_now_source(self):
if self.now_source == "NTP": # Will switch to S
self.now_source = "S"
self.mw_emit([self.gui.main_window.ui.utc_label.setText, "UTC.S"])
self.mw_emit([self.gui.main_window.ui.utc_label.setToolTip, "UTC.S : Using System Time"])
elif self.now_source == "S": # Will switch to NTP
self.ntp_delta = None # Refresh NTP
self.now_source = "NTP"
self.mw_emit([self.gui.main_window.ui.utc_label.setText, "UTC.NTP"])
self.mw_emit([self.gui.main_window.ui.utc_label.setToolTip, "UTC.NTP : Using Network Time Protocol, Online Time"])
self.gui.remove_message(0, 1) # Remove will sync message
def time_thread_loop(self):
self.time_run = True
while self.time_run:
now = self.get_now()
self.mw_emit([self.gui.main_window.ui.real_time_hour.setText, "{:02d}".format(now.hour)])
# self.mw_emit([self.gui.main_window.ui.real_time_seperator.setText, str(two_dots))
self.mw_emit([self.gui.main_window.ui.real_time_minute.setText, "{:02d}".format(now.minute)])
self.mw_emit([self.gui.main_window.ui.real_time_second.setText, "{:02d}".format(now.second)])
self.mw_emit([self.gui.main_window.ui.real_date.setText, "{:02d}.{:02d}.{}".format(now.day, now.month, now.year)])
# print(now)
if bool(self.offset):
offsetted_dt = self.offset + now
self.gui.main_window_act(self.gui.main_window.ui.left_value.setText, f"{offsetted_dt.strftime('%H:%M | %d.%m.%Y')}")
else:
self.gui.main_window_act(self.gui.main_window.ui.left_value.setText, "")
self.gui.main_window_act(self.gui.main_window.ui.left_status.setText, str(self.offset))
time.sleep(0.5)
def toggle_live_sync(self):
print("toggle live sync")
if not self.live_sync_enabled:
self.enable_live_sync()
else:
self.disable_live_sync()
def disable_live_sync(self):
self.live_sync_enabled = False
self.gui.remove_message(0, 1) # Remove will sync message
self.mw_emit([self.gui.main_window.ui.live_button.setIcon, self.gui.icons["sync_disabled"]])
self.mw_emit([self.gui.main_window.ui.live_button.setToolTip, "Live Sync: Disabled"])
def enable_live_sync(self):
self.live_sync_enabled = True
self.mw_emit([self.gui.main_window.ui.live_button.setIcon, self.gui.icons["sync"]])
self.mw_emit([self.gui.main_window.ui.live_button.setToolTip, "Live Sync: Enabled"])
def restart_sync_thread_runner(self, restart):
self.mw_act(self.gui.main_window.ui.sim_label.setText, 'Waiting Simulator...')
self.mw_act(self.gui.main_window.ui.sim_time_hour.setText, "")
self.mw_act(self.gui.main_window.ui.sim_time_minute.setText, "")
self.mw_act(self.gui.main_window.ui.sim_time_second.setText, "")
self.mw_act(self.gui.main_window.ui.sim_time_second.setToolTip, "")
self.mw_act(self.gui.main_window.ui.sim_time_seperator.setText, "")
self.mw_act(self.gui.main_window.ui.sim_date.setText, "")
self.fs_sync.reset()
time.sleep(10) # Sleep, make sure FSUIPC exits completely.
self.sync_thread_runner(restart=restart + 1)
def sync_thread_runner(self, restart=0):
# Est. FSUIPC connection.
# Try every 10 seconds.
self.sync_run = True
while self.sync_run:
if not self.fs_sync.connect_pyuipc():
# print(self.fs_sync.connect_pyuipc())
# print("Cannot connect FSUIPC.")
time.sleep(10)
continue
self.mw_act(self.gui.main_window.ui.sim_label.setText, self.fs_sync.opened_sim)
break
offsets = {
"TIME_SECOND": [0x023A, "b"],
"TIME_HOUR": [0x023B, "b"],
"TIME_MINUTE": [0x023C, "b"],
"DATE_DAY": [0x023D, "b"],
"DATE_MONTH": [0x0242, "b"],
"DATE_YEAR": [0x0240, "H"], # Fixed from local
}
if self.fs_sync.pyuipc_open == SIMULATOR_XPLANE_ID:
offsets["DAY_OF_YEAR"] = [0x023E, "h"]
self.time_offsets = self.fs_sync.create_offset_set(offsets)
try:
self.sync_routine_loop()
except (pyuipc.FSUIPCException, SystemError) as exc:
if exc.errorCode == 12 or exc.errorCode == 13:
print(f"Catched PYUIPC error, will restart. Code:{exc.errorCode}")
self.restart_sync_thread_runner(restart=restart)
def sync_routine_loop(self):
two_dots = FlipFlop(":")
while self.sync_run:
data_delta = self.sync_sim()
if not data_delta:
continue
data = data_delta[0]
delta = data_delta[1]
datetime = data_delta[2]
self.mw_emit([self.gui.main_window.ui.sim_time_hour.setText, "{:02d}".format(data["TIME_HOUR"])])
self.mw_emit([self.gui.main_window.ui.sim_time_seperator.setText, str(two_dots)])
self.mw_emit([self.gui.main_window.ui.sim_time_minute.setText, "{:02d}".format(data["TIME_MINUTE"])])
self.mw_emit([self.gui.main_window.ui.sim_time_second.setText, "{:02d}".format(data["TIME_SECOND"])])
self.mw_emit([self.gui.main_window.ui.sim_date.setText, datetime.strftime('%d.%m.%Y')])
self.mw_emit([self.gui.main_window.ui.sim_time_second.setToolTip, "ε: ±{}s Δ: {}s".format(30, int(delta))])
# print(data)
time.sleep(1)
def sync_sim(self, force=False):
"""
Returns initial data if no sync.
Returns new data if there has been sync.
"""
if not self.fs_sync.pyuipc_open:
return
try:
data = self.time_offsets.read()
if self.fs_sync.pyuipc_open == SIMULATOR_XPLANE_ID:
data["DATE_YEAR"] += SIMULATOR_XPLANE_YEAR_DELTA
time_from_data = datetime(data["DATE_YEAR"], 1, 1, data["TIME_HOUR"], data["TIME_MINUTE"], second=data["TIME_SECOND"])
time_from_data = time_from_data + timedelta(days=data["DAY_OF_YEAR"])
else:
time_from_data = datetime(data["DATE_YEAR"], data["DATE_MONTH"], data["DATE_DAY"], data["TIME_HOUR"], data["TIME_MINUTE"], second=data["TIME_SECOND"])
now = self.offset + self.get_now()
delta = (now - time_from_data).total_seconds()
except ValueError as exc:
# ValueError generally thrown when FSUIPC is reporting year out of range.
# Happens on scenerio screen.
print(exc)
time.sleep(0.5)
return False
if self.live_sync_enabled or force:
if not self.fs_sync.pyuipc_open:
return
if abs(delta) > 30 or force:
if force:
if data["TIME_SECOND"] - now.second > 20:
self.time_offsets.write("TIME_SECOND", 0)
else:
if now.second > 3:
self.gui.add_message(0, 1, "Will Sync At: {:02d}:{:02d}z".format(now.hour, now.minute + 1))
return [data, delta, time_from_data]
self.time_offsets.write("TIME_SECOND", 0)
print("DOING A ZULU TIME SYNC.")
if self.fs_sync.pyuipc_open == SIMULATOR_XPLANE_ID:
self.time_offsets.write("DATE_YEAR", now.year - SIMULATOR_XPLANE_YEAR_DELTA)
self.time_offsets.write("DAY_OF_YEAR", int(now.strftime('%j')) - 1)
self.time_offsets.write("TIME_HOUR", now.hour)
self.time_offsets.write("TIME_MINUTE", now.minute)
else:
self.time_offsets.write("DATE_YEAR", now.year)
self.time_offsets.write("DATE_MONTH", now.month)
self.time_offsets.write("DATE_DAY", now.day)
self.time_offsets.write("TIME_HOUR", now.hour)
self.time_offsets.write("TIME_MINUTE", now.minute)
self.gui.remove_message(0, 1) # Remove will sync message
self.gui.add_message(0, 2, "Last Sync: {:02d}:{:02d}:{:02d}z".format(now.hour, now.minute, now.second))
return [self.time_offsets.read(), delta, time_from_data] # Return fresh data
return [data, delta, time_from_data]
if __name__ == "__main__":
ROOT = FSTimeSync()
ROOT.start()