Exemplo n.º 1
0
def main():
    tbapi = TbApi(motherShipUrl, username, password)

    # Lookup missing fields, such as zip, lat, and lon
    birdhouse_utils.update_customer_data(cust_info)

    if cust_info["lat"] is None or cust_info["lon"] is None:
        print("Must have valid lat/lon to continue!")
        exit(1)

    name = birdhouse_utils.make_device_name(birdhouse_number)

    cust = tbapi.get_customer(name)
    devices = tbapi.get_customer_devices(cust)

    print(devices)

    device = tbapi.get_device_by_name(name)

    customer = tbapi.update_customer(cust, None, cust_info["address"],
                                     cust_info["address2"], cust_info["city"],
                                     cust_info["state"], cust_info["zip"],
                                     cust_info["country"])
    server_attributes = {
        "latitude": cust_info["lat"],
        "longitude": cust_info["lon"],
        "address": birdhouse_utils.one_line_address(cust_info)
    }
    tbapi.set_server_attributes(device, server_attributes)

    exit()

    cust = tbapi.get_customer(name)
Exemplo n.º 2
0
def create_server_objects(tbapi, birdhouse_number):
    """ Returns secret token of created device """
    if settings.dashboard_template_name is None:
        print(
            "Please ensure you have a local file called config.py that defines the following variables:"
        )
        print(
            "\tdashboard_template_name:     The name of an existing dashboard to be used as a template for new devices"
        )
        exit()

    device_name, cust_name, dash_name = birdhouse_utils.get_server_object_names_for_birdhouse(
        birdhouse_number)

    print("Creating server objects for '" + device_name + "'...", end='')

    if (tbapi.get_customer_by_name(cust_name) is not None
            or tbapi.get_device_by_name(device_name) is not None
            or tbapi.get_dashboard_by_name(dash_name) is not None):
        print(" error")
        print("Objects already exist for device '" + device_name + "'")
        print("Aborting!")
        exit()

    # Get a definition of our template dashboard, returns device_token for the created device
    template_dash = tbapi.get_dashboard_by_name(
        settings.dashboard_template_name)
    if template_dash is None:
        print(" error")
        print(
            "Cannot retrieve template dashboard '%s'.  Is that the correct name?"
            % settings.dashboard_template_name)
        exit()

    dash_def = tbapi.get_dashboard_definition(template_dash)

    customer = create_customer(tbapi, cust_name, cust_info)

    server_attributes = {
        "latitude": cust_info["lat"],
        "longitude": cust_info["lon"],
        "address": birdhouse_utils.one_line_address(cust_info)
    }

    shared_attributes = None  # ??? Not sure

    sensor_type = birdhouse_utils.get_sensor_type(birdhouse_number)[0]
    device = tbapi.add_device(device_name, sensor_type, shared_attributes,
                              server_attributes)
    device_id = tbapi.get_id(device)

    # We need to store the device token as a server attribute so our REST services can get access to it
    device_token = tbapi.get_device_token(device_id)

    server_attributes = {"device_token": device_token}

    tbapi.set_server_attributes(device_id, server_attributes)

    try:
        # Upate the dash def. to point at the device we just created (modifies dash_def)

        template_device = tbapi.get_device_by_name(dashboard_template_device)
        template_device_id = tbapi.get_id(template_device)
        birdhouse_utils.reassign_dash_to_new_device(
            dash_def, birdhouse_utils.make_dash_name(birdhouse_number),
            template_device_id, device_id, device_name)
    except Exception as ex:
        print(" error")
        print("Exception encountered: Cleaning up...")
        birdhouse_utils.purge_server_objects_for_device(
            tbapi, birdhouse_number)
        raise ex

    # Create a new dash with our definition, and assign it to the new customer
    dash = tbapi.create_dashboard_for_customer(dash_name, dash_def)
    tbapi.assign_dash_to_user(dash, customer)

    print(" ok")
    print("Device token for " + device_name +
          " (set device token: setparams?deviceToken=" + device_token + ")")

    return device_token
Exemplo n.º 3
0
def create_device(cleanup):

    # Get a definition of our template dashboard
    template_dash = tbapi.get_dashboard_by_name(dashboard_template_name)
    if template_dash is None:
        print("Cannot retrieve template dash %s.  Is that the correct name?" %
              dashboard_template_name)
        sys.exit()

    dash_def = tbapi.get_dashboard_definition(tbapi.get_id(template_dash))

    customer = create_customer(cust_info)
    if customer is None:
        exit()

    server_attributes = {
        "latitude": cust_info["lat"],
        "longitude": cust_info["lon"],
        "address": birdhouse_utils.one_line_address(cust_info)
    }

    shared_attributes = {"LED": "Unknown"}

    device = tbapi.add_device(
        birdhouse_utils.make_device_name(birdhouse_number), sensor_type,
        shared_attributes, server_attributes)
    device_id = tbapi.get_id(device)

    # We need to store the device token as a server attribute so our REST services can get access to it
    device_token = tbapi.get_device_token(device_id)

    server_attributes = {"device_token": device_token}

    tbapi.set_server_attributes(device_id, server_attributes)

    try:
        # Upate the dash def. to point at the device we just created (modifies dash_def)
        update_dash_def(dash_def, cust_info["name"], device_id)
    except Exception as ex:
        print("Exception encountered: Cleaning up...")
        tbapi.delete_device(device_id)
        tbapi.delete_customer_by_id(tbapi.get_id(customer))
        raise ex

    # Create a new dash with our definition, and assign it to the new customer
    dash = tbapi.create_dashboard_for_customer(cust_info["name"] + ' Dash',
                                               dash_def)
    tbapi.assign_dash_to_user(tbapi.get_id(dash), tbapi.get_id(customer))

    print("Device token for " + birdhouse_name +
          " (set device token: setparams?deviceToken=" + device_token + ")")

    if cleanup:
        # input("Press Enter to continue...")   # Don't run from Sublime with this line enabled!!!

        print("Cleaning up! (device token rendered invalid)")
        tbapi.delete_dashboard(tbapi.get_id(dash))
        tbapi.delete_device(device_id)
        tbapi.delete_customer_by_id(tbapi.get_id(customer))

    return device_token