# stores and manages clue DB # also manages currently available bike teams from datastructs import * import router import csv, time # variables homeBase = Point(42.340226, -71.088395) # krentzman, can be changed on dashboard clues = [] bikes = [] routes = {"clusters" : [], "individual_routes" : []} #geojson polylines, both between all the clusters assigned_clues = [] clues_last_changed = time.time() home_last_changed = time.time() routes_last_changed = time.time() # called every time a node is added # a bike is added/removed # determines/assigns clusters, and assigns routes to bikes def updateRoutes(): clusters, paths = router.getClusters(bikes, clues, homeBase) routes['clusters'] = paths routes_last_changed = time.time() # interface functions def getTime(): return time.time() def getHomeBaseJSON(timestamp): if timestamp < 0 or home_last_changed - timestamp > 0: return homeBase.toJSON() return False def setHomeBase(latitude, longitude): homeBase.setCoords(latitude, longitude) home_last_changed = time.time() def getRoutesJSON(timestamp): if timestamp < 0 or routes_last_changed - timestamp > 0: return routes return False def addBike(team_name, latitude, longitude): for bike in bikes: if bike.name == team_name: # already exists bike.ping() bike.setCoords(latitude, longitude) return # return code indicating already exists, although status still OK newBike = Bike(latitude, longitude, team_name, "ACTIVE") bikes.append(newBike) def pingBike(team_name, latitude, longitude): for bike in bikes: if bike.name == team_name: bike.ping() bike.setCoords(latitude, longitude) break else: # bike team does not exist yet newBike = Bike(latitude, longitude, team_name, "ACTIVE") bikes.append(newBike) updateRoutes() def getBikesJSON(): global bikes bikes = [x for x in bikes if x.checkStatus() >= 0] return [x.toJSON() for x in bikes] def getCluesJSON(timestamp): if timestamp < 0 or clues_last_changed - timestamp > 0: return [x.toJSON() for x in clues] return False def addClue(clue_name, clue_info, longitude, latitude, visited="UNVISITED"): newClue = Clue(latitude, longitude, clue_name, clue_info, visited) clues.append(newClue) clues_last_changed = time.time() def deleteClue(clue_name): for i, clue in enumereate(clues): if clue.name == clue_name: clues.pop(i) clues_last_changed = time.time() break def visitClue(clue_name): for clue in clues: if clue.name == clue_name: clue.visit() clues_last_changed = time.time() # junk for testing with open("all_clues.csv", newline='') as f: csvreader = csv.reader(f, delimiter=',', quotechar='"') i = 1 for row in csvreader: coords = row[1].split(",") coords[0] = float(coords[0]); coords[1] = float(coords[1]); newClue = Clue(coords[0], coords[1], f"Clue #{i}", row[0], "UNVISITED" if i < 50 else "VISITED") clues.append(newClue) i += 1 bike1 = Bike(42.340226, -71.088395, 'speedster', 'ACTIVE') bike2 = Bike(42.320226, -71.100395, 'slowpoke', "ACTIVE") bike1.setTarget("Clue #6") bikes.append(bike1); bikes.append(bike2) updateRoutes() def moveBike2Test(): bike1.move(0, -0.001); bike1.ping();