# Path: devicecloud/devicecore.py
# ADD_GROUP_TEMPLATE = \
#     """
# <DeviceCore>
#     <devConnectwareId>{connectware_id}</devConnectwareId>
#     <grpPath>{group_path}</grpPath>
# </DeviceCore>
# """
# TAGS_TEMPLATE = \
#     """
# <DeviceCore>
#     <devConnectwareId>{connectware_id}</devConnectwareId>
#     <dpTags>{tags}</dpTags>
# </DeviceCore>
# """
# class DeviceCoreAPI(APIBase):
# class Group(object):
# class Device(object):
#     def __init__(self, conn, sci):
#     def get_devices(self, condition=None, page_size=1000):
#     def get_group_tree_root(self, page_size=1000):
#     def get_groups(self, condition=None, page_size=1000):
#     def delete_device(self, dev):
#     def provision_device(self, **kwargs):
#     def provision_devices(self, devices):
#         def write_tag(tag, val):
#         def maybe_write_element(tag, val):
#     def __init__(self, group_id, name, description, path, parent_id):
#     def from_json(cls, json_data):
#     def __repr__(self):
#     def print_subtree(self, fobj=sys.stdout, level=0):
#     def is_root(self):
#     def add_child(self, group):
#     def get_children(self):
#     def get_id(self):
#     def get_name(self):
#     def get_description(self):
#     def get_path(self):
#     def get_parent_id(self):
#     def __init__(self, conn, sci, device_json):
#     def __repr__(self):
#     def get_device_json(self, use_cached=True):
#     def get_tags(self, use_cached=True):
#     def is_connected(self, use_cached=True):
#     def get_connectware_id(self, use_cached=True):
#     def get_device_id(self, use_cached=True):
#     def get_ip(self, use_cached=True):
#     def get_mac(self, use_cached=True):
#     def get_mac_last4(self, use_cached=True):
#     def get_registration_dt(self, use_cached=True):
#     def get_meid(self, use_cached=True):
#     def get_customer_id(self, use_cached=True):
#     def get_group_id(self, use_cached=True):
#     def get_group_path(self, use_cached=True):
#     def get_vendor_id(self, use_cached=True):
#     def get_device_type(self, use_cached=True):
#     def get_firmware_level(self, use_cached=True):
#     def get_firmware_level_description(self, use_cached=True):
#     def get_restricted_status(self, use_cached=True):
#     def get_last_known_ip(self, use_cached=True):
#     def get_global_ip(self, use_cached=True):
#     def get_last_connected_dt(self, use_cached=True):
#     def get_contact(self, use_cached=True):
#     def get_description(self, use_cached=True):
#     def get_location(self, use_cached=True):
#     def get_latlon(self, use_cached=True):
#     def get_user_metadata(self, use_cached=True):
#     def get_zb_pan_id(self, use_cached=True):
#     def get_zb_extended_address(self, use_cached=True):
#     def get_server_id(self, use_cached=True):
#     def get_provision_id(self, use_cached=True):
#     def get_current_connect_pw(self, use_cached=True):
#     def add_to_group(self, group_path):
#     def remove_from_group(self):
#     def add_tag(self, new_tags):
#     def remove_tag(self, tag):
# 
# Path: devicecloud/examples/example_helpers.py
# def get_authenticated_dc():
#     while True:
#         base_url = os.environ.get('DC_BASE_URL', 'https://devicecloud.digi.com')
# 
#         username = os.environ.get('DC_USERNAME', None)
#         if not username:
#             username = input("username: ")
# 
#         password = os.environ.get('DC_PASSWORD', None)
#         if not password:
#             password = getpass("password: ")
# 
#         dc = DeviceCloud(username, password, base_url=base_url)
#         if dc.has_valid_credentials():
#             print("Credentials accepted!")
#             return dc
#         else:
#             print("Invalid username or password provided, try again")


from devicecloud.devicecore import dev_mac, group_path
from devicecloud.examples.example_helpers import get_authenticated_dc
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2015-2018 Digi International Inc.


def show_group_tree(dc):
    stats = {}  # group -> devices count including children

    def count_nodes(group):
        count_for_this_node = \
            len(list(dc.devicecore.get_devices(group_path == group.get_path())))
        subnode_count = 0
        for child in group.get_children():
            subnode_count += count_nodes(child)
        total = count_for_this_node + subnode_count
        stats[group] = total
        return total

    count_nodes(dc.devicecore.get_group_tree_root())
    print(stats)
    dc.devicecore.get_group_tree_root().print_subtree()


if __name__ == '__main__':
    dc = get_authenticated_dc()
    devices = dc.devicecore.get_devices(
        (dev_mac == '00:40:9D:50:B0:EA')
    )

