thing-it-device-aircable
Version:
[thing-it-node] Device Plugin for AIRcable products (e.g. AIRcable SmartDimmer).
267 lines (228 loc) • 5.94 kB
JavaScript
module.exports = {
metadata: {
family: "airCableSmartDimmer",
plugin: "airCableSmartDimmer",
label: "AirCable SmartDimmer",
tangible: true,
discoverable: true,
state: [{
id: "switch",
label: "Switch",
type: {
id: "boolean"
}
}, {
id: "level",
label: "Level",
type: {
id: "number"
}
}, {
id: "batteryLevel",
label: "Battery Level",
type: {
id: "number"
}
}],
actorTypes: [],
sensorTypes: [],
services: [{
id: "on",
label: "On"
}, {
id: "off",
label: "Off"
}, {
id: "toggle",
label: "Toggle"
}, {
id: "changeLevel",
label: "Change Level"
}, {
id: "levelUp",
label: "Level Up"
}, {
id: "levelDown",
label: "Level Down"
}],
configuration: [{
id: "simulated",
label: "Simulated",
type: {
id: "boolean"
}
}, {
id: "minimumLevel",
label: "Minimum Level",
type: {
id: "integer"
},
defaultValue: 0
}, {
id: "maximumLevel",
label: "Maximum Level",
type: {
id: "integer"
},
defaultValue: 100
}, {
id: "step",
label: "Step",
type: {
id: "integer"
},
defaultValue: 5
}, {
id: "location",
label: "Location",
type: {
id: "string"
}
}, {
id: "energyConsumption",
label: "Energy Consumption (Watts)",
type: {
id: "decimal"
},
defaultValue: 0
}]
},
create: function (device) {
return new AirCableSmartDimmer();
},
discovery: function (options) {
var discovery = new AirCableSmartDimmer();
discovery.options = options;
return discovery;
}
};
var q = require('q');
//var noble = require('noble');
function AirCableSmartDimmerDiscovery() {
/**
*
* @param options
*/
AirCableSmartDimmerDiscovery.prototype.start = function () {
if (this.node.isSimulated()) {
} else {
noble.on("discover", function (peripheral) {
if (peripheral.advertisement.localName &&
peripheral.advertisement.localName.indexOf('RS_') === 0) {
console.log("Found a AIRCable Dimmer " + peripheral.advertisement.localName);
if (peripheral.uuid) {
console.log("GUID " + peripheral.uuid.replace(GUID_PATTERN, GUID_REPLACEMENT));
}
console.log(peripheral.uuid);
var drone = new AirCableSmartDimmer();
drone.peripheral = peripheral;
drone.uuid = peripheral.uuid;
this.advertiseDevice(drone);
}
}.bind(this));
noble.startScanning();
}
};
/**
*
* @param options
*/
AirCableSmartDimmerDiscovery.prototype.stop = function () {
};
}
/**
*
*/
function AirCableSmartDimmer() {
/**
*
*/
AirCableSmartDimmer.prototype.start = function () {
var deferred = q.defer();
this.state = {
switch: false,
level: 0,
batteryLevel: 100
};
if (this.isSimulated()) {
setInterval(function () {
this.state.batteryLevel = 100 - 20 * Math.floor((Math.random() * 6));
this.publishStateChange();
}.bind(this), 10000);
deferred.resolve();
} else {
deferred.resolve();
}
return deferred.promise;
};
/**
*
*/
AirCableSmartDimmer.prototype.setState = function (state) {
this.state = state;
this.publishStateChange();
};
/**
*
*/
AirCableSmartDimmer.prototype.getState = function () {
return this.state;
};
/**
*
*
*/
AirCableSmartDimmer.prototype.on = function () {
this.state.switch = true;
if (!this.isSimulated()) {
}
this.publishStateChange();
};
/**
*
*
*/
AirCableSmartDimmer.prototype.off = function () {
this.state.switch = false;
if (!this.isSimulated()) {
}
this.publishStateChange();
};
/**
*
*
*/
AirCableSmartDimmer.prototype.toggle = function () {
this.state.switch = !this.state.switch;
if (!this.isSimulated()) {
}
this.publishStateChange();
};
/**
*
*/
AirCableSmartDimmer.prototype.changeLevel = function (parameters) {
this.state.level = Math.max(Math.min(parameters.level, this.configuration.maximumLevel), 0);
if (!this.isSimulated()) {
}
this.publishStateChange();
};
/**
*
*/
AirCableSmartDimmer.prototype.levelUp = function () {
this.state.level = Math.min(this.state.level + this.configuration.step, this.configuration.maximumLevel);
if (!this.isSimulated()) {
}
this.publishStateChange();
};
/**
*
*/
AirCableSmartDimmer.prototype.levelDown = function () {
this.state.level = Math.max(this.state.level - this.configuration.step, 0);
if (!this.isSimulated()) {
}
this.publishStateChange();
};
}