def topic_traffic_nearby_req_callback(mqttc, obj, msg): payload = str(msg.payload.decode("utf-8")) items = payload.split(',') veh_id = items[0] latitude = float(items[1]) longitude = float(items[2]) radius = float(items[3]) global features user_geolocation = VSA.Geolocation(latitude, longitude) features_res = [] features_tmp = features last_radius = 0 last_lat = 0 last_lng = 0 for feature in features_tmp: include = False for geometry in feature.Geometries: for coordinate in geometry.Coordinates: if (VSA.distance(user_geolocation, coordinate) <= radius): include = True break if (include): break if (include): features_res.append(feature) print(str(len(features_res)) + " events found.") client.publish(VSA.TOPIC_TRAFFIC_NEARBY_RETURN + "/" + veh_id, json.dumps(features_res, default=VSA.serialize))
def topic_traffic_callback(mqttc, obj, msg): payload = str(msg.payload.decode("utf-8")) json_features = json.loads(payload) global features features = [] for data in json_features: feature = VSA.Feature(data) features.append(feature) print("Features updated.")
def topic_vehprop_callback(mqttc, obj, msg): payload = str(msg.payload.decode("utf-8")) items = payload.split(',') uid = items[0] name = items[1] type = items[2] left = float(items[3]) top = float(items[4]) right = float(items[5]) bottom = float(items[6]) dimensions = (left, top, right, bottom) global vehicles if uid in vehicles: del vehicles[uid] vehicle = VSA.Vehicle(uid, name, type, dimensions) vehicles[uid] = vehicle
client.connect(Broker.ADDRESS, Broker.PORT) print("Connected to broker.") #initialise vehicles for index in range(0, NODES_LENGTH): uid = str(index + 1) name = "vehicle-" + str(index) type = "cycle" dimensions = (1, 1, 1, 1) latitude = STARTING_LAT + (index * COORD_VARIATION) longitude = STARTING_LON + (random.random() * COORD_VARIATION) velocity = 0 position_error = random.random() * 100 rotation_angle = random.random() * 100 vehicle = VSA.Vehicle(uid, name, type, dimensions) vehicle.update_status(latitude, longitude, velocity, position_error, rotation_angle) vehicles.append(vehicle) topic = VSA.TOPIC_LEVEL_A_VEHPROP message = ','.join( [uid, name, type, ','.join(list(map(str, list(dimensions))))]) client.publish(topic, message) #update status try: while True: for index in range(0, NODES_LENGTH): vehicle = vehicles[index] latitude = vehicle.coordinate.latitude - COORD_VARIATION