Esempio n. 1
0
def ensure_devices_exist(device_nums):
    """
    Ensure all devices we're going to process exist on both the source and destination machines
    """
    tbapi_old = TbApi(birdhouse_utils.make_mothership_url(old_server_ip),
                      thingsboard_username, thingsboard_password)
    tbapi_new = TbApi(birdhouse_utils.make_mothership_url(new_server_ip),
                      thingsboard_username, thingsboard_password)

    print(
        "Making sure every device we want to migrate exists on both source and desitnation machines"
    )

    for num in device_nums:
        print(".", end="")

        name = birdhouse_utils.make_device_name(num)

        old_device = tbapi_old.get_device_by_name(name)
        if old_device is None:
            print(f"\nDevice {name} does not exist on old server!")
            exit()

        new_device = tbapi_new.get_device_by_name(name)
        if new_device is None:
            print(f"\nDevice {name} does not exist on new server!")
            exit()

        old_key = tbapi_old.get_id(old_device)
        new_key = tbapi_new.get_id(new_device)

        if old_key != new_key:
            print("\nDevice keys are different on old and new servers!")
            exit()
    print()
Esempio n. 2
0
def check_for_remigration(to_do_list):
    """
    Make sure we're not queueing up a device we've already migrated
    """
    tbapi_old = TbApi(birdhouse_utils.make_mothership_url(old_server_ip),
                      thingsboard_username, thingsboard_password)
    tbapi_new = TbApi(birdhouse_utils.make_mothership_url(new_server_ip),
                      thingsboard_username, thingsboard_password)

    old_client = create_client(old_server_ip)
    new_client = create_client(new_server_ip)

    print(
        "Conducting remigration checks (to make sure device hasn't already been migrated to new server)"
    )

    for num in to_do_list:
        print(".", end="")
        name = birdhouse_utils.make_device_name(num)

        if not device_has_data(tbapi_old, old_client, name):
            print(
                f"\nThere is no data on the old server for device {name}.  Nothing to migrate."
            )
            exit()

        if device_has_been_migrated(tbapi_old, old_client, tbapi_new,
                                    new_client, name):
            print(f"\nDevice {name} has already been migrated.")
            exit()
    print()
Esempio n. 3
0
def find_unmigrated_devices(old_ip, new_ip):
    tbapi_old = TbApi(birdhouse_utils.make_mothership_url(old_ip),
                      thingsboard_username, thingsboard_password)
    tbapi_new = TbApi(birdhouse_utils.make_mothership_url(new_ip),
                      thingsboard_username, thingsboard_password)

    old_client = create_client(old_ip)
    new_client = create_client(new_ip)

    devices_on_old = tbapi_old.get_all_devices()
    devices_on_new = tbapi_new.get_all_devices()

    unmigrated_ready_to_go, migrated, unmigrated, no_data, no_data_2, new_only = list(
    ), list(), list(), list(), list(), list()

    for dev in devices_on_old:
        name = dev["name"]

        print(f"Device {name}")
        has_telem_on_old = has_telemetry(tbapi_old, dev)
        has_telem_on_new = has_telemetry(tbapi_new, dev)

        if not device_has_data(tbapi_old, old_client, name):
            no_data_2.append(name)

        if has_telem_on_old:
            if has_telem_on_new:
                # Telemetry exists on both servers; has it been migrated, or merely switched itself over?
                if device_has_been_migrated(tbapi_old, old_client, tbapi_new,
                                            new_client, name):
                    migrated.append(name)
                else:
                    unmigrated_ready_to_go.append(name)
            else:  # on old, not on new
                unmigrated.append(name)
        else:  # not old
            if has_telem_on_new:
                new_only.append(name)
            else:
                no_data.append(name)

    for dev in devices_on_new:
        if dev not in devices_on_old:
            new_only.append(dev["name"])

    print(f"Migrated {sorted(migrated)}")
    print(
        f"Unmigrated and unswitched (possibly no longer collecting data): {sorted(unmigrated)}"
    )
    print(f"Ready to migrate: {sorted(unmigrated_ready_to_go)}")
    # Not sure which of these is more reliable -- they generate their results using different methods
    print(f"No data: {sorted(no_data)}")
    print(f"No data2: {sorted(no_data_2)}")
    print(f"New only, nothing to do: {sorted(new_only)}")

    exit()
def make_params(nums):
    mothership_url = birdhouse_utils.make_mothership_url(args)

    tbapi = TbApi(mothership_url, thingsboard_username, thingsboard_password)

    params = []

    for num in nums:
        print(f"Retrieving details for device {num}... ", end='', flush=True)
        device_name = birdhouse_utils.make_device_name(num)
        dash_name = birdhouse_utils.make_dash_name(num)

        device = tbapi.get_device_by_name(device_name)
        dash = tbapi.get_dashboard_by_name(dash_name)
        dash_url = tbapi.get_public_dash_url(dash)
        tiny_url = make_tiny_url(dash_url)

        if device is None:
            print(f"Failed.\nCould not find device {num}... Aborting.")
            exit()

        token = tbapi.get_device_token(device)

        params.append((
            birdhouse_utils.get_sensor_type(num)[1],
            birdhouse_utils.make_device_number(num),
            token,
            tiny_url
        ))
        print("done.")

    return params
