Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/lifecycle management #1

Draft
wants to merge 24 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
# Logs
logs
*.log
Expand Down
48 changes: 46 additions & 2 deletions lib/Bluez.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class Bluez extends EventEmitter {
this.mediaplayer = {};
this.mediatransport = {};
this.mediacontrol = {};

this._path = {};
}

Expand All @@ -46,6 +45,51 @@ class Bluez extends EventEmitter {
this.objectManager.GetManagedObjects((err, objs) => {
Object.keys(objs).forEach((k) => this.onInterfacesAdded(k, objs[k]))
});
this.bus.on('signal', this.signalHandler.bind(this));
}

signalHandler(uniqueBusName, sender, objectPath, interfaceName, signalName, args) {
if (signalName === 'PropertiesChanged' && objectPath.includes('bluez')) {
if (Array.isArray(args) && args.length > 1) {
let [[key, value]] = Object.entries(args[1]);
let addr = objectPath.substr('/org/bluez/hci0/dev_'.length, 17).replace(/_/g, ':');
switch (args[0]) {
case 'org.bluez.Device1':
this.emit('device_property', addr, key, value);
break;
case 'org.bluez.MediaPlayer1':
this.emit('player_property', addr, key, value);
break;
case 'org.bluez.MediaTransport1':
this.emit('transport_property', addr, key, value);
break;
case 'org.bluez.MediaControl1':
this.emit('control_property', addr, key, value);
break;
}
}
}
}

close() {
Object.keys(this.adapter).forEach((key) => {
if (this.adapter[key].removeAllListeners) {
this.adapter[key].removeAllListeners();
}
if (this.adapter[key].destroy) {
this.adapter[key].destroy();
}
});
this.objectManager.removeAllListeners('InterfacesAdded');
this.objectManager.removeAllListeners('InterfacesRemoved');
this.objectManager.removeAllListeners();
this.profileManager.removeAllListeners();
this.agentManager.removeAllListeners();
if (this.userServiceObject) {
this.userService.removeObject(this.userServiceObject);
this.userServiceObject = undefined;
}
this.bus.removeAllListeners('signal');
}

async getAdapter(dev) {
Expand Down Expand Up @@ -401,4 +445,4 @@ class Bluez extends EventEmitter {
}
}

module.exports = Bluez;
module.exports = Bluez;
28 changes: 2 additions & 26 deletions lib/Device.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
const EventEmitter = require('events').EventEmitter;

class Device extends EventEmitter {
class Device {
constructor(_interface, DbusObject) {
super();
this._interface = _interface;
this.services = {};
this._DBusObject = DbusObject;
}

initEvents()
{
this._DBusObject.on('signal', (uniqueBusName, sender, objectPath, interfaceName, signalName, args) => {

if(objectPath == this._interface.objectPath)
{
if(Array.isArray(args))
{
if(args.length > 1 && args[0] == 'org.bluez.Device1')
{
Object.entries(args[1]).forEach(entry => {
let key = entry[0];
let value = entry[1];
this.emit("PropertyChanged", this._interface.objectPath, key, value);
});
}
}
}
});
}

/*
This is a generic method to connect any profiles
the remote device supports that can be connected
Expand Down Expand Up @@ -451,4 +427,4 @@ class Device extends EventEmitter {
}
}

module.exports = Device;
module.exports = Device;
26 changes: 1 addition & 25 deletions lib/MediaControl.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
const EventEmitter = require('events').EventEmitter;

class MediaControl extends EventEmitter {
class MediaControl {
constructor(_interface, DbusObject) {
super();
this._interface = _interface;
this._DBusObject = DbusObject;
}

initEvents()
{
this._DBusObject.on('signal', (uniqueBusName, sender, objectPath, interfaceName, signalName, args) => {

if(objectPath == this._interface.objectPath)
{
if(Array.isArray(args))
{
if(args.length > 1 && args[0] == 'org.bluez.MediaControl1')
{
Object.entries(args[1]).forEach(entry => {
let key = entry[0];
let value = entry[1];
this.emit("PropertyChanged", key, value);
});
}
}
}
});
}

/****** Properties ******/
getProperties() {
return new Promise((resolve, reject)=>{
Expand Down
26 changes: 1 addition & 25 deletions lib/MediaPlayer.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
const EventEmitter = require('events').EventEmitter;

class MediaPlayer extends EventEmitter {
class MediaPlayer {
constructor(_interface, DbusObject) {
super();
this._interface = _interface;
this._DBusObject = DbusObject;
}

initEvents()
{
this._DBusObject.on('signal', (uniqueBusName, sender, objectPath, interfaceName, signalName, args) => {

if(objectPath == this._interface.objectPath)
{
if(Array.isArray(args))
{
if(args.length > 1 && args[0] == 'org.bluez.MediaPlayer1')
{
Object.entries(args[1]).forEach(entry => {
let key = entry[0];
let value = entry[1];
this.emit("PropertyChanged", key, value);
});
}
}
}
});
}

/****** Properties ******/

getProperties() {
Expand Down
26 changes: 1 addition & 25 deletions lib/MediaTransport.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
const EventEmitter = require('events').EventEmitter;

class MediaTransport extends EventEmitter {
class MediaTransport {
constructor(_interface, DbusObject) {
super();
this._interface = _interface;
this._DBusObject = DbusObject;
}

initEvents()
{
this._DBusObject.on('signal', (uniqueBusName, sender, objectPath, interfaceName, signalName, args) => {

if(objectPath == this._interface.objectPath)
{
if(Array.isArray(args))
{
if(args.length > 1 && args[0] == 'org.bluez.MediaTransport1')
{
Object.entries(args[1]).forEach(entry => {
let key = entry[0];
let value = entry[1];
this.emit("PropertyChanged", key, value);
});
}
}
}
});
}

/****** Properties ******/
getProperties() {
return new Promise((resolve, reject)=>{
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.