# Path: devicecloud/devicecore.py
# ADD_GROUP_TEMPLATE = \
#     """
# <DeviceCore>
#     <devConnectwareId>{connectware_id}</devConnectwareId>
#     <grpPath>{group_path}</grpPath>
# </DeviceCore>
# """
# TAGS_TEMPLATE = \
#     """
# <DeviceCore>
#     <devConnectwareId>{connectware_id}</devConnectwareId>
#     <dpTags>{tags}</dpTags>
# </DeviceCore>
# """
# class DeviceCoreAPI(APIBase):
# class Group(object):
# class Device(object):
#     def __init__(self, conn, sci):
#     def get_devices(self, condition=None, page_size=1000):
#     def get_group_tree_root(self, page_size=1000):
#     def get_groups(self, condition=None, page_size=1000):
#     def delete_device(self, dev):
#     def provision_device(self, **kwargs):
#     def provision_devices(self, devices):
#         def write_tag(tag, val):
#         def maybe_write_element(tag, val):
#     def __init__(self, group_id, name, description, path, parent_id):
#     def from_json(cls, json_data):
#     def __repr__(self):
#     def print_subtree(self, fobj=sys.stdout, level=0):
#     def is_root(self):
#     def add_child(self, group):
#     def get_children(self):
#     def get_id(self):
#     def get_name(self):
#     def get_description(self):
#     def get_path(self):
#     def get_parent_id(self):
#     def __init__(self, conn, sci, device_json):
#     def __repr__(self):
#     def get_device_json(self, use_cached=True):
#     def get_tags(self, use_cached=True):
#     def is_connected(self, use_cached=True):
#     def get_connectware_id(self, use_cached=True):
#     def get_device_id(self, use_cached=True):
#     def get_ip(self, use_cached=True):
#     def get_mac(self, use_cached=True):
#     def get_mac_last4(self, use_cached=True):
#     def get_registration_dt(self, use_cached=True):
#     def get_meid(self, use_cached=True):
#     def get_customer_id(self, use_cached=True):
#     def get_group_id(self, use_cached=True):
#     def get_group_path(self, use_cached=True):
#     def get_vendor_id(self, use_cached=True):
#     def get_device_type(self, use_cached=True):
#     def get_firmware_level(self, use_cached=True):
#     def get_firmware_level_description(self, use_cached=True):
#     def get_restricted_status(self, use_cached=True):
#     def get_last_known_ip(self, use_cached=True):
#     def get_global_ip(self, use_cached=True):
#     def get_last_connected_dt(self, use_cached=True):
#     def get_contact(self, use_cached=True):
#     def get_description(self, use_cached=True):
#     def get_location(self, use_cached=True):
#     def get_latlon(self, use_cached=True):
#     def get_user_metadata(self, use_cached=True):
#     def get_zb_pan_id(self, use_cached=True):
#     def get_zb_extended_address(self, use_cached=True):
#     def get_server_id(self, use_cached=True):
#     def get_provision_id(self, use_cached=True):
#     def get_current_connect_pw(self, use_cached=True):
#     def add_to_group(self, group_path):
#     def remove_from_group(self):
#     def add_tag(self, new_tags):
#     def remove_tag(self, tag):
# 
# Path: devicecloud/examples/example_helpers.py
# def get_authenticated_dc():
#     while True:
#         base_url = os.environ.get('DC_BASE_URL', 'https://devicecloud.digi.com')
# 
#         username = os.environ.get('DC_USERNAME', None)
#         if not username:
#             username = input("username: ")
# 
#         password = os.environ.get('DC_PASSWORD', None)
#         if not password:
#             password = getpass("password: ")
# 
#         dc = DeviceCloud(username, password, base_url=base_url)
#         if dc.has_valid_credentials():
#             print("Credentials accepted!")
#             return dc
#         else:
#             print("Invalid username or password provided, try again")


# Path: devicecloud/examples/devicecore_playground.py
from devicecloud.devicecore import dev_mac, group_path
from devicecloud.examples.example_helpers import get_authenticated_dc

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2015-2018 Digi International Inc.


def show_group_tree(dc):
    stats = {}  # group -> devices count including children

    def count_nodes(group):
        count_for_this_node = \
            len(list(dc.devicecore.get_devices(group_path == group.get_path())))
        subnode_count = 0
        for child in group.get_children():
            subnode_count += count_nodes(child)
        total = count_for_this_node + subnode_count
        stats[group] = total
        return total

    count_nodes(dc.devicecore.get_group_tree_root())
    print(stats)
    dc.devicecore.get_group_tree_root().print_subtree()


if __name__ == '__main__':
    dc = get_authenticated_dc()
    devices = dc.devicecore.get_devices(
        (dev_mac == '00:40:9D:50:B0:EA')
    )

    for dev in devices: