@tanislav000/bluez
Version:
Bluez5 D-Bus bindings for easy to use bluetooth access in Node.js
79 lines • 3.58 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Adapter = void 0;
const Adapter1_1 = require("./dbus/Adapter1");
const device_1 = require("./device");
const dbus_next_1 = require("dbus-next");
const debug_1 = __importDefault(require("debug"));
const utilts_1 = require("./utilts");
const debug = (0, debug_1.default)("bluez:Adapter");
class Adapter extends Adapter1_1.OrgBluezAdapter1 {
constructor(dbusObject, bluez) {
super(dbusObject);
this.bluez = bluez;
this.bluez.getObjectManager().on("InterfacesAdded", (objPath, interfaces) => {
if (objPath.startsWith(dbusObject.path) && "org.bluez.Device1" in interfaces) {
debug("Device Added", objPath);
// keep subnodes array up to date
this.dbusObject.nodes.push(objPath);
const props = (0, utilts_1.unwrapDbusVariantObject)(interfaces["org.bluez.Device1"]);
this.emit("DeviceAdded", props.Address, props);
}
});
this.bluez.getObjectManager().on("InterfacesRemoved", (objPath, interfaces) => {
if (objPath.startsWith(dbusObject.path) && interfaces.includes("org.bluez.Device1")) {
debug("Device Removed", objPath);
// keep subnodes array up to date
this.dbusObject.nodes = this.dbusObject.nodes.filter((p) => p !== objPath);
// get address from dbus node
const path = objPath.split("/");
const address = path[path.length - 1].replace(/^dev_/, "").replace(/_/g, ":");
this.emit("DeviceRemoved", address);
}
});
}
/**
* Find a Device by its address
* @param address
* @throws {DBusError} org.bluez.Error.DoesNotExist
*/
async getDevice(address) {
// use dbus node name for faster search
const nodeName = "dev_" + address.toUpperCase().replace(/:/g, "_");
const node = this.dbusObject.nodes.find((objPath) => {
const path = objPath.split("/");
return path[path.length - 1] === nodeName;
});
if (node) {
return this.bluez.getDeviceFromObject(node);
}
// node names are only a cache, load fresh list
const devs = await this.listDevices();
for (const [path, dev] of Object.entries(devs)) {
if (dev.Address === address) {
return this.bluez.getDeviceFromObject(path);
}
}
throw new dbus_next_1.DBusError("org.bluez.Error.DoesNotExist", "Device not found");
}
/**
* Returns a list of known devices
* It returns key value pairs for each device.
* The key is the object path and the value is a snapshot of the properties of the device.
* To get the full device interface use `Bluez.getDeviceFromObject(key)`
*/
async listDevices() {
const objs = await this.bluez.getObjectManager().GetManagedObjects();
const res = Object.fromEntries(Object.entries(objs)
.filter(([path, ifs]) => path.startsWith(this.dbusObject.path) && device_1.Device.DbusInterfaceName in ifs)
.map(([path, ifs]) => [path, ifs[device_1.Device.DbusInterfaceName]]));
// update node cache
this.dbusObject.nodes = Object.keys(res);
return res;
}
}
exports.Adapter = Adapter;
//# sourceMappingURL=adapter.js.map