1 | var EventEmitter = require('events').EventEmitter;
|
2 | var util = require('util');
|
3 |
|
4 | function Peripheral(controller, rssi, data, address) {
|
5 | this.rssi = rssi;
|
6 | this.services = {};
|
7 | this.advertisingData = data;
|
8 | this.address = address;
|
9 | this.connection = null;
|
10 | this.flags = null;
|
11 | this.bondHandle = null;
|
12 |
|
13 |
|
14 | this.characteristics = {};
|
15 |
|
16 | this._controller = controller;
|
17 | this._unassignedCharacteristics = [];
|
18 | this._unassignedDescriptors = [];
|
19 | }
|
20 |
|
21 | util.inherits(Peripheral, EventEmitter);
|
22 |
|
23 | Peripheral.prototype.toString = function() {
|
24 | return JSON.stringify({
|
25 | address: this.address.toString(),
|
26 | services: this.services,
|
27 | rssi: this.rssi
|
28 | });
|
29 | };
|
30 |
|
31 | Peripheral.prototype.connect = function(callback) {
|
32 | this._controller.connect(this, callback);
|
33 | };
|
34 |
|
35 | Peripheral.prototype.disconnect = function(callback) {
|
36 | this._controller.disconnect(this, callback);
|
37 | };
|
38 |
|
39 | Peripheral.prototype.discoverServices = function(services, callback) {
|
40 | this._controller.discoverServices(this, services, callback);
|
41 | };
|
42 |
|
43 | Peripheral.prototype.discoverAllServices = function(callback) {
|
44 | this._controller.discoverAllServices(this, callback);
|
45 | };
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | Peripheral.prototype.syncService = function(service, callback) {
|
53 |
|
54 | this.services[service.uuid.toString()] = service;
|
55 |
|
56 | newUnassigned = [];
|
57 |
|
58 | for (var i in this._unassignedCharacteristics) {
|
59 |
|
60 | var characteristic = this._unassignedCharacteristics[i];
|
61 |
|
62 | if (characteristic.handle >= service._startHandle && characteristic.handle <= service._endHandle) {
|
63 |
|
64 |
|
65 | service.characteristics[characteristic.uuid.toString()] = characteristic;
|
66 |
|
67 |
|
68 | setImmediate(function() {
|
69 | service.emit('characteristicDiscover', characteristic);
|
70 | });
|
71 | }
|
72 | else {
|
73 |
|
74 | newUnassigned.push(characteristic);
|
75 | }
|
76 | }
|
77 |
|
78 |
|
79 | this._unassignedCharacteristics = newUnassigned;
|
80 |
|
81 | callback && callback();
|
82 | }
|
83 |
|
84 |
|
85 | Peripheral.prototype.sortServices = function(callback) {
|
86 | var sortable = [];
|
87 |
|
88 | for (var service in this.services) {
|
89 | sortable.push(this.services[service]);
|
90 | }
|
91 |
|
92 | sortable.sort(function(a, b) {return a._startHandle - b._startHandle});
|
93 |
|
94 | callback && callback(sortable);
|
95 |
|
96 | return sortable;
|
97 | }
|
98 |
|
99 | Peripheral.prototype.discoverCharacteristics = function(characteristics, callback) {
|
100 | this._controller.discoverCharacteristics(this, characteristics, callback);
|
101 | };
|
102 |
|
103 | Peripheral.prototype.discoverAllCharacteristics = function(callback) {
|
104 | this._controller.discoverAllCharacteristics(this, callback);
|
105 | };
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | Peripheral.prototype.syncCharacteristic = function(characteristic, callback) {
|
112 |
|
113 | this.characteristics[characteristic.handle] = characteristic;
|
114 |
|
115 |
|
116 | this.sortServices(function(sortedServices) {
|
117 |
|
118 | for (var i = 0; i < sortedServices.length; i++) {
|
119 |
|
120 | var service = sortedServices[i];
|
121 |
|
122 | if (characteristic.handle >= service._startHandle && characteristic.handle <= service._endHandle) {
|
123 |
|
124 | service.characteristics[characteristic.uuid.toString()] = characteristic;
|
125 |
|
126 | return;
|
127 | }
|
128 | }
|
129 |
|
130 | this._unassignedCharacteristics.push(characteristic);
|
131 |
|
132 | return;
|
133 | }.bind(this));
|
134 | }
|
135 |
|
136 | Peripheral.prototype.discoverAllAttributes = function(callback) {
|
137 | this._controller.discoverAllAttributes(this, callback);
|
138 | }
|
139 |
|
140 | Peripheral.prototype.discoverAllServicesAndCharacteristics = function(callback) {
|
141 | this._controller.discoverAllServicesAndCharacteristics(this, callback);
|
142 | }
|
143 |
|
144 | Peripheral.prototype.discoverAllDescriptors = function(callback) {
|
145 | this._controller.discoverAllDescriptors(this, callback);
|
146 | }
|
147 |
|
148 | Peripheral.prototype.deleteBond = function(callback) {
|
149 | this._controller.deleteBond(this, callback);
|
150 | }
|
151 |
|
152 | Peripheral.prototype.startEncryption = function(callback) {
|
153 | this._controller.startEncryption(this, callback);
|
154 | }
|
155 |
|
156 | Peripheral.prototype.enterPasskey = function(passKey, callback) {
|
157 | this._controller.enterPasskey(this, passkey, callback);
|
158 | }
|
159 |
|
160 | Peripheral.prototype.updateRssi = function(callback) {
|
161 | this._controller.updateRssi(this, callback);
|
162 | };
|
163 |
|
164 | module.exports = Peripheral;
|