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