-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar.py
185 lines (152 loc) · 5.46 KB
/
car.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
import json
import os
import random
import datetime
from twisted.internet import reactor
from twisted.python import log
from interfaces import ICar, IUpdatable
from navigator import Navigator
from utils.geo import GeoVector, GeoPoint
from utils.units_helpers import to_meters_per_second, to_liters_per_meter
class Car(ICar, IUpdatable):
def __init__(self, vin, city, schedule):
"""
:param vin:
:param city:
:type schedule: utils.datehelper.Schedule
"""
# TODO: internal logger
self._vin = vin
self._saver = CarSaver(vin, self)
data = self._saver.load()
self._navigator = Navigator(city, data.get('position'))
self._max_speed = to_meters_per_second(60)
self._speed = 0
self._heading = data.get('heading', 0)
self._mileage = data.get('mileage', 0)
self._tank_value = 50.
self._fuel_level = data.get('fuel_level', self._tank_value)
self._fuel_speed = to_liters_per_meter(12)
self._schedule = schedule
self._delayed_start(3)
def get_heading(self):
return self._heading
def get_speed(self):
return self._speed
def get_location(self):
return self._navigator.get_location()
def get_odometer_value(self):
return 0
def get_full_mileage(self):
return self._mileage
def get_fuel_level(self):
return self._fuel_level
def get_fuel_level_rel(self):
return (self._fuel_level / self._tank_value) * 100
def update(self, t):
if not self._navigator.is_arrived():
path = self._move(t)
self._update_fuel(path)
def _get_fuel_speed(self):
"""
Get random fuel consumption in range 0.8origin .. 1.2origin
:return:
"""
return self._fuel_speed * random.uniform(0.8, 1.2)
def _update_fuel(self, path):
"""
:param path: length on moved path in m
"""
# Fuel spent in liters
value = path * self._get_fuel_speed()
self._fuel_level -= value
# TODO: logic for smart fuel refills
if self._fuel_level < 0:
self._fuel_level = 0
def _update_refuel(self):
if self._fuel_level <= 5.:
free_value = self._tank_value - self._fuel_level
self._fuel_level += random.randint(int(0.5 * free_value), int(free_value))
def _move(self, t):
"""
:param t: Time passed since last update in seconds
:return: Path moved in meters
"""
potential_distance = t * self.get_speed() # meters
moved_distance = 0
while not self._navigator.is_arrived():
old_location = self.get_location()
next_point = self._navigator.get_next_point()
vector_to_point = next_point - old_location
self._heading = vector_to_point.heading
if potential_distance < vector_to_point.meters:
potential_vector = GeoVector(
value=potential_distance, heading=vector_to_point.heading)
new_location = old_location + potential_vector
moved_distance += potential_distance
self._navigator.set_location(new_location)
break
potential_distance -= vector_to_point.meters
moved_distance += vector_to_point.meters
self._navigator.pop_next_point()
else:
# arrived
self._on_arrived()
return moved_distance
def _on_arrived(self):
log.msg("Car {} arrived to destination point".format(self._vin))
self._speed = 0
# TODO: custom reactor
self._delayed_start()
def _delayed_start(self, delay=300):
now = datetime.datetime.utcnow()
timeout = self._schedule.get_time_to_next_ride(now, delay=delay,
precision=2)
reactor.callLater(timeout, self._start)
def _start(self):
if not self._navigator.build_path():
self._delayed_start()
else:
log.msg("Car {} arrived to destination point. Fuel level: {}"
"".format(self._vin, self._fuel_level))
self._update_refuel()
self._speed = self._max_speed
class CarSaver(object):
period = 5
def __init__(self, vin, obj):
"""
:type obj: Car
"""
self._vin = vin
self._obj = obj
def _get_filename(self):
return 'data/{}.json'.format(self._vin)
def load(self):
"""
:rtype: utils.GeoPoint
"""
self.call_later()
if os.path.exists(self._get_filename()):
try:
with open(self._get_filename(), 'r') as f:
data = json.load(f)
if 'type' in data:
return {
'position': GeoPoint.from_dict(data)
}
data['position'] = GeoPoint.from_dict(data['position'])
return data
finally:
pass
def save(self):
data = {
'position': self._obj.get_location().to_dict(),
'mileage': self._obj.get_odometer_value(),
'heading': self._obj.get_heading(),
'fuel_level': self._obj.get_fuel_level()
}
with open(self._get_filename(), 'w') as f:
json.dump(data, f)
self.call_later()
def call_later(self):
reactor.callLater(self.period, self.save)