1 | var events = require('events');
|
2 | var util = require('util');
|
3 | var characteristics = require('./characteristics.json');
|
4 |
|
5 | function Characteristic(peripheral, uuid, handle, value) {
|
6 | this._peripheral = peripheral;
|
7 |
|
8 | this.uuid = uuid;
|
9 | this.handle = handle;
|
10 | this.descriptors = {};
|
11 | this.name = null;
|
12 | this.type = null;
|
13 | this.value = value;
|
14 |
|
15 | if (uuid) {
|
16 | this.setUUID(uuid);
|
17 | }
|
18 | }
|
19 |
|
20 | util.inherits(Characteristic, events.EventEmitter);
|
21 |
|
22 | Characteristic.prototype.toString = function() {
|
23 | return JSON.stringify({
|
24 | uuid: this.uuid,
|
25 | name: this.name,
|
26 | type: this.type,
|
27 | handle : this.handle,
|
28 | value : this.value,
|
29 | });
|
30 | }
|
31 |
|
32 | Characteristic.prototype.setUUID = function(uuid) {
|
33 | this.uuid = uuid;
|
34 |
|
35 | var characteristic = characteristics[uuid.toString()];
|
36 | if (characteristic) {
|
37 | this.name = characteristic.name;
|
38 | this.type = characteristic.type;
|
39 | }
|
40 | }
|
41 |
|
42 | Characteristic.prototype.read = function(callback) {
|
43 | this._peripheral._controller.read(this, callback);
|
44 | };
|
45 |
|
46 | Characteristic.prototype.write = function(data, callback) {
|
47 | this._peripheral._controller.write(this, data, callback);
|
48 | };
|
49 |
|
50 | Characteristic.prototype.startNotifications = function(callback) {
|
51 | this._peripheral._controller.startNotifications(this, callback);
|
52 | }
|
53 |
|
54 | Characteristic.prototype.stopNotifications = function(callback) {
|
55 | this._peripheral._controller.stopNotifications(this, callback);
|
56 | }
|
57 |
|
58 | Characteristic.prototype.startIndications = function(callback) {
|
59 | this._peripheral._controller.startNotifications(this, callback);
|
60 | }
|
61 |
|
62 | Characteristic.prototype.stopIndications = function(callback) {
|
63 | this._peripheral._controller.stopIndications(this, callback);
|
64 | }
|
65 |
|
66 | Characteristic.prototype.confirmIndication = function(callback) {
|
67 | this._peripheral._controller.confirmIndication(this, callback);
|
68 | }
|
69 | Characteristic.prototype.discoverAllDescriptors = function(callback) {
|
70 | this._peripheral._controller.discoverDescriptorsOfCharacteristic(this, callback);
|
71 | };
|
72 |
|
73 |
|
74 | module.exports = Characteristic;
|