-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathneutronplotter.py
71 lines (59 loc) · 2.03 KB
/
neutronplotter.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
import json
import time
from threading import Lock
from threading import Thread
import requests
# Core API link
API = "https://spansh.co.uk/api/"
# Timeout value
TIMEOUT = 30
class NeutronPlotter:
def __init__(self, globals):
self.globals = globals
self.origin = None
self.destination = None
self.efficiency = None
self.range = None
# Special data handling
self.route = None
self.job = None
# synchronize
self.lock = Lock()
def request_calculation(self, origin, destination, efficiency, ship_range):
self.globals.logger.debug("Neutron plotter -> calculation requested")
self.origin = origin
self.destination = destination
self.efficiency = efficiency
self.range = ship_range
# Prepare link for job queue
link_job = API + "route?from={}&to={}&efficiency={}&range={}".format(origin, destination,efficiency, ship_range)
self.globals.logger.debug("Neutron -> request target is {}".format(link_job))
# API calls for job queue
request = requests.get(link_job).json()
self.job = request["job"]
thread = Thread(target=self.wait_for_route)
thread.start()
# After timeout we dont care anymore
thread.join(timeout=TIMEOUT+1)
# synchronize critical path
with self.lock:
return self.route
def wait_for_route(self,):
if self.job is None or len(self.job) < 1:
return
# Prepare link for route data
link_data = API + "results/{}".format(self.job)
# WAIT for data
data = None
time_start = time.time()
data_request = requests.get(link_data)
while data_request.status_code != 200:
data_request = requests.get(link_data)
time_elapsed = time.time() - time_start
if time_elapsed > TIMEOUT:
break
# Get json
data = data_request.json()
# Synchronize
with self.lock:
self.route = data