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)
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)