Esempio n. 5
0
def main():

    mothership_url = birdhouse_utils.make_mothership_url(args, config)
    tbapi = TbApi(mothership_url, thingsboard_username, thingsboard_password)

    print(f"Retrieving template dashboard {template_dash}... ",
          end='',
          flush=True)
    template_dash_def = tbapi.get_dashboard_definition(
        tbapi.get_dashboard_by_name(template_dash))

    # We also need the id of the device being swapped out
    template_device_id = tbapi.get_id(
        tbapi.get_device_by_name(
            birdhouse_utils.make_device_name(args["<device>"])))

    print(" done.")

    all_devices = tbapi.get_devices_by_name(copy_to_pattern)

    for device in all_devices:
        num = birdhouse_utils.get_device_number_from_name(device["name"])

        print(f"Updating dashboard for {device['name']}")
        dash_name_being_replaced = birdhouse_utils.make_dash_name(num)
        device_name = birdhouse_utils.make_device_name(num)
        device = tbapi.get_device_by_name(device_name)
        device_id = tbapi.get_id(device)

        # The dash we are replacing:
        dash_being_replaced = tbapi.get_dashboard_by_name(
            dash_name_being_replaced)
        dash_id_being_replaced = tbapi.get_id(dash_being_replaced)

        dash_def = tbapi.get_dashboard_definition(
            tbapi.get_dashboard_by_name(
                template_dash))  # dash_def will be modified
        birdhouse_utils.reassign_dash_to_new_device(dash_def,
                                                    dash_name_being_replaced,
                                                    template_device_id,
                                                    device_id, device_name)

        dash_def["id"]["id"] = dash_id_being_replaced

        # del_humidity(dash_def)
        # exit()

        tbapi.save_dashboard(dash_def)
Esempio n. 6
0
def main():

    mothership_url = birdhouse_utils.make_mothership_url(args, config)
    tbapi = TbApi(mothership_url, thingsboard_username, thingsboard_password)

    for num in args["<num>"]:
        print(f"Retrieving details for device {num}... ", end='', flush=True)
        name = birdhouse_utils.make_device_name(num)
        device = tbapi.get_device_by_name(name)

        if device is None:
            print(f"Failed.\nCould not find device {num}... Aborting.")
            exit()

        token = tbapi.get_device_token(device)
        print("done.")
        print(token)
Esempio n. 7
0
from thingsboard_api_tools import TbApi  # pip install git+git://github.com/eykamp/thingsboard_api_tools.git --upgrade
from config import thingsboard_username, thingsboard_password  # You'll need to create this... Be sure to gitignore it!
import config

args = docopt(__doc__)

winscp_profile = args['<winscpprofile>']
devices = args['<devices>'] if not args['all'] else "all"
clean = args['--clean']
build_only = args['build']

# clean = True
# build_only = False

mothership_url = birdhouse_utils.make_mothership_url(args, config)
tbapi = TbApi(mothership_url, thingsboard_username, thingsboard_password)

winscp_program_location = r"c:\Program Files (x86)\WinSCP\WinSCP.com"
arduino_build_exe_location = r"c:\Program Files (x86)\Arduino\arduino_debug.exe"

source_name = "firmware"
source_file = source_name + ".ino"
build_folder = r"C:\Temp\BirdhouseFirmwareBuildFolder"  # Where we store the firmware image we're building
remote_dir = "/sensorbot/firmware_images"  # Where we store the firmware images on the update server


def main():
    version = extract_version(source_file)
    build_target_file = source_name + "_" + str(version) + ".bin"
Esempio n. 8
0
def verify_devices_remapped(device_nums,
                            min_interval=120,
                            age_considered_offline=7 * DAYS):  # In seconds
    """
    Look at the ages of the most recent telemetry, and ensure each device is sending current telemetry to the new server.
    The theory is that once a device is talking to the new server, it will never go back to the old, and we can safely
    shift all telemetry from the old server.

    min_interval is the minimum time, in seconds, that the new server must be ahead of the old server for this check to pass.

    Note that if the most recent telemetry on the old server is older than age_considered_offline, we'll assume the device is out of contact
    (perhaps disconnceted or off), and we'll go ahead and migrate the data over.
    """
    tbapi_old = TbApi(birdhouse_utils.make_mothership_url(old_server_ip),
                      thingsboard_username, thingsboard_password)
    tbapi_new = TbApi(birdhouse_utils.make_mothership_url(new_server_ip),
                      thingsboard_username, thingsboard_password)

    old_client = create_client(old_server_ip)
    new_client = create_client(new_server_ip)

    print(
        "Verifying devices have attached themselves to the new server and aren't still sending data to the old"
    )

    now = int(time.time() * 1000)

    for num in device_nums:
        print(".", end="")

        name = birdhouse_utils.make_device_name(num)
        latest_old = get_latest_telemetry_date(tbapi_old, old_client,
                                               name) or 0

        if latest_old == 0:  # There is no data on the old server; no need to migrate, but no problem moving forward, either
            continue

        latest_new = get_latest_telemetry_date(tbapi_new, new_client,
                                               name) or 0

        age_of_old = int(
            (now - latest_old) /
            1000)  # Age of most recent telemetry on old server in seconds

        diff = int(
            (latest_new - latest_old) / 1000
        )  # Diffrence in ages between most recent telmetry on new and old server, in seconds

        if age_of_old < age_considered_offline and diff < min_interval:
            print(
                f"\nIt looks like device {name} is still active and hasn't switched to new server yet.  Not ready to migrate data!"
            )
            print(
                f"\tLast telemetry on old server was {format_time_delta(age_of_old)} ago."
            )
            if latest_new == 0:
                print(
                    "\tThe device has not yet sent any data to the new server."
                )
            else:
                print(
                    f"\tLast telemetry on new server was {format_time_delta(now - latest_new)} ago."
                )
            exit()
    print()