def get_busy_points(request): arr = [] with open('static/roads.json', 'r') as f: file_data = load(f) for copter in copters: if copter.status != 'land': try: if copter.last_point != -1: n = int(copter.path[0][:-1]) nav_point = file_data['points'][n] nav_point['z'] = c.first_layer_height if copter.path[0][-1:] == '0': nav_point['z'] = c.first_layer_height elif copter.path[0][-1:] == '1': nav_point['z'] = c.second_layer_height dist = get_distance(nav_point['x'], nav_point['y'], nav_point['z'], copter.x, copter.y, copter.z) if dist > threshold: arr.append(int(copter.last_point[:-1])) except: pass try: arr.append(int(copter.path[0][:-1])) except: pass return JsonResponse({'busy': arr})
def get_nearest_point(c): index = 0 min_dist = 9999999 with open('static/roads.json', 'r') as f: data = load(f) for i in range(0, len(data['points'])): dist = get_distance(c.x, c.y, 0, data['points'][i]['x'], data['points'][i]['y'], 0) if min_dist > dist: index = i min_dist = dist return index
def calc_path(path): dist = 0 with open('static/roads.json', 'r') as f: data = load(f) for i in range(0, len(path[:-1])): p1 = { 'x': data['points'][int(path[i][:-1])]['x'], 'y': data['points'][int(path[i][:-1])]['y'], } if path[i][-1] == 0: p1['z'] = c.first_layer_height else: p1['z'] = c.second_layer_height p2 = { 'x': data['points'][int(path[i + 1][:-1])]['x'], 'y': data['points'][int(path[i + 1][:-1])]['y'], } if path[i + 1][-1] == 0: p2['z'] = c.first_layer_height else: p2['z'] = c.second_layer_height dist += get_distance(p1['x'], p1['y'], p1['z'], p2['x'], p2['y'], p2['z']) return dist
def ask_taxi(request): data = request.GET.dict() busy = 0 points = [] for i in copters: if i.path != []: busy += 1 points.append(-1) else: points.append(get_nearest_point(i)) if busy == len(copters): return JsonResponse({'m': 'busy'}) busy_points = [] for i in copters: busy_points += i.busy_points if str(data['o']) + '0' in busy_points or str( data['t']) + '0' in busy_points: return JsonResponse({'m': 'wrong'}) with open('static/roads.json', 'r') as f: destination_point = load(f)['points'][int(data['t'])] for copter in copters: if copter.status == 'land': if get_distance(copter.x, copter.y, 0, destination_point['x'], destination_point['y'], 0) < nearest_copter_threshold: return JsonResponse({'m': 'wrong'}) paths = [] for i in range(0, len(points)): if points[i] != -1: paths.append( calc_path( build_path(str(points[i]) + '0', str(data['o']) + '0'))) else: paths.append(9999999) nearest_copter = copters[paths.index(min(paths))] first_path = build_path( str(get_nearest_point(nearest_copter)) + '0', str(data['o']) + '0') if len(first_path) != 1: nearest_copter.path += first_path nearest_copter.path.append('-1') nearest_copter.busy_points += [ str(data['o']) + '0', str(data['t']) + '0' ] else: nearest_copter.busy_points += [str(data['t']) + '0'] nearest_copter.path += build_path( str(data['o']) + '0', str(data['t']) + '0') r = lambda: random.randint(0, 255) nearest_copter.led = '#%02X%02X%02X' % (r(), r(), r()) return JsonResponse({ 'm': 'ok', 'color': nearest_copter.led, 'ip': nearest_copter.ip })