Last active
January 31, 2019 08:39
-
-
Save andrenatal/89c190e6f1724ba8a4ad33a20f710176 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* example-adapter.js - Example adapter. | |
* | |
* This Source Code Form is subject to the terms of the Mozilla Public | |
* License, v. 2.0. If a copy of the MPL was not distributed with this | |
* file, You can obtain one at http://mozilla.org/MPL/2.0/.* | |
*/ | |
'use strict'; | |
let Adapter, Device, Property, Event; | |
try { | |
const gwa = require('gateway-addon'); | |
Adapter = gwa.Adapter; | |
Device = gwa.Device; | |
Property = gwa.Property; | |
Event = gwa.Event; | |
} catch (e) { | |
if (e.code !== 'MODULE_NOT_FOUND') { | |
throw e; | |
} | |
} | |
class ActiveProperty extends Property { | |
constructor(device, name, propertyDescription) { | |
super(device, name, propertyDescription); | |
this.setCachedValue(propertyDescription.value); | |
this.device.notifyPropertyChanged(this); | |
} | |
/** | |
* Set the value of the property. | |
* | |
* @param {*} value The new value to set | |
* @returns a promise which resolves to the updated value. | |
* | |
* @note it is possible that the updated value doesn't match | |
* the value passed in. | |
*/ | |
setValue(value) { | |
return new Promise((resolve, reject) => { | |
super.setValue(value).then((updatedValue) => { | |
resolve(updatedValue); | |
console.log('here1'); | |
this.device.eventNotify(new Event(this.device, | |
'virtualEvent', | |
Math.floor(Math.random() * 100))); | |
console.log('here2'); | |
this.device.notifyPropertyChanged(this); | |
}).catch((err) => { | |
console.log('err ', err); | |
reject(err); | |
}); | |
}); | |
} | |
} | |
class ClapSensor extends Device { | |
constructor(adapter, id, deviceDescription) { | |
super(adapter, id); | |
this.name = deviceDescription.name; | |
this.type = deviceDescription.type; | |
this['@type'] = deviceDescription['@type']; | |
this.description = deviceDescription.description; | |
for (const propertyName in deviceDescription.properties) { | |
const propertyDescription = deviceDescription.properties[propertyName]; | |
const property = new ActiveProperty(this, propertyName, | |
propertyDescription); | |
this.properties.set(propertyName, property); | |
} | |
for (const event in deviceDescription.events) { | |
console.log('addedEvent', deviceDescription.events[event].name,deviceDescription.events[event].metadata); | |
this.addEvent(deviceDescription.events[event].name, | |
deviceDescription.events[event].metadata); | |
} | |
} | |
} | |
class ClapSensorAdapter extends Adapter { | |
constructor(addonManager, packageName) { | |
super(addonManager, 'ClapSensorAdapter', packageName); | |
addonManager.addAdapter(this); | |
} | |
/** | |
* Example process to add a new device to the adapter. | |
* | |
* The important part is to call: `this.handleDeviceAdded(device)` | |
* | |
* @param {String} deviceId ID of the device to add. | |
* @param {String} deviceDescription Description of the device to add. | |
* @return {Promise} which resolves to the device added. | |
*/ | |
addDevice(deviceId, deviceDescription) { | |
return new Promise((resolve, reject) => { | |
if (deviceId in this.devices) { | |
reject(`Device: ${deviceId} already exists.`); | |
} else { | |
const device = new ClapSensor(this, deviceId, deviceDescription); | |
this.handleDeviceAdded(device); | |
resolve(device); | |
} | |
}); | |
} | |
/** | |
* Example process ro remove a device from the adapter. | |
* | |
* The important part is to call: `this.handleDeviceRemoved(device)` | |
* | |
* @param {String} deviceId ID of the device to remove. | |
* @return {Promise} which resolves to the device removed. | |
*/ | |
removeDevice(deviceId) { | |
return new Promise((resolve, reject) => { | |
const device = this.devices[deviceId]; | |
if (device) { | |
this.handleDeviceRemoved(device); | |
resolve(device); | |
} else { | |
reject(`Device: ${deviceId} not found.`); | |
} | |
}); | |
} | |
/** | |
* Start the pairing/discovery process. | |
* | |
* @param {Number} timeoutSeconds Number of seconds to run before timeout | |
*/ | |
startPairing(_timeoutSeconds) { | |
console.log('ClapSensorAdapter:', this.name, | |
'id', this.id, 'pairing started'); | |
} | |
/** | |
* Cancel the pairing/discovery process. | |
*/ | |
cancelPairing() { | |
console.log('ClapSensorAdapter:', this.name, 'id', this.id, | |
'pairing cancelled'); | |
} | |
/** | |
* Unpair the provided the device from the adapter. | |
* | |
* @param {Object} device Device to unpair with | |
*/ | |
removeThing(device) { | |
console.log('ClapSensorAdapter:', this.name, 'id', this.id, | |
'removeThing(', device.id, ') started'); | |
this.removeDevice(device.id).then(() => { | |
console.log('ClapSensorAdapter: device:', device.id, 'was unpaired.'); | |
}).catch((err) => { | |
console.error('ClapSensorAdapter: unpairing', device.id, 'failed'); | |
console.error(err); | |
}); | |
} | |
/** | |
* Cancel unpairing process. | |
* | |
* @param {Object} device Device that is currently being paired | |
*/ | |
cancelRemoveThing(device) { | |
console.log('ClapSensorAdapter:', this.name, 'id', this.id, | |
'cancelRemoveThing(', device.id, ')'); | |
} | |
} | |
function loadClapSensorAdapter(addonManager, manifest, _errorCallback) { | |
const adapter = new ClapSensorAdapter(addonManager, manifest.name); | |
const device = new ClapSensor(adapter, 'example-plug-2', { | |
name: 'example-plug-2', | |
'@type': ['OnOffSwitch'], | |
type: 'onOffSwitch', | |
description: 'Example Device', | |
properties: { | |
on: { | |
'@type': 'OnOffProperty', | |
label: 'On/Off', | |
name: 'on', | |
type: 'boolean', | |
value: false, | |
}, | |
}, | |
events: [ | |
{ | |
name: 'virtualEvent', | |
metadata: { | |
description: 'An event from a virtual thing', | |
type: 'number', | |
}, | |
}, | |
], | |
}); | |
adapter.handleDeviceAdded(device); | |
} | |
module.exports = loadClapSensorAdapter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment