rpi4-osdev/part12-wgt/controller-node/characteristic.js

52 lines
1.4 KiB
JavaScript

var util = require('util');
var bleno = require('bleno-mac');
var BlenoCharacteristic = bleno.Characteristic;
var EchoCharacteristic = function() {
EchoCharacteristic.super_.call(this, {
uuid: 'ec0e',
properties: ['read', 'write', 'notify'],
value: null
});
this._value = new Buffer(0);
this._updateValueCallback = null;
};
util.inherits(EchoCharacteristic, BlenoCharacteristic);
EchoCharacteristic.prototype.onReadRequest = function(offset, callback) {
console.log('EchoCharacteristic - onReadRequest: value = ' + this._value.toString('hex'));
callback(this.RESULT_SUCCESS, this._value);
};
EchoCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) {
this._value = data;
console.log('EchoCharacteristic - onWriteRequest: value = ' + this._value.toString('hex'));
if (this._updateValueCallback) {
console.log('EchoCharacteristic - onWriteRequest: notifying');
this._updateValueCallback(this._value);
}
callback(this.RESULT_SUCCESS);
};
EchoCharacteristic.prototype.onSubscribe = function(maxValueSize, updateValueCallback) {
console.log('EchoCharacteristic - onSubscribe');
this._updateValueCallback = updateValueCallback;
};
EchoCharacteristic.prototype.onUnsubscribe = function() {
console.log('EchoCharacteristic - onUnsubscribe');
this._updateValueCallback = null;
};
module.exports = EchoCharacteristic;