示例#1
0
def add_device(device_data=None):
    valid_auth_code()
    device = device_data or get_post_json_data()
    sn = device.get("sn")
    if not sn:
        throw(_("Request fields not found. fields: sn"))

    if IOTDevice.check_sn_exists(sn):
        # TODO: Check for bunch code when device is existing.
        return IOTDevice.get_device_doc(sn)

    device.update({"doctype": "IOT Device"})
    doc = frappe.get_doc(device).insert().as_dict()

    url = IOTHDBSettings.get_callback_url()
    if url:
        """ Fire callback data """
        user_list = IOTDevice.find_owners_by_bunch(device.get("bunch"))

        frappe.enqueue('iot.hdb_api.fire_callback',
                       cb_url=url + "/api/datachanged",
                       cb_data={
                           'cmd': 'add_device',
                           'sn': sn,
                           'users': user_list
                       })

    return doc
示例#2
0
def update_device_bunch(device_data=None):
    valid_auth_code()
    data = device_data or get_post_json_data()
    bunch = data.get("bunch")
    sn = data.get("sn")
    if sn is None:
        throw(_("Request fields not found. fields: sn"))

    dev = IOTDevice.get_device_doc(sn)
    if not dev:
        throw(_("Device is not found. SN:{0}").format(sn))

    if bunch == "":
        bunch = None
    if IOTHDBSettings.is_default_bunch_enabled() and not bunch:
        bunch = IOTHDBSettings.get_default_bunch()
    if dev.bunch == bunch:
        return __generate_hdb(dev)

    if not frappe.get_value("IOT Device Bunch", bunch, "name"):
        throw(_("Device Bunch Code {0} is not valid!").format(bunch))

    org_bunch = dev.bunch
    dev.update_bunch(bunch)

    frappe.enqueue('iot.hdb_api.on_device_bunch_update',
                   device=dev,
                   org_bunch=org_bunch)

    return __generate_hdb(dev)
示例#3
0
def get_device(sn=None):
    valid_auth_code()
    sn = sn or frappe.form_dict.get('sn')
    if not sn:
        throw(_("Request fields not found. fields: sn"))

    dev = IOTDevice.get_device_doc(sn)
    return __generate_hdb(dev)
示例#4
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
示例#5
0
def on_device_bunch_update(device, org_bunch=None):
    url = None  #IOTHDBSettings.get_redis_url()
    print(device.sn, device.bunch)
    if url:
        """ Fire callback data """
        cb_data = {
            'cmd': 'add_device',
            'sn': device.sn,
        }
        if org_bunch is not None:
            cb_data['cmd'] = 'update_device'
            cb_data['add_users'] = IOTDevice.find_owners_by_bunch(device.bunch)
            cb_data['del_users'] = IOTDevice.find_owners_by_bunch(org_bunch)
        else:
            cb_data['users'] = IOTDevice.find_owners_by_bunch(device.bunch)
        print('------------------------------------------')
        print(json.dumps(cb_data))
        print('------------------------------------------')
示例#6
0
def update_device_name():
    valid_auth_code()
    data = get_post_json_data()
    name = data.get("name")
    sn = data.get("sn")
    if not (sn and name):
        throw(_("Request fields not found. fields: sn\tname"))

    dev = IOTDevice.get_device_doc(sn)
    if not dev:
        throw(_("Device is not found. SN:{0}").format(sn))

    dev.update_dev_name(name)
    return __generate_hdb(dev)
示例#7
0
def update_device_status(device_data=None):
    valid_auth_code()
    data = device_data or get_post_json_data()
    status = data.get("status")
    sn = data.get("sn")
    if not (sn and status):
        throw(_("Request fields not found. fields: sn\tstatus"))

    dev = IOTDevice.get_device_doc(sn)
    if not dev:
        throw(_("Device is not found. SN:{0}").format(sn))

    dev.update_status(status)
    return __generate_hdb(dev)
示例#8
0
def add_device(device_data=None):
    valid_auth_code()
    device = device_data or get_post_json_data()
    sn = device.get("sn")
    if IOTHDBSettings.is_default_bunch_enabled() and not device.get("bunch"):
        device["bunch"] = IOTHDBSettings.get_default_bunch()
    if not sn:
        throw(_("Request fields not found. fields: sn"))

    if IOTDevice.check_sn_exists(sn):
        return IOTDevice.get_device_doc(sn)

    device.update({"doctype": "IOT Device"})

    bunch = device.get("bunch")
    if not frappe.get_value("IOT Device Bunch", bunch, "name"):
        throw(_("Device Bunch Code {0} is not valid!").format(bunch))

    dev = frappe.get_doc(device).insert()

    frappe.enqueue('iot.hdb_api.on_device_bunch_update', device=dev)

    return __generate_hdb(dev)
示例#9
0
def update_device_hdb(device_data=None):
    valid_auth_code()
    data = device_data or get_post_json_data()
    hdb = data.get("hdb")
    sn = data.get("sn")
    if not (sn and hdb):
        throw(_("Request fields not found. fields: sn\thdb"))

    dev = IOTDevice.get_device_doc(sn)
    if not dev:
        throw(_("Device is not found. SN:{0}").format(sn))

    if dev.hdb != hdb:
        dev.update_hdb(hdb)
    return __generate_hdb(dev)
示例#10
0
def update_device_bunch(device_data=None):
    valid_auth_code()
    data = device_data or get_post_json_data()
    bunch = data.get("bunch")
    sn = data.get("sn")
    if sn is None:
        throw(_("Request fields not found. fields: sn"))

    dev = IOTDevice.get_device_doc(sn)
    if not dev:
        throw(_("Device is not found. SN:{0}").format(sn))

    if bunch == "":
        bunch = None
    if dev.bunch == bunch:
        return dev

    org_bunch = dev.bunch
    dev.update_bunch(bunch)

    url = IOTHDBSettings.get_callback_url()
    if url:
        """ Fire callback data """
        org_user_list = IOTDevice.find_owners_by_bunch(org_bunch)
        user_list = IOTDevice.find_owners_by_bunch(bunch)

        frappe.enqueue('iot.hdb_api.fire_callback',
                       cb_url=url + "/api/datachanged",
                       cb_data={
                           'cmd': 'update_device',
                           'sn': sn,
                           'add_users': user_list,
                           'del_users': org_user_list
                       })

    return dev
示例#11
0
def update_device_position():
    valid_auth_code()
    data = get_post_json_data()
    pos = data.get("position")
    if not isinstance(pos, basestring):
        pos = json.loads(pos)

    sn = data.get("sn")
    if not (sn and pos):
        throw(_("Request fields not found. fields: sn\tposition"))

    dev = IOTDevice.get_device_doc(sn)
    if not dev:
        throw(_("Device is not found. SN:{0}").format(sn))

    dev.update_dev_pos(pos.get("long"), pos.get("lati"))
    return __generate_hdb(dev)
示例#12
0
def add_device_error(err_data=None):
    """
	Add device error
	:param err_data: {"device": device_sn, "error_type": Error Type defined, "error_key": any text, "error_level": int, "error_info": any text}
	:return: iot_device_error
	"""
    valid_auth_code()
    err_data = err_data or get_post_json_data()
    device = err_data.get("device")
    if not device:
        throw(_("Request fields not found. fields: device"))

    if not IOTDevice.check_sn_exists(device):
        throw(_("Device {0} not found.").format(device))

    err_data.update({"doctype": "IOT Device Error"})
    doc = frappe.get_doc(err_data).insert().as_dict()

    return doc
示例#13
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