Пример #1
0
def get_state():
    """Get the current drone state from the drone server."""
    drone = get_drone()
    drone_state = drone["State"]
    drone_state["DroneID"] = drone["DroneID"]

    return drone_state
Пример #2
0
def init_datastream_locally():
    """Initialize the datasteam locally."""
    drone = get_drone()
    id_ = drone["DroneID"]
    position = drone["State"]["Position"]
    datastream = gen_Datastream("0", position, id_)
    add_datastream(datastream)
    print("Datastream initialized locally")
Пример #3
0
def init_datastream_locally():
    """Initialize the datasteam locally."""
    drone = get_drone()
    id_ = drone["DroneID"]
    position = drone["State"]["Position"]
    datastream = gen_Datastream("0", position, id_)
    add_datastream(datastream)
    print("Datastream initialized locally")
Пример #4
0
def update_drone_id(id_):
    """Update the drone identifier."""
    # GET current drone object
    drone = get_drone()
    # Update the drone id
    drone["DroneID"] = id_

    # Update drone object
    update_drone(drone)
    print("DroneID updated successfully", id_)
    return None
Пример #5
0
def update_state(state):
    """Update the drone state on drone server."""
    drone = get_drone()
    if drone["DroneID"] == state["DroneID"]:
        # Remove the DroneID key from state
        state.pop("DroneID", None)

        # Update the drone state
        drone["State"] = state
        update_drone(drone)
        print("Drone state updated successfully.")
    else:
        print("ERROR: DroneID %s not valid." % (state["DroneID"]))
Пример #6
0
def init_drone():
    """Initialize drone."""
    # Add drone to the central_server and get identifier
    init_drone_locally()
    print("DRone initialization success")

    drone = get_drone()
    drone.pop("DroneID", None)
    drone_id = int(add_drone(drone))

    # Update the drone on localhost
    update_drone_id(drone_id)

    print("Drone initialized successfully!")
    return None
Пример #7
0
def init_drone():
    """Initialize drone."""
    # Add drone to the central_server and get identifier
    init_drone_locally()

    drone = get_drone()
    drone_id = drone["DroneID"]

    # If drone has default negative id initialize else remove old drone and then inintialize.
    if drone_id == -1000:
        drone_id = add_drone(drone)
    else:
        # Remove old drone
        remove_drone(drone_id)
        print("Previous drone successfully deleted from the central server.")
        drone_id = add_drone(drone)

    # Update the drone at localhost
    drone["DroneID"] = drone_id

    update_drone(drone)
    update_drone_at_controller(drone, drone_id)

    print("Drone initialized successfully!")
Пример #8
0
def init_drone():
    """Initialize drone."""
    # Add drone to the central_server and get identifier
    init_drone_locally()

    drone = get_drone()
    drone_id = drone["DroneID"]

    # If drone has default negative id initialize else remove old drone and then inintialize.
    if drone_id == -1000:
        drone_id = add_drone(drone)
    else:
        # Remove old drone
        remove_drone(drone_id)
        print("Previous drone successfully deleted from the central server.")
        drone_id = add_drone(drone)

    # Update the drone at localhost
    drone["DroneID"] = drone_id

    update_drone(drone)
    update_drone_at_controller(drone, drone_id)

    print("Drone initialized successfully!")
Пример #9
0
def main():
    """Main 15 second time loop for drone mechanics."""
    try:
        print("Retrieving the drone details")
        drone = get_drone()
        drone_identifier = drone["DroneID"]
        datastream = None

        # Commands will be executed in any state
        drone = handle_drone_commands(drone)

        if is_not_off(drone):

            ## Handle drone battery change
            drone = handle_drone_battery(drone)

            ## Handle drone general behaviour
            anomaly = get_anomaly()
            if anomaly is not None:
                if anomaly["Status"] == "Confirming" and drone["State"][
                        "Status"] == "Active":
                    drone["State"]["Status"] = "Confirming"

            if is_confirming(drone):
                print("Drone handling anomaly")
                drone = handle_anomaly(drone)

            elif is_inactive(drone):
                print("Drone battery low, needs to charge")
                drone = handle_drone_low_battery(drone)

            elif is_active(drone):
                anomaly = gen_grid_anomaly(drone)
                if anomaly is not None:
                    print("New anomaly created")
                    send_anomaly(anomaly, drone_identifier)
                    datastream = gen_Datastream(gen_abnormal_sensor_data(),
                                                drone["State"]["Position"],
                                                drone_identifier)
                else:
                    datastream = gen_Datastream(gen_normal_sensor_data(),
                                                drone["State"]["Position"],
                                                drone_identifier)

            # Handle positions change
            drone = handle_drone_position(drone)

        # update the drone both locally and on the controller
        update_drone(drone)

        update_drone_at_controller(drone, drone_identifier)

        if datastream is not None:
            # Send datastream to central controller
            send_datastream(datastream)
            # Update datastream locally
            update_datastream(datastream)

    except Exception as e:
        print(e)

    finally:
        # call main() again in LOOP_TIME
        threading.Timer(LOOP_TIME, main).start()
Пример #10
0

def get_drone_iri(drone):
    """Get the DroneID from a drone object and return DroneID, drone."""
    drone_id = drone.pop("DroneID", None)
    drone_id = "/serverapi/DroneCollection/" + drone_id
    return drone_id, drone


def post_drone(id_, drone):
    """Update a drone object in  the collection given command @id attribute."""
    try:
        i = Resource.from_iri(CENTRAL_SERVER_URL + id_)
        # name = i.value(SCHEMA.name)
        resp, _ = i.find_suitable_operation(SCHEMA.UpdateAction,
                                            CENTRAL_SERVER.Drone)(drone)
        if resp.status // 100 != 2:
            return "error updating <%s>" % i.identifier
        else:
            return "successfully updated <%s>" % i.identifier
    except:
        return {404: "Resource with Id %s not found!" % (id_, )}


# print(delete_command("/api/CommandCollection/175"))

drone = get_drone()
drone_id, drone = get_drone_iri(drone)
print(drone_id, drone)
print(post_drone(drone_id, drone))