Skip to content

Instantly share code, notes, and snippets.

@DurandA
Last active November 2, 2018 04:53
Show Gist options
  • Save DurandA/4396348a733bc784bd93266d4a2ac117 to your computer and use it in GitHub Desktop.
Save DurandA/4396348a733bc784bd93266d4a2ac117 to your computer and use it in GitHub Desktop.
Thingy:52 Web Thing server using webthing-node
const {Action, Event, Property, Thing, WebThingServer} = require('./index');
const uuidv4 = require('uuid/v4');
var Thingy = require('thingy52');
var Color = require('color');
console.log('Reading Thingy environment sensors!');
class ButtonEvent extends Event {
constructor(thing, state) {
super(thing, state, 'Button' + state);
}
}
class LedColorProperty extends Property {
constructor(thing, thingy) {
super(thing, 'ledcolor', {type: 'string'});
this.thingy = thingy;
}
setValue(value) {
let color = Color(value);
this.thingy.led_set(color.object(), function(error) {
console.log('LED color changed ' + ((error) ? error : ''));
});
super.setValue(value);
}
}
LedOneShotAction = thingy => class extends Action {
constructor(thing, params) {
super(uuidv4(), thing, 'led_one_shot', params);
}
performAction() {
return new Promise((resolve, reject) => {
thingy.led_one_shot(this.params, function(error) {
reject(error) ? error : resolve();
});
});
}
}
function run_server(thingy) {
console.log('Discovered: ' + thingy);
thingy.on('disconnect', function() {
console.log('Disconnected!');
});
const thing = new Thing('WoT Thingy', 'thing', 'A WoT-connected Thingy:52');
let temperatureProperty = new Property(thing,
'temperature',
{type: 'number',
unit: 'celsius',
description: 'An ambient temperature sensor'});
thing.addProperty(temperatureProperty);
let pressureProperty = new Property(thing,
'pressure',
{type: 'number',
unit: 'hectopascal'});
thing.addProperty(pressureProperty);
let humidityProperty = new Property(thing,
'humidity',
{type: 'number',
unit: 'percent'});
thing.addProperty(humidityProperty);
let gasProperty = new Property(thing,
'gas',
{type: 'object'});
thing.addProperty(gasProperty);
let colorProperty = new Property(thing,
'color',
{type: 'object'});
thing.addProperty(colorProperty);
let luminosityProperty = new Property(thing,
'luminosity',
{type: 'number',
unit: 'lux'});
thing.addProperty(luminosityProperty);
let batteryLevelProperty = new Property(thing,
'battery',
{type: 'number',
unit: 'percent'});
thing.addProperty(batteryLevelProperty);
thing.addProperty(
new LedColorProperty(thing,
thingy));
console.log('setup');
thingy.connectAndSetUp(function(error) {
console.log('Connected! ' + error ? error : '');
thingy.on('temperatureNotif', temperatureProperty.setValue.bind(temperatureProperty));
thingy.on('pressureNotif', pressureProperty.setValue.bind(pressureProperty));
thingy.on('humidityNotif', humidityProperty.setValue.bind(humidityProperty));
thingy.on('gasNotif', gasProperty.setValue.bind(gasProperty));
thingy.on('colorNotif', value => {
let color = Color({r: value.red,
g: value.green,
b: value.blue});
colorProperty.setValue(value);
luminosityProperty.setValue(value['clear']);
});
thingy.on('batteryLevelChange', batteryLevelProperty.setValue.bind(batteryLevelProperty));
thingy.on('buttonNotif', state => thing.addEvent(new ButtonEvent(thing, state)));
thingy.temperature_interval_set(1000, function(error) {
if (error) {
console.log('Temperature sensor configure! ' + error);
}
});
thingy.pressure_interval_set(1000, function(error) {
if (error) {
console.log('Pressure sensor configure! ' + error);
}
});
thingy.humidity_interval_set(1000, function(error) {
if (error) {
console.log('Humidity sensor configure! ' + error);
}
});
thingy.color_interval_set(1000, function(error) {
if (error) {
console.log('Color sensor configure! ' + error);
}
});
thingy.gas_mode_set(1, function(error) {
if (error) {
console.log('Gas sensor configure! ' + error);
}
});
thingy.temperature_enable(function(error) {
console.log('Temperature sensor started! ' + ((error) ? error : ''));
});
thingy.pressure_enable(function(error) {
console.log('Pressure sensor started! ' + ((error) ? error : ''));
});
thingy.humidity_enable(function(error) {
console.log('Humidity sensor started! ' + ((error) ? error : ''));
});
thingy.color_enable(function(error) {
console.log('Color sensor started! ' + ((error) ? error : ''));
});
thingy.gas_enable(function(error) {
console.log('Gas sensor started! ' + ((error) ? error : ''));
});
thingy.button_enable(function(error) {
console.log('Button started! ' + ((error) ? error : ''));
});
thingy.notifyBatteryLevel(function(error) {
console.log('Battery Level Notifications enabled! ' + ((error) ? error : ''));
});
});
thing.addActionDescription('led_one_shot', 'Set the RGB LED', LedOneShotAction(thingy));
thing.addEventDescription('pressed', 'Button pressed');
thing.addEventDescription('released', 'Button released');
const server = new WebThingServer(thing, 8888);
server.start();
}
Thingy.discover(run_server);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment