UNPKG

thing-it-device-ibeacon

Version:
226 lines (203 loc) 5.59 kB
module.exports = { metadata: { family: "iBeacon", plugin: "iBeacon", label: "iBeacon", manufacturer: "Generic", tangible: true, discoverable: true, interfaces: ['Trackable', 'TrackableRssiRawData'], state: [{ id: "rssi", label: "RSSI", type: { id: "decimal" } }, { id: "accuracy", label: "Accuracy", type: { id: "decimal" } }, { id: "proximity", label: "Promiximity", type: { id: "string" } }], actorTypes: [], sensorTypes: [], services: [], events: [], configuration: [{ id: "simulated", label: "Simulated", type: { id: "boolean" } }, { id: "uuid", label: "UUID", type: { id: "string" }, mandatory: true }, { id: "major", label: "Major", type: { id: "integer" } }, { id: "minor", label: "Minor", type: { id: "integer" } }, { id: "builtIn", label: "Built-in", type: { id: "boolean" } }, { id: "measuredPower", label: "Measured Power (1m)", type: { id: "decimal" }, defaultValue: -50 }, { id: "measuredPower3m", label: "Measured Power (3m)", type: { id: "decimal" } }, { id: "measuredPower5m", label: "Measured Power (5m)", type: { id: "decimal" } }, { id: "threshold", label: "Surpress Threshold", type: { id: "decimal" } }] }, create: function () { return new IBeacon(); }, discovery: function (options) { var discovery = new IBeaconDiscovery(); discovery.options = options; return discovery; } }; var q = require('q'); var bleacon; function IBeaconDiscovery() { /** * * @param options */ IBeaconDiscovery.prototype.start = function () { if (this.node.isSimulated()) { } else { } }; /** * * @param options */ IBeaconDiscovery.prototype.stop = function () { }; } /** * */ function IBeacon() { /** * */ IBeacon.prototype.start = function () { var deferred = q.defer(); this.state = { measuredPower: -59, rssi: -14, accuracy: 0.06, proximity: 'immediate' }; if (this.sensorTag) { this.connect(); deferred.resolve(); } else { if (!this.isSimulated()) { if (!bleacon) { bleacon = require('bleacon-fork'); } if (this.configuration.builtIn) { bleacon.startAdvertising(this.configuration.uuid, this.configuration.major, this.configuration.minor, this.configuration.measuredPower); this.state = { measuredPower: this.configuration.measuredPower, }; this.publishStateChange(); } else { bleacon.on('discover', function (beacon) { this.state = { measuredPower: beacon.measuredPower, rssi: beacon.rssi, accuracy: beacon.accuracy, proximity: beacon.proximity }; this.publishStateChange(); }.bind(this)); bleacon.startScanning(this.configuration.uuid); } deferred.resolve(); } else { this.simulationInterval = setInterval(function () { this.state = { measuredPower: -Math.random() * 30, rssi: -Math.random() * 10, accuracy: Math.random(), proximity: new Date().getTime() % 0 ? 'immediate' : 'near' }; this.publishStateChange(); }.bind(this), 20000); deferred.resolve(); } } return deferred.promise; }; /** * */ IBeacon.prototype.stop = function () { if (!this.isSimulated()) { if (this.configuration.builtIn) { bleacon.stopAdvertising(); } } else { if (this.simulationInterval) { clearInterval(this.simulationInterval); } } }; /** * */ IBeacon.prototype.setState = function (state) { this.state = state; }; /** * */ IBeacon.prototype.getState = function () { return this.state; }; }