Beispiel #1
0
def list_iot_devices(user):
    frappe.logger(__name__).debug(_("List Devices for user {0}").format(user))

    devices = {"group_devices": []}

    if frappe.get_value("IOT User", user):
        user_doc = frappe.get_doc("IOT User", user)
        if cint(
                frappe.get_value('IOT Enterprise', user_doc.enterprise,
                                 'enabled')):
            groups = user_doc.get("group_assigned")
            for g in groups:
                bunch_codes = [
                    d[0] for d in frappe.db.get_values(
                        "IOT Device Bunch", {
                            "owner_id": g.group,
                            "owner_type": "IOT Employee Group"
                        }, "code")
                ]

                sn_list = []
                for c in bunch_codes:
                    sn_list.append({
                        "bunch": c,
                        "sn": IOTDevice.list_device_sn_by_bunch(c)
                    })
                devices['group_devices'].append({
                    "group": g.group,
                    "devices": sn_list
                })

    bunch_codes = [
        d[0] for d in frappe.db.get_values("IOT Device Bunch", {
            "owner_id": user,
            "owner_type": "User"
        }, "code")
    ]
    sn_list = []
    for c in bunch_codes:
        sn_list.append({
            "bunch": c,
            "sn": IOTDevice.list_device_sn_by_bunch(c)
        })
    devices["private_devices"] = sn_list

    return devices
Beispiel #2
0
def list_iot_devices(user):
    frappe.logger(__name__).debug(_("List Devices for user {0}").format(user))

    # Get Enteprise Devices
    ent_devices = []
    groups = _list_user_groups(user)
    companies = list_user_companies(user)
    for g in groups:
        bunch_codes = [
            d[0]
            for d in frappe.db.get_values("IOT Device Bunch", {
                "owner_id": g.name,
                "owner_type": "Cloud Company Group"
            }, "code")
        ]

        sn_list = []
        for c in bunch_codes:
            sn_list.append({
                "bunch": c,
                "sn": IOTDevice.list_device_sn_by_bunch(c)
            })
        ent_devices.append({
            "group": g.name,
            "devices": sn_list,
            "role": g.role
        })

    # Get Shared Devices
    shd_devices = []
    for shared_group in [
            d[0] for d in frappe.db.get_values("IOT ShareGroupUser",
                                               {"user": user}, "parent")
    ]:
        # Make sure we will not having shared device from your company
        if frappe.get_value("IOT Share Group", shared_group,
                            "company") in companies:
            continue
        role = frappe.get_value("IOT Share Group", shared_group, "role")

        dev_list = []
        for dev in [
                d[0] for d in frappe.db.get_values(
                    "IOT ShareGroupDevice", {"parent": shared_group}, "device")
        ]:
            dev_list.append(dev)
        shd_devices.append({
            "group": shared_group,
            "devices": dev_list,
            "role": role
        })

    # Get Private Devices
    bunch_codes = [
        d[0] for d in frappe.db.get_values("IOT Device Bunch", {
            "owner_id": user,
            "owner_type": "User"
        }, "code")
    ]
    pri_devices = []
    for c in bunch_codes:
        pri_devices.append({
            "bunch": c,
            "sn": IOTDevice.list_device_sn_by_bunch(c)
        })

    devices = {
        "company_devices": ent_devices,
        "private_devices": pri_devices,
        "shared_devices": shd_devices,
    }
    return devices