Skip to content

Instantly share code, notes, and snippets.

@murilopolese
Created September 19, 2018 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murilopolese/20cb1e1eafe1df73df1a0e981483cfa4 to your computer and use it in GitHub Desktop.
Save murilopolese/20cb1e1eafe1df73df1a0e981483cfa4 to your computer and use it in GitHub Desktop.
Pixel Thing
const {
Action,
Property,
SingleThing,
Thing,
Value,
WebThingServer,
} = require('webthing');
const {
DeviceManager,
RetailPixelKit
} = require('community-sdk');
const onecolor = require('onecolor');
const uuidv4 = require('uuid/v4');
let on = true;
let brightness = 50;
let color = '#ffff00';
class FadeAction extends Action {
constructor(thing, input) {
super(uuidv4(), thing, 'fade', input);
}
performAction() {
return new Promise((resolve) => {
let currentBright = this.thing.getProperty('brightness');
let delta = this.input.brightness - currentBright;
let interval = this.input.duration / Math.abs(delta);
let count = 0;
let iterval_id = setInterval(() => {
count++;
console.log('fading')
if(delta > 0) {
this.thing.setProperty('brightness', brightness + 1);
} else if (delta < 0) {
this.thing.setProperty('brightness', brightness - 1);
}
if(count > Math.abs(delta)) {
clearInterval(iterval_id)
resolve()
}
}, interval)
});
}
}
function makeThing() {
DeviceManager.listConnectedDevices()
.then((devices) => {
// Filter devices to find a Motion Sensor Kit
let rpk = devices.find((device) => {
return device instanceof RetailPixelKit;
});
if(!rpk) {
console.log('No Pixel Kit was found :(');
} else {
console.log('Pixel Kit found!');
setInterval(() => {
let frame = [];
for(let i = 0; i < 128; i++) {
if(on) {
frame.push(
// color
onecolor(color)
.lightness(brightness/100)
.hex()
);
} else {
frame.push('#000000');
}
}
rpk.streamFrame(frame)
.catch((error) => {
console.log('Problem streaming frame', error);
});
}, 100);
}
})
const thing = new Thing('My Pixel Kit',
['OnOffSwitch', 'Light', 'ColorControl'],
'A web connected Pixel Kit');
thing.addProperty(
new Property(thing,
'on',
new Value(on, (update) => {
on = update
console.log('on', update)
}),
{
'@type': 'OnOffProperty',
label: 'On/Off',
type: 'boolean',
description: 'Whether the lamp is turned on',
}));
thing.addProperty(
new Property(thing,
'brightness',
new Value(brightness, (update) => {
brightness = update
console.log('brightness', update)
}),
{
'@type': 'BrightnessProperty',
label: 'Brightness',
type: 'number',
description: 'The level of light from 0-100',
minimum: 0,
maximum: 100,
unit: 'percent',
}));
thing.addProperty(
new Property(thing,
'color',
new Value(brightness, (update) => {
color = update
console.log('color', update)
}),
{
'@type': 'ColorProperty',
label: 'Color',
type: 'color',
description: 'The color',
}));
thing.addAvailableAction(
'fade',
{
label: 'Fade',
description: 'Fade the lamp to a given level',
input: {
type: 'object',
required: [
'brightness',
'duration',
],
properties: {
brightness: {
type: 'number',
minimum: 0,
maximum: 100,
unit: 'percent',
},
duration: {
type: 'number',
minimum: 1,
unit: 'milliseconds',
},
},
},
},
FadeAction);
return thing;
}
function runServer() {
const thing = makeThing();
// If adding more than one thing, use MultipleThings() with a name.
// In the single thing case, the thing's name will be broadcast.
const server = new WebThingServer(new SingleThing(thing), 8888);
process.on('SIGINT', () => {
server.stop();
process.exit();
});
server.start();
}
runServer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment