-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBluezDevice.js
148 lines (122 loc) · 3.89 KB
/
BluezDevice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const EventEmitter = require('events').EventEmitter;
const util = require('util');
const _ = require('underscore');
const bluezDBus = require('./BluezDBus');
const BluezService = require('./BluezService');
module.exports = BluezDevice;
function BluezDevice(path) {
this.debug = require('debug')('Bluez:Device:' + path);
this.path = path;
this.debug('New device');
}
util.inherits(BluezDevice, EventEmitter);
BluezDevice.prototype.init = function(cb) {
/* Get Device1 interface */
bluezDBus.getInterface(this.path, 'org.bluez.Device1', (err, iface) => {
if (err) {
this.debug('Failed getting Device1 interface:' + err);
if (cb) cb(err);
return;
}
/* Save this interface */
this.iface = iface;
/* Get Protperties interface */
this.props = bluezDBus.getProperties(this.path, 'org.bluez.Device1',
this._devicePropertiesUpdate.bind(this), /* Property changed */
(err) => { /* All propertires were resolved */
if (err) {
this.debug('Failed getting all properties:' + err);
if (cb) cb(err);
return;
}
/* The new device is ready */
if (cb) cb();
});
});
/* Save the event handler so we can remove it later */
this.ifaceEvents = bluezDBus.onInterfaces(this._interfaceAdded.bind(this),
this._interfaceRemoved.bind(this));
}
BluezDevice.prototype.toString = function() {
return '[Object BluezDevice (' + this.path + ')]';
}
BluezDevice.prototype._isOwnService = function(path, objects) {
/* If the services were not resolved yet, we don't care about cached ones */
if (!this.ServicesResolved)
return false;
/* Make sure this services was discovered on this device */
if (!path.startsWith(this.path))
return false;
/* We're only interested in services */
if (objects['org.bluez.GattService1'] === undefined)
return false;
return true;
}
BluezDevice.prototype._onServicesResolvedChanged = function(servicesResolved) {
if (!servicesResolved)
return;
/* The device is resolved, now let's check if there are any relevant objects
* (services) */
bluezDBus.getAllObjects((err, objects) => {
if (err)
throw 'Failed getting all objects';
Object.keys(objects).forEach((key) => {
this._interfaceAdded(key, objects[key]);
});
});
}
BluezDevice.prototype._devicePropertiesUpdate = function(key, value) {
if (_.isEqual(this[key], value))
return;
this.debug(key + ' changed from ' + this[key] + ' to ' + value);
this[key] = value;
if (key == 'ServicesResolved')
this._onServicesResolvedChanged(value);
this.emit('propertyChanged', key, value);
}
BluezDevice.prototype._interfaceAdded = function(path, objects) {
/* We're only interested in services under this device */
if (!this._isOwnService(path, objects))
return;
this.debug('A service was added: ' + path);
var service = new BluezService(path);
service.init((err) => {
if (err) {
this.debug('Failed initializing new service ' + path + ': ' + err);
return;
}
this.emit('service', service);
});
}
BluezDevice.prototype._interfaceRemoved = function(path, objects) {
/* We're only interested in ourselves */
if (this.path !== path)
return;
this.debug('Removed device');
this.emit('removed');
this.removeAllListeners();
this.props.close();
this.ifaceEvents.close();
}
BluezDevice.prototype.Connect = function(cb) {
this.iface.Connect['finish'] = () => {
this.debug('Connected');
if (cb) cb();
};
this.iface.Connect['error'] = (err) => {
this.debug('Failed connecting: ' + err);
if (cb) cb(err);
};
this.iface.Connect();
}
BluezDevice.prototype.Disconnect = function(cb) {
this.iface.Disconnect['finish'] = () => {
this.debug('Disconnected');
if (cb) cb();
};
this.iface.Disconnect['error'] = (err) => {
this.debug('Failed disconnecting: ' + err);
if (cb) cb(err);
};
this.iface.Disconnect();